Use Test::Moose
[gitmo/MooseX-StrictConstructor.git] / t / basic.t
index d06235f..81f0f6b 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -1,8 +1,9 @@
 use strict;
 use warnings;
 
-use Test::More tests => 6;
-
+use Test::Exception;
+use Test::Moose qw( with_immutable );
+use Test::More;
 
 {
     package Standard;
@@ -15,6 +16,7 @@ use Test::More tests => 6;
 {
     package Stricter;
 
+    use Moose;
     use MooseX::StrictConstructor;
 
     has 'thing' => ( is => 'rw' );
@@ -23,6 +25,7 @@ use Test::More tests => 6;
 {
     package Subclass;
 
+    use Moose;
     use MooseX::StrictConstructor;
 
     extends 'Stricter';
@@ -33,12 +36,12 @@ use Test::More tests => 6;
 {
     package Tricky;
 
+    use Moose;
     use MooseX::StrictConstructor;
 
     has 'thing' => ( is => 'rw' );
 
-    sub BUILD
-    {
+    sub BUILD {
         my $self   = shift;
         my $params = shift;
 
@@ -46,21 +49,54 @@ use Test::More tests => 6;
     }
 }
 
+{
+    package InitArg;
+
+    use Moose;
+    use MooseX::StrictConstructor;
+
+    has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
+    has 'size'  => ( is => 'rw', 'init_arg' => undef );
+}
+
+my @classes = qw( Standard Stricter Subclass Tricky InitArg );
+
+with_immutable {
+    lives_ok { Standard->new( thing => 1, bad => 99 ) }
+    'standard Moose class ignores unknown params';
+
+    throws_ok { Stricter->new( thing => 1, bad => 99 ) }
+    qr/unknown attribute.+: bad/,
+        'strict constructor blows up on unknown params';
+
+    lives_ok { Subclass->new( thing => 1, size => 'large' ) }
+    'subclass constructor handles known attributes correctly';
+
+    throws_ok { Subclass->new( thing => 1, bad => 99 ) }
+    qr/unknown attribute.+: bad/,
+        'subclass correctly recognizes bad attribute';
+
+    lives_ok { Tricky->new( thing => 1, spy => 99 ) }
+    'can work around strict constructor by deleting params in BUILD()';
 
-eval { Standard->new( thing => 1, bad => 99 ) };
-is( $@, '', 'standard Moose class ignores unknown params' );
+    throws_ok { Tricky->new( thing => 1, agent => 99 ) }
+    qr/unknown attribute.+: agent/,
+        'Tricky still blows up on unknown params other than spy';
 
-eval { Stricter->new( thing => 1, bad => 99 ) };
-like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
+    throws_ok { Subclass->new( thing => 1, bad => 99 ) }
+    qr/unknown attribute.+: bad/,
+        'subclass constructor blows up on unknown params';
 
-eval { Tricky->new( thing => 1, spy => 99 ) };
-is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
+    throws_ok { InitArg->new( thing => 1 ) }
+    qr/unknown attribute.+: thing/,
+        'InitArg blows up with attribute name';
 
-eval { Tricky->new( thing => 1, agent => 99 ) };
-like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
+    throws_ok { InitArg->new( size => 1 ) }
+    qr/unknown attribute.+: size/,
+        'InitArg blows up when given attribute with undef init_arg';
 
-eval { Subclass->new( thing => 1, bad => 99 ) };
-like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
+    lives_ok { InitArg->new( other => 1 ) }
+    'InitArg works when given proper init_arg';
+} @classes;
 
-eval { Subclass->new( thing => 1, size => 'large' ) };
-is( $@, '', 'subclass constructor handles known attributes correctly' );
+done_testing();