summaryrefslogtreecommitdiff
path: root/lib/Test/Harness/t/strap.t
blob: 2980d970ffb7b3dac6937fcc28bd9f827d72da40 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/perl -w

BEGIN {
    if( $ENV{PERL_CORE} ) {
        chdir 't';
        @INC = ('../lib', 'lib');
    }
    else {
        unshift @INC, 't/lib';
    }
}

use strict;

use Test::More tests => 170;

use_ok('Test::Harness::Straps');

my $strap = Test::Harness::Straps->new;
isa_ok( $strap, 'Test::Harness::Straps', 'new()' );

### Testing _is_comment()

my $comment;
ok( !$strap->_is_comment("foo", \$comment), '_is_comment(), not a comment'  );
ok( !defined $comment,                      '  no comment set'              );

ok( !$strap->_is_comment("f # oo", \$comment), '  not a comment with #'     );
ok( !defined $comment,                         '  no comment set'           );

my %comments = (
                "# stuff and things # and stuff"    => 
                                        ' stuff and things # and stuff',
                "    # more things "                => ' more things ',
                "#"                                 => '',
               );

for my $line ( sort keys %comments ) {
    my $line_comment = $comments{$line};
    my $strap = Test::Harness::Straps->new;
    isa_ok( $strap, 'Test::Harness::Straps' );

    my $name = substr($line, 0, 20);
    ok( $strap->_is_comment($line, \$comment),        "  comment '$name'"   );
    is( $comment, $line_comment,                      '  right comment set' );
}



### Testing _is_header()

my @not_headers = (' 1..2',
                   '1..M',
                   '1..-1',
                   '2..2',
                   '1..a',
                   '',
                  );

foreach my $unheader (@not_headers) {
    my $strap = Test::Harness::Straps->new;

    ok( !$strap->_is_header($unheader),     
        "_is_header(), not a header '$unheader'" );

    ok( (!grep { exists $strap->{$_} } qw(max todo skip_all)),
        "  max, todo and skip_all are not set" );
}


my @attribs = qw(max skip_all todo);
my %headers = (
   '1..2'                               => { max => 2 },
   '1..1'                               => { max => 1 },
   '1..0'                               => { max => 0,
                                             skip_all => '',
                                           },
   '1..0 # Skipped: no leverage found'  => { max      => 0,
                                             skip_all => 'no leverage found',
                                           },
   '1..4 # Skipped: no leverage found'  => { max      => 4,
                                             skip_all => 'no leverage found',
                                           },
   '1..0 # skip skip skip because'      => { max      => 0,
                                             skip_all => 'skip skip because',
                                           },
   '1..10 todo 2 4 10'                  => { max        => 10,
                                             'todo'       => { 2  => 1,
                                                               4  => 1,
                                                               10 => 1,
                                                           },
                                           },
   '1..10 todo'                         => { max        => 10 },
   '1..192 todo 4 2 13 192 # Skip skip skip because'   => 
                                           { max     => 192,
                                             'todo'    => { 4   => 1, 
                                                            2   => 1, 
                                                            13  => 1, 
                                                            192 => 1,
                                                        },
                                             skip_all => 'skip skip because'
                                           }
);

for my $header ( sort keys %headers ) {
    my $expect = $headers{$header};
    my $strap = Test::Harness::Straps->new;
    isa_ok( $strap, 'Test::Harness::Straps' );

    ok( $strap->_is_header($header),    "_is_header() is a header '$header'" );

    is( $strap->{skip_all}, $expect->{skip_all},      '  skip_all set right' )
      if defined $expect->{skip_all};

    ok( eq_set( [map $strap->{$_},  grep defined $strap->{$_},  @attribs],
                [map $expect->{$_}, grep defined $expect->{$_}, @attribs] ),
        '  the right attributes are there' );
}



### Testing _is_test()

my %tests = (
             'ok'       => { 'ok' => 1 },
             'not ok'   => { 'ok' => 0 },

             'ok 1'     => { 'ok' => 1, number => 1 },
             'not ok 1' => { 'ok' => 0, number => 1 },

             'ok 2938'  => { 'ok' => 1, number => 2938 },

             'ok 1066 - and all that'   => { 'ok'     => 1,
                                             number => 1066,
                                             name   => "- and all that" },
             'not ok 42 - universal constant'   => 
                                      { 'ok'     => 0,
                                        number => 42,
                                        name   => '- universal constant',
                                      },
             'not ok 23 # TODO world peace'     => { 'ok'     => 0,
                                                     number => 23,
                                                     type   => 'todo',
                                                     reason => 'world peace'
                                                   },
             'ok 11 - have life # TODO get a life'  => 
                                      { 'ok'     => 1,
                                        number => 11,
                                        name   => '- have life',
                                        type   => 'todo',
                                        reason => 'get a life'
                                      },
             'not ok # TODO'    => { 'ok'     => 0,
                                     type   => 'todo',
                                     reason => ''
                                   },
             'ok # skip'        => { 'ok'     => 1,
                                     type   => 'skip',
                                   },
             'not ok 11 - this is \# all the name # skip this is not'
                                => { 'ok'     => 0,
                                     number => 11,
                                     name   => '- this is \# all the name',
                                     type   => 'skip',
                                     reason => 'this is not'
                                   },
             "ok 42 - _is_header() is a header '1..192 todo 4 2 13 192 \\# Skip skip skip because"
                                => { 'ok'   => 1,
                                     number => 42,
                                     name   => "- _is_header() is a header '1..192 todo 4 2 13 192 \\# Skip skip skip because",
                                   },
            );

for my $line ( sort keys %tests ) {
    my $expect = $tests{$line};
    my %test;
    ok( $strap->_is_test($line, \%test),    "_is_test() spots '$line'" );

    foreach my $type (qw(ok number name type reason)) {
        cmp_ok( $test{$type}, 'eq', $expect->{$type}, "  $type" );
    }
}

my @untests = (
               ' ok',
               'not',
               'okay 23',
              );
foreach my $line (@untests) {
    my $strap = Test::Harness::Straps->new;
    isa_ok( $strap, 'Test::Harness::Straps' );

    my %test = ();
    ok( !$strap->_is_test($line, \%test),    "_is_test() disregards '$line'" );

    # is( keys %test, 0 ) won't work in 5.004 because it's undef.
    ok( !keys %test,                         '  and produces no test info'   );
}


### Test _is_bail_out()

my %bails = (
             'Bail out!'                 =>  undef,
             'Bail out!  Wing on fire.'  => 'Wing on fire.',
             'BAIL OUT!'                 => undef,
             'bail out! - Out of coffee' => '- Out of coffee',
            );

for my $line ( sort keys %bails ) {
    my $expect = $bails{$line};
    my $strap = Test::Harness::Straps->new;
    isa_ok( $strap, 'Test::Harness::Straps' );

    my $reason;
    ok( $strap->_is_bail_out($line, \$reason), "_is_bail_out() spots '$line'");
    is( $reason, $expect,                       '  with the right reason' );
}

my @unbails = (
               '  Bail out!',
               'BAIL OUT',
               'frobnitz',
               'ok 23 - BAIL OUT!',
              );

foreach my $line (@unbails) {
    my $strap = Test::Harness::Straps->new;
    isa_ok( $strap, 'Test::Harness::Straps' );

    my $reason;

    ok( !$strap->_is_bail_out($line, \$reason),  
                                       "_is_bail_out() ignores '$line'" );
    is( $reason, undef,                         '  and gives no reason' );
}