-
Notifications
You must be signed in to change notification settings - Fork 135
/
attributes.t
82 lines (65 loc) · 1.88 KB
/
attributes.t
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
use v6;
use Test;
# L<S03/Item assignment precedence>
plan 12;
# Tests for binding instance and class attributes
# note that only attributes themselves ($!foo) can be bound,
# not accessors ($.foo)
{
my $var = 42;
class Klass1 { has $.x; method bind { $!x := $var } }
my $obj1 = Klass1.new;
lives_ok { $obj1.bind() }, 'attribute binding lives';
#?pugs 3 todo 'bug'
is $obj1.x, 42, 'binding $!x instance attribute (1)';
$var = 23;
is $obj1.x, 23, 'binding $!x instance attribute (2)';
}
{
my $var = 42;
class Klass2 {
has $x;
method bind { $x := $var }
method get_x { $x }
method set_x ($new_x) { $x = $new_x }
}
my $obj2 = Klass2.new;
$obj2.bind();
#?pugs 3 todo 'bug'
is $obj2.get_x, 42, 'binding $x instance attribute (1)';
$var = 23;
is $obj2.get_x, 23, 'binding $x instance attribute (2)';
$obj2.set_x(19);
is $var, 19, 'binding $x instance attribute (3)';
}
# Public class attributes
#?rakudo skip 'class attributes'
{
my $var = 42;
class Klass3 { our $.x; method bind { $!x := $var } }
try { Klass3.bind() };
#?pugs 3 todo 'bug'
is try { Klass3.x }, 42, "binding public class attribute (1)";
$var = 23;
is try { Klass3.x }, 23, "binding public class attribute (2)";
try { Klass3.x = 19 };
is $var, 19, "binding public class attribute (3)";
}
# Private class attributes
#?rakudo skip 'class attributes'
{
my $var = 42;
class Klass4 {
our $x;
method bind { $x := $var }
method get_x { $x }
method set_x ($new_x) { $x = $new_x }
}
try { Klass4.bind() };
is Klass4.get_x, 42, "binding private class attribute (1)";
$var = 23;
is Klass4.get_x, 23, "binding private class attribute (2)";
Klass4.set_x(19);
is $var, 19, "binding private class attribute (3)";
}
# vim: ft=perl6