File: attr.t

package info (click to toggle)
libclass-contract-perl 1.14-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 1,434; makefile: 10
file content (71 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (6)
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
# 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'   => "attr accessors are private to class' package namespace",
     'expect' => qr/^attribute Attribute::foo inaccessible from package main/s,
     'code'   => <<'CODE');
#=> Attributes accessors are private to the class in which they're declared
#   Calling an attribute accessor outside the namespace of the class'
#   package will raise an exception
package Attribute;
use Class::Contract;
contract { class attr 'foo' => 'SCALAR' };
${Attribute->foo} = 1;

package main;
${Attribute->foo};
CODE

::ok('desc'   => 'attr preconditions are inherited',
     'expect' => 3,
#     'need'   => 'Extended Contracts',
     'code'   => <<'CODE');
#package main;
#my $o = Baz->new();
#$o->get_name;
#delete $::pre{'attr'};
3
CODE

::ok('desc'   => 'attr cannot have implementation',
     'expect' => qr/^Attribute cannot have implementation/,
     'code'   => <<'CODE');
package Attribute::Impl;
use Class::Contract;
contract { attr 'baz'; impl {1} };
CODE

::ok('desc'   => 'exception if attempt to access obj attr with class ref',
     'expect' => qr/^Can\'t access object attr w\/ class reference/,
     'code'   => <<'CODE');
package Attribute::Obj;
use Class::Contract;
contract {
  attr 'baz';
  class method 'get_baz';
    impl { ${self->baz} };
};

package main;
Attribute::Obj->get_baz;
CODE


1;
__END__