Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 142 lines (122 sloc) 5.54 kB
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
1 use v6;
2 use Test;
3
2002a2f @moritz more typed exceptions
moritz authored
4 #?DOES 1
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
5 sub throws_like($code, $ex_type, *%matcher) {
6ff177c @moritz even nicer test output
moritz authored
6 my $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
7 if $code ~~ Callable {
6ff177c @moritz even nicer test output
moritz authored
8 $msg = 'code dies';
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
9 $code()
10 } else {
6ff177c @moritz even nicer test output
moritz authored
11 $msg = "'$code' died";
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
12 eval $code;
13 }
6ff177c @moritz even nicer test output
moritz authored
14 ok 0, $msg;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
15 skip 'Code did not die, can not check exception', 1 + %matcher.elems;
16 CATCH {
17 default {
6ff177c @moritz even nicer test output
moritz authored
18 ok 1, $msg;
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
19 my $type_ok = $_.WHAT === $ex_type;
20 ok $type_ok , "right exception type ({$ex_type.^name})";
21 if $type_ok {
22 for %matcher.kv -> $k, $v {
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
23 my $got = $_."$k"();
24 my $ok = $got ~~ $v,;
25 ok $ok, ".$k matches $v";
26 unless $ok {
27 diag "Got: $got\n"
28 ~"Expected: $v";
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}";
6467df2 @moritz test X::Attribute::Package and X::Attribute::NoPackage
moritz authored
34 diag "Exception message: $_.message()";
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
35 skip 'wrong exception type', %matcher.elems;
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
36 }
37 }
38 }
39 }
40
41 throws_like { Buf.new().Str }, X::Buf::AsStr, method => 'Str';
6763664 @moritz fix test for X::Attribute::Undeclared
moritz authored
42 throws_like 'my class Foo { method a() { $!bar } }', X::Attribute::Undeclared,
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
43 name => '$!bar', package-name => 'Foo';
44 throws_like 'sub f() { $^x }', X::Signature::Placeholder,
45 line => 1;
46
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
47 #?rakudo skip 'parsing of $& and other p5 variables'
48 throws_like '$&', X::Obsolete, old => '$@ variable', new => '$/ or $()';
49
f52c2ed @moritz test presence of placeholder attribute of X::Placeholder::Block excep…
moritz authored
50 throws_like 'do { $^x }', X::Placeholder::Block, placeholder => '$^x';
51 throws_like 'do { @_ }', X::Placeholder::Block, placeholder => '@_';
52 throws_like 'class { $^x }', X::Placeholder::Block, placeholder => '$^x';
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
53 throws_like '$^x', X::Placeholder::Mainline, placeholder => '$^x';
d5ac5a6 @moritz RT #73502, @_ in the mainline
moritz authored
54 # RT #73502
55 throws_like '@_', X::Placeholder::Mainline, placeholder => '@_';
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
56 throws_like 'sub f(*@a = 2) { }', X::Parameter::Default, how => 'slurpy';
57 throws_like 'sub f($x! = 3) { }', X::Parameter::Default, how => 'required';
58 throws_like 'sub f(:$x! = 3) { }', X::Parameter::Default, how => 'required';
59 throws_like 'sub f($:x) { }', X::Parameter::Placeholder,
60 parameter => '$:x',
61 right => ':$x';
62 throws_like 'sub f($?x) { }', X::Parameter::Twigil,
63 parameter => '$?x',
64 twigil => '?';
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
65 throws_like 'sub (Int Str $x) { }', X::Parameter::MultipleTypeConstraints;
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
66
67
68
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
69 # some of these redeclaration errors take different code
70 # paths in rakudo, so we over-test a bit to catch them all,
71 # even if the tests look rather boring;
0b7d7d2 @moritz more typed exception tests
moritz authored
72 throws_like 'my @a; my @a', X::Redeclaration, symbol => '@a';
73 throws_like 'sub a { }; sub a { }',X::Redeclaration, symbol => 'a', what => 'routine';
63626c2 @moritz another redeclaration test
moritz authored
74 throws_like 'sub a { }; multi sub a { }',X::Redeclaration, symbol => 'a', what => 'routine';
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
75 throws_like 'my class A { }; my class A { }', X::Redeclaration, symbol => 'A';
76 throws_like 'my class B { }; my subset B { }', X::Redeclaration, symbol => 'B';
77 throws_like 'CATCH { }; CATCH { }', X::Phaser::Multiple, block => 'CATCH';
fbb78ed @moritz test for X::Placeholder::Block
moritz authored
78
6467df2 @moritz test X::Attribute::Package and X::Attribute::NoPackage
moritz authored
79 throws_like 'my class A { my @a; @a!List::foo() }',
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
80 X::Method::Private::Permission,
81 method => 'foo',
82 calling-package => 'A',
83 source-package => 'List';
84
85 throws_like '1!foo()',
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
86 X::Method::Private::Unqualified,
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
87 method => 'foo';
88
a824698 @moritz typed binding excpetions
moritz authored
89 throws_like 'sub f() { }; f() := 2', X::Bind::WrongLHS;
90 throws_like 'my int $x := 2', X::Bind::NativeType;
91
dc765eb @moritz tests for typed exceptions
moritz authored
92 throws_like 'for (1; 1; 1) { }', X::Obsolete,
93 old => rx/<<for>>/,
94 replacement => rx/<<loop>>/;
95 throws_like 'foreach (1..10) { }', X::Obsolete,
96 old => "'foreach'",
97 replacement => "'for'";
98 throws_like 'undef', X::Obsolete,
99 old => rx/<<undef>>/;
100
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
101 throws_like 'my $a::::b', X::Syntax::Name::Null;
d1f0fd8 @moritz tests for typed syntax errors
moritz authored
102 throws_like 'unless 1 { } else { }', X::Syntax::UnlessElse;
103 throws_like 'for my $x (1, 2, 3) { }', X::Syntax::P5;
104 throws_like ':!foo(3)', X::Syntax::NegatedPair;
105 throws_like 'my $0', X::Syntax::Variable::Numeric;
106 throws_like 'my $<a>', X::Syntax::Variable::Match;
33e961e @moritz RT #69230, my $!foo
moritz authored
107 throws_like 'my class A { my $!foo }', X::Syntax::Variable::Twigil, twigil => '!', scope => 'my';
277e13d @moritz more tests for typed syntax errors
moritz authored
108 throws_like 'my $::("foo")', X::Syntax::Variable::IndirectDeclaration;
171ad0a @moritz test X::Undeclared
moritz authored
109 throws_like '@a', X::Undeclared, symbol => '@a';
277e13d @moritz more tests for typed syntax errors
moritz authored
110 throws_like 'augment class Any { }', X::Syntax::Augment::WithoutMonkeyTyping;
111 throws_like 'use MONKEY_TYPING; augment role Positional { }', X::Syntax::Augment::Role;
112
113 throws_like 'sub f($a?, $b) { }', X::Parameter::WrongOrder,
114 misplaced => 'required',
115 after => 'optional';
116 throws_like 'sub f(*@a, $b) { }', X::Parameter::WrongOrder,
117 misplaced => 'required',
118 after => 'variadic';
119 throws_like 'sub f(*@a, $b?) { }', X::Parameter::WrongOrder,
120 misplaced => 'optional positional',
121 after => 'variadic';
03faea7 @moritz X::Syntax::Name::NotNull
moritz authored
122
2002a2f @moritz more typed exceptions
moritz authored
123 #?rakudo skip 'parsing regression'
124 throws_like '#`', X::Syntax::Comment::Embedded;
125 #?rakudo skip 'parsing regression'
126 throws_like '=begin', X::Syntax::Pod::BeginWithoutIdentifier;
127
128 throws_like '@', X::Syntax::SigilWithoutName;
129 throws_like '1∞', X::Syntax::Confused;
130
59f3f35 @moritz test exception type for stuff that must be known at compile time
moritz authored
131 throws_like 'm:i(@*ARGS[0])/foo/', X::Value::Dynamic;
132 throws_like 'my enum Foo (:x(@*ARGS[0]))', X::Value::Dynamic;
133
5c0802d @moritz more exception tests
moritz authored
134 throws_like 'self', X::Syntax::Self::WithoutObject;
478ed38 @moritz more tests for availabilty-of-self related errors
moritz authored
135 throws_like 'class { has $.x = $.y }', X::Syntax::VirtualCall, call => '$.y';
136 throws_like '$.a', X::Syntax::NoSelf, variable => '$.a';
5c0802d @moritz more exception tests
moritz authored
137
6467df2 @moritz test X::Attribute::Package and X::Attribute::NoPackage
moritz authored
138 throws_like 'has $.x', X::Attribute::NoPackage;
139 throws_like 'my module A { has $.x }', X::Attribute::Package, package-type => 'module';
140
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
141 done;
Something went wrong with that request. Please try again.