Skip to content
Newer
Older
100644 71 lines (61 sloc) 2.44 KB
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
1 use v6;
2 use Test;
3
4 sub throws_like($code, $ex_type, *%matcher) {
6ff177c @moritz even nicer test output
moritz authored Jan 24, 2012
5 my $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
6 if $code ~~ Callable {
6ff177c @moritz even nicer test output
moritz authored Jan 24, 2012
7 $msg = 'code dies';
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
8 $code()
9 } else {
6ff177c @moritz even nicer test output
moritz authored Jan 24, 2012
10 $msg = "'$code' died";
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
11 eval $code;
12 }
6ff177c @moritz even nicer test output
moritz authored Jan 24, 2012
13 ok 0, $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
14 skip 'Code did not die, can not check exception', 1 + %matcher.elems;
15 CATCH {
16 default {
6ff177c @moritz even nicer test output
moritz authored Jan 24, 2012
17 ok 1, $msg;
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored Dec 18, 2011
18 my $type_ok = $_.WHAT === $ex_type;
19 ok $type_ok , "right exception type ({$ex_type.^name})";
20 if $type_ok {
21 for %matcher.kv -> $k, $v {
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored Jan 24, 2012
22 my $got = $_."$k"();
23 my $ok = $got ~~ $v,;
24 ok $ok, ".$k matches $v";
25 unless $ok {
26 diag "Got: $got\n"
27 ~"Expected: $v";
28
29 }
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored Dec 18, 2011
30 }
31 } else {
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored Jan 24, 2012
32 diag "Got: {$_.WHAT.gist}\n"
33 ~"Expected: {$ex_type.gist}";
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored Dec 18, 2011
34 skip 'wrong exception type', %matcher.elems;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
35 }
36 }
37 }
38 }
39
40 throws_like { Buf.new().Str }, X::Buf::AsStr, method => 'Str';
41 throws_like 'class Foo { $!bar }', X::Attribute::Undeclared,
42 name => '$!bar', package-name => 'Foo';
43 throws_like 'sub f() { $^x }', X::Signature::Placeholder,
44 line => 1;
45
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored Dec 18, 2011
46 #?rakudo skip 'parsing of $& and other p5 variables'
47 throws_like '$&', X::Obsolete, old => '$@ variable', new => '$/ or $()';
48
f52c2ed @moritz test presence of placeholder attribute of X::Placeholder::Block excep…
moritz authored Jan 23, 2012
49 throws_like 'do { $^x }', X::Placeholder::Block, placeholder => '$^x';
50 throws_like 'do { @_ }', X::Placeholder::Block, placeholder => '@_';
51 throws_like 'class { $^x }', X::Placeholder::Block, placeholder => '$^x';
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored Jan 24, 2012
52 throws_like '$^x', X::Placeholder::Mainline, placeholder => '$^x';
53 throws_like 'sub f(*@a = 2) { }', X::Parameter::Default, how => 'slurpy';
54 throws_like 'sub f($x! = 3) { }', X::Parameter::Default, how => 'required';
55 throws_like 'sub f(:$x! = 3) { }', X::Parameter::Default, how => 'required';
56 throws_like 'sub f($:x) { }', X::Parameter::Placeholder,
57 parameter => '$:x',
58 right => ':$x';
59 throws_like 'sub f($?x) { }', X::Parameter::Twigil,
60 parameter => '$?x',
61 twigil => '?';
62 throws_like 'sub (Int Str $x) { }', X::Parameter::TypeConstraint;
63
64
65
0b7d7d2 @moritz more typed exception tests
moritz authored Jan 23, 2012
66 throws_like 'my @a; my @a', X::Redeclaration, symbol => '@a';
67 throws_like 'sub a { }; sub a { }',X::Redeclaration, symbol => 'a', what => 'routine';
68 throws_like 'CATCH { }; CATCH { }', X::Phaser::Once, block => 'CATCH';
fbb78ed @moritz test for X::Placeholder::Block
moritz authored Jan 23, 2012
69
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored Dec 17, 2011
70 done;
Something went wrong with that request. Please try again.