more tests for TCs
[gitmo/Moose.git] / t / 700_xs / 001_basic.t
CommitLineData
1ea12c91 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
8
9BEGIN {
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
f253044f 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
160f9ca7 29 my $i;
30 my %checks = map { $_ => $i++ } qw(
31 Any
32 Undef
33 Defined
34 Str
35 Num
36 Int
37 GlobRef
38 ArrayRef
39 HashRef
40 CodeRef
41 Ref
42 ScalarRef
43 FileHandle
44 RegexpRef
45 Object
46 ClassName
47 );
48
49 # aliases
50 $checks{Bool} = $checks{Item} = $checks{Any};
51 $checks{Value} = $checks{Str};
52
53 sub tc_params {
54 my $tc = shift;
55
56 return ( undef, 0, undef ) unless $tc;
57
58 if ( ref $tc eq 'Moose::Meta::TypeConstraint' or ref $tc eq 'Moose::Meta::TypeConstraint::Parameterizable') {
59 # builtin moose type #
60 return ( $tc, 1, $checks{$tc->name} );
61 } elsif ( $tc->isa("Moose::Meta::TypeConstraint::Class") ) {
62 return ( $tc, 2, $tc->class );
63 } else {
64 warn ref $tc;
65 return ( $tc, 3, $tc->_compiled_type_constraint );
66 }
67 }
68
f253044f 69 sub meta_instance_to_attr_descs {
70 my $mi = shift;
71
72 return (
73 $mi->associated_metaclass->name,
160f9ca7 74 [ map {[
75 $_,
76 [$_->slots],
77
78 $_->is_weak_ref,
79 $_->should_coerce,
80 $_->is_lazy,
81
82 tc_params($_->type_constraint),
83 $_->trigger,
84 $_->initializer,
85
86 $_->has_default,
87 $_->default,
88 $_->builder,
89 ]} $mi->get_all_attributes ]
f253044f 90 );
91 }
92}
93
de2f2e97 94ok( defined &Moose::XS::new_getter );
95ok( defined &Moose::XS::new_setter );
96ok( defined &Moose::XS::new_accessor );
97ok( defined &Moose::XS::new_predicate );
1ea12c91 98
99{
100 package Foo;
101 use Moose;
102
103 has x => ( is => "rw", predicate => "has_x" );
104 has y => ( is => "ro" );
105 has z => ( reader => "z", setter => "set_z" );
de2f2e97 106 has ref => ( is => "rw", weak_ref => 1 );
4c6fbfb1 107 has i => ( isa => "Int", is => "rw" );
108 has s => ( isa => "Str", is => "rw" );
109 has a => ( isa => "ArrayRef", is => "rw" );
a812574b 110 has o => ( isa => "Object", is => "rw" );
111 has f => ( isa => "Foo", is => "rw" );
112 has c => ( isa => "ClassName", is => "rw" );
160f9ca7 113
a812574b 114 # FIXME Regexp, ScalarRef, parametrized, filehandle
de2f2e97 115}
116
117{
a812574b 118 my ( $x, $y, $z, $ref, $a, $s, $i, $o, $f, $c ) = map { Foo->meta->get_attribute($_) } qw(x y z ref a s i o f c);
de2f2e97 119 $x->Moose::XS::new_accessor("Foo::x");
120 $x->Moose::XS::new_predicate("Foo::has_x");
121 $y->Moose::XS::new_getter("Foo::y");
122 $z->Moose::XS::new_getter("Foo::z");
123 $z->Moose::XS::new_setter("Foo::set_z");
124 $ref->Moose::XS::new_accessor("Foo::ref");
4c6fbfb1 125 $a->Moose::XS::new_accessor("Foo::a");
126 $s->Moose::XS::new_accessor("Foo::s");
127 $i->Moose::XS::new_accessor("Foo::i");
a812574b 128 $o->Moose::XS::new_accessor("Foo::o");
129 $f->Moose::XS::new_accessor("Foo::f");
130 $c->Moose::XS::new_accessor("Foo::c");
1ea12c91 131}
132
1ea12c91 133
de2f2e97 134my $ref = [ ];
135
136my $foo = Foo->new( x => "ICKS", y => "WHY", z => "ZEE", ref => $ref );
1ea12c91 137
138is( $foo->x, "ICKS" );
139is( $foo->y, "WHY" );
140is( $foo->z, "ZEE" );
de2f2e97 141is( $foo->ref, $ref, );
1ea12c91 142
143lives_ok { $foo->x("YASE") };
144
145is( $foo->x, "YASE" );
146
147dies_ok { $foo->y("blah") };
148
149is( $foo->y, "WHY" );
150
151dies_ok { $foo->z("blah") };
152
153is( $foo->z, "ZEE" );
154
155lives_ok { $foo->set_z("new") };
156
157is( $foo->z, "new" );
158
159ok( $foo->has_x );
160
161ok( !Foo->new->has_x );
162
de2f2e97 163undef $ref;
164
165is( $foo->ref(), undef );
166
167$ref = { };
168
169$foo->ref($ref);
170
171is( $foo->ref, $ref, );
172
173undef $ref;
174
175is( $foo->ref(), undef );
176
4c6fbfb1 177ok( !eval { $foo->a("not a ref"); 1 } );
178ok( !eval { $foo->i(1.3); 1 } );
179ok( !eval { $foo->s(undef); 1 } );
a812574b 180ok( !eval { $foo->o({}); 1 } );
181ok( !eval { $foo->f(bless {}, "Bar"); 1 } );
182ok( !eval { $foo->c("Horse"); 1 } );
4c6fbfb1 183
184ok( eval { $foo->a([]); 1 } );
185ok( eval { $foo->i(3); 1 } );
186ok( eval { $foo->s("foo"); 1 } );
a812574b 187ok( eval { $foo->o(bless {}, "Bar"); 1 } );
188ok( eval { $foo->f(Foo->new); 1 } );
189ok( eval { $foo->c("Foo"); 1 } );
4c6fbfb1 190
de2f2e97 191use Data::Dumper;
192warn Dumper($foo);