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