annotate Lib/IMPL/Test/Plan.pm @ 23:716b287d4795

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