compute using the MOP
[gitmo/Moose.git] / t / 700_xs / 001_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 BEGIN {
10     plan skip_all => "no XSLoader" unless eval { require XSLoader };
11
12     plan skip_all => $@ unless eval {
13         require Moose;
14         Moose->XSLoader::load($Moose::VERSION);
15         1;
16     };
17
18     plan 'no_plan';
19 }
20
21 {
22     package Moose::XS;
23
24     sub attr_to_meta_instance {
25         my $attr = shift;
26         return $attr->associated_class->get_meta_instance;
27     }
28
29     sub meta_instance_to_attr_descs {
30         my $mi = shift;
31
32         return (
33             $mi->associated_metaclass->name,
34             [ map { {
35                 meta => $_,
36                 key  => ($_->slots)[0],
37             } } $mi->get_all_attributes ]
38         );
39     }
40 }
41
42 ok( defined &Moose::XS::new_getter );
43 ok( defined &Moose::XS::new_setter );
44 ok( defined &Moose::XS::new_accessor );
45 ok( defined &Moose::XS::new_predicate );
46
47 {
48     package Foo;
49     use Moose;
50
51     has x => ( is => "rw", predicate => "has_x" );
52     has y => ( is => "ro" );
53     has z => ( reader => "z", setter => "set_z" );
54     has ref => ( is => "rw", weak_ref => 1 );
55 }
56
57 {
58     my ( $x, $y, $z, $ref ) = map { Foo->meta->get_attribute($_) } qw(x y z ref);
59     $x->Moose::XS::new_accessor("Foo::x");
60     $x->Moose::XS::new_predicate("Foo::has_x");
61     $y->Moose::XS::new_getter("Foo::y");
62     $z->Moose::XS::new_getter("Foo::z");
63     $z->Moose::XS::new_setter("Foo::set_z");
64     $ref->Moose::XS::new_accessor("Foo::ref");
65 }
66
67
68 my $ref = [ ];
69
70 my $foo = Foo->new( x => "ICKS", y => "WHY", z => "ZEE", ref => $ref );
71
72 is( $foo->x, "ICKS" );
73 is( $foo->y, "WHY" );
74 is( $foo->z, "ZEE" );
75 is( $foo->ref, $ref, );
76
77 lives_ok { $foo->x("YASE") };
78
79 is( $foo->x, "YASE" );
80
81 dies_ok { $foo->y("blah") };
82
83 is( $foo->y, "WHY" );
84
85 dies_ok { $foo->z("blah") };
86
87 is( $foo->z, "ZEE" );
88
89 lives_ok { $foo->set_z("new") };
90
91 is( $foo->z, "new" );
92
93 ok( $foo->has_x );
94
95 ok( !Foo->new->has_x );
96
97 undef $ref;
98
99 is( $foo->ref(), undef );
100
101 $ref = { };
102
103 $foo->ref($ref);
104
105 is( $foo->ref, $ref, );
106
107 undef $ref;
108
109 is( $foo->ref(), undef );
110
111 use Data::Dumper;
112 warn Dumper($foo);