Skip to content

Commit d6fcf74

Browse files
authored
Merge pull request #1917 from kathackeray/mojolicious-command-subtests
Convert t/mojolicious/command.t to use subtests
2 parents 8190137 + 724df65 commit d6fcf74

File tree

1 file changed

+107
-88
lines changed

1 file changed

+107
-88
lines changed

t/mojolicious/command.t

+107-88
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,42 @@ use Test::More;
66
use Mojo::File qw(path tempdir);
77
use Mojolicious::Command;
88

9-
# Application
10-
my $command = Mojolicious::Command->new;
11-
isa_ok $command->app, 'Mojolicious', 'right application';
9+
subtest 'Application' => sub {
10+
my $command = Mojolicious::Command->new;
11+
isa_ok $command->app, 'Mojolicious', 'right application';
12+
};
1213

13-
# Creating directories
14-
my $cwd = path;
15-
my $dir = tempdir;
16-
chdir $dir;
17-
my $buffer = '';
18-
{
19-
open my $handle, '>', \$buffer;
20-
local *STDOUT = $handle;
21-
$command->create_rel_dir('foo/bar');
22-
}
23-
like $buffer, qr/[mkdir]/, 'right output';
24-
ok -d path('foo', 'bar'), 'directory exists';
25-
$buffer = '';
26-
{
27-
open my $handle, '>', \$buffer;
28-
local *STDOUT = $handle;
29-
$command->create_rel_dir('foo/bar');
30-
}
31-
like $buffer, qr/\[exist\]/, 'right output';
32-
chdir $cwd;
14+
subtest 'Creating directories' => sub {
15+
my $command = Mojolicious::Command->new;
16+
my $cwd = path;
17+
my $dir = tempdir;
18+
my $buffer = '';
19+
chdir $dir;
20+
{
21+
open my $handle, '>', \$buffer;
22+
local *STDOUT = $handle;
23+
$command->create_rel_dir('foo/bar');
24+
}
25+
like $buffer, qr/[mkdir]/, 'right output';
26+
ok -d path('foo', 'bar'), 'directory exists';
3327

34-
# Generating files
35-
is $command->rel_file('foo/bar.txt')->basename, 'bar.txt', 'right result';
36-
my $template = <<'EOF';
28+
$buffer = '';
29+
{
30+
open my $handle, '>', \$buffer;
31+
local *STDOUT = $handle;
32+
$command->create_rel_dir('foo/bar');
33+
}
34+
like $buffer, qr/\[exist\]/, 'right output';
35+
chdir $cwd;
36+
};
37+
38+
subtest 'Generating files' => sub {
39+
my $command = Mojolicious::Command->new;
40+
my $cwd = path;
41+
my $dir = tempdir;
42+
is $command->rel_file('foo/bar.txt')->basename, 'bar.txt', 'right result';
43+
44+
my $template = <<'EOF';
3745
@@ foo_bar
3846
% my $word = shift;
3947
just <%= $word %>!
@@ -42,69 +50,80 @@ just <%= $word %>!
4250
@@ bar_baz
4351
just <%= $word %> too!
4452
EOF
45-
open my $data, '<', \$template;
46-
no strict 'refs';
47-
*{"Mojolicious::Command::DATA"} = $data;
48-
chdir $dir;
49-
$buffer = '';
50-
{
51-
open my $handle, '>', \$buffer;
52-
local *STDOUT = $handle;
53-
$command->template({})->render_to_rel_file('foo_bar', 'bar/baz.txt', 'works');
54-
}
55-
like $buffer, qr/\[mkdir\].*\[write\]/s, 'right output';
56-
open my $txt, '<', $command->rel_file('bar/baz.txt');
57-
is join('', <$txt>), "just works!\n", 'right result';
58-
$buffer = '';
59-
{
60-
open my $handle, '>', \$buffer;
61-
local *STDOUT = $handle;
62-
$command->template({vars => 1})->render_to_rel_file('bar_baz', 'bar/two.txt', {word => 'works'});
63-
}
64-
like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
65-
open $txt, '<', $command->rel_file('bar/two.txt');
66-
is join('', <$txt>), "just works too!\n", 'right result';
67-
$buffer = '';
68-
{
69-
open my $handle, '>', \$buffer;
70-
local *STDOUT = $handle;
71-
$command->chmod_rel_file('bar/baz.txt', 0700);
72-
}
73-
like $buffer, qr/\[chmod\]/, 'right output';
74-
ok -e $command->rel_file('bar/baz.txt'), 'file is executable';
75-
$buffer = '';
76-
{
77-
open my $handle, '>', \$buffer;
78-
local *STDOUT = $handle;
79-
$command->write_rel_file('123.xml', "seems\nto\nwork");
80-
}
81-
like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
82-
$buffer = '';
83-
{
84-
open my $handle, '>', \$buffer;
85-
local *STDOUT = $handle;
86-
$command->write_rel_file('123.xml', 'fail');
87-
}
88-
like $buffer, qr/\[exist\]/, 'right output';
89-
open my $xml, '<', $command->rel_file('123.xml');
90-
is join('', <$xml>), "seems\nto\nwork", 'right result';
91-
eval { $command->render_data('dies') };
92-
like $@, qr/template error/, 'right error';
93-
chdir $cwd;
53+
open my $data, '<', \$template;
54+
no strict 'refs';
55+
*{"Mojolicious::Command::DATA"} = $data;
56+
chdir $dir;
57+
my $buffer = '';
58+
{
59+
open my $handle, '>', \$buffer;
60+
local *STDOUT = $handle;
61+
$command->template({})->render_to_rel_file('foo_bar', 'bar/baz.txt', 'works');
62+
}
63+
like $buffer, qr/\[mkdir\].*\[write\]/s, 'right output';
64+
open my $txt, '<', $command->rel_file('bar/baz.txt');
65+
is join('', <$txt>), "just works!\n", 'right result';
66+
67+
$buffer = '';
68+
{
69+
open my $handle, '>', \$buffer;
70+
local *STDOUT = $handle;
71+
$command->template({vars => 1})->render_to_rel_file('bar_baz', 'bar/two.txt', {word => 'works'});
72+
}
73+
like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
74+
open $txt, '<', $command->rel_file('bar/two.txt');
75+
is join('', <$txt>), "just works too!\n", 'right result';
76+
77+
$buffer = '';
78+
{
79+
open my $handle, '>', \$buffer;
80+
local *STDOUT = $handle;
81+
$command->chmod_rel_file('bar/baz.txt', 0700);
82+
}
83+
like $buffer, qr/\[chmod\]/, 'right output';
84+
ok -e $command->rel_file('bar/baz.txt'), 'file is executable';
85+
86+
$buffer = '';
87+
{
88+
open my $handle, '>', \$buffer;
89+
local *STDOUT = $handle;
90+
$command->write_rel_file('123.xml', "seems\nto\nwork");
91+
}
92+
like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
93+
94+
$buffer = '';
95+
{
96+
open my $handle, '>', \$buffer;
97+
local *STDOUT = $handle;
98+
$command->write_rel_file('123.xml', 'fail');
99+
}
100+
like $buffer, qr/\[exist\]/, 'right output';
101+
open my $xml, '<', $command->rel_file('123.xml');
102+
is join('', <$xml>), "seems\nto\nwork", 'right result';
103+
104+
eval { $command->render_data('dies') };
105+
like $@, qr/template error/, 'right error';
106+
chdir $cwd;
107+
};
94108

95-
# Quiet
96-
chdir $dir;
97-
$buffer = '';
98-
{
99-
open my $handle, '>', \$buffer;
100-
local *STDOUT = $handle;
101-
$command->quiet(1)->write_rel_file('123.xml', 'fail');
102-
}
103-
is $buffer, '', 'no output';
104-
chdir $cwd;
109+
subtest 'Quiet' => sub {
110+
my $command = Mojolicious::Command->new;
111+
my $cwd = path;
112+
my $dir = tempdir;
113+
chdir $dir;
114+
my $buffer = '';
115+
{
116+
open my $handle, '>', \$buffer;
117+
local *STDOUT = $handle;
118+
$command->quiet(1)->write_rel_file('123.xml', 'fail');
119+
}
120+
is $buffer, '', 'no output';
121+
chdir $cwd;
122+
};
105123

106-
# Abstract methods
107-
eval { Mojolicious::Command->run };
108-
like $@, qr/Method "run" not implemented by subclass/, 'right error';
124+
subtest 'Abstract methods' => sub {
125+
eval { Mojolicious::Command->run };
126+
like $@, qr/Method "run" not implemented by subclass/, 'right error';
127+
};
109128

110129
done_testing();

0 commit comments

Comments
 (0)