Add explicit return values for (almost) all native delegation mutating methods
[gitmo/Moose.git] / t / 070_native_traits / 060_trait_number.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8b9641b8 6use lib 't/lib';
7
f1ae167f 8use Moose ();
2c963694 9use Moose::Util::TypeConstraints;
8b9641b8 10use NoInlineAttribute;
f1ae167f 11use Test::Exception;
a28e50e4 12use Test::More;
e3c07b19 13use Test::Moose;
14
e3c07b19 15{
f1ae167f 16 my %handles = (
17 abs => 'abs',
18 add => 'add',
19 inc => [ add => 1 ],
20 div => 'div',
21 cut_in_half => [ div => 2 ],
22 mod => 'mod',
23 odd => [ mod => 2 ],
24 mul => 'mul',
25 set => 'set',
26 sub => 'sub',
27 dec => [ sub => 1 ],
e3c07b19 28 );
f1ae167f 29
30 my $name = 'Foo1';
31
32 sub build_class {
33 my %attr = @_;
34
35 my $class = Moose::Meta::Class->create(
36 $name++,
37 superclasses => ['Moose::Object'],
38 );
39
8b9641b8 40 my @traits = 'Number';
41 push @traits, 'NoInlineAttribute'
42 if delete $attr{no_inline};
43
f1ae167f 44 $class->add_attribute(
45 integer => (
8b9641b8 46 traits => \@traits,
f1ae167f 47 is => 'ro',
48 isa => 'Int',
49 default => 5,
50 handles => \%handles,
51 clearer => '_clear_integer',
52 %attr,
53 ),
54 );
55
56 return ( $class->name, \%handles );
57 }
e3c07b19 58}
59
f1ae167f 60{
61 run_tests(build_class);
62 run_tests( build_class( lazy => 1 ) );
cf0da4e2 63 run_tests( build_class( trigger => sub { } ) );
8b9641b8 64 run_tests( build_class( no_inline => 1 ) );
2c963694 65
66 # Will force the inlining code to check the entire hashref when it is modified.
67 subtype 'MyInt', as 'Int', where { 1 };
68
69 run_tests( build_class( isa => 'MyInt' ) );
70
71 coerce 'MyInt', from 'Int', via { $_ };
72
73 run_tests( build_class( isa => 'MyInt', coerce => 1 ) );
f1ae167f 74}
e3c07b19 75
f1ae167f 76sub run_tests {
77 my ( $class, $handles ) = @_;
e3c07b19 78
f1ae167f 79 can_ok( $class, $_ ) for sort keys %{$handles};
e3c07b19 80
f1ae167f 81 with_immutable {
82 my $obj = $class->new;
e3c07b19 83
f1ae167f 84 is( $obj->integer, 5, 'Default to five' );
e3c07b19 85
7f5ec80d 86 is( $obj->add(10), 15, 'add returns new value' );
e3c07b19 87
f1ae167f 88 is( $obj->integer, 15, 'Add ten for fithteen' );
e3c07b19 89
167ec185 90 throws_ok { $obj->add( 10, 2 ) }
91 qr/Cannot call add with more than 1 argument/,
92 'add throws an error when 2 arguments are passed';
93
7f5ec80d 94 is( $obj->sub(3), 12, 'sub returns new value' );
e3c07b19 95
f1ae167f 96 is( $obj->integer, 12, 'Subtract three for 12' );
e3c07b19 97
167ec185 98 throws_ok { $obj->sub( 10, 2 ) }
99 qr/Cannot call sub with more than 1 argument/,
100 'sub throws an error when 2 arguments are passed';
101
7f5ec80d 102 is( $obj->set(10), 10, 'set returns new value' );
e3c07b19 103
f1ae167f 104 is( $obj->integer, 10, 'Set to ten' );
e3c07b19 105
167ec185 106 throws_ok { $obj->set( 10, 2 ) }
107 qr/Cannot call set with more than 1 argument/,
108 'set throws an error when 2 arguments are passed';
109
7f5ec80d 110 is( $obj->div(2), 5, 'div returns new value' );
e3c07b19 111
f1ae167f 112 is( $obj->integer, 5, 'divide by 2' );
e3c07b19 113
167ec185 114 throws_ok { $obj->div( 10, 2 ) }
115 qr/Cannot call div with more than 1 argument/,
116 'div throws an error when 2 arguments are passed';
117
7f5ec80d 118 is( $obj->mul(2), 10, 'mul returns new value' );
e3c07b19 119
f1ae167f 120 is( $obj->integer, 10, 'multiplied by 2' );
e3c07b19 121
167ec185 122 throws_ok { $obj->mul( 10, 2 ) }
123 qr/Cannot call mul with more than 1 argument/,
124 'mul throws an error when 2 arguments are passed';
125
7f5ec80d 126 is( $obj->mod(2), 0, 'mod returns new value' );
e3c07b19 127
f1ae167f 128 is( $obj->integer, 0, 'Mod by 2' );
e3c07b19 129
167ec185 130 throws_ok { $obj->mod( 10, 2 ) }
131 qr/Cannot call mod with more than 1 argument/,
132 'mod throws an error when 2 arguments are passed';
133
f1ae167f 134 $obj->set(7);
e3c07b19 135
f1ae167f 136 $obj->mod(5);
e3c07b19 137
f1ae167f 138 is( $obj->integer, 2, 'Mod by 5' );
e3c07b19 139
f1ae167f 140 $obj->set(-1);
e3c07b19 141
7f5ec80d 142 is( $obj->abs, 1, 'abs returns new value' );
59de9de4 143
167ec185 144 throws_ok { $obj->abs(10) }
145 qr/Cannot call abs with any arguments/,
146 'abs throws an error when an argument is passed';
147
f1ae167f 148 is( $obj->integer, 1, 'abs 1' );
59de9de4 149
f1ae167f 150 $obj->set(12);
59de9de4 151
f1ae167f 152 $obj->inc;
59de9de4 153
f1ae167f 154 is( $obj->integer, 13, 'inc 12' );
59de9de4 155
f1ae167f 156 $obj->dec;
e3c07b19 157
f1ae167f 158 is( $obj->integer, 12, 'dec 13' );
d50fc84a 159
f1ae167f 160 if ( $class->meta->get_attribute('integer')->is_lazy ) {
161 my $obj = $class->new;
162
163 $obj->add(2);
164
165 is( $obj->integer, 7, 'add with lazy default' );
166
167 $obj->_clear_integer;
168
169 $obj->mod(2);
170
171 is( $obj->integer, 1, 'mod with lazy default' );
172 }
173 }
174 $class;
175}
e3c07b19 176
a28e50e4 177done_testing;