From f506534d4df57aeb241bc87010ee7294d9927f1d Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Wed, 28 Jul 2010 00:23:04 -0700 Subject: [PATCH] Refactor Test.pm6 to use an inner Builder --- Test.pm6 | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/Test.pm6 b/Test.pm6 index 35b014d3..156759b5 100644 --- a/Test.pm6 +++ b/Test.pm6 @@ -2,21 +2,47 @@ module Test; # XXX our due to the STD pad bug constant $?TRANSPARENT = 1; -sub blame() { - my $frame = caller; - while $frame.hints('$?TRANSPARENT') { - $frame = $frame.caller; +class Builder { + has $.current-test; + + method new() { + $*TEST-BUILDER; } - $frame.file ~ (" line " ~ $frame.line); -} -my $testnum = 1; -sub ok($bool, $tag) is export { - my $not = (if $bool { "" } else { "not " }); - say ($not ~ ("ok " ~ ($testnum++ ~ (" - " ~ $tag)))); - if !$bool { say ("# " ~ blame()); } -} + method blame() { + my $frame = caller; + while $frame.hints('$?TRANSPARENT') { + $frame = $frame.caller; + } + $frame.file ~ (" line " ~ $frame.line); + } + + method _output($text) { + say $text; + } + + method reset() { + $.current-test = 1; + } -sub plan($num) is export { - say ("1.." ~ $num); + method note($m) { + self._output("# " ~ $m); + 0; + } + + method ok($bool, $tag) { + my $not = $bool ?? "" !! "not "; + self._output($not ~ ("ok " ~ ($.current-test++ ~ (" - " ~ $tag)))); + if !$bool { self.note(self.blame); } + } + + method expected-tests($num) { + self._output("1.." ~ $num); + } } + +$GLOBAL::TEST-BUILDER = Builder.CREATE; +$GLOBAL::TEST-BUILDER.reset; + +sub ok($bool, $tag) is export { $*TEST-BUILDER.ok($bool, $tag) } +sub plan($num) is export { $*TEST-BUILDER.expected-tests($num) }