File: 20_Class_Throwable_subException_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 (75 lines) | stat: -rw-r--r-- 2,342 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 17;

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

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

eval {
	throw Class::Throwable "This is our first exception";
};
my $e1 = $@;
isa_ok($e1, 'Class::Throwable');

eval {
    throw Class::Throwable "This is our second exception", $e1;
};
my $e2 = $@;
isa_ok($e2, 'Class::Throwable');

eval {
    throw Class::Throwable "This is our third exception", $e2;
};
my $e3 = $@;
isa_ok($e3, 'Class::Throwable');

can_ok($e1, 'hasSubException');
can_ok($e1, 'getSubException');
can_ok($e1, 'stringValue');

ok(!$e1->hasSubException(), '... e1 does not have a sub-exception');

ok($e2->hasSubException(), '... e2 does have a sub-exception');
is($e2->getSubException()->stringValue(), $e1->stringValue(), '... e2\'s sub-exception is e1');

ok($e3->hasSubException(), '... e3 does have a sub-exception');
is($e3->getSubException()->stringValue(), $e2->stringValue(), '... e3\'s sub-exception is e2');

my $expected = <<EXPECTED;
Class::Throwable : This is our third exception
  |--[ main::(eval) called in t${path_seperator}20_Class_Throwable_subException_test.t line 28 ]
  + Class::Throwable : This is our second exception
      |--[ main::(eval) called in t${path_seperator}20_Class_Throwable_subException_test.t line 22 ]
      + Class::Throwable : This is our first exception
          |--[ main::(eval) called in t${path_seperator}20_Class_Throwable_subException_test.t line 16 ]
EXPECTED

is($e3->toString(2), $expected, '... toString prints subexceptions too');

eval {
    my $test = sub { $_[0] / 0 };
    eval { $test->(2) };
    throw Class::Throwable "Testing non-object sub-Exceptions", $@;
};
isa_ok($@, 'Class::Throwable');

ok($@->hasSubException(), '... we do have a sub-exception');
like($@->getSubException(), 
     qr/Illegal division by zero/, 
     '... our sub-exception is a string');
     
my $expected2 = <<EXPECTED2;
Class::Throwable : Testing non-object sub-Exceptions
  |--[ main::(eval) called in t${path_seperator}20_Class_Throwable_subException_test.t line 57 ]
  + Illegal division by zero at t${path_seperator}20_Class_Throwable_subException_test.t line 58.
EXPECTED2

is($@->toString(2), $expected2, '... toString prints what we expected');