summaryrefslogtreecommitdiff
path: root/t/moose-tags-ranges.t
diff options
context:
space:
mode:
Diffstat (limited to 't/moose-tags-ranges.t')
-rw-r--r--t/moose-tags-ranges.t136
1 files changed, 136 insertions, 0 deletions
diff --git a/t/moose-tags-ranges.t b/t/moose-tags-ranges.t
new file mode 100644
index 0000000..bb8fb06
--- /dev/null
+++ b/t/moose-tags-ranges.t
@@ -0,0 +1,136 @@
+#!perl
+use strict;
+use warnings;
+
+package Foo;{
+use Moose;
+use Data::MultiValued::AttributeTrait::TagsAndRanges;
+
+has stuff => (
+ is => 'rw',
+ isa => 'Int',
+ traits => ['MultiValued::TagsAndRanges'],
+ default => 3,
+ predicate => 'has_stuff',
+ clearer => 'clear_stuff',
+);
+
+has other => (
+ is => 'rw',
+ isa => 'Str',
+ traits => ['MultiValued::TagsAndRanges'],
+ predicate => 'has_other',
+ clearer => 'clear_other',
+);
+}
+package main;
+use Test::Most 'die';
+use Data::Printer;
+
+subtest 'default' => sub {
+ my $obj = Foo->new();
+
+ ok(!$obj->has_other,'not has other');
+ ok($obj->has_stuff,'has stuff');
+
+ is($obj->stuff,3,'default');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
+ [[undef, [[undef,undef]]]],
+ 'stuff all_ranges');
+ cmp_deeply(
+ [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
+ [],
+ 'other all_ranges');
+};
+
+subtest 'constructor param' => sub {
+ my $obj = Foo->new({stuff=>12,other=>'bar'});
+
+ ok($obj->has_other,'has other');
+ ok($obj->has_stuff,'has stuff');
+
+ is($obj->stuff,12,'param');
+ is($obj->other,'bar','param');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
+ [[undef, [[undef,undef]]]],
+ 'stuff all_ranges');
+ cmp_deeply(
+ [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
+ [[undef, [[undef,undef]]]],
+ 'other all_ranges');
+};
+
+subtest 'with ranges' => sub {
+ my $obj = Foo->new();
+
+ my $opts = {from=>10,to=>20,at=>15};
+
+ ok($obj->has_stuff,'has stuff');
+ ok($obj->has_stuff_multi($opts),'has stuff ranged (forever)');
+ ok(!$obj->has_other,'not has other');
+ ok(!$obj->has_other_multi($opts),'not has other ranged');
+
+ $obj->stuff_multi($opts,7);
+ $obj->other_multi($opts,'foo');
+
+ is($obj->stuff,3,'default');
+ is($obj->stuff_multi($opts),7,'stuff ranged');
+ is($obj->other_multi($opts),'foo','other ranged');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags($obj)],
+ [undef],
+ 'stuff all_tags');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
+ [[undef, [[undef,10],[10,20],[20,undef]]]],
+ 'stuff all_tags_and_ranges');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
+ [[undef, [[10,20]]]],
+ 'other all_tags_and_ranges');
+};
+
+subtest 'with tags and ranges' => sub {
+ my $obj = Foo->new();
+
+ my $opts = {from=>10,to=>20,at=>15,tag=>'x'};
+
+ ok($obj->has_stuff,'has stuff');
+ ok(!$obj->has_stuff_multi($opts),'has stuff ranged (forever)');
+ ok(!$obj->has_other,'not has other');
+ ok(!$obj->has_other_multi($opts),'not has other ranged');
+
+ $obj->stuff_multi($opts,7);
+ $obj->other_multi($opts,'foo');
+
+ is($obj->stuff,3,'default');
+ is($obj->stuff_multi($opts),7,'stuff ranged');
+ is($obj->other_multi($opts),'foo','other ranged');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags($obj)],
+ bag(undef,'x'),
+ 'stuff all_tags');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
+ bag(
+ ['x', [[10,20]]],
+ [undef,[[undef,undef]]],
+ ),
+ 'stuff all_tags_and_ranges');
+
+ cmp_deeply(
+ [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
+ [['x', [[10,20]]]],
+ 'other all_tags_and_ranges');
+};
+
+done_testing();