7587d57d3a9cab53c917a317662646998c98067d
[gitmo/Moose.git] / t / 070_native_traits / 020_trait_bool.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Moose ();
7 use Test::More;
8 use Test::Exception;
9 use Test::Moose;
10
11 {
12     my %handles = (
13         illuminate  => 'set',
14         darken      => 'unset',
15         flip_switch => 'toggle',
16         is_dark     => 'not',
17     );
18
19     my $name = 'Foo1';
20
21     sub build_class {
22         my %attr = @_;
23
24         my $class = Moose::Meta::Class->create(
25             $name++,
26             superclasses => ['Moose::Object'],
27         );
28
29         $class->add_attribute(
30             is_lit => (
31                 traits  => ['Bool'],
32                 is      => 'rw',
33                 isa     => 'Bool',
34                 default => 0,
35                 handles => \%handles,
36                 clearer => '_clear_is_list',
37                 %attr,
38             ),
39         );
40
41         return ( $class->name, \%handles );
42     }
43 }
44
45 {
46     run_tests(build_class);
47     run_tests( build_class( lazy => 1 ) );
48 }
49
50 sub run_tests {
51     my ( $class, $handles ) = @_;
52
53     can_ok( $class, $_ ) for sort keys %{$handles};
54
55     with_immutable {
56         my $obj = $class->new;
57
58         $obj->illuminate;
59         ok( $obj->is_lit,   'set is_lit to 1 using ->illuminate' );
60         ok( !$obj->is_dark, 'check if is_dark does the right thing' );
61
62         throws_ok { $obj->illuminate(1) }
63         qr/Cannot call set with any arguments/,
64             'set throws an error when an argument is passed';
65
66         $obj->darken;
67         ok( !$obj->is_lit, 'set is_lit to 0 using ->darken' );
68         ok( $obj->is_dark, 'check if is_dark does the right thing' );
69
70         throws_ok { $obj->darken(1) }
71         qr/Cannot call unset with any arguments/,
72             'unset throws an error when an argument is passed';
73
74         $obj->flip_switch;
75         ok( $obj->is_lit,   'toggle is_lit back to 1 using ->flip_switch' );
76         ok( !$obj->is_dark, 'check if is_dark does the right thing' );
77
78         throws_ok { $obj->flip_switch(1) }
79         qr/Cannot call toggle with any arguments/,
80             'toggle throws an error when an argument is passed';
81
82         $obj->flip_switch;
83         ok( !$obj->is_lit,
84             'toggle is_lit back to 0 again using ->flip_switch' );
85         ok( $obj->is_dark, 'check if is_dark does the right thing' );
86     }
87     $class;
88 }
89
90 done_testing;