File: ListenerTest.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 (89 lines) | stat: -rw-r--r-- 2,014 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
package ListenerTest;
    
# Test class used in SuiteTest

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

use Test::Unit::Result;

sub new {
    my $self = shift()->SUPER::new(@_);
    $self->{_my_result} = 0;
    $self->{_my_start_count} = 0;
    $self->{_my_end_count} = 0;
    $self->{_my_failure_count} = 0;
    $self->{_my_error_count} = 0;
    return $self;
}

sub add_error {
    my $self = shift;
    my ($test, $t) = @_;
    $self->{_my_error_count}++;
}
    
sub add_failure {
    my $self = shift;
    my ($test, $t) = @_;
    $self->{_my_failure_count}++;
}
    
sub end_test {
    my $self = shift;
    my ($test) = @_;
    $self->{_my_end_count}++;
}

sub set_up {
    my $self = shift;
    $self->{_my_result} = Test::Unit::Result->new();
    $self->{_my_result}->add_listener($self);
    $self->{_my_start_count} = 0;
    $self->{_my_end_count} = 0;
    $self->{_my_failure_count} = 0;
}

sub start_test {
    my $self = shift;
    $self->{_my_start_count}++;
}

sub add_pass {
}

# the tests
sub make_dummy_testcase {
    my $self = shift;
    my $sub  = pop;
    my $method_name = shift || 'run_test';

    Class::Inner->new(parent  => 'Test::Unit::TestCase',
                      methods => { $method_name => $sub },
                      args    => [ $method_name ]);
}

sub test_error {
    my $self = shift;
    my $test = $self->make_dummy_testcase(sub {die});
    $test->run($self->{_my_result});
    $self->assert(1 == $self->{_my_error_count});
    $self->assert(1 == $self->{_my_end_count});
}

sub test_failure {
    my $self = shift;
    my $test = $self->make_dummy_testcase(sub {shift->fail()});
    $test->run($self->{_my_result});
    $self->assert(1 == $self->{_my_failure_count});
    $self->assert(1 == $self->{_my_end_count});
}

sub test_start_stop {
    my $self = shift;
    my $test = $self->make_dummy_testcase(sub {});
    $test->run($self->{_my_result});
    $self->assert(1 == $self->{_my_start_count});
    $self->assert(1 == $self->{_my_end_count});
}

1;