File: runner.t

package info (click to toggle)
libtest-lectrotest-perl 0.5001-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: perl: 1,808; sh: 13; makefile: 6
file content (396 lines) | stat: -rwxr-xr-x 9,954 bytes parent folder | download | duplicates (6)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/perl

use warnings;
use strict;

use File::Temp;

use Test::More tests => 28;
use Test::LectroTest::Generator ':all';
use Test::LectroTest::Property;
use Test::LectroTest::TestRunner;

BEGIN { unshift @INC, 't/lib'; }
use CaptureOutput;


=head1 NAME

t/runner.t - tests for Property and TestRunner

=head1 SYNOPSIS

perl -Ilib t/runner.t

=head1 DESCRIPTION

This test suite excercises Property and TestRunner, which work
hand in hand.

=head2 SET UP

First, we declare a few helper functions.

=cut

sub check($@) {
    my $property = shift;
    my $runner = Test::LectroTest::TestRunner->new( @_ );
    my $details = $runner->run( $property )->details;
    return $details;
}

=pod

Next, we declare a few simple properties to check.

=cut

my $except_gen   = Gen { die "gen go boom!" };

my $null_1gens   = Property { ##[ ]## 1 };
my $null_2gens   = Property { ##[ ], [ ]## 1 };
my $null_retry   = Property { ##[ ]## $tcon->retry };
my $except_prop1 = Property { ##[ ]## die "prop go boom!" };
my $except_prop2 = Property { ##[ x <- $except_gen ]## 1 };
my $except_prop3 = Property { ##[ x <- Int ], [ x <- $except_gen ]## 1 };
my $ex_retry     = Property { ##[ x <- Int ], [ x <- $except_gen ]## 
                              $tcon->retry };

=pod 

=head2 TRIALS

Some tests to see if the C<trials> control knob is working.

=cut

like( check( $null_1gens, trials => 1 ),
      qr/^ok.*1 attempts/,
      "1 gen set + trials=>1 --> 1 trial" );

like( check( $null_2gens, trials => 1 ),
      qr/^ok.*2 attempts/,
      "2 gen set + trials=>1 --> 2 trials" );

=pod

=head2 RETRIES

Some tests to see if the C<retries> control knob is working.

=cut

# should not finish the first trial but abort after 10 retries

like( check( $null_retry, trials => 1, retries => 10 ),
      qr/^not ok.*incomplete/,
      "retry-always prop --> incomplete" );

# we should exhaust all of our retries on the first property check
# (using the first set of bindings) and never get to the second, which
# uses a generator that will throw an exception; therefore the
# check should be marked "incomplete"

like( check( $ex_retry, trials => 1, retries => 10 ),
      qr/^not ok.*incomplete/,
      "retry before exception prop --> incomplete" );

=pod

=head2 EXCEPTION HANDLING

Some tests to see if exceptions are caught and reported properly:

=cut

for (qw(1 2 3)) {
    my $prop_str = '$except_prop' . $_;
    my $prop = eval $prop_str or die "can't get $prop_str";
    like( check( $prop, trials => 1, retries => 10),
          qr/^not ok.*exception/s,
          "$prop_str dies and is caught" );
}

=pod

=head2 LABELING

Some tests to observe labeling properties.

=cut

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->label(); 1 } )
             , trials => 10 )
      , qr/%/s,
      , "labeling every trial with an empty label yields no label output" );

like( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->label("all"); 1 } )
           , trials => 10 )
    , qr/^ok.*100% all/s,
    , "labeling every trial --> 100%" );

like( check( ( Property { ##[ x <- Unit(0) ], [ x <- Unit(1) ]##
                          $tcon->label("odd") if $x; 1 } )
           , trials => 10 )
    , qr/^ok.*50% odd/s,
    , "labeling half of trials --> 50%" );


sub labler {
    my @labels = @_;
    my $count = 0;
    return Property {
        ##[ ]##
        $tcon->label( $labels[$count++] );
        $count = 0 if $count == @labels;
        1;
    };
}

# the following test assumes that the number of trials
# is a multiple of 4

like( check( labler(qw|a a a b|), trials => 1000 ),
      qr/ 75% a.*25% b/s,
      "75/25 labeling case checks" );

# the following test assumes that the number of trials
# is a multiple of 10

like( check( labler(qw|a a a a a a a b b c|), trials => 1000),
      qr/ 70% a.*20% b.*10% c/s,
      "70/20/10 labeling case checks" );


my $trivial = Property { ##[ #]##
    $tcon->trivial;
    1;
};

like( check($trivial, trials => 100),
      qr/100% trivial/,
      "100% trivial labeling case checks" );

=pod

=pod

=head2 COUNTEREXAMPLE NOTES

Now we check to see whether notes attached to a failing
trial are emitted as part of a counterexample.

=cut

# notes should be emitted only when the property check fails

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->note("XXXX"); 1 } )
             , trials => 10 )
      , qr/XXXX/s,
      , "notes appear only when a check fails"
);

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->dump(0, "XXXX"); 1 } )
             , trials => 10 )
      , qr/XXXX/s,
      , "dump notes appear only when a check fails"
);



# when the check fails, all notes must be emitted, in order

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->note(1,2,3,4,5); 0 } )
             , trials => 10 )
      , qr/Notes:\s+1\s+2\s+3\s+4\s+5/s,
      , "all notes are emitted, in order, when check fails"
);

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->dump("XXX", "x");
                          $tcon->dump("YYY", "y");
                          0 } )
             , trials => 10 )
      , qr/Notes:\s+\$x = "XXX";\s+\$y = "YYY"/s,
      , "dump notes are emitted, in order, when check fails"
);

unlike( check( ( Property { ##[ x <- Unit(0) ]##
                          $tcon->dump("XXX");
                          $tcon->dump("YYY");
                          0 } )
             , trials => 10 )
      , qr/Notes:\s+\$VAR1 = "XXX";\s+\$VAR2 = "YYY"/s,
      , "unnamed dump notes are emitted, in order, when check fails"
);


=head2 SCALEFN

Here we check to see whether our scaling function is being
used.

=cut

my $gen_scale = Gen { $_[0] };  # return scaling guidance as gen'd value
sub prop_scale($) {
    my $scale = shift;
    Property { ##[ x <- $gen_scale ]##
        $tcon->label("desired scale") if $x == $scale;
        1
    }
}

for (qw(0 1 10)) {
    my $scale = $_;
    like( check( prop_scale($_), scalefn => sub { $scale }, trials => 10 )
          , qr/^ok.*100% desired scale/s,
          , "desired scale $_ --> 100%" );
}


=pod

=head2 TEST NUMBERING

Here we see whether we can override the TestRunner's built in
numbering.

=cut

like( Test::LectroTest::TestRunner->new->run($null_1gens, 123)->summary,
      qr/ok 123/, "TestRunner->run(x,N) respects given test number N"
);

=pod

=head2 VERBOSITY

Now we check to see whether the verbosity indicator is respected.

=cut

# this sub captures the output for a suite of property checks

for ([1, \&like, "does"], [0, \&unlike, "does not"]) {
    my ($verbose, $testfn, $does) = @$_;

    $testfn->( check_suite( verbose => $verbose,
                            trials  => 10,
                            Property { ##[ x <- Unit(0) ]##
                                       $tcon->label("all"); 1 } ),
               , qr/%/s,
               , "verbose=>$verbose $does include label statistics"
    );
}

for ([1, \&like, "does"], [0, \&unlike, "does not"]) {
    my ($verbose, $testfn, $does) = @$_;

    $testfn->( check_suite( verbose => $verbose,
                            trials  => 10,
                            Property { ##[ x <- Unit(0) ]##
                                       $x > 0 } ),
               , qr/counterexample/i,
               , "verbose=>$verbose $does include counterexample"
    );
}


=pod

=head2 FAILURE RECORDING

Now we check to see if we can record failures and play them
back as regression tests.

=cut

{
    my $tmp       = File::Temp->new();

    my @vals;
    my $prop_fail = Property { ##[ x <- Int ]## push @vals, $x; 0 };
    my $prop_succ = Property { ##[ x <- Int ]## push @vals, $x; 1 };

    my $checkit = sub {
        my $prop = shift;
        check_suite(($prop) x 10, trials => 1, @_);
    };

    # record ten failures into the regression file and save the
    # values of x for each in @vals

    $checkit->($prop_fail, record_failures => $tmp->filename);
    my @recorded_vals = @vals;
    @vals = ();

    # check ten successful properties using the regression file from
    # earlier; because these properties have the same name as the
    # failing properties checked above ("Unnamed"), the ten recorded
    # failure cases will be tried for each of these properties, in
    # addition to the one random case that would normally be tried
    # for each

    $checkit->($prop_succ, playback_failures => $tmp->filename);

    # now @vals should contain 10 played-back failues and 1 random
    # trial for *each* for the ten successful property checks; here we
    # remove the random-trial value for each property check so
    # that we may compare the played back recording to the original

    splice(@vals, 11 * $_ + 10, 1) for reverse 0..9;
    is_deeply( \@vals,
              [ (@recorded_vals) x 10 ],
              "recorded failures are played back as regression tests" );


    my $prop_newname = Property { ##[ x <- Int ]## push @vals, $x; 1 },
                       name => "a new name";
    @vals = ();
    $checkit->($prop_newname, playback_failures => $tmp->filename);
    is( scalar @vals, 10,
        "failures recorded for a different prop are ignored" );

}


=pod

=head2 HELPER FUNCTIONS

The following helper checks the given properties as a suite
and returns the test output as a string.

=cut

sub check_suite {
    my @props = grep  is_prop($_), @_;
    my @opts  = grep !is_prop($_), @_;
    my $recorder = capture(*STDOUT);
    Test::LectroTest::TestRunner->new(@opts)->run_suite(@props);
    return $recorder->();
}

sub is_prop {
    ref $_[0] eq 'Test::LectroTest::Property';
}



=head1 AUTHOR

Tom Moertel (tom@moertel.com)

=head1 COPYRIGHT and LICENSE

Copyright (C) 2004 by Thomas G Moertel.  All rights reserved.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.