49
|
1 package IMPL::Test;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
278
|
5 use IMPL::Const qw(:access);
|
84
|
6 require IMPL::Test::SkipException;
|
|
7
|
49
|
8 require Exporter;
|
|
9 our @ISA = qw(Exporter);
|
188
|
10 our @EXPORT_OK = qw(&test &shared &failed &cmparray &skip &run_plan &assert &GetCallerSourceLine);
|
49
|
11
|
|
12 require IMPL::Test::Unit;
|
159
|
13 require IMPL::Test::Plan;
|
|
14 require IMPL::Test::TAPListener;
|
49
|
15
|
|
16 sub test($$) {
|
|
17 my ($name,$code) = @_;
|
|
18 my $class = caller;
|
|
19
|
|
20 $class->set_meta(
|
|
21 new IMPL::Test::Unit::TestInfo( $name, $code )
|
|
22 );
|
|
23 }
|
|
24
|
|
25 sub shared($) {
|
|
26 my ($propInfo) = @_;
|
|
27
|
|
28 my $class = caller;
|
|
29
|
275
|
30 die new IMPL::Exception("Only properties could be declared as shared",$propInfo->name) unless eval {$propInfo->isa('IMPL::Class::PropertyInfo')};
|
278
|
31 die new IMPL::Exception("You can't mark the readonly property as shared",$propInfo->name) unless $propInfo->setter;
|
|
32 die new IMPL::Exception("Only public properties could be declared as shared",$propInfo->name) unless $propInfo->access == ACCESS_PUBLIC;
|
49
|
33
|
275
|
34 $class->set_meta(new IMPL::Test::Unit::SharedData($propInfo->name));
|
49
|
35 }
|
|
36
|
|
37 sub failed($;@) {
|
|
38 die new IMPL::Test::FailException(@_);
|
|
39 }
|
|
40
|
165
|
41 sub assert {
|
194
|
42 my ($condition,@params) = @_;
|
|
43
|
|
44 die new IMPL::Test::FailException(@params ? @params : ("Assertion failed" , _GetSourceLine( (caller)[1,2] )) ) unless $condition;
|
165
|
45 }
|
|
46
|
84
|
47 sub skip($;@) {
|
194
|
48 die new IMPL::Test::SkipException(@_);
|
84
|
49 }
|
|
50
|
49
|
51 sub cmparray {
|
|
52 my ($a,$b) = @_;
|
|
53
|
|
54 return 0 unless @$a == @$b;
|
|
55
|
|
56 for (my $i=0; $i < @$a; $i++ ) {
|
|
57 return 0 unless $a->[$i] eq $b->[$i];
|
|
58 }
|
|
59
|
|
60 return 1;
|
|
61 }
|
159
|
62
|
165
|
63 sub _GetSourceLine {
|
194
|
64 my ($file,$line) = @_;
|
|
65
|
|
66 open my $hFile, $file or return "failed to open file: $file: $!";
|
|
67
|
|
68 my $text;
|
|
69 $text = <$hFile> for ( 1 .. $line);
|
|
70 chomp $text;
|
|
71 $text =~ s/^\s+//;
|
|
72 return "line $line: $text";
|
165
|
73 }
|
|
74
|
188
|
75 sub GetCallerSourceLine {
|
194
|
76 my $line = shift || 0;
|
|
77 return _GetSourceLine( (caller($line + 1))[1,2] )
|
188
|
78 }
|
|
79
|
159
|
80 sub run_plan {
|
194
|
81 my (@units) = @_;
|
|
82
|
|
83 my $plan = new IMPL::Test::Plan(@units);
|
|
84
|
|
85 $plan->Prepare;
|
|
86 $plan->AddListener(new IMPL::Test::TAPListener);
|
|
87 $plan->Run;
|
159
|
88 }
|
49
|
89 1;
|