| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Test | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Module defining a test harness which processes template input and | ||
| 7 | # then compares the output against pre-define expected output. | ||
| 8 | # Uses Test::Builder for TAP output, compatible with Test::Harness | ||
| 9 | # and subtests. This was originally the t/texpect.pl script. | ||
| 10 | # | ||
| 11 | # AUTHOR | ||
| 12 | # Andy Wardley <abw@wardley.org> | ||
| 13 | # | ||
| 14 | # COPYRIGHT | ||
| 15 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||
| 16 | # | ||
| 17 | # This module is free software; you can redistribute it and/or | ||
| 18 | # modify it under the same terms as Perl itself. | ||
| 19 | # | ||
| 20 | #============================================================================ | ||
| 21 | |||
| 22 | package Template::Test; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Template; | ||
| 27 | use Exporter; | ||
| 28 | use Test::Builder; | ||
| 29 | |||
| 30 | use constant MSWin32 => $^O eq 'MSWin32'; | ||
| 31 | |||
| 32 | our $VERSION = '3.100'; | ||
| 33 | our $DEBUG = 0; | ||
| 34 | our @ISA = qw( Exporter ); | ||
| 35 | our @EXPORT = qw( ntests ok is match flush skip_all test_expect callsign banner ); | ||
| 36 | our @EXPORT_OK = ( 'assert' ); | ||
| 37 | our %EXPORT_TAGS = ( all => [ @EXPORT_OK, @EXPORT ] ); | ||
| 38 | $| = 1; | ||
| 39 | |||
| 40 | our $REASON = 'not applicable on this platform'; | ||
| 41 | our $NO_FLUSH = 0; | ||
| 42 | our $EXTRA = 0; # any extra tests to come after test_expect() | ||
| 43 | our $PRESERVE = 0 # don't mangle newlines in output/expect | ||
| 44 | unless defined $PRESERVE; | ||
| 45 | |||
| 46 | our ($loaded, %callsign); | ||
| 47 | |||
| 48 | # always set binmode on Win32 machines so that any output generated | ||
| 49 | # is true to what we expect | ||
| 50 | $Template::BINMODE = (MSWin32) ? 1 : 0; | ||
| 51 | |||
| 52 | my $TB = Test::Builder->new; | ||
| 53 | my @results = (); | ||
| 54 | my $plan_declared = 0; | ||
| 55 | *is = \&match; | ||
| 56 | |||
| 57 | END { | ||
| 58 | # ensure flush() is called to print any cached results | ||
| 59 | flush(); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | #------------------------------------------------------------------------ | ||
| 64 | # ntests($n) | ||
| 65 | # | ||
| 66 | # Declare how many (more) tests are expected to come. If ok() is called | ||
| 67 | # before ntests() then the results are cached instead of being printed | ||
| 68 | # to STDOUT. When ntests() is called, the total number of tests | ||
| 69 | # (including any cached) is known and the "1..$ntests" line can be | ||
| 70 | # printed along with the cached results. After that, calls to ok() | ||
| 71 | # generated printed output immediately. | ||
| 72 | #------------------------------------------------------------------------ | ||
| 73 | |||
| 74 | sub ntests { | ||
| 75 | my $n = shift; | ||
| 76 | # add any pre-declared extra tests, or pre-stored test @results, to | ||
| 77 | # the grand total of tests | ||
| 78 | $n += $EXTRA + scalar @results; | ||
| 79 | |||
| 80 | if ($n) { | ||
| 81 | $TB->plan(tests => $n); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $TB->skip_all($REASON); | ||
| 85 | } | ||
| 86 | $plan_declared = 1; | ||
| 87 | |||
| 88 | # flush cached results | ||
| 89 | foreach my $pre_test (@results) { | ||
| 90 | ok(@$pre_test); | ||
| 91 | } | ||
| 92 | @results = (); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | #------------------------------------------------------------------------ | ||
| 97 | # ok($truth, $msg) | ||
| 98 | # | ||
| 99 | # Tests the value passed for truth and generates an "ok $n" or "not ok $n" | ||
| 100 | # line accordingly. If ntests() hasn't been called then we cached | ||
| 101 | # results for later, instead. | ||
| 102 | #------------------------------------------------------------------------ | ||
| 103 | |||
| 104 | sub ok { | ||
| 105 | my ($ok, $msg) = @_; | ||
| 106 | |||
| 107 | # cache results if ntests() not yet called | ||
| 108 | unless ($plan_declared) { | ||
| 109 | push(@results, [ $ok, $msg ]); | ||
| 110 | return $ok; | ||
| 111 | } | ||
| 112 | |||
| 113 | $TB->ok($ok, $msg); | ||
| 114 | return $ok; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | #------------------------------------------------------------------------ | ||
| 120 | # assert($truth, $error) | ||
| 121 | # | ||
| 122 | # Test value for truth, die if false. | ||
| 123 | #------------------------------------------------------------------------ | ||
| 124 | |||
| 125 | sub assert { | ||
| 126 | my ($ok, $err) = @_; | ||
| 127 | return ok(1) if $ok; | ||
| 128 | |||
| 129 | # failed | ||
| 130 | my ($pkg, $file, $line) = caller(); | ||
| 131 | $err ||= "assert failed"; | ||
| 132 | $err .= " at $file line $line\n"; | ||
| 133 | ok(0); | ||
| 134 | die $err; | ||
| 135 | } | ||
| 136 | |||
| 137 | #------------------------------------------------------------------------ | ||
| 138 | # match( $result, $expect ) | ||
| 139 | #------------------------------------------------------------------------ | ||
| 140 | |||
| 141 | sub match { | ||
| 142 | my ($result, $expect, $msg) = @_; | ||
| 143 | |||
| 144 | # force stringification of $result to avoid 'no eq method' overload errors | ||
| 145 | $result = "$result" if ref $result; | ||
| 146 | |||
| 147 | if ($result eq $expect) { | ||
| 148 | return ok(1, $msg); | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | my $count = $plan_declared ? $TB->current_test + 1 : scalar @results + 1; | ||
| 152 | $TB->diag("FAILED $count:\n expect: [$expect]\n result: [$result]"); | ||
| 153 | return ok(0, $msg); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | #------------------------------------------------------------------------ | ||
| 159 | # flush() | ||
| 160 | # | ||
| 161 | # Flush any tests results. | ||
| 162 | #------------------------------------------------------------------------ | ||
| 163 | |||
| 164 | sub flush { | ||
| 165 | ntests(0) | ||
| 166 | unless $plan_declared || $NO_FLUSH; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | #------------------------------------------------------------------------ | ||
| 171 | # skip_all($reason) | ||
| 172 | # | ||
| 173 | # Skip all tests, setting $REASON to contain any message passed. Calls | ||
| 174 | # exit(0) which triggers flush() which generates a "1..0 # $REASON" | ||
| 175 | # string to keep to test harness happy. | ||
| 176 | #------------------------------------------------------------------------ | ||
| 177 | |||
| 178 | sub skip_all { | ||
| 179 | $REASON = join('', @_); | ||
| 180 | $NO_FLUSH = 1; | ||
| 181 | $TB->skip_all($REASON); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | #------------------------------------------------------------------------ | ||
| 186 | # test_expect($input, $template, \%replace) | ||
| 187 | # | ||
| 188 | # This is the main testing sub-routine. The $input parameter should be a | ||
| 189 | # text string or a filehandle reference (e.g. GLOB or IO::Handle) from | ||
| 190 | # which the input text can be read. The input should contain a number | ||
| 191 | # of tests which are split up and processed individually, comparing the | ||
| 192 | # generated output against the expected output. Tests should be defined | ||
| 193 | # as follows: | ||
| 194 | # | ||
| 195 | # -- test -- | ||
| 196 | # test input | ||
| 197 | # -- expect -- | ||
| 198 | # expected output | ||
| 199 | # | ||
| 200 | # -- test -- | ||
| 201 | # etc... | ||
| 202 | # | ||
| 203 | # The number of tests is determined and ntests() is called to generate | ||
| 204 | # the "0..$n" line compatible with Test::Harness. Each test input is | ||
| 205 | # then processed by the Template object passed as the second parameter, | ||
| 206 | # $template. This may also be a hash reference containing configuration | ||
| 207 | # which are used to instantiate a Template object, or may be left | ||
| 208 | # undefined in which case a default Template object will be instantiated. | ||
| 209 | # The third parameter, also optional, may be a reference to a hash array | ||
| 210 | # defining template variables. This is passed to the template process() | ||
| 211 | # method. | ||
| 212 | #------------------------------------------------------------------------ | ||
| 213 | |||
| 214 | sub test_expect { | ||
| 215 | my ($src, $tproc, $params) = @_; | ||
| 216 | my ($input, @tests); | ||
| 217 | my ($output, $expect, $match); | ||
| 218 | my $count = 0; | ||
| 219 | my $ttprocs; | ||
| 220 | |||
| 221 | # read input text | ||
| 222 | eval { | ||
| 223 | local $/ = undef; | ||
| 224 | $input = ref $src ? <$src> : $src; | ||
| 225 | }; | ||
| 226 | if ($@) { | ||
| 227 | ntests(1); ok(0); | ||
| 228 | warn "Cannot read input text from $src\n"; | ||
| 229 | return undef; | ||
| 230 | } | ||
| 231 | |||
| 232 | # remove any comment lines | ||
| 233 | $input =~ s/^#.*?\n//gm; | ||
| 234 | |||
| 235 | # remove anything before '-- start --' and/or after '-- stop --' | ||
| 236 | $input = $' if $input =~ /\s*--\s*start\s*--\s*/; | ||
| 237 | $input = $` if $input =~ /\s*--\s*stop\s*--\s*/; | ||
| 238 | |||
| 239 | @tests = split(/^\s*--\s*test\s*--\s*\n/im, $input); | ||
| 240 | |||
| 241 | # if the first line of the file was '--test--' (optional) then the | ||
| 242 | # first test will be empty and can be discarded | ||
| 243 | shift(@tests) if $tests[0] =~ /^\s*$/; | ||
| 244 | |||
| 245 | ntests(3 + scalar(@tests) * 2); | ||
| 246 | |||
| 247 | # first test is that Template loaded OK, which it did | ||
| 248 | ok(1, 'running test_expect()'); | ||
| 249 | |||
| 250 | # optional second param may contain a Template reference or a HASH ref | ||
| 251 | # of constructor options, or may be undefined | ||
| 252 | if (ref($tproc) eq 'HASH') { | ||
| 253 | # create Template object using hash of config items | ||
| 254 | $tproc = Template->new($tproc) | ||
| 255 | || die Template->error(), "\n"; | ||
| 256 | } | ||
| 257 | elsif (ref($tproc) eq 'ARRAY') { | ||
| 258 | # list of [ name => $tproc, name => $tproc ], use first $tproc | ||
| 259 | $ttprocs = { @$tproc }; | ||
| 260 | $tproc = $tproc->[1]; | ||
| 261 | } | ||
| 262 | elsif (! ref $tproc) { | ||
| 263 | $tproc = Template->new() | ||
| 264 | || die Template->error(), "\n"; | ||
| 265 | } | ||
| 266 | # otherwise, we assume it's a Template reference | ||
| 267 | |||
| 268 | # test: template processor created OK | ||
| 269 | ok($tproc, 'template processor is engaged'); | ||
| 270 | |||
| 271 | # third test is that the input read ok, which it did | ||
| 272 | ok(1, 'input read and split into ' . scalar @tests . ' tests'); | ||
| 273 | |||
| 274 | # the remaining tests are defined in @tests... | ||
| 275 | foreach $input (@tests) { | ||
| 276 | $count++; | ||
| 277 | my $name = ''; | ||
| 278 | |||
| 279 | if ($input =~ s/^\s*-- name:? (.*?) --\s*\n//im) { | ||
| 280 | $name = $1; | ||
| 281 | } | ||
| 282 | else { | ||
| 283 | $name = "template text $count"; | ||
| 284 | } | ||
| 285 | |||
| 286 | # Configure a test as TODO | ||
| 287 | my $todo = ''; | ||
| 288 | if ($input =~ s/^\s*-- todo:? (.*?) --\s*\n//im) { | ||
| 289 | $todo = ( $1 eq '' ) ? 'No reason given' : $1; | ||
| 290 | } | ||
| 291 | |||
| 292 | # split input by a line like "-- expect --" | ||
| 293 | ($input, $expect) = | ||
| 294 | split(/^\s*--\s*expect\s*--\s*\n/im, $input); | ||
| 295 | $expect = '' | ||
| 296 | unless defined $expect; | ||
| 297 | |||
| 298 | $output = ''; | ||
| 299 | |||
| 300 | # input text may be prefixed with "-- use name --" to indicate a | ||
| 301 | # Template object in the $ttproc hash which we should use | ||
| 302 | if ($input =~ s/^\s*--\s*use\s+(\S+)\s*--\s*\n//im) { | ||
| 303 | my $ttname = $1; | ||
| 304 | my $ttlookup; | ||
| 305 | if ($ttlookup = $ttprocs->{ $ttname }) { | ||
| 306 | $tproc = $ttlookup; | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | warn "no such template object to use: $ttname\n"; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | # process input text | ||
| 314 | $tproc->process(\$input, $params, \$output) || do { | ||
| 315 | warn "Template process failed: ", $tproc->error(), "\n"; | ||
| 316 | # report failure and automatically fail the expect match | ||
| 317 | ok(0, "$name process FAILED: " . subtext($input)); | ||
| 318 | ok(0, '(obviously did not match expected)'); | ||
| 319 | next; | ||
| 320 | }; | ||
| 321 | |||
| 322 | # processed OK | ||
| 323 | ok(1, "$name processed OK: " . subtext($input)); | ||
| 324 | |||
| 325 | # another hack: if the '-- expect --' section starts with | ||
| 326 | # '-- process --' then we process the expected output | ||
| 327 | # before comparing it with the generated output. This is | ||
| 328 | # slightly twisted but it makes it possible to run tests | ||
| 329 | # where the expected output isn't static. See t/date.t for | ||
| 330 | # an example. | ||
| 331 | |||
| 332 | if ($expect =~ s/^\s*--+\s*process\s*--+\s*\n//im) { | ||
| 333 | my $out; | ||
| 334 | $tproc->process(\$expect, $params, \$out) || do { | ||
| 335 | warn( | ||
| 336 | "Template process failed (expect): ", | ||
| 337 | $tproc->error(), "\n" | ||
| 338 | ); | ||
| 339 | # report failure and automatically fail the expect match | ||
| 340 | ok( | ||
| 341 | 0, | ||
| 342 | "failed to process expected output [" . subtext($expect) . ']' | ||
| 343 | ); | ||
| 344 | next; | ||
| 345 | }; | ||
| 346 | $expect = $out; | ||
| 347 | }; | ||
| 348 | |||
| 349 | # strip any trailing blank lines from expected and real output | ||
| 350 | foreach ($expect, $output) { | ||
| 351 | s/[\n\r]*\Z//mg; | ||
| 352 | } | ||
| 353 | |||
| 354 | $match = ($expect eq $output) ? 1 : 0; | ||
| 355 | if (! $match || $DEBUG) { | ||
| 356 | $TB->diag("MATCH FAILED") | ||
| 357 | unless $match; | ||
| 358 | |||
| 359 | my ($copyi, $copye, $copyo) = ($input, $expect, $output); | ||
| 360 | unless ($PRESERVE) { | ||
| 361 | foreach ($copyi, $copye, $copyo) { | ||
| 362 | s/\n/\\n/g; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | $TB->diag(sprintf( | ||
| 366 | " input: [%s]\nexpect: [%s]\noutput: [%s]", | ||
| 367 | $copyi, $copye, $copyo | ||
| 368 | )); | ||
| 369 | } | ||
| 370 | |||
| 371 | my $testprefix = $name; | ||
| 372 | if ( $todo ) { | ||
| 373 | $testprefix = "# TODO $todo - $name"; | ||
| 374 | } | ||
| 375 | |||
| 376 | ok($match, $match ? "$testprefix matched expected" : "$testprefix did not match expected"); | ||
| 377 | }; | ||
| 378 | } | ||
| 379 | |||
| 380 | #------------------------------------------------------------------------ | ||
| 381 | # callsign() | ||
| 382 | # | ||
| 383 | # Returns a hash array mapping lower a..z to their phonetic alphabet | ||
| 384 | # equivalent. | ||
| 385 | #------------------------------------------------------------------------ | ||
| 386 | |||
| 387 | sub callsign { | ||
| 388 | my %callsign; | ||
| 389 | @callsign{ 'a'..'z' } = qw( | ||
| 390 | alpha bravo charlie delta echo foxtrot golf hotel india | ||
| 391 | juliet kilo lima mike november oscar papa quebec romeo | ||
| 392 | sierra tango umbrella victor whisky x-ray yankee zulu ); | ||
| 393 | return \%callsign; | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | #------------------------------------------------------------------------ | ||
| 398 | # banner($text) | ||
| 399 | # | ||
| 400 | # Prints a banner with the specified text if $DEBUG is set. | ||
| 401 | #------------------------------------------------------------------------ | ||
| 402 | |||
| 403 | sub banner { | ||
| 404 | return unless $DEBUG; | ||
| 405 | my $text = join('', @_); | ||
| 406 | my $count = $TB->current_test; | ||
| 407 | $TB->note("-" x 72 . "\n$text ($count tests completed)\n" . "-" x 72); | ||
| 408 | } | ||
| 409 | |||
| 410 | |||
| 411 | sub subtext { | ||
| 412 | my $text = shift; | ||
| 413 | $text =~ s/\s*$//sg; | ||
| 414 | $text = substr($text, 0, 32) . '...' if length $text > 32; | ||
| 415 | $text =~ s/\n/\\n/g; | ||
| 416 | return $text; | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | 1; | ||
| 421 | |||
| 422 | __END__ |