0
|
1 package IMPL::Test::Plan;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object);
|
|
6 use IMPL::Class::Property;
|
|
7
|
|
8 use IMPL::Exception;
|
|
9 use IMPL::Test::Result;
|
|
10 use IMPL::Test::BadUnit;
|
|
11 use Error qw(:try);
|
|
12
|
|
13 use IMPL::Serialization;
|
|
14 use IMPL::Serialization::XmlFormatter;
|
|
15
|
|
16 BEGIN {
|
|
17 public property Units => prop_all | prop_list;
|
|
18 public property Results => prop_all | prop_list;
|
|
19 public property Listeners => prop_all | prop_list;
|
|
20 private property _Cache => prop_all | prop_list;
|
|
21 private property _Count => prop_all;
|
|
22 }
|
|
23
|
|
24 sub CTOR {
|
|
25 my $this = shift;
|
|
26 $this->Units(\@_);
|
|
27 }
|
|
28
|
|
29 sub restore {
|
|
30 my ($class,$data,$instance) = @_;
|
|
31
|
|
32 $instance ||= $class->surrogate;
|
|
33
|
|
34 $instance->callCTOR();
|
|
35
|
|
36 my %args = @$data;
|
|
37
|
|
38 $instance->Units($args{Units});
|
|
39 $instance->Results($args{Results}) if $args{Results};
|
|
40 $instance->Listeners($args{Listeners}) if $args{Listeners};
|
|
41 }
|
|
42
|
|
43 sub save {
|
|
44 my ($this,$ctx) = @_;
|
|
45
|
|
46 $ctx->AddVar(Units => [$this->Units]);
|
|
47 $ctx->AddVar(Results => [$this->Results]) if $this->Results;
|
|
48 $ctx->AddVar(Listeners => [$this->Listeners]) if $this->Listeners;
|
|
49 }
|
|
50
|
|
51 sub AddListener {
|
|
52 my ($this,$listener) = @_;
|
|
53
|
|
54 $this->Listeners($this->Listeners,$listener);
|
|
55 }
|
|
56
|
|
57 sub Prepare {
|
|
58 my ($this) = @_;
|
|
59
|
|
60 my $count = 0;
|
|
61 my @cache;
|
|
62
|
|
63 foreach my $Unit ($this->Units){
|
|
64 my %info;
|
|
65
|
3
|
66 # preload module
|
|
67 eval "require $Unit" unless (ref $Unit);
|
|
68
|
0
|
69 $info{Unit} = $Unit;
|
|
70 try {
|
|
71 $info{Tests} = [map $Unit->new($_), $Unit->List];
|
|
72 } otherwise {
|
3
|
73 $info{Tests} = [$info{Unit} = new IMPL::Test::BadUnit($Unit->can('UnitName') ? $Unit->UnitName : $Unit,"Failed to extract tests",$@)];
|
0
|
74 };
|
|
75 $count += @{$info{Tests}};
|
|
76 push @cache, \%info if @{$info{Tests}};
|
|
77 }
|
|
78
|
|
79 $this->_Count($count);
|
|
80 $this->_Cache(\@cache);
|
|
81 }
|
|
82
|
|
83 sub Count {
|
|
84 my ($this) = @_;
|
|
85 return $this->_Count;
|
|
86 }
|
|
87
|
|
88 sub Run {
|
|
89 my $this = shift;
|
|
90
|
|
91 die new IMPL::InvalidOperationException("You must call the prepare method before running the plan") unless $this->_Cache;
|
|
92
|
|
93 $this->_Tell(RunPlan => $this);
|
|
94
|
|
95 my @resultsTotal;
|
|
96
|
|
97 foreach my $info ($this->_Cache) {
|
|
98 $this->_Tell(RunUnit => $info->{Unit});
|
|
99
|
|
100 my $data;
|
|
101 undef $@;
|
|
102 eval {
|
|
103 $data = $info->{Unit}->StartUnit;
|
|
104 };
|
|
105
|
|
106 my @results;
|
|
107
|
|
108 if (not $@) {
|
|
109 foreach my $test (@{$info->{Tests}}) {
|
|
110 $this->_Tell(RunTest => $test);
|
|
111 my $result = $test->Run($data);
|
|
112 $this->_Tell(EndTest => $test,$result);
|
|
113 push @results,$result;
|
|
114 }
|
|
115 } else {
|
|
116 my $e = $@;
|
|
117 foreach my $test (@{$info->{Tests}}) {
|
|
118 $this->_Tell(RunTest => $test);
|
|
119 my $result = new IMPL::Test::Result(
|
|
120 Name => $test->Name,
|
|
121 State => IMPL::Test::Result::FAIL,
|
|
122 Exception => $e
|
|
123 );
|
|
124 $this->_Tell(EndTest => $test,$result);
|
|
125 push @results,$result;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 eval {
|
|
130 $info->{Unit}->FinishUnit($data);
|
|
131 };
|
|
132
|
|
133 undef $@;
|
|
134
|
|
135 push @resultsTotal, { Unit => $info->{Unit}, Results => \@results};
|
|
136
|
|
137 $this->_Tell(EndUnit => $info->{Unit},\@results);
|
|
138 }
|
|
139
|
|
140 $this->Results(\@resultsTotal);
|
|
141 $this->_Tell(EndPlan => $this);
|
|
142 }
|
|
143
|
|
144 sub _Tell {
|
|
145 my ($this,$what,@args) = @_;
|
|
146
|
|
147 $_->$what(@args) foreach $this->Listeners;
|
|
148 }
|
|
149
|
|
150 sub SaveXML {
|
|
151 my ($this,$out) = @_;
|
|
152
|
|
153 my $h;
|
|
154
|
|
155 if (ref $out eq 'GLOB') {
|
|
156 $h = $out;
|
|
157 } elsif ($out and not ref $out) {
|
|
158 open $h, ">", $out or die new IMPL::Exception("Failed to open file",$out);
|
|
159 } else {
|
|
160 die new IMPL::InvalidOperationException("Invalid output specified");
|
|
161 }
|
|
162
|
|
163 my $s = new IMPL::Serializer(Formatter => new IMPL::Serialization::XmlFormatter( IdentOutput => 1, SkipWhitespace => 1) );
|
|
164 $s->Serialize($h,$this);
|
|
165 }
|
|
166
|
|
167 sub LoadXML {
|
|
168 my ($self,$in) = @_;
|
|
169
|
|
170 my $h;
|
|
171
|
|
172 if (ref $in eq 'GLOB') {
|
|
173 $h = $in;
|
|
174 } elsif ($in and not ref $in) {
|
|
175 open $h, ">", $in or die new IMPL::Exception("Failed to open file",$in);
|
|
176 } else {
|
|
177 die new IMPL::InvalidOperationException("Invalid input specified");
|
|
178 }
|
|
179
|
|
180 my $s = new IMPL::Serializer(Formatter => new IMPL::Serialization::XmlFormatter( IdentOutput => 1, SkipWhitespace => 1) );
|
|
181 return $s->Deserialize($h);
|
|
182 }
|
|
183
|
|
184 sub xml {
|
|
185 my $this = shift;
|
|
186 my $str = '';
|
|
187
|
|
188 open my $h,'>',\$str or die new IMPL::Exception("Failed to create stream");
|
|
189 $this->SaveXML($h);
|
|
190 undef $h;
|
|
191 return $str;
|
|
192 }
|
|
193
|
|
194 sub LoadXMLString {
|
|
195 my $self = shift;
|
|
196 my $str = shift;
|
|
197
|
|
198 open my $h,'<',\$str or die new IMPL::Exception("Failed to create stream");
|
|
199 return $self->LoadXML($h);
|
|
200 }
|
|
201
|
|
202
|
|
203 1;
|