annotate Lib/IMPL/Test/Plan.pm @ 0:03e58a454b20

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children 2e546a5175dd
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
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 $info{Unit} = $Unit;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 try {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 $info{Tests} = [map $Unit->new($_), $Unit->List];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 } otherwise {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 $info{Tests} = [$info{Unit} = new IMPL::Test::BadUnit($Unit->UnitName,"Failed to extract tests",$@)];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 $count += @{$info{Tests}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 push @cache, \%info if @{$info{Tests}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 $this->_Count($count);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 $this->_Cache(\@cache);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 sub Count {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 return $this->_Count;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 sub Run {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 die new IMPL::InvalidOperationException("You must call the prepare method before running the plan") unless $this->_Cache;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 $this->_Tell(RunPlan => $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 my @resultsTotal;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 foreach my $info ($this->_Cache) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 $this->_Tell(RunUnit => $info->{Unit});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 my $data;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 undef $@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 eval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 $data = $info->{Unit}->StartUnit;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 my @results;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 if (not $@) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 foreach my $test (@{$info->{Tests}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 $this->_Tell(RunTest => $test);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 my $result = $test->Run($data);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 $this->_Tell(EndTest => $test,$result);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 push @results,$result;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 my $e = $@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 foreach my $test (@{$info->{Tests}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 $this->_Tell(RunTest => $test);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 my $result = new IMPL::Test::Result(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 Name => $test->Name,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 State => IMPL::Test::Result::FAIL,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 Exception => $e
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121 $this->_Tell(EndTest => $test,$result);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 push @results,$result;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 eval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 $info->{Unit}->FinishUnit($data);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 undef $@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 push @resultsTotal, { Unit => $info->{Unit}, Results => \@results};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134 $this->_Tell(EndUnit => $info->{Unit},\@results);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 $this->Results(\@resultsTotal);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138 $this->_Tell(EndPlan => $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
140
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
141 sub _Tell {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
142 my ($this,$what,@args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
143
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
144 $_->$what(@args) foreach $this->Listeners;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
145 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
146
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
147 sub SaveXML {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
148 my ($this,$out) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
149
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
150 my $h;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
151
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152 if (ref $out eq 'GLOB') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153 $h = $out;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154 } elsif ($out and not ref $out) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 open $h, ">", $out or die new IMPL::Exception("Failed to open file",$out);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
156 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
157 die new IMPL::InvalidOperationException("Invalid output specified");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160 my $s = new IMPL::Serializer(Formatter => new IMPL::Serialization::XmlFormatter( IdentOutput => 1, SkipWhitespace => 1) );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 $s->Serialize($h,$this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
162 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
163
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
164 sub LoadXML {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
165 my ($self,$in) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
166
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
167 my $h;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
168
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
169 if (ref $in eq 'GLOB') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
170 $h = $in;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
171 } elsif ($in and not ref $in) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
172 open $h, ">", $in or die new IMPL::Exception("Failed to open file",$in);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
173 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
174 die new IMPL::InvalidOperationException("Invalid input specified");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
175 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
176
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
177 my $s = new IMPL::Serializer(Formatter => new IMPL::Serialization::XmlFormatter( IdentOutput => 1, SkipWhitespace => 1) );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
178 return $s->Deserialize($h);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
179 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
180
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
181 sub xml {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
182 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
183 my $str = '';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
184
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
185 open my $h,'>',\$str or die new IMPL::Exception("Failed to create stream");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
186 $this->SaveXML($h);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
187 undef $h;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
188 return $str;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
189 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
190
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
191 sub LoadXMLString {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
192 my $self = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
193 my $str = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
194
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
195 open my $h,'<',\$str or die new IMPL::Exception("Failed to create stream");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
196 return $self->LoadXML($h);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
197 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
198
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
199
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
200 1;