Skip to content

Commit b2e005e

Browse files
add names to all tests, and show correct line of failure
1 parent 2123174 commit b2e005e

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

t/basic.t

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,38 @@ use Test::More;
55
use Moose::Util::TypeConstraints;
66
use MooseX::Types::Moose ':all';
77
use MooseX::Types::Structured ':all';
8-
98
use MooseX::Types::Meta ':all';
9+
use Scalar::Util 'blessed';
1010

1111
sub test {
1212
my ($name, $code) = @_;
13+
local $Test::Builder::Level = $Test::Builder::Level + 1;
1314
subtest $name => sub {
1415
$code->();
1516
done_testing;
1617
};
1718
}
1819

20+
sub check_is {
21+
my ($type, $thing) = @_;
22+
local $Test::Builder::Level = $Test::Builder::Level + 1;
23+
(my $type_name = $type->name) =~ s/^MooseX::Types::Meta:://;
24+
ok(
25+
$type->check($thing),
26+
(blessed($thing) && $thing->can('name') ? $thing->name : $thing) . ' isa ' . $type_name,
27+
);
28+
}
29+
30+
sub check_isnt {
31+
my ($type, $thing) = @_;
32+
local $Test::Builder::Level = $Test::Builder::Level + 1;
33+
(my $type_name = $type->name) =~ s/^MooseX::Types::Meta:://;
34+
ok(
35+
!$type->check($thing),
36+
(blessed($thing) && $thing->can('name') ? $thing->name : $thing) . ' is not a ' . $type_name,
37+
);
38+
}
39+
1940
{
2041
package TestClass;
2142
use Moose;
@@ -48,58 +69,58 @@ sub test {
4869
}
4970

5071
test TypeConstraint => sub {
51-
ok(TypeConstraint->check($_)) for TypeConstraint, Int;
52-
ok(!TypeConstraint->check($_)) for \42, 'Moose::Meta::TypeConstraint';
72+
check_is(TypeConstraint, $_) for TypeConstraint, Int;
73+
check_isnt(TypeConstraint, $_) for \42, 'Moose::Meta::TypeConstraint';
5374
};
5475

5576
test Class => sub {
56-
ok(Class->check($_)) for (
77+
check_is(Class, $_) for (
5778
MooseX::Types::Meta->meta,
5879
TestClass->meta,
5980
Moose::Meta::Class->meta,
6081
);
6182

62-
ok(!Class->check($_)) for 42, TestRole->meta;
83+
check_isnt(Class, $_) for 42, TestRole->meta;
6384
};
6485

6586
test Role => sub {
66-
ok(Role->check($_)) for TestRole->meta;
67-
ok(!Role->check($_)) for TestClass->meta, 13;
87+
check_is(Role, $_) for TestRole->meta;
88+
check_isnt(Role, $_) for TestClass->meta, 13;
6889
};
6990

7091
test Attribute => sub {
71-
ok(Attribute->check($_)) for (
92+
check_is(Attribute, $_) for (
7293
TestClass->meta->get_attribute('attr'),
7394
Moose::Meta::Class->meta->get_attribute('constructor_class'),
7495
);
7596

76-
ok(!Attribute->check($_)) for (
97+
check_isnt(Attribute, $_) for (
7798
TestRole->meta->get_attribute('attr'),
7899
\42,
79100
);
80101
};
81102

82103
test RoleAttribute => sub {
83-
ok(RoleAttribute->check($_)) for (
104+
check_is(RoleAttribute, $_) for (
84105
TestRole->meta->get_attribute('attr'),
85106
);
86107

87-
ok(!RoleAttribute->check($_)) for (
108+
check_isnt(RoleAttribute, $_) for (
88109
TestClass->meta->get_attribute('attr'),
89110
Moose::Meta::Class->meta->get_attribute('constructor_class'),
90111
TestClass->meta,
91112
);
92113
};
93114

94115
test Method => sub {
95-
ok(Method->check($_)) for (
116+
check_is(Method, $_) for (
96117
(map { TestClass->meta->get_method($_) } qw(foo bar baz attr)),
97118
(map { TestRole->meta->get_method($_) } qw(foo attr)),
98119
Moose::Meta::Class->meta->get_method('create'),
99120
Moose::Meta::Class->meta->get_method('new'),
100121
);
101122

102-
ok(!Method->check($_)) for (
123+
check_isnt(Method, $_) for (
103124
TestClass->meta->get_attribute('attr'),
104125
TestClass->meta,
105126
);
@@ -109,12 +130,12 @@ test TypeCoercion => sub {
109130
my $tc = subtype as Int;
110131
coerce $tc, from Str, via { 0 + $_ };
111132

112-
ok(TypeCoercion->check($_)) for $tc->coercion;
113-
ok(!TypeCoercion->check($_)) for $tc, Str, 42;
133+
check_is(TypeCoercion, $_) for $tc->coercion;
134+
check_isnt(TypeCoercion, $_) for $tc, Str, 42;
114135
};
115136

116137
test StructuredTypeConstraint => sub {
117-
ok(StructuredTypeConstraint->check($_)) for (
138+
check_is(StructuredTypeConstraint, $_) for (
118139
Dict,
119140
Dict[],
120141
Dict[foo => Int],
@@ -127,7 +148,7 @@ test StructuredTypeConstraint => sub {
127148
(subtype as Dict[]),
128149
);
129150

130-
ok(!StructuredTypeConstraint->check($_)) for (
151+
check_isnt(StructuredTypeConstraint, $_) for (
131152
ArrayRef,
132153
ArrayRef[Dict[]],
133154
);
@@ -137,23 +158,23 @@ test StructuredTypeCoercion => sub {
137158
my $tc = subtype as Dict[];
138159
coerce $tc, from Undef, via { +{} };
139160

140-
ok(StructuredTypeCoercion->check($_)) for $tc->coercion;
141-
ok(!StructuredTypeCoercion->check($_)) for $tc, Str, 42;
161+
check_is(StructuredTypeCoercion, $_) for $tc->coercion;
162+
check_isnt(StructuredTypeCoercion, $_) for $tc, Str, 42;
142163
};
143164

144165
test TypeEquals => sub {
145-
ok((TypeEquals[Num])->check($_)) for Num;
146-
ok(!(TypeEquals[Num])->check($_)) for Int, Str;
166+
check_is(TypeEquals[Num], $_) for Num;
167+
check_isnt(TypeEquals[Num], $_) for Int, Str;
147168
};
148169

149170
test SubtypeOf => sub {
150-
ok((SubtypeOf[Str])->check($_)) for Num, Int, ClassName, RoleName;
151-
ok(!(SubtypeOf[Str])->check($_)) for Str, Value, Ref, Defined, Any, Item;
171+
check_is(SubtypeOf[Str], $_) for Num, Int, ClassName, RoleName;
172+
check_isnt(SubtypeOf[Str], $_) for Str, Value, Ref, Defined, Any, Item;
152173
};
153174

154175
test TypeOf => sub {
155-
ok((TypeOf[Str])->check($_)) for Str, Num, Int, ClassName, RoleName;
156-
ok(!(TypeOf[Str])->check($_)) for Value, Ref, Defined, Any, Item;
176+
check_is(TypeOf[Str], $_) for Str, Num, Int, ClassName, RoleName;
177+
check_isnt(TypeOf[Str], $_) for Value, Ref, Defined, Any, Item;
157178
};
158179

159180
test 'MooseX::Role::Parameterized' => sub {
@@ -170,24 +191,24 @@ role {
170191
EOR
171192

172193
test ParameterizableRole => sub {
173-
ok(ParameterizableRole->check($_)) for (
194+
check_is(ParameterizableRole, $_) for (
174195
TestRole::Parameterized->meta,
175196
);
176197

177-
ok(!ParameterizableRole->check($_)) for (
198+
check_isnt(ParameterizableRole, $_) for (
178199
TestRole->meta,
179200
);
180201
};
181202

182203
test ParameterizedRole => sub {
183-
ok(ParameterizedRole->check($_)) for (
204+
check_is(ParameterizedRole, $_) for (
184205
TestRole::Parameterized->meta->generate_role(
185206
consumer => Moose::Meta::Class->create_anon_class,
186207
parameters => {},
187208
),
188209
);
189210

190-
ok(!ParameterizedRole->check($_)) for (
211+
check_isnt(ParameterizedRole, $_) for (
191212
TestRole->meta,
192213
);
193214
};

0 commit comments

Comments
 (0)