Debian Codegraph @

t/unit/Test2/Harness/Settings.t

raw
1 use Test2::V0 -target => 'Test2::Harness::Settings';
2 use File::Temp qw/tempfile/;
3 use Test2::Harness::Util::JSON qw/encode_json/;
4
5 my $one = $CLASS->new();
6 isa_ok($one, [$CLASS], "Created an instance");
7
8 ok(!$one->check_prefix('foo'), "foo is not defined");
9 like(dies { $one->foo }, qr/The 'foo' prefix is not defined/, "Cannot call foo if it is not defined");
10 like(dies { $one->prefix('foo') }, qr/The 'foo' prefix is not defined/, "Cannot call prefix(foo) if it is not defined");
11
12 $one->define_prefix('foo');
13 isa_ok($one->foo, ['Test2::Harness::Settings::Prefix'], "Defined the prefix");
14 ok($one->check_prefix('foo'), "foo is now defined");
15 ok($one->foo, "Can call foo if it is defined");
16 ok($one->prefix('foo'), "Can call prefix(foo) if it is defined");
17
18 is($one->TO_JSON, {foo => exact_ref($one->foo)}, "TO_JSON");
19
20 like(dies { $CLASS->foo }, qr/Method foo\(\) must be called on a blessed instance/, "Need a blessed instance");
21 like(dies { $one->foo(1) }, qr/Too many arguments for foo\(\)/, "No args");
22
23 {
24 $INC{'XXX.pm'} = __FILE__;
25 package XXX;
26 sub new { shift; bless {@_}, 'XXX' };
27 }
28
29 $one->foo->vivify_field('xxx');
30 $one->foo->field(xxx => 'yyy');
31
32 my $thing = $one->build('foo', 'XXX', a => 'b');
33 isa_ok($thing, ['XXX'], "Got a blessed instance of XXX");
34 is(
35 $thing,
36 {
37 a => 'b',
38 xxx => 'yyy',
39 },
40 "Instance is composed as expected"
41 );
42
43 my ($fh, $name) = tempfile(UNLINK => 1);
44 print $fh encode_json($one);
45 close($fh);
46
47 my $two = $CLASS->new($name);
48 isa_ok($two, [$CLASS], "Correct class");
49 is($two, $one, "Serialized and deserialized round trip");
50 ref_is_not($two, $one, "2 different refs");
51
52 like(
53 dies { $CLASS->new(foo => []) },
54 qr/All prefixes must be defined as hashes/,
55 "Prefixes must be hashes"
56 );
57
58 like(
59 dies { $CLASS->new(foo => bless({}, 'XXX')) },
60 qr/All prefixes must contain instances of Test2::Harness::Settings::Prefix/,
61 "Blessed Prefixes must be prefixes"
62 );
63
64 done_testing;