Created
September 28, 2012 04:43
-
-
Save kwatch/3797954 to your computer and use it in GitHub Desktop.
define OK() assertion function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
## | |
## define OK() assertion function | |
## | |
use strict; | |
use warnings; | |
my $spec = undef; | |
my $num = 0; | |
sub spec { | |
my ($text, $block) = @_; | |
$spec = $text; | |
$num++; | |
_run_spec($text, $num, $block); | |
} | |
sub _run_spec { | |
my ($text, $num, $block) = @_; | |
if (! $block) { | |
print "not ok $num - $text # TODO not implemented yet\n"; | |
return; | |
} | |
eval { | |
$block->(); | |
}; | |
my $err = $@; | |
$@ = undef; | |
if (! $err) { | |
print "ok $num - $text\n"; | |
} | |
else { | |
print "not ok $num - $text\n"; | |
$err =~ s/^/# /mg; | |
$err .= "\n" unless $err =~ /\n\z/; | |
print STDERR $err; | |
} | |
} | |
sub OK { | |
my ($expr) = @_; | |
unless ($expr) { | |
my ($pkgname, $filepath, $linenum) = caller(); | |
die "AssertionFailed at $filepath line $linenum.\n"; | |
} | |
} | |
if ($0 eq __FILE__) { | |
#use Test::More tests => 2; | |
print "1..2\n"; | |
spec "1+1 should be 2", sub { | |
OK(1+1 == 2); | |
}; | |
spec "1-1 should be 0", sub { | |
OK(1-1 == 0); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment