Commit | Line | Data |
ec155203 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 11; |
7 | use Test::Exception; |
8 | use Moose::Meta::Class; |
9 | |
10 | BEGIN { |
11 | use_ok('MooseX::AttributeHelpers'); |
12 | } |
13 | |
14 | my $test_index = 0; |
15 | my %defaults = ( |
16 | is => 'ro', |
17 | isa => 'Int', |
18 | default => 0, |
19 | provides => { |
20 | Counter => { |
21 | inc => 'inc_foo', |
22 | reset => 'reset_foo', |
23 | }, |
24 | Number => { |
25 | mul => 'mul_foo', |
26 | }, |
27 | }, |
28 | ); |
29 | sub make_test_class { |
30 | ++$test_index; |
31 | my %overrides = @_; |
32 | my $options = {%defaults}; |
33 | while (my ($k,$v) = each(%overrides)) { |
34 | $options->{$k} = $v; |
35 | } |
36 | my $classname = "MooseX::AttributeHelpers::Test::$test_index"; |
37 | |
38 | my $meta = Moose::Meta::Class->create($classname, |
39 | superclasses => ['Moose::Object'], |
40 | ); |
41 | $meta->add_attribute(foo => $options); |
42 | return $classname; |
43 | } |
44 | sub trait { traits => ['MooseX::AttributeHelpers::Composite::Trait'] } |
45 | sub metaclass { metaclass => 'Composite' } |
46 | |
47 | sub simple_test { |
48 | my $class = make_test_class(@_); |
49 | my $foo = $class->new; |
50 | $foo->inc_foo; |
51 | $foo->inc_foo; |
52 | $foo->mul_foo(3); |
53 | is($foo->foo, 6, "math is correct"); |
54 | return $foo->foo; |
55 | } |
56 | |
57 | lives_ok { |
58 | simple_test(metaclass()); |
59 | } '... works with metaclass'; |
60 | |
61 | lives_ok { |
62 | simple_test(trait()); |
63 | } '... works with trait'; |
64 | |
65 | dies_ok { |
66 | simple_test(metaclass(), isa => 'Num'); |
67 | } 'dies without proper subtype'; |
68 | |
69 | my $class = make_test_class(trait()); |
70 | |
71 | #check meta |
72 | |
73 | is_deeply($class->meta->get_attribute('foo')->{provides}, { |
74 | Counter => { |
75 | inc => 'inc_foo', |
76 | reset => 'reset_foo', |
77 | }, |
78 | Number => { |
79 | mul => 'mul_foo', |
80 | }, |
81 | }, 'provides is correct'); |
82 | |
83 | ok($class->can('inc_foo'), 'can increment'); |
84 | ok($class->can('reset_foo'), 'can reset'); |
85 | ok($class->can('mul_foo'), 'can multiply'); |
86 | ok(!$class->can('div_foo'), 'cannot do anything else'); |