From: Jesse Luehrs Date: Thu, 8 Mar 2012 06:28:54 +0000 (-0600) Subject: clean some things up, add some more tests X-Git-Tag: 2.0500~23 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose.git;a=commitdiff_plain;h=4a0bc88fdddbc5c840810dd31b8873c88ff85282 clean some things up, add some more tests --- diff --git a/lib/Class/MOP/Mixin/HasMethods.pm b/lib/Class/MOP/Mixin/HasMethods.pm index bfea578..9ecfd69 100644 --- a/lib/Class/MOP/Mixin/HasMethods.pm +++ b/lib/Class/MOP/Mixin/HasMethods.pm @@ -208,6 +208,7 @@ sub _full_method_map { my $overload_operators; sub overload_operators { $overload_operators ||= [map { split /\s+/ } values %overload::ops]; + return @$overload_operators; } # XXX this could probably stand to be cached, but i figure it should be @@ -218,7 +219,7 @@ sub _overload_map { return {} unless overload::Overloaded($self->name); my %map; - for my $op (@{ $self->overload_operators }) { + for my $op ($self->overload_operators) { my $body = overload::Method($self->name, $op); next unless defined $body; $map{$op} = $body; @@ -252,7 +253,7 @@ sub get_overloaded_operator { return $self->_wrap_overload($op, $body); } -sub add_overload { +sub add_overloaded_operator { my $self = shift; my ($op, $body) = @_; $self->name->overload::OVERLOAD($op => $body); diff --git a/t/metaclasses/overloading.t b/t/metaclasses/overloading.t index 8c5bdd1..3621dd9 100644 --- a/t/metaclasses/overloading.t +++ b/t/metaclasses/overloading.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Fatal; { package Foo; @@ -9,13 +10,25 @@ use Test::More; } { - is_deeply([Foo->meta->get_overload_list], []); - is_deeply([Foo->meta->get_overloaded_operators], []); + my $meta = Foo->meta; + + is_deeply([sort $meta->overload_operators], + [sort map { split /\s+/ } values %overload::ops]); + + ok(!$meta->has_overloaded_operator('+')); + ok(!$meta->has_overloaded_operator('-')); + + is_deeply([$meta->get_overloaded_operators], []); + + is_deeply([$meta->get_overload_list], []); + + is($meta->get_overloaded_operator('+'), undef); + is($meta->get_overloaded_operator('-'), undef); } -my $plus; +my $plus = 0; my $plus_impl; -BEGIN { $plus_impl = sub { $plus = 1; $_[0] + $_[1] } } +BEGIN { $plus_impl = sub { $plus = 1; "plus" } } { package Foo::Overloaded; use Moose; @@ -23,8 +36,14 @@ BEGIN { $plus_impl = sub { $plus = 1; $_[0] + $_[1] } } } { - is_deeply([Foo::Overloaded->meta->get_overloaded_operators], ['+']); - my @overloads = Foo::Overloaded->meta->get_overload_list; + my $meta = Foo::Overloaded->meta; + + ok($meta->has_overloaded_operator('+')); + ok(!$meta->has_overloaded_operator('-')); + + is_deeply([$meta->get_overloaded_operators], ['+']); + + my @overloads = $meta->get_overload_list; is(scalar(@overloads), 1); my $plus_meth = $overloads[0]; isa_ok($plus_meth, 'Class::MOP::Method::Overload'); @@ -32,7 +51,45 @@ BEGIN { $plus_impl = sub { $plus = 1; $_[0] + $_[1] } } is($plus_meth->name, '(+'); is($plus_meth->body, $plus_impl); is($plus_meth->package_name, 'Foo::Overloaded'); - is($plus_meth->associated_metaclass, Foo::Overloaded->meta); + is($plus_meth->associated_metaclass, $meta); + + my $plus_meth2 = $meta->get_overloaded_operator('+'); + { local $TODO = "we don't cache these yet"; + is($plus_meth2, $plus_meth); + } + is($plus_meth2->operator, '+'); + is($plus_meth2->body, $plus_impl); + is($meta->get_overloaded_operator('-'), undef); + + is($plus, 0); + is(Foo::Overloaded->new + Foo::Overloaded->new, "plus"); + is($plus, 1); + + my $minus = 0; + my $minus_impl = sub { $minus = 1; "minus" }; + + like(exception { Foo::Overloaded->new - Foo::Overloaded->new }, + qr/Operation "-": no method found/); + + $meta->add_overloaded_operator('-' => $minus_impl); + + ok($meta->has_overloaded_operator('-')); + + is_deeply([sort $meta->get_overloaded_operators], ['+', '-']); + + is(scalar($meta->get_overload_list), 2); + + my $minus_meth = $meta->get_overloaded_operator('-'); + isa_ok($minus_meth, 'Class::MOP::Method::Overload'); + is($minus_meth->operator, '-'); + is($minus_meth->name, '(-'); + is($minus_meth->body, $minus_impl); + is($minus_meth->package_name, 'Foo::Overloaded'); + is($minus_meth->associated_metaclass, $meta); + + is($minus, 0); + is(Foo::Overloaded->new - Foo::Overloaded->new, "minus"); + is($minus, 1); } done_testing;