annotate Lib/IMPL/Test/Plan.pm @ 31:d59526f6310e

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