File: SuiteTest.pm

package info (click to toggle)
libtest-unit-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: perl: 4,299; makefile: 5
file content (167 lines) | stat: -rw-r--r-- 5,321 bytes parent folder | download | duplicates (8)
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
package SuiteTest;

use strict;

use base qw(Test::Unit::TestCase);

use Test::Unit::Result;
use Test::Unit::TestSuite;
use TornDown;
use WasRun;
require Test::Unit::Assertion::CodeRef;

my %method_hash = (runs => 'run_count',
                   failures => 'failure_count',
                   success  => 'was_successful',
                   errors   => 'error_count',);
sub new {
    my $self = shift()->SUPER::new(@_);
    $self->{_my_result} = undef;
    $self->{__default_assertion} =
        Test::Unit::Assertion::CodeRef->new(sub {
            my $arg_hash = shift;
            for (qw/runs failures errors/) {
                next unless exists $arg_hash->{$_};
                my $method = $method_hash{$_};
                my $expected = $arg_hash->{$_};
                my $got      = $self->result->$method();
                $expected == $got or
                    die "Expected $expected $_, got $got\n";
            }
            if (exists $arg_hash->{'success'}) {
                my $method = $method_hash{'success'};
                my $expected = $arg_hash->{'success'};
                my $got = $self->result->$method();
                $expected && $got || !$expected && !$got or
                    die "Expected ", $expected ? 'success,' : 'failure,',
                        ' got ', $got ? 'success.' : 'failure.', "\n";
            }
            1;
        });
    return $self;
}

sub basic_assertion {
    my $self = shift;
    $self->{__default_assertion}->do_assertion(ref($_[0]) ? shift : {@_});
}

sub result {
    my $self = shift;
    return $self->{_my_result};
}
    
sub set_up {
    my $self = shift;
    $self->{_my_result} = Test::Unit::Result->new();
}

sub suite {
    my $class = shift;
    my $suite = Test::Unit::TestSuite->empty_new("Suite Tests");
    $suite->add_test(SuiteTest->new("test_no_test_case_class"));
    $suite->add_test(SuiteTest->new("test_no_test_cases"));
    $suite->add_test(SuiteTest->new("test_one_test_case"));
    $suite->add_test(SuiteTest->new("test_not_existing_test_case"));
    $suite->add_test(SuiteTest->new("test_inherited_tests"));
    $suite->add_test(SuiteTest->new("test_inherited_inherited_tests"));
    $suite->add_test(SuiteTest->new("test_shadowed_tests"));
    $suite->add_test(SuiteTest->new("test_complex_inheritance"));
    return $suite;
}

# test subs

sub test_inherited_tests {
    my $self = shift;
    my $suite = Test::Unit::TestSuite->new("InheritedTestCase");
    $suite->run($self->result());
    $self->basic_assertion({success => 1, runs => 2});
    $self->assert($self->result()->was_successful());
    $self->assert(2 == $self->result->run_count);
}

sub test_complex_inheritance {
    my $self = shift;
    eval q{
        package _SuperClass;
        use base qw(Test::Unit::TestCase);
        sub test_case {
            my $self = shift;
            $self->assert($self->override_this_method );
        }
        sub override_this_method { 0 ; }
        
        package _SubClass;
        use base qw(_SuperClass);
        sub override_this_method { 1 ; }
    };
    die $@ if $@;
    my $suite = Test::Unit::TestSuite->new("_SubClass");
    my $result = $self->result;
    $suite->run($result);
    
    $self->assert($result->was_successful());
    $self->assert(1 == $self->result->run_count);
}

sub test_inherited_inherited_tests {
    my $self = shift;
    my $suite = Test::Unit::TestSuite->new("InheritedInheritedTestCase");
    $suite->run($self->result());
    $self->basic_assertion(success => 1, runs => 3);
    $self->assert($self->result()->was_successful());
    $self->assert(3 == $self->result()->run_count());
}

sub test_no_test_case_class {
    my $self = shift;
    eval {
      my $suite = Test::Unit::TestSuite->new("NoTestCaseClass");
    };
    $self->assert_str_equals("Class NoTestCaseClass was not a test case or test suite.\n", "$@");
}

sub test_no_test_cases {
    my $self = shift;
    my $t = Test::Unit::TestSuite->new("NoTestCases");
    $t->run($self->result());
    $self->basic_assertion(runs => 1, failures => 1, success => 0);
    $self->assert(1 == $self->result()->run_count()); # warning test
    $self->assert(1 == $self->result()->failure_count());
    $self->assert(not $self->result()->was_successful());
}

sub test_not_existing_test_case {
    my $self = shift;
    my $t = SuiteTest->new("not_existing_method");
    $t->run($self->result());
    $self->basic_assertion(runs => 1, failures => 1, errors => 0);
    $self->assert(1 == $self->result()->run_count());
    $self->assert(1 == $self->result()->failure_count());
    $self->assert(0 == $self->result()->error_count());
}

sub test_one_test_case {
    my $self = shift;
    my $t = Test::Unit::TestSuite->new("OneTestCase");
    $t->run($self->result());
    $self->basic_assertion(runs => 1, failures => 0, errors => 0, success => 1);
    $self->assert(1 == $self->result()->run_count());
    $self->assert(0 == $self->result()->failure_count());
    $self->assert(0 == $self->result()->error_count());
    $self->assert($self->result()->was_successful());
}

sub test_shadowed_tests {
    my $self = shift;
    my $t = Test::Unit::TestSuite->new("OverrideTestCase");
    $t->run($self->result());
    $self->basic_assertion(runs => 1);
    $self->assert(1 == $self->result()->run_count());
}



1;