use strict;
use warnings;
-use Test::More tests => 85;
+use Test::More;
use Test::Exception;
use Scalar::Util ();
type Number => where { Scalar::Util::looks_like_number($_) };
-type String
+type String
=> where { !ref($_) && !Number($_) }
=> message { "This is not a string ($_)" };
-subtype Natural
- => as Number
+subtype Natural
+ => as Number
=> where { $_ > 0 };
-subtype NaturalLessThanTen
+subtype NaturalLessThanTen
=> as Natural
=> where { $_ < 10 }
=> message { "The number '$_' is not less than 10" };
-
+
Moose::Util::TypeConstraints->export_type_constraints_as_functions();
ok(Number(5), '... this is a Num');
is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
-
-# anon sub-typing
-
+
+# anon sub-typing
+
my $negative = subtype Number => where { $_ < 0 };
ok(defined $negative, '... got a value back from negative');
isa_ok($negative, 'Moose::Meta::TypeConstraint');
ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String');
ok($negative2->has_message, '... it has a message');
-is($negative2->validate(2),
+is($negative2->validate(2),
'2 is not a negative number',
'... validated unsuccessfully (got error)');
ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
-is($natural_less_than_ten->validate(15),
- "The number '15' is not less than 10",
+is($natural_less_than_ten->validate(15),
+ "The number '15' is not less than 10",
'... validated unsuccessfully (got error)');
my $natural = find_type_constraint('Natural');
ok(!defined($natural->validate(5)), '... validated successfully (no error)');
-is($natural->validate(-5),
- "Validation failed for 'Natural' failed with value -5",
+is($natural->validate(-5),
+ "Validation failed for 'Natural' with value -5",
'... validated unsuccessfully (got error)');
my $string = find_type_constraint('String');
ok(!defined($string->validate("Five")), '... validated successfully (no error)');
-is($string->validate(5),
-"This is not a string (5)",
+is($string->validate(5),
+"This is not a string (5)",
'... validated unsuccessfully (got error)');
lives_ok { Moose::Meta::Attribute->new('bob', isa => 'Spong') }
# sugar was indistinguishable from calling directly.
{
+ no warnings 'redefine';
+ *Moose::Deprecated::deprecated = sub { return };
+}
+
+{
my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
ok( $type->check(5), '... this is a Num' );
ok( ! $subtype->check('Foo'), '... this is not a Natural');
}
+done_testing;