From: Shawn M Moore <sartak@gmail.com>
Date: Sun, 25 May 2008 00:57:28 +0000 (+0000)
Subject: Test script for a trait-based Counter
X-Git-Tag: 0.18_01~31
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=43c2a5e71fff93bbb198a1c77e253b1c199e5a82;p=gitmo%2FMooseX-AttributeHelpers.git

Test script for a trait-based Counter
---

diff --git a/t/201_trait_counter.t b/t/201_trait_counter.t
new file mode 100644
index 0000000..c91ae92
--- /dev/null
+++ b/t/201_trait_counter.t
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 14;
+use Test::Moose 'does_ok';
+
+BEGIN {
+    use_ok('MooseX::AttributeHelpers');   
+}
+
+{
+    package MyHomePage;
+    use Moose;
+
+    has 'counter' => (
+        traits    => [qw/Counter/],
+        is        => 'ro',
+        isa       => 'Int',
+        default   => sub { 0 },
+        provides  => {
+            inc   => 'inc_counter',
+            dec   => 'dec_counter',
+            reset => 'reset_counter',
+        }
+    );
+}
+
+my $page = MyHomePage->new();
+isa_ok($page, 'MyHomePage');
+
+can_ok($page, $_) for qw[
+    dec_counter 
+    inc_counter
+    reset_counter
+];
+
+is($page->counter, 0, '... got the default value');
+
+$page->inc_counter; 
+is($page->counter, 1, '... got the incremented value');
+
+$page->inc_counter; 
+is($page->counter, 2, '... got the incremented value (again)');
+
+$page->dec_counter; 
+is($page->counter, 1, '... got the decremented value');
+
+$page->reset_counter;
+is($page->counter, 0, '... got the original value');
+
+# check the meta ..
+
+my $counter = $page->meta->get_attribute('counter');
+does_ok($counter, 'MooseX::AttributeHelpers::Trait::Counter');
+
+is($counter->helper_type, 'Num', '... got the expected helper type');
+
+is($counter->type_constraint->name, 'Int', '... got the expected type constraint');
+
+is_deeply($counter->provides, { 
+    inc   => 'inc_counter',
+    dec   => 'dec_counter',
+    reset => 'reset_counter',        
+}, '... got the right provides methods');
+