throw error on failed new in run_if_script
[catagits/Web-Simple.git] / t / run_if_script.t
CommitLineData
fd8747e5 1use strictures;
2
3use Test::More;
7346dbd3 4use Test::Fatal 'exception';
fd8747e5 5
6{
7 use Web::Simple 'RunTest';
8
9 package RunTest;
10 use Moo;
11 has test => is => ro => default => sub { 2 };
12 sub to_psgi_app { "to_psgi_app" }
13 sub run { @_, "run" }
14}
15
7346dbd3 16{
17 use Web::Simple 'DieTest';
18
19 package DieTest;
20 use Moo;
21 has die => is => ro => required => 1;
22}
23
fd8747e5 24is(
25 sub { RunTest->run_if_script }
26 ->(),
27 "to_psgi_app",
28 "to_psgi_app is called when run_if_script is called inside a function"
29);
30
31is(
32 ( RunTest->run_if_script )[0]->test, #
33 2,
34 "calling run_if_script on a class name instantiates it and runs it"
35);
36
37my $rt = RunTest->new( test => 3 );
38
39is_deeply(
40 [ $rt->run_if_script ], #
41 [ $rt, $rt, "run" ],
42 "calling run_if_script on an object runs it directly"
43);
44
45is_deeply(
46 [ $rt->run_if_script( 4 ) ], #
47 [ $rt, $rt, 4, "run" ],
48 "passing arguments to run_if_script has them passed on to the run method"
49);
50
7346dbd3 51like(
52 exception { DieTest->_build_for_run_if_script }, #
53 qr/^Failed to create new 'DieTest' object during/,
54 "object creation in run_if_script decorates failure with useful information"
55);
56
fd8747e5 57done_testing;