15 ok(!$meta->is_overloaded);
17 is_deeply([sort $meta->overload_operators],
18 [sort map { split /\s+/ } values %overload::ops]);
20 ok(!$meta->has_overloaded_operator('+'));
21 ok(!$meta->has_overloaded_operator('-'));
23 is_deeply([$meta->get_overload_list], []);
25 is_deeply([$meta->get_all_overloaded_operators], []);
27 is($meta->get_overloaded_operator('+'), undef);
28 is($meta->get_overloaded_operator('-'), undef);
33 BEGIN { $plus_impl = sub { $plus = 1; "plus" } }
35 package Foo::Overloaded;
37 use overload '+' => $plus_impl;
41 my $meta = Foo::Overloaded->meta;
43 ok($meta->is_overloaded);
45 ok($meta->has_overloaded_operator('+'));
46 ok(!$meta->has_overloaded_operator('-'));
48 is_deeply([$meta->get_overload_list], ['+']);
50 my @overloads = $meta->get_all_overloaded_operators;
51 is(scalar(@overloads), 1);
52 my $plus_meth = $overloads[0];
53 isa_ok($plus_meth, 'Class::MOP::Method::Overload');
54 is($plus_meth->operator, '+');
55 is($plus_meth->name, '(+');
56 is($plus_meth->body, $plus_impl);
57 is($plus_meth->package_name, 'Foo::Overloaded');
58 is($plus_meth->associated_metaclass, $meta);
60 my $plus_meth2 = $meta->get_overloaded_operator('+');
61 { local $TODO = "we don't cache these yet";
62 is($plus_meth2, $plus_meth);
64 is($plus_meth2->operator, '+');
65 is($plus_meth2->body, $plus_impl);
66 is($meta->get_overloaded_operator('-'), undef);
69 is(Foo::Overloaded->new + Foo::Overloaded->new, "plus");
73 my $minus_impl = sub { $minus = 1; "minus" };
75 like(exception { Foo::Overloaded->new - Foo::Overloaded->new },
76 qr/Operation "-": no method found/);
78 $meta->add_overloaded_operator('-' => $minus_impl);
80 ok($meta->has_overloaded_operator('-'));
82 is_deeply([sort $meta->get_overload_list], ['+', '-']);
84 is(scalar($meta->get_all_overloaded_operators), 2);
86 my $minus_meth = $meta->get_overloaded_operator('-');
87 isa_ok($minus_meth, 'Class::MOP::Method::Overload');
88 is($minus_meth->operator, '-');
89 is($minus_meth->name, '(-');
90 is($minus_meth->body, $minus_impl);
91 is($minus_meth->package_name, 'Foo::Overloaded');
92 is($minus_meth->associated_metaclass, $meta);
95 is(Foo::Overloaded->new - Foo::Overloaded->new, "minus");
98 $meta->remove_overloaded_operator('-');
100 like(exception { Foo::Overloaded->new - Foo::Overloaded->new },
101 qr/Operation "-": no method found/);
107 package Foo::OverloadedMethod;
109 use overload '*' => 'times';
111 sub times { $times = 1; "times" }
112 sub divided { $divided = 1; "divided" }
116 my $meta = Foo::OverloadedMethod->meta;
118 ok($meta->is_overloaded);
120 ok($meta->has_overloaded_operator('*'));
121 ok(!$meta->has_overloaded_operator('/'));
123 is_deeply([$meta->get_overload_list], ['*']);
125 my @overloads = $meta->get_all_overloaded_operators;
126 is(scalar(@overloads), 1);
127 my $times_meth = $overloads[0];
128 isa_ok($times_meth, 'Class::MOP::Method::Overload');
129 is($times_meth->operator, '*');
130 is($times_meth->name, '(*');
131 is($times_meth->body, $meta->get_method('times')->body);
132 is($times_meth->package_name, 'Foo::OverloadedMethod');
133 is($times_meth->associated_metaclass, $meta);
135 my $times_meth2 = $meta->get_overloaded_operator('*');
136 { local $TODO = "we don't cache these yet";
137 is($times_meth2, $times_meth);
139 is($times_meth2->operator, '*');
140 is($times_meth2->body, $meta->get_method('times')->body);
141 is($meta->get_overloaded_operator('/'), undef);
144 is(Foo::OverloadedMethod->new * Foo::OverloadedMethod->new, "times");
147 like(exception { Foo::OverloadedMethod->new / Foo::OverloadedMethod->new },
148 qr{Operation "/": no method found});
150 $meta->add_overloaded_operator('/' => 'divided');
152 ok($meta->has_overloaded_operator('/'));
154 is_deeply([sort $meta->get_overload_list], ['*', '/']);
156 is(scalar($meta->get_all_overloaded_operators), 2);
158 my $divided_meth = $meta->get_overloaded_operator('/');
159 isa_ok($divided_meth, 'Class::MOP::Method::Overload');
160 is($divided_meth->operator, '/');
161 is($divided_meth->name, '(/');
162 is($divided_meth->body, $meta->get_method('divided')->body);
163 is($divided_meth->package_name, 'Foo::OverloadedMethod');
164 is($divided_meth->associated_metaclass, $meta);
167 is(Foo::OverloadedMethod->new / Foo::OverloadedMethod->new, "divided");
170 $meta->remove_overloaded_operator('/');
172 like(exception { Foo::OverloadedMethod->new / Foo::OverloadedMethod->new },
173 qr{Operation "/": no method found});