predicate fixes
[gitmo/Class-MOP.git] / t / 061_instance_inline.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP::Instance');
11 }
12
13 my $C = 'Class::MOP::Instance';
14
15 {
16     my $instance  = '$self';
17     my $slot_name = '"foo"';
18     my $value     = '$value';
19
20     is($C->inline_get_slot_value($instance, $slot_name),
21       'exists $self->{"foo"} ? $self->{"foo"} : undef',
22       '... got the right code for get_slot_value');
23
24     is($C->inline_set_slot_value($instance, $slot_name, $value),
25       '$self->{"foo"} = $value',
26       '... got the right code for set_slot_value');
27
28     is($C->inline_initialize_slot($instance, $slot_name),
29       '$self->{"foo"} = undef',
30       '... got the right code for initialize_slot');
31
32     is($C->inline_is_slot_initialized($instance, $slot_name),
33       'exists $self->{"foo"} ? 1 : 0',
34       '... got the right code for get_slot_value');
35
36     is($C->inline_weaken_slot_value($instance, $slot_name),
37       'Scalar::Util::weaken( $self->{"foo"} )',
38       '... got the right code for weaken_slot_value');
39
40     is($C->inline_strengthen_slot_value($instance, $slot_name),
41       '$self->{"foo"} = $self->{"foo"}',
42       '... got the right code for strengthen_slot_value');
43 }
44
45 {
46     my $instance  = '$_[0]';
47     my $slot_name = '$attr_name';
48     my $value     = '[]';
49
50     is($C->inline_get_slot_value($instance, $slot_name),
51       'exists $_[0]->{$attr_name} ? $_[0]->{$attr_name} : undef',
52       '... got the right code for get_slot_value');
53
54     is($C->inline_set_slot_value($instance, $slot_name, $value),
55       '$_[0]->{$attr_name} = []',
56       '... got the right code for set_slot_value');
57
58     is($C->inline_initialize_slot($instance, $slot_name),
59       '$_[0]->{$attr_name} = undef',
60       '... got the right code for initialize_slot');
61
62     is($C->inline_is_slot_initialized($instance, $slot_name),
63       'exists $_[0]->{$attr_name} ? 1 : 0',
64       '... got the right code for get_slot_value');
65
66     is($C->inline_weaken_slot_value($instance, $slot_name),
67       'Scalar::Util::weaken( $_[0]->{$attr_name} )',
68       '... got the right code for weaken_slot_value');
69
70     is($C->inline_strengthen_slot_value($instance, $slot_name),
71       '$_[0]->{$attr_name} = $_[0]->{$attr_name}',
72       '... got the right code for strengthen_slot_value');
73 }
74
75 my $accessor_string = "sub {\n"
76 . $C->inline_set_slot_value('$_[0]', '$attr_name', '$_[1]')
77 . " if scalar \@_ == 2;\n"
78 . $C->inline_get_slot_value('$_[0]', '$attr_name')
79 . ";\n}";
80
81 is($accessor_string,
82    q|sub {
83 $_[0]->{$attr_name} = $_[1] if scalar @_ == 2;
84 exists $_[0]->{$attr_name} ? $_[0]->{$attr_name} : undef;
85 }|,
86     '... got the right code string for accessor');
87
88 my $reader_string = "sub {\n"
89 . $C->inline_get_slot_value('$_[0]', '$attr_name')
90 . ";\n}";
91
92 is($reader_string,
93    q|sub {
94 exists $_[0]->{$attr_name} ? $_[0]->{$attr_name} : undef;
95 }|,
96     '... got the right code string for reader');
97
98 my $writer_string = "sub {\n"
99 . $C->inline_set_slot_value('$_[0]', '$attr_name', '$_[1]')
100 . ";\n}";
101
102 is($writer_string,
103    q|sub {
104 $_[0]->{$attr_name} = $_[1];
105 }|,
106     '... got the right code string for writer');
107
108