File: 10_Class_Throwable_test.t

package info (click to toggle)
libclass-throwable-perl 0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 108 kB
  • ctags: 20
  • sloc: perl: 447; makefile: 43
file content (148 lines) | stat: -rw-r--r-- 4,889 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 30;

BEGIN { 
    use_ok('Class::Throwable');
}

my $path_seperator = "/";
$path_seperator = "\\" if $^O eq 'MSWin32';
$path_seperator = ":"  if $^O eq 'MacOS';

can_ok("Class::Throwable", 'throw');

# test without a message

eval { throw Class::Throwable };
isa_ok($@, 'Class::Throwable');

can_ok($@, 'getMessage');
is($@->getMessage(), 
  'An Class::Throwable Exception has been thrown', 
  '... the error is as we expected');

can_ok($@, 'toString');
is($@->toString(), 
  'Class::Throwable : An Class::Throwable Exception has been thrown', 
  '... the error is as we expected');

# test with a message 

eval { Class::Throwable->throw("Test Message") };
isa_ok($@, 'Class::Throwable');

is($@->getMessage(), 
  'Test Message',
  '... the error is as we expected');

is($@->toString(), 
  'Class::Throwable : Test Message', 
  '... the error is as we expected');
  
# test the stack trace now

can_ok($@, 'getStackTrace');
is_deeply(scalar $@->getStackTrace(),
        # these are the values in the stack trace:
        # $package, $filename, $line, $subroutine, 
        # $hasargs, $wantarray, $evaltext, $is_require
        [[ 'main', "t${path_seperator}10_Class_Throwable_test.t", '35', '(eval)', 0, undef, undef, undef ]],
        '... got the stack trace we expected');
        
is_deeply($@->getStackTrace(),
        # same thing in array context :)
        [ 'main', "t${path_seperator}10_Class_Throwable_test.t", '35', '(eval)', 0, undef, undef, undef ],
        '... got the stack trace we expected');   
        
can_ok($@, 'stackTraceToString');
is($@->stackTraceToString(),
   qq{  |--[ main::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 35 ]},
   '... got the stack trace string we expected');    
   
ok(overload::Overloaded($@), '... stringified overload');

Class::Throwable->import(VERBOSE => 0);

is("$@", '', '... got the stringified result we expected');  

Class::Throwable->import(VERBOSE => 1);

is("$@", 'Class::Throwable : Test Message', '... got the stringified result we expected');  

Class::Throwable->import(VERBOSE => 2);

is("$@",
   qq{Class::Throwable : Test Message
  |--[ main::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 35 ]
},
   '... got the stringified result we expected');    
   
my $e = $@;
eval { throw $e };
isa_ok($@, 'Class::Throwable');

is($@->stringValue(), $e->stringValue(), '... it is the same object, just re-thrown');

# some misc. weird stuff

eval {
    throw Class::Throwable [ 1 .. 5 ];
};
isa_ok($@, 'Class::Throwable');

is_deeply($@->getMessage(),
          [ 1 .. 5 ],
          '... you can use anything for a message');
                                                    
my $exception = Class::Throwable->new("A message for you");
isa_ok($exception, 'Class::Throwable');

is($exception->getMessage(), 'A message for you', '... got the message we expected');
is_deeply(scalar $exception->getStackTrace(), [], '... we dont have a stack trace yet');

eval {
	throw $exception;
};
isa_ok($@, 'Class::Throwable');
is($@, $exception, '... it is the same exception too');

is_deeply($@->getStackTrace(),
		  [ 'main', "t${path_seperator}10_Class_Throwable_test.t", '107', '(eval)', 0, undef, undef, undef ],
          '... got the stack trace we expected');  

{
    package Foo;
    sub foo { eval { Bar::bar() }; throw Class::Throwable "Foo!!", $@ }
    package Bar;
    sub bar { eval { Baz::baz() }; throw Class::Throwable "Bar!!", $@ }
    package Baz;
    sub baz { throw Class::Throwable "Baz!!" }
}

eval { Foo::foo() };

my $expected_big = <<EXPECTED_BIG;
Class::Throwable : Foo!!
  |--[ Foo::foo called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
  |--[ main::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
  + Class::Throwable : Bar!!
      |--[ Bar::bar called in t${path_seperator}10_Class_Throwable_test.t line 119 ]
      |--[ Foo::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 119 ]
      |--[ Foo::foo called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
      |--[ main::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
      + Class::Throwable : Baz!!
          |--[ Baz::baz called in t${path_seperator}10_Class_Throwable_test.t line 121 ]
          |--[ Bar::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 121 ]
          |--[ Bar::bar called in t${path_seperator}10_Class_Throwable_test.t line 119 ]
          |--[ Foo::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 119 ]
          |--[ Foo::foo called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
          |--[ main::(eval) called in t${path_seperator}10_Class_Throwable_test.t line 126 ]
EXPECTED_BIG

is($@->toString(2), $expected_big, '... got the big stack trace we were expecting');