Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (90 sloc) 3.8 kb
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
1 use v6;
2 use Test;
3
4 sub throws_like($code, $ex_type, *%matcher) {
6ff177c @moritz even nicer test output
moritz authored
5 my $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
6 if $code ~~ Callable {
6ff177c @moritz even nicer test output
moritz authored
7 $msg = 'code dies';
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
8 $code()
9 } else {
6ff177c @moritz even nicer test output
moritz authored
10 $msg = "'$code' died";
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
11 eval $code;
12 }
6ff177c @moritz even nicer test output
moritz authored
13 ok 0, $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
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
17 ok 1, $msg;
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
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
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
30 }
31 } else {
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
32 diag "Got: {$_.WHAT.gist}\n"
33 ~"Expected: {$ex_type.gist}";
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
34 skip 'wrong exception type', %matcher.elems;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
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
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 exceptio...
moritz authored
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
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 => '?';
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
62 throws_like 'sub (Int Str $x) { }', X::Parameter::MultipleTypeConstraints;
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
63
64
65
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
66 # some of these redeclaration errors take different code
67 # paths in rakudo, so we over-test a bit to catch them all,
68 # even if the tests look rather boring;
0b7d7d2 @moritz more typed exception tests
moritz authored
69 throws_like 'my @a; my @a', X::Redeclaration, symbol => '@a';
70 throws_like 'sub a { }; sub a { }',X::Redeclaration, symbol => 'a', what => 'routine';
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
71 throws_like 'my class A { }; my class A { }', X::Redeclaration, symbol => 'A';
72 throws_like 'my class B { }; my subset B { }', X::Redeclaration, symbol => 'B';
73 throws_like 'CATCH { }; CATCH { }', X::Phaser::Multiple, block => 'CATCH';
fbb78ed @moritz test for X::Placeholder::Block
moritz authored
74
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
75 throws_like 'class A { my @a; @a!List::foo() }',
76 X::Method::Private::Permission,
77 method => 'foo',
78 calling-package => 'A',
79 source-package => 'List';
80
81 throws_like '1!foo()',
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
82 X::Method::Private::Unqualified,
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
83 method => 'foo';
84
a824698 @moritz typed binding excpetions
moritz authored
85 throws_like 'sub f() { }; f() := 2', X::Bind::WrongLHS;
86 throws_like 'my int $x := 2', X::Bind::NativeType;
87
dc765eb @moritz tests for typed exceptions
moritz authored
88 throws_like 'for (1; 1; 1) { }', X::Obsolete,
89 old => rx/<<for>>/,
90 replacement => rx/<<loop>>/;
91 throws_like 'foreach (1..10) { }', X::Obsolete,
92 old => "'foreach'",
93 replacement => "'for'";
94 throws_like 'undef', X::Obsolete,
95 old => rx/<<undef>>/;
96
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
97 throws_like 'my $a::::b', X::Syntax::Name::Null;
d1f0fd8 @moritz tests for typed syntax errors
moritz authored
98 throws_like 'unless 1 { } else { }', X::Syntax::UnlessElse;
99 throws_like 'for my $x (1, 2, 3) { }', X::Syntax::P5;
100 throws_like ':!foo(3)', X::Syntax::NegatedPair;
101 throws_like 'my $0', X::Syntax::Variable::Numeric;
102 throws_like 'my $<a>', X::Syntax::Variable::Match;
03faea7 @moritz X::Syntax::Name::NotNull
moritz authored
103
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
104 done;
Something went wrong with that request. Please try again.