File: 061_instance_inline.t

package info (click to toggle)
libclass-mop-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 552 kB
  • ctags: 209
  • sloc: perl: 6,157; makefile: 46
file content (108 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 16;
use Test::Exception;

BEGIN {
    use_ok('Class::MOP::Instance');    
}

my $C = 'Class::MOP::Instance';

{
    my $instance  = '$self';
    my $slot_name = '"foo"';
    my $value     = '$value';

    is($C->inline_get_slot_value($instance, $slot_name), 
      '$self->{"foo"}',
      '... got the right code for get_slot_value');
  
    is($C->inline_set_slot_value($instance, $slot_name, $value), 
      '$self->{"foo"} = $value',
      '... got the right code for set_slot_value');  

    is($C->inline_initialize_slot($instance, $slot_name), 
      '$self->{"foo"} = undef',
      '... got the right code for initialize_slot');
  
    is($C->inline_is_slot_initialized($instance, $slot_name), 
      'exists $self->{"foo"} ? 1 : 0',
      '... got the right code for get_slot_value');  
  
    is($C->inline_weaken_slot_value($instance, $slot_name), 
      'Scalar::Util::weaken( $self->{"foo"} )',
      '... got the right code for weaken_slot_value');  
  
    is($C->inline_strengthen_slot_value($instance, $slot_name), 
      '$self->{"foo"} = $self->{"foo"}',
      '... got the right code for strengthen_slot_value');  
} 
  
{
    my $instance  = '$_[0]';
    my $slot_name = '$attr_name';
    my $value     = '[]';

    is($C->inline_get_slot_value($instance, $slot_name), 
      '$_[0]->{$attr_name}',
      '... got the right code for get_slot_value');
  
    is($C->inline_set_slot_value($instance, $slot_name, $value), 
      '$_[0]->{$attr_name} = []',
      '... got the right code for set_slot_value');  
  
    is($C->inline_initialize_slot($instance, $slot_name), 
      '$_[0]->{$attr_name} = undef',
      '... got the right code for initialize_slot');  
  
    is($C->inline_is_slot_initialized($instance, $slot_name), 
      'exists $_[0]->{$attr_name} ? 1 : 0',
      '... got the right code for get_slot_value');  
  
    is($C->inline_weaken_slot_value($instance, $slot_name), 
      'Scalar::Util::weaken( $_[0]->{$attr_name} )',
      '... got the right code for weaken_slot_value');  
  
    is($C->inline_strengthen_slot_value($instance, $slot_name), 
      '$_[0]->{$attr_name} = $_[0]->{$attr_name}',
      '... got the right code for strengthen_slot_value');  
}  
  
my $accessor_string = "sub {\n"
. $C->inline_set_slot_value('$_[0]', '$attr_name', '$_[1]')
. " if scalar \@_ == 2;\n"
. $C->inline_get_slot_value('$_[0]', '$attr_name')
. ";\n}";

is($accessor_string, 
   q|sub {
$_[0]->{$attr_name} = $_[1] if scalar @_ == 2;
$_[0]->{$attr_name};
}|, 
    '... got the right code string for accessor');

my $reader_string = "sub {\n"
. $C->inline_get_slot_value('$_[0]', '$attr_name')
. ";\n}";

is($reader_string, 
   q|sub {
$_[0]->{$attr_name};
}|, 
    '... got the right code string for reader');
    
my $writer_string = "sub {\n"
. $C->inline_set_slot_value('$_[0]', '$attr_name', '$_[1]')
. ";\n}";

is($writer_string, 
   q|sub {
$_[0]->{$attr_name} = $_[1];
}|, 
    '... got the right code string for writer');