throw error on failed new in run_if_script
[catagits/Web-Simple.git] / t / run_if_script.t
1 use strictures;
2
3 use Test::More;
4 use Test::Fatal 'exception';
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
16 {
17     use Web::Simple 'DieTest';
18
19     package DieTest;
20     use Moo;
21     has die => is => ro => required => 1;
22 }
23
24 is(
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
31 is(
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
37 my $rt = RunTest->new( test => 3 );
38
39 is_deeply(
40     [ $rt->run_if_script ],                 #
41     [ $rt, $rt, "run" ],
42     "calling run_if_script on an object runs it directly"
43 );
44
45 is_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
51 like(
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
57 done_testing;