Add a test file for duck_type
gfx [Wed, 9 Dec 2009 12:54:11 +0000 (21:54 +0900)]
t/001_mouse/062-duck_type.t [new file with mode: 0644]

diff --git a/t/001_mouse/062-duck_type.t b/t/001_mouse/062-duck_type.t
new file mode 100644 (file)
index 0000000..d4a5396
--- /dev/null
@@ -0,0 +1,53 @@
+#!perl
+use strict;
+use warnings;
+
+use Test::More tests => 15;
+
+use Mouse::Util::TypeConstraints qw(duck_type);
+
+{
+    package Foo;
+    use Mouse;
+
+    sub a {}
+
+    package Bar;
+    use Mouse;
+
+    extends qw(Foo);
+
+    sub b {}
+
+    package Baz;
+    use Mouse;
+
+    sub can {
+        my($class, $method) = @_;
+        return $method eq 'b';
+    }
+}
+
+my $CanA   = duck_type CanA => qw(a);
+my $CanB  = duck_type CanB => [qw(b)];
+my $CanAB = duck_type [qw(a b)];
+
+is $CanA->name, 'CanA';
+is $CanB->name, 'CanB';
+is $CanAB->name, '__ANON__';
+
+ok $CanA->check(Foo->new);
+ok $CanA->check(Bar->new);
+ok!$CanA->check(Baz->new);
+
+ok!$CanB->check(Foo->new);
+ok $CanB->check(Bar->new);
+ok $CanB->check(Baz->new);
+
+ok!$CanAB->check(Foo->new);
+ok $CanAB->check(Bar->new);
+ok!$CanAB->check(Baz->new);
+
+ok!$CanA->check(undef);
+ok!$CanA->check(1);
+ok!$CanA->check({});