The start of a working implementation. Several things left to be
[gitmo/MooseX-ClassAttribute.git] / t / lib / SharedTests.pm
diff --git a/t/lib/SharedTests.pm b/t/lib/SharedTests.pm
new file mode 100644 (file)
index 0000000..3d9c242
--- /dev/null
@@ -0,0 +1,86 @@
+package SharedTests;
+
+use strict;
+use warnings;
+
+use Scalar::Util qw( isweak );
+use Test::More tests => 9;
+
+
+{
+    package HasClassAttribute;
+
+    use Moose;
+    use MooseX::ClassAttribute;
+
+    has 'ObjectCount' =>
+        ( metaclass => 'ClassAttribute',
+          is        => 'rw',
+          isa       => 'Int',
+          default   => 0,
+        );
+
+    has 'WeakAttribute' =>
+        ( metaclass => 'ClassAttribute',
+          is        => 'rw',
+          isa       => 'Object',
+          weak_ref  => 1,
+        );
+
+    has 'size' =>
+        ( is      => 'rw',
+          isa     => 'Int',
+          default => 5,
+        );
+
+    sub BUILD
+    {
+        my $class = shift;
+
+        $class->ObjectCount( $class->ObjectCount() + 1 );
+    }
+}
+
+sub run_tests
+{
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    {
+        is( HasClassAttribute->ObjectCount(), 0,
+            'ObjectCount() is 0' );
+
+        my $hca1 = HasClassAttribute->new();
+        is( $hca1->size(), 5,
+            'size is 5 - object attribute works as expected' );
+        is( HasClassAttribute->ObjectCount(), 1,
+            'ObjectCount() is 1' );
+
+        my $hca2 = HasClassAttribute->new( size => 10 );
+        is( $hca2->size(), 10,
+            'size is 10 - object attribute can be set via constructor' );
+        is( HasClassAttribute->ObjectCount(), 2,
+            'ObjectCount() is 2' );
+        is( $hca2->ObjectCount(), 2,
+            'ObjectCount() is 2 - can call class attribute accessor on object' );
+    }
+
+    {
+        my $hca3 = HasClassAttribute->new( ObjectCount => 20 );
+        is( $hca3->ObjectCount(), 3,
+            'class attributes are not affected by constructor params' );
+        is( HasClassAttribute->ObjectCount(), 3,
+            'class attributes are not affected by constructor params' );
+    }
+
+    {
+        my $object = bless {}, 'Thing';
+
+        HasClassAttribute->WeakAttribute($object);
+
+        ok( isweak( $HasClassAttribute::__ClassAttribute{WeakAttribute} ),
+            'weak class attributes are weak' );
+    }
+}
+
+
+1;