From: Paul Driver Date: Fri, 11 Apr 2008 15:15:10 +0000 (+0000) Subject: Added unit tests for the new COmposite functionality. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ec15520371bd8f2c8067b970816860d4c9b2f713;p=gitmo%2FMooseX-AttributeHelpers.git Added unit tests for the new COmposite functionality. --- diff --git a/t/030_composite.t b/t/030_composite.t new file mode 100644 index 0000000..1972132 --- /dev/null +++ b/t/030_composite.t @@ -0,0 +1,86 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 11; +use Test::Exception; +use Moose::Meta::Class; + +BEGIN { + use_ok('MooseX::AttributeHelpers'); +} + +my $test_index = 0; +my %defaults = ( + is => 'ro', + isa => 'Int', + default => 0, + provides => { + Counter => { + inc => 'inc_foo', + reset => 'reset_foo', + }, + Number => { + mul => 'mul_foo', + }, + }, +); +sub make_test_class { + ++$test_index; + my %overrides = @_; + my $options = {%defaults}; + while (my ($k,$v) = each(%overrides)) { + $options->{$k} = $v; + } + my $classname = "MooseX::AttributeHelpers::Test::$test_index"; + + my $meta = Moose::Meta::Class->create($classname, + superclasses => ['Moose::Object'], + ); + $meta->add_attribute(foo => $options); + return $classname; +} +sub trait { traits => ['MooseX::AttributeHelpers::Composite::Trait'] } +sub metaclass { metaclass => 'Composite' } + +sub simple_test { + my $class = make_test_class(@_); + my $foo = $class->new; + $foo->inc_foo; + $foo->inc_foo; + $foo->mul_foo(3); + is($foo->foo, 6, "math is correct"); + return $foo->foo; +} + +lives_ok { + simple_test(metaclass()); +} '... works with metaclass'; + +lives_ok { + simple_test(trait()); +} '... works with trait'; + +dies_ok { + simple_test(metaclass(), isa => 'Num'); +} 'dies without proper subtype'; + +my $class = make_test_class(trait()); + +#check meta + +is_deeply($class->meta->get_attribute('foo')->{provides}, { + Counter => { + inc => 'inc_foo', + reset => 'reset_foo', + }, + Number => { + mul => 'mul_foo', + }, +}, 'provides is correct'); + +ok($class->can('inc_foo'), 'can increment'); +ok($class->can('reset_foo'), 'can reset'); +ok($class->can('mul_foo'), 'can multiply'); +ok(!$class->can('div_foo'), 'cannot do anything else');