Commit | Line | Data |
13287dd5 |
1 | #!/usr/bin/perl -Tw |
2 | |
3 | BEGIN { |
4 | if( $ENV{PERL_CORE} ) { |
5 | chdir 't'; |
6 | @INC = ('../lib', 'lib'); |
7 | } |
8 | else { |
9 | unshift @INC, 't/lib'; |
10 | } |
11 | } |
12 | |
13 | use strict; |
14 | |
15 | use Test::More tests => 146; |
16 | |
17 | |
18 | use_ok('Test::Harness::Straps'); |
19 | |
20 | my $strap = Test::Harness::Straps->new; |
21 | ok( defined $strap && $strap->isa("Test::Harness::Straps"), 'new()' ); |
22 | |
23 | |
24 | ### Testing _is_comment() |
25 | |
26 | my $comment; |
27 | ok( !$strap->_is_comment("foo", \$comment), '_is_comment(), not a comment' ); |
28 | ok( !defined $comment, ' no comment set' ); |
29 | |
30 | ok( !$strap->_is_comment("f # oo", \$comment), ' not a comment with #' ); |
31 | ok( !defined $comment, ' no comment set' ); |
32 | |
33 | my %comments = ( |
34 | "# stuff and things # and stuff" => |
35 | ' stuff and things # and stuff', |
36 | " # more things " => ' more things ', |
37 | "#" => '', |
38 | ); |
39 | |
40 | while( my($line, $line_comment) = each %comments ) { |
41 | my $strap = Test::Harness::Straps->new; |
42 | |
43 | my $name = substr($line, 0, 20); |
44 | ok( $strap->_is_comment($line, \$comment), " comment '$name'" ); |
45 | is( $comment, $line_comment, ' right comment set' ); |
46 | } |
47 | |
48 | |
49 | |
50 | ### Testing _is_header() |
51 | |
52 | my @not_headers = (' 1..2', |
53 | '1..M', |
54 | '1..-1', |
55 | '2..2', |
56 | '1..a', |
57 | '', |
58 | ); |
59 | |
60 | foreach my $unheader (@not_headers) { |
61 | my $strap = Test::Harness::Straps->new; |
62 | |
63 | ok( !$strap->_is_header($unheader), |
64 | "_is_header(), not a header '$unheader'" ); |
65 | |
66 | ok( (!grep { exists $strap->{$_} } qw(max todo skip_all)), |
67 | " max, todo and skip_all are not set" ); |
68 | } |
69 | |
70 | |
71 | my @attribs = qw(max skip_all todo); |
72 | my %headers = ( |
73 | '1..2' => { max => 2 }, |
74 | '1..1' => { max => 1 }, |
75 | '1..0' => { max => 0 }, |
76 | '1..0 # Skipped: no leverage found' => { max => 0, |
77 | skip_all => 'no leverage found', |
78 | }, |
79 | '1..4 # Skipped: no leverage found' => { max => 4, |
80 | skip_all => 'no leverage found', |
81 | }, |
82 | '1..0 # skip skip skip because' => { max => 0, |
83 | skip_all => 'skip skip because', |
84 | }, |
85 | '1..10 todo 2 4 10' => { max => 10, |
86 | 'todo' => { 2 => 1, |
87 | 4 => 1, |
88 | 10 => 1, |
89 | }, |
90 | }, |
91 | '1..10 todo' => { max => 10 }, |
92 | '1..192 todo 4 2 13 192 # Skip skip skip because' => |
93 | { max => 192, |
94 | 'todo' => { 4 => 1, |
95 | 2 => 1, |
96 | 13 => 1, |
97 | 192 => 1, |
98 | }, |
99 | skip_all => 'skip skip because' |
100 | } |
101 | ); |
102 | |
103 | while( my($header, $expect) = each %headers ) { |
104 | my $strap = Test::Harness::Straps->new; |
105 | |
106 | ok( $strap->_is_header($header), "_is_header() is a header '$header'" ); |
107 | |
108 | is( $strap->{skip_all}, $expect->{skip_all}, ' skip_all set right' ) |
109 | if defined $expect->{skip_all}; |
110 | |
111 | ok( eq_set( [map $strap->{$_}, grep defined $strap->{$_}, @attribs], |
112 | [map $expect->{$_}, grep defined $expect->{$_}, @attribs] ), |
113 | ' the right attributes are there' ); |
114 | } |
115 | |
116 | |
117 | |
118 | ### Testing _is_test() |
119 | |
120 | my %tests = ( |
121 | 'ok' => { 'ok' => 1 }, |
122 | 'not ok' => { 'ok' => 0 }, |
123 | |
124 | 'ok 1' => { 'ok' => 1, number => 1 }, |
125 | 'not ok 1' => { 'ok' => 0, number => 1 }, |
126 | |
127 | 'ok 2938' => { 'ok' => 1, number => 2938 }, |
128 | |
129 | 'ok 1066 - and all that' => { 'ok' => 1, |
130 | number => 1066, |
131 | name => "- and all that" }, |
132 | 'not ok 42 - universal constant' => |
133 | { 'ok' => 0, |
134 | number => 42, |
135 | name => '- universal constant', |
136 | }, |
137 | 'not ok 23 # TODO world peace' => { 'ok' => 0, |
138 | number => 23, |
139 | type => 'todo', |
140 | reason => 'world peace' |
141 | }, |
142 | 'ok 11 - have life # TODO get a life' => |
143 | { 'ok' => 1, |
144 | number => 11, |
145 | name => '- have life', |
146 | type => 'todo', |
147 | reason => 'get a life' |
148 | }, |
149 | 'not ok # TODO' => { 'ok' => 0, |
150 | type => 'todo', |
151 | reason => '' |
152 | }, |
153 | 'ok # skip' => { 'ok' => 1, |
154 | type => 'skip', |
155 | }, |
156 | 'not ok 11 - this is \# all the name # skip this is not' |
157 | => { 'ok' => 0, |
158 | number => 11, |
159 | name => '- this is \# all the name', |
160 | type => 'skip', |
161 | reason => 'this is not' |
162 | }, |
163 | "ok 42 - _is_header() is a header '1..192 todo 4 2 13 192 \\# Skip skip skip because" |
164 | => { 'ok' => 1, |
165 | number => 42, |
166 | name => "- _is_header() is a header '1..192 todo 4 2 13 192 \\# Skip skip skip because", |
167 | }, |
168 | ); |
169 | |
170 | while( my($line, $expect) = each %tests ) { |
171 | my %test; |
172 | ok( $strap->_is_test($line, \%test), "_is_test() spots '$line'" ); |
173 | |
174 | foreach my $type (qw(ok number name type reason)) { |
175 | cmp_ok( $test{$type}, 'eq', $expect->{$type}, " $type" ); |
176 | } |
177 | } |
178 | |
179 | my @untests = ( |
180 | ' ok', |
181 | 'not', |
182 | 'okay 23', |
183 | ); |
184 | foreach my $line (@untests) { |
185 | my $strap = Test::Harness::Straps->new; |
186 | my %test = (); |
187 | ok( !$strap->_is_test($line, \%test), "_is_test() disregards '$line'" ); |
188 | |
189 | # is( keys %test, 0 ) won't work in 5.004 because it's undef. |
190 | ok( !keys %test, ' and produces no test info' ); |
191 | } |
192 | |
193 | |
194 | ### Test _is_bail_out() |
195 | |
196 | my %bails = ( |
197 | 'Bail out!' => undef, |
198 | 'Bail out! Wing on fire.' => 'Wing on fire.', |
199 | 'BAIL OUT!' => undef, |
200 | 'bail out! - Out of coffee' => '- Out of coffee', |
201 | ); |
202 | |
203 | while( my($line, $expect) = each %bails ) { |
204 | my $strap = Test::Harness::Straps->new; |
205 | my $reason; |
206 | ok( $strap->_is_bail_out($line, \$reason), "_is_bail_out() spots '$line'"); |
207 | is( $reason, $expect, ' with the right reason' ); |
208 | } |
209 | |
210 | my @unbails = ( |
211 | ' Bail out!', |
212 | 'BAIL OUT', |
213 | 'frobnitz', |
214 | 'ok 23 - BAIL OUT!', |
215 | ); |
216 | |
217 | foreach my $line (@unbails) { |
218 | my $strap = Test::Harness::Straps->new; |
219 | my $reason; |
220 | |
221 | ok( !$strap->_is_bail_out($line, \$reason), |
222 | "_is_bail_out() ignores '$line'" ); |
223 | is( $reason, undef, ' and gives no reason' ); |
224 | } |