annotate lib/IMPL/Test/Unit.pm @ 416:cc2cf8c0edc2 ref20150831

sync
author cin
date Thu, 29 Oct 2015 03:50:25 +0300
parents 9335cf010b23
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Test::Unit;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use parent qw(IMPL::Object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use Time::HiRes qw(gettimeofday tv_interval);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use Error qw(:try);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 use File::Spec();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 use IMPL::Test::Result();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 use IMPL::Test::FailException();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 use IMPL::Exception();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 public property Name => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 public property Code => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my ($this,$info) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 die new IMPL::InvalidArgumentException("TestInfo should be supplied as an argument") unless $info;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->Name($info->Name || 'Annon');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $this->Code($info->Code)or die new IMPL::InvalidOperationException("Can't create test without entry point");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub UnitName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($self) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $self->toString;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 sub Cleanup {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my ($this,$session) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 $session->{$_} = $this->$_() foreach map $_->DataList, $this->get_meta('IMPL::Test::Unit::SharedData',undef,1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 sub StartUnit {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $class = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 return {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 sub InitTest {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my ($this,$session) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $this->$_($session->{$_}) foreach map $_->DataList, $this->get_meta('IMPL::Test::Unit::SharedData',undef,1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 sub FinishUnit {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my ($class,$session) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 sub List {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 return $self->get_meta('IMPL::Test::Unit::TestInfo',undef,1); # deep search with no criteria
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 sub Run {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 my ($this,$session) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my $t = [gettimeofday];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 return try {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 $this->InitTest($session);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 my $code = $this->Code;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 my $t0 = [gettimeofday];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 my $elapsed;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 try {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 $this->$code();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 $elapsed = tv_interval ( $t0 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 } finally {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 # we need to call Cleanup anyway
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 $this->Cleanup($session);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 return new IMPL::Test::Result(
410
9335cf010b23 refactoring
cin
parents: 407
diff changeset
89 name => $this->Name,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
90 state => IMPL::Test::Result::SUCCESS,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
91 timeExclusive => $elapsed,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
92 timeInclusive => tv_interval ( $t )
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 } catch IMPL::Test::FailException with {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 my $e = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 return new IMPL::Test::Result(
410
9335cf010b23 refactoring
cin
parents: 407
diff changeset
97 name => $this->Name,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
98 state => IMPL::Test::Result::FAIL,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
99 exception => $e,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
100 timeInclusive => tv_interval ( $t )
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 } otherwise {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 my $e = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 return new IMPL::Test::Result(
410
9335cf010b23 refactoring
cin
parents: 407
diff changeset
105 name => $this->Name,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
106 state => IMPL::Test::Result::ERROR,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
107 exception => $e,
9335cf010b23 refactoring
cin
parents: 407
diff changeset
108 timeInclusive => tv_interval ( $t )
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 sub GetResourceFile {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 my ($this,@path) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 my ($cwd) = map m/(.*)/, File::Spec->rel2abs(File::Spec->curdir());
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 return File::Spec->catfile($cwd,@path);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 sub GetResourceDir {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 my ($this,@path) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 my ($cwd) = map m/(.*)/, File::Spec->rel2abs(File::Spec->curdir());
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 return File::Spec->catdir($cwd,@path);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 package IMPL::Test::Unit::TestInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 use parent qw(IMPL::Object::Meta);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 require IMPL::Exception;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 public property Name => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 public property Code => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 my ($this,$name,$code) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 $this->Name($name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 $this->Code($code) or die new IMPL::InvalidArgumentException("The Code is a required parameter");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 package IMPL::Test::Unit::SharedData;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 use parent qw(IMPL::Object::Meta);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 public property DataList => prop_all | prop_list;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 $this->DataList(\@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 1;