/
014_test_path_router.t
155 lines (135 loc) · 4.06 KB
/
014_test_path_router.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl6
use v6;
use Test;
use Test::Path::Router;
use IO::String;
use Path::Router;
my $router = Path::Router.new;
# Do the things that Test::Builder would do in Perl manually since the
# equivalent tools in Perl6 aren't quite there yet.
sub init-io {
my $output = IO::String.new;
my $error = IO::String.new;
Test::output() = $output;
Test::failure_output() = $error;
Test::todo_output() = $output;
return ($output, $error);
}
$router.add-route('blog' => %(
defaults => { controller => 'Blog' }
));
$router.add-route('feed' => %(
defaults => { controller => 'Feed' }
));
my %tests = (
mapping-not-ok => {
pass => {
desc => 'mapping-not-ok passes when mapping not found',
args => [{controller => 'Wiki'}],
},
fail => {
desc => 'mapping-not-ok fails when mapping found',
args => [{controller => 'Blog'}],
},
coderef => &mapping-not-ok,
},
mapping-ok => {
pass => {
desc => 'mapping-ok passes when mapping found',
args => [{controller => 'Blog'}],
},
fail => {
desc => 'mapping-ok fails when mapping not found',
args => [{controller => 'Wiki'}],
},
coderef => &mapping-ok,
},
mapping-is => {
pass => {
desc => 'mapping-is passes when mapping matches path',
args => [{controller => 'Blog'}, 'blog'],
},
fail => {
desc => 'mapping-is fails when mapping does not match path',
args => [{controller => 'Wiki'}, 'blog'],
},
coderef => &mapping-is,
},
path-not-ok => {
pass => {
desc => 'path-not-ok passes when path not found',
args => ['wiki'],
},
fail => {
desc => 'path-not-ok fails when path found',
args => ['blog'],
},
coderef => &path-not-ok,
},
path-ok => {
pass => {
desc => 'path-ok passes when path found',
args => ['blog'],
},
fail => {
desc => 'path-ok fails when path not found',
args => ['wiki'],
},
coderef => &path-ok,
},
path-is => {
pass => {
desc => 'path-is passes when path matches mapping',
args => ['blog', {controller => 'Blog'}],
},
fail => {
desc => 'path-is fails when path does not match mapping',
args => ['blog', {controller => 'Wiki'}],
},
coderef => &path-is,
},
routes-ok => {
pass => {
desc => 'routes-ok passes when all paths and mappings match',
args => [${
blog => {controller => 'Blog'},
feed => {controller => 'Feed'},
}],
},
fail => {
desc => 'routes-ok fails when all paths and mappings do not match',
args => [${
blog => {controller => 'Blog'},
feed => {controller => 'Wiki'},
}],
},
coderef => &routes-ok,
},
);
my $i = 0;
for %tests.keys.sort -> $function {
my &coderef = %tests{$function}<coderef>;
for <pass fail> -> $state {
my ($output, $error) = init-io;
my @arguments = %tests{$function}{$state}<args>.Slip;
my $description = %tests{$function}{$state}<desc>;
# This is a trick to prevent Test.pm from blowing up the exit status
# which isn't yet easy to control.
todo 'Test.pm blows up the exit code unless this is a TODO', 1
if $state eq 'fail';
coderef($router, |@arguments, $description);
$i++;
my $expect = $state eq 'pass' ?? "ok" !! "not ok";
$expect ~= " " ~ $i;
if ~$output ~~ /^^$expect/ {
say "ok $i - $description";
}
else {
say "not ok $i - $description";
note "# Got test like: $output";
note "# Expected test like: $expect";
note ~$error if ~$error;
}
}
}
say "1..$i";