Added bootstrap tests for Testcase.nqp.
[kakapo:rurbans-kakapo.git] / src / UnitTest / Testcase.nqp
1 # Copyright (C) 2009-2010, Austin Hastings. See accompanying LICENSE file, or 
2 # http://www.opensource.org/licenses/artistic-license-2.0.php for license.
3
4 module UnitTest::Testcase;
5
6 INIT {
7         use(    'P6metaclass' );
8         
9         has(    '$.name',
10                 '$!verify',
11         );
12         
13         export( 'assert_that', 'fail', 'verify_that' );
14         export( 'assert_false', 'assert_true', :tags('ASSERTS'));
15 }
16
17 my method assert_match($target, $matcher) {
18         unless $matcher.matches($target) {
19                 my $explain := $matcher.describe_self("Expected: ")
20                         ~ $matcher.describe_failure($target, "\n     but: ");           
21                 self._fail($explain);
22         }
23 }
24
25 sub assert_that($target, $matcher) {
26         get_self().assert_match($target, $matcher);
27 }
28
29 sub assert_false($bool, $message?) {
30         if $bool {
31                 get_self()._fail($message);
32         }
33 }
34
35 sub assert_true($bool, $message?) {
36         unless $bool {
37                 get_self()._fail($message);
38         }
39 }
40
41 my method default_loader() {
42         UnitTest::Loader.new;
43 }
44
45 my method default_result() {
46         my $result := UnitTest::Result.new;
47         $result.add_listener(UnitTest::Listener::TAP.new);
48         return $result;
49 }
50
51 my method _fail($why) {
52         Exception::UnitTestFailure.new(:message($why)).throw;
53 }
54
55 sub fail($why) {
56         get_self()._fail($why);
57 }
58
59 sub get_self() {
60         my $self := pir::find_dynamic_lex__PS('self');
61         
62         if pir::isnull($self) {
63                 pir::die("Fatal: No 'self' lexical in any caller scope");
64         }
65         
66         return $self;
67 }
68
69 our method num_tests() {
70         return 1;
71 }
72
73 method run($result?) {
74         unless $result.defined {
75                 $result := self.default_result;
76         }
77         
78         $result.start_test(self);
79         my $exception;
80         
81         try {
82                 self.set_up();
83                 self.run_test();
84                 
85                 CATCH {
86                         $exception := $!;
87                         $!.handled(1);
88                         
89                         if $!.type == Exception::UnitTestFailure.type {
90                                 $result.add_failure(self, $!);
91                         }
92                         else {
93                                 $result.add_error(self, $!);
94                         }
95                 }
96         };
97         
98         try {
99                 self.tear_down();
100                 
101                 CATCH {
102                         $!.handled(1);
103                         
104                         unless $exception.defined {
105                                 $exception := $!;
106                                 
107                                 if $!.type == Exception::UnitTestFailure.type {
108                                         $result.add_failure(self, $!);
109                                 }
110                                 else {
111                                         $result.add_error(self, $!);
112                                 }
113                         }
114                 }
115         };
116         
117         unless $exception.defined {
118                 # say("Caught: $exception");
119                 # say($exception.backtrace_string);
120                 
121                 $result.end_test(self);
122         }
123         
124         return $result;
125 }
126
127 method run_test() {
128         Parrot::call_method(self, self.name);
129 }
130
131 our method set_up() { }
132
133 our method suite() {
134         return self.default_loader.load_tests_from_testcase(self);
135 }
136
137 our method tear_down() { }
138
139 sub verify_that(*@text) {
140         get_self().verify(@text.join);
141 }