1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
# This script should be runnable with 'make test'.
######################### We start with some black magic to print on failure.
BEGIN { $| = 1 }
END { print "not ok 1\n" unless $loaded }
use lib qw( ./t );
use Magic;
use Class::Contract;
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
::ok('desc' => 'Simple Contract',
'expect' => 1,
'code' => <<'CODE');
package Simple;
use Class::Contract;
contract {
method 'my_method'; impl {1};
abstract method 'my_abstract_method';
class abstract method 'my_class_abstract_method';
abstract class method 'my_abstract_class_method';
attr 'my_attr';
class attr 'my_class_attr';
};
CODE
::ok('desc' => 'Extended Contracts',
'expect' => 1,
'code' => <<'CODE');
package Grandfather;
use Class::Contract;
contract {
ctor 'new';
impl { shift if $Class::Contract::VERSION < 1.10; push @::test, [0, @_]; };
class method 'incr';
pre { $::test{'pre'}++; 1 };
impl { $::test{'impl'}++; 1 };
post { $::test{'post'}++; 1 };
eval qq|class method '_$_'; pre {1}; impl {1}| foreach qw(a b c d e f g h i);
eval qq|class method '_$_'; impl {1} | foreach qw(j k l m n o p q r);
eval qq|class method '_$_'; pre {0}; impl {1}| foreach qw(s t u v w x y z 1);
die $@ if $@;
};
package Father;
use Class::Contract;
contract {
inherits 'Grandfather';
ctor 'new';
impl { shift if $Class::Contract::VERSION < 1.10; push @::test, [1, @_]; };
class method 'incr';
pre { $::test{'pre'}++; 0 };
impl { $::test{'impl'}++; 1 };
post { $::test{'post'}++; 1 };
eval qq|class method '_$_'; pre {1}; impl {1}| foreach qw(a b c j k l s t u);
eval qq|class method '_$_'; impl {1} | foreach qw(d e f m n o v w x);
eval qq|class method '_$_'; pre {0}; impl {1}| foreach qw(g h i p q r y z 1);
die $@ if $@;
};
package Son;
use Class::Contract;
contract {
inherits 'Father';
ctor 'new';
impl { shift if $Class::Contract::VERSION < 1.10; push @::test, [2, @_]; };
class method 'incr';
pre { $::test{'pre'}++; 0 };
impl { $::test{'impl'}++; 1 };
post { $::test{'post'}++; 1 };
eval qq|class method '_$_'; pre {1}; impl {1}| foreach qw(a d g j m p s v y);
eval qq|class method '_$_'; impl {1} | foreach qw(b e h k n q t w z);
eval qq|class method '_$_'; pre {0}; impl {1}| foreach qw(c f i l o r u x 1);
die $@ if $@;
};
package Args;
use Class::Contract;
contract {
ctor 'new';
pre { $::test{'pre'} = \@_; 1 };
impl { $::test{'impl'} = \@_; 1 };
post { $::test{'post'} = \@_; 1 };
class method 'same';
pre { @{$::test{'same_pre'}} = (@_); 1 };
impl { @{$::test{'same_impl'}} = (@_); 1 };
post { @{$::test{'same_post'}} = (@_); 1 };
class method 'modify_pre';
pre {
shift if $Class::Contract::VERSION < 1.10;
$_[0] = 'pre'; $::test{'pre'} = $_[0]; 1;
};
impl { shift if $Class::Contract::VERSION < 1.10; $::test{'impl'}=$_[0]; 1};
post { shift if $Class::Contract::VERSION < 1.10; $::test{'post'}=$_[0]; 1};
class method 'modify_impl';
pre { shift if $Class::Contract::VERSION < 1.10; $::test{'pre'}=$_[0]; 1};
impl {
shift if $Class::Contract::VERSION < 1.10;
$_[0] = 'impl'; $::test{'impl'} = $_[0]; 1;
};
post {
shift if $Class::Contract::VERSION < 1.10;
$::test{'post'} = $_[0]; 1
};
class method 'modify_post';
pre {
shift if $Class::Contract::VERSION < 1.10;
$::test{'pre'}=$_[0]; 1
};
impl {
shift if $Class::Contract::VERSION < 1.10;
$::test{'impl'}=$_[0]; 1
};
post {
shift if $Class::Contract::VERSION < 1.10;
$_[0] = 'post'; $::test{'post'} = $_[0]; 1;
};
};
CODE
::ok('desc' => "pre gets shallow copy of args to be passed to impl",
'expect' => 1,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
undef %::test;
my @args = qw(1 a 2 b 4 c);
Args->same(@args);
my $fail = 0;
foreach (0..$#args) {
$fail++
if $::test{'same_pre'}->[$_] ne $::test{'same_impl'}->[$_]
}
$fail ? 0 : 1;
CODE
::ok('desc' => "pre can't modify args passed to method or impl",
'expect' => 1,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
%test = ();
my $arg = 'foo';
Args->modify_pre($arg);
join('', $arg, @test{qw(pre impl post)}) eq 'fooprefoofoo';
CODE
::ok('desc' => "impl can modify args passed to method and post",
'expect' => 1,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
%test = ();
my $arg = 'foo';
Args->modify_impl($arg);
join('', $arg, @test{qw( pre impl post )}) eq 'implfooimplimpl';
CODE
::ok('desc' => "post can't modify args passed to method",
'expect' => 1,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
%test = ();
my $arg = 'foo';
Args->modify_post($arg);
join('', $arg, @test{qw( pre impl post )}) eq 'foofoofoopost';
CODE
::ok('desc' => 'invoking abstract method raises exception',
'expect' => qr/^Can\'t call abstract method/s,
'need' => 'Simple Contract',
'code' => <<'CODE');
package main;
Simple->my_class_abstract_method;
CODE
::ok('desc' => 'missing impl raise an exception',
'expect' => qr/^No implementation for method foo/s,
'code' => <<'CODE');
package UnDeclared_Method;
use Class::Contract;
contract { method 'foo' };
CODE
::ok('desc' => 'simple implementation based on abstract',
'expect' => 1,
'need' => 'Simple Contract',
'code' => <<'CODE');
package Inherited_Class_Method;
use Class::Contract;
contract {
inherits 'Simple';
class method 'my_class_abstract_method';
impl { 1 };
};
package main;
Inherited_Class_Method->my_class_abstract_method,
CODE
::ok('desc' => "implementation not inherited",
'expect' => 1,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
%test = ();
Son->incr;
$test{'impl'};
CODE
::ok('desc' => 'post-conditions are inherited',
'expect' => 3,
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
%test = ();
Son->incr;
$test{'post'};
CODE
::ok('desc' => 'pre check must satisfy self or an ascending ancestor',
'expect' => '',
'need' => 'Extended Contracts',
'code' => <<'CODE');
package main;
undef $@;
$fail = '';
# No inheritence involvement G
eval qq|Grandfather->_a|; $fail .= 'a' if $@; # 1
eval qq|Grandfather->_j|; $fail .= 'j' if $@; # null
eval qq|Grandfather->_s|; $fail .= 's' unless $@; undef $@; # 0
# Parent Child inheritence G F
eval qq|Father->_a|; $fail .= 'a' if $@; # 1 1
eval qq|Father->_d|; $fail .= 'd' if $@; # 1 null
eval qq|Father->_g|; $fail .= 'g' if $@; # 1 0
eval qq|Father->_j|; $fail .= 'j' if $@; # null 1
eval qq|Father->_m|; $fail .= 'm' if $@; # null null
eval qq|Father->_p|; $fail .= 'p' unless $@; undef $@; # null 0
eval qq|Father->_s|; $fail .= 's' if $@; # 0 1
eval qq|Father->_v|; $fail .= 'v' unless $@; undef $@; # 0 null
eval qq|Father->_y|; $fail .= 'y' unless $@; undef $@; # 0 0
# Grandparent Parent Child inheritence G F S
eval qq|Son->_a|; $fail .= 'a' if $@; # 1 1 1
eval qq|Son->_b|; $fail .= 'b' if $@; # 1 1 null
eval qq|Son->_c|; $fail .= 'c' if $@; # 1 1 0
eval qq|Son->_d|; $fail .= 'd' if $@; # 1 null 1
eval qq|Son->_e|; $fail .= 'e' if $@; # 1 null null
eval qq|Son->_f|; $fail .= 'f' if $@; # 1 null 0
eval qq|Son->_g|; $fail .= 'g' if $@; # 1 0 1
eval qq|Son->_h|; $fail .= 'h' if $@; # 1 0 null
eval qq|Son->_i|; $fail .= 'i' if $@; # 1 0 0
eval qq|Son->_j|; $fail .= 'j' if $@; # null 1 1
eval qq|Son->_k|; $fail .= 'k' if $@; # null 1 null
eval qq|Son->_l|; $fail .= 'l' if $@; # null 1 0
eval qq|Son->_m|; $fail .= 'm' if $@; # null null 1
eval qq|Son->_n|; $fail .= 'n' if $@; # null null null
eval qq|Son->_o|; $fail .= 'o' unless $@; undef $@; # null null 0
eval qq|Son->_p|; $fail .= 'p' if $@; # null 0 1
eval qq|Son->_q|; $fail .= 'q' unless $@; undef $@; # null 0 null
eval qq|Son->_r|; $fail .= 'r' unless $@; undef $@; # null 0 0
eval qq|Son->_s|; $fail .= 's' if $@; # 0 1 1
eval qq|Son->_t|; $fail .= 't' if $@; # 0 1 null
eval qq|Son->_u|; $fail .= 'u' if $@; # 0 1 0
eval qq|Son->_v|; $fail .= 'v' if $@; # 0 null 1
eval qq|Son->_w|; $fail .= 'w' unless $@; undef $@; # 0 null null
eval qq|Son->_x|; $fail .= 'x' unless $@; undef $@; # 0 null 0
eval qq|Son->_y|; $fail .= 'y' if $@; # 0 0 1
eval qq|Son->_z|; $fail .= 'z' unless $@; undef $@; # 0 0 null
eval qq|Son->_1|; $fail .= '1' unless $@; undef $@; # 0 0 0
print "fail: $fail\n" if $fail;
$fail;
CODE
::ok('desc' => 'private inheritance stays private',
'expect' => qr/^Forget 'private'?/s,
'code' => <<'CODE');
package Private;
use Class::Contract;
contract {
private class method 'foo';
impl {1};
};
package Private::Still;
use Class::Contract;
contract {
inherits 'Private';
class method 'foo';
impl {1};
};
CODE
__DATA__
##ok('desc' => 'ancestor class method derived as object method',
# 'expect' => 1,
# 'need' => 'additional thought',
# 'code' => <<'CODE');
#1
#CODE
# REQ: Pre and post conditions for methods shall receive the same argument
# list as the implementation (Pre gets copy, Post identical)
# ?: Do attribute preconditions get argument list?
# ?: Are there limits as to where &old, &self, and &value can be called?
# REQ: Invariant, pre, and post conditions may be declared optional.
# REQ: Optional condition checking may be switched on or off using the
# &check method (see examples below).
# REQ: The implementation's return value shall be available in the
# method's post condition(s) through the subroutine &value,
# which returns a reference to a scalar or an array
# (depending on the calling context).
# REQ: &value shall also provide access to the value of an attribute within
# that attribute's pre and post conditions.
# REQ: The value of the object prior to a method shall be available in the
# post-conditions via the &old subroutine, which returns a copy
# of the object as it was prior to the method call.
### Garrett's additions ###
# REQ: C<method> and C<ctor> shall croak if the naming clashes with the
# subroutines exported by Class::Contract
# REQ: It shall not be possible to redefine a Contract
# REQ: ctor plays nicely with Overload.pm
# REQ: &value plays nicely with Overload.pm
|