look at init_arg, not attribute name
[gitmo/MooseX-StrictConstructor.git] / t / basic.t
index e65d3f2..65aef05 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 7;
+use Test::More tests => 12;
 
 
 {
@@ -47,6 +47,15 @@ use Test::More tests => 7;
 }
 
 {
+    package InitArg;
+
+    use MooseX::StrictConstructor;
+
+    has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
+    has 'size'  => ( is => 'rw', 'init_arg' => undef );
+}
+
+{
     package Immutable;
 
     use MooseX::StrictConstructor;
@@ -57,6 +66,25 @@ use Test::More tests => 7;
     __PACKAGE__->meta()->make_immutable();
 }
 
+{
+    package ImmutableTricky;
+
+    use MooseX::StrictConstructor;
+
+    has 'thing' => ( is => 'rw' );
+
+    sub BUILD
+    {
+        my $self   = shift;
+        my $params = shift;
+
+        delete $params->{spy};
+    }
+
+    no Moose;
+    __PACKAGE__->meta()->make_immutable();
+}
+
 
 eval { Standard->new( thing => 1, bad => 99 ) };
 is( $@, '', 'standard Moose class ignores unknown params' );
@@ -79,3 +107,23 @@ is( $@, '', 'subclass constructor handles known attributes correctly' );
 eval { Immutable->new( thing => 1, bad => 99 ) };
 like( $@, qr/unknown attribute.+: bad/,
       'strict constructor in immutable class blows up on unknown params' );
+
+eval { ImmutableTricky->new( thing => 1, spy => 99 ) };
+is( $@, '',
+    'immutable class can work around strict constructor by deleting params in BUILD()' );
+
+eval { ImmutableTricky->new( thing => 1, agent => 99 ) };
+like( $@, qr/unknown attribute.+: agent/,
+      'ImmutableTricky still blows up on unknown params other than spy' );
+
+eval { InitArg->new( thing => 1 ) };
+like( $@, qr/unknown attribute.+: thing/,
+      'InitArg blows up with attribute name' );
+
+eval { InitArg->new( size => 1 ) };
+like( $@, qr/unknown attribute.+: size/,
+      'InitArg blows up when given attribute with undef init_arg' );
+
+eval { InitArg->new( other => 1 ) };
+is( $@, '',
+    'InitArg works when given proper init_arg' );