new tests for default behaviour, using classes in first test file
[gitmo/MooseX-UndefTolerant.git] / t / defaults.t
index aa8680a..b43d012 100644 (file)
@@ -1,23 +1,98 @@
-#!/usr/bin/perl
+use Test::More tests => 22;
 
-use Test::More;
+use MooseX::UndefTolerant::Attribute ();
 
-package Foo;
+{
+    package Foo;
+    use Moose;
 
-use Moose;
-use MooseX::UndefTolerant::Attribute;
+    has 'attr1' => (
+        traits => [ qw(MooseX::UndefTolerant::Attribute)],
+        is => 'ro',
+        isa => 'Num',
+        predicate => 'has_attr1',
+        default => 1,
+    );
+    has 'attr2' => (
+        is => 'ro',
+        isa => 'Num',
+        predicate => 'has_attr2',
+        default => 2,
+    );
+}
 
-has bar => (
-   is => 'rw',
-   traits => ['MooseX::UndefTolerant::Attribute'],
-   default => 'baz'
-);
+{
+    package Bar;
+    use Moose;
+    use MooseX::UndefTolerant;
+
+    has 'attr1' => (
+        is => 'ro',
+        isa => 'Num',
+        predicate => 'has_attr1',
+        default => 1,
+    );
+    has 'attr2' => (
+        is => 'ro',
+        isa => 'Num',
+        predicate => 'has_attr2',
+        default => 2,
+    );
+}
 
-1;
 
 package main;
 
-my $foo = Foo->new( bar => undef );
-is ( $foo->bar, 'baz', 'does the default value get set when passing undef in the constructor' );
+note 'Default behaviour';
+note '';
+
+note 'Testing class with a single UndefTolerant attribute';
+{
+    my $obj = Foo->new;
+    ok($obj->has_attr1, 'attr1 has a value');
+    ok($obj->has_attr2, 'attr2 has a value');
+    is($obj->attr1, 1, 'attr1\'s value is its default');
+    is($obj->attr2, 2, 'attr2\'s value is its default');
+}
+
+{
+    my $obj = Foo->new(attr1 => undef);
+    ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
+    is($obj->attr1, 1, 'attr1\'s value is its default');
+    is($obj->attr2, 2, 'attr2\'s value is its default');
+}
+
+{
+    my $obj = Foo->new(attr1 => 1234, attr2 => 5678);
+    is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
+    ok($obj->has_attr1, '...and the predicate returns true as normal');
+    is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
+    ok($obj->has_attr2, '...and the predicate returns true as normal');
+}
+
+
+note '';
+note 'Testing class with the entire class being UndefTolerant';
+{
+    my $obj = Bar->new;
+    ok($obj->has_attr1, 'attr1 has a value');
+    ok($obj->has_attr2, 'attr2 has a value');
+    is($obj->attr1, 1, 'attr1\'s value is its default');
+    is($obj->attr2, 2, 'attr2\'s value is its default');
+}
+
+{
+    my $obj = Bar->new(attr1 => undef);
+    ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
+    is($obj->attr1, 1, 'attr1\'s value is its default');
+    is($obj->attr2, 2, 'attr2\'s value is its default');
+}
+
+{
+    my $obj = Bar->new(attr1 => 1234, attr2 => 5678);
+    is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
+    ok($obj->has_attr1, '...and the predicate returns true as normal');
+    is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
+    ok($obj->has_attr2, '...and the predicate returns true as normal');
+}
 
-done_testing;