Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 188 lines (158 sloc) 8.029 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'
9fb1b22 @moritz test for qr obsoletion message
moritz authored
48 throws_like '$&', X::Obsolete, old => '$@ variable', replacement => '$, rx/<<rx>>// or $()';
49 throws_like 'qr/a/', X::Obsolete, old => rx/<<qr>>/, replacement => rx/<<rx>>/;
4c68d1a @moritz skipped tests for X::Obsolete
moritz authored
50
f52c2ed @moritz test presence of placeholder attribute of X::Placeholder::Block excep…
moritz authored
51 throws_like 'do { $^x }', X::Placeholder::Block, placeholder => '$^x';
52 throws_like 'do { @_ }', X::Placeholder::Block, placeholder => '@_';
53 throws_like 'class { $^x }', X::Placeholder::Block, placeholder => '$^x';
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
54 throws_like '$^x', X::Placeholder::Mainline, placeholder => '$^x';
d5ac5a6 @moritz RT #73502, @_ in the mainline
moritz authored
55 # RT #73502
56 throws_like '@_', X::Placeholder::Mainline, placeholder => '@_';
bea87e2 @moritz RT #85942
moritz authored
57 # RT #85942
58 throws_like '"foo".{ say $^a }', X::Placeholder::Mainline;
59
60
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
61 throws_like 'sub f(*@a = 2) { }', X::Parameter::Default, how => 'slurpy';
62 throws_like 'sub f($x! = 3) { }', X::Parameter::Default, how => 'required';
63 throws_like 'sub f(:$x! = 3) { }', X::Parameter::Default, how => 'required';
64 throws_like 'sub f($:x) { }', X::Parameter::Placeholder,
65 parameter => '$:x',
66 right => ':$x';
67 throws_like 'sub f($?x) { }', X::Parameter::Twigil,
68 parameter => '$?x',
69 twigil => '?';
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
70 throws_like 'sub (Int Str $x) { }', X::Parameter::MultipleTypeConstraints;
8d41f56 @moritz more typed exception tests, better diagnosis
moritz authored
71
72
73
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
74 # some of these redeclaration errors take different code
75 # paths in rakudo, so we over-test a bit to catch them all,
76 # even if the tests look rather boring;
0b7d7d2 @moritz more typed exception tests
moritz authored
77 throws_like 'my @a; my @a', X::Redeclaration, symbol => '@a';
78 throws_like 'sub a { }; sub a { }',X::Redeclaration, symbol => 'a', what => 'routine';
63626c2 @moritz another redeclaration test
moritz authored
79 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
80 throws_like 'my class A { }; my class A { }', X::Redeclaration, symbol => 'A';
81 throws_like 'my class B { }; my subset B { }', X::Redeclaration, symbol => 'B';
82 throws_like 'CATCH { }; CATCH { }', X::Phaser::Multiple, block => 'CATCH';
fbb78ed @moritz test for X::Placeholder::Block
moritz authored
83
6467df2 @moritz test X::Attribute::Package and X::Attribute::NoPackage
moritz authored
84 throws_like 'my class A { my @a; @a!List::foo() }',
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
85 X::Method::Private::Permission,
86 method => 'foo',
87 calling-package => 'A',
88 source-package => 'List';
89
90 throws_like '1!foo()',
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
91 X::Method::Private::Unqualified,
c01e5d6 @moritz tests for typed private method call exceptions
moritz authored
92 method => 'foo';
93
a824698 @moritz typed binding excpetions
moritz authored
94 throws_like 'sub f() { }; f() := 2', X::Bind::WrongLHS;
95 throws_like 'my int $x := 2', X::Bind::NativeType;
96
dc765eb @moritz tests for typed exceptions
moritz authored
97 throws_like 'for (1; 1; 1) { }', X::Obsolete,
98 old => rx/<<for>>/,
99 replacement => rx/<<loop>>/;
100 throws_like 'foreach (1..10) { }', X::Obsolete,
101 old => "'foreach'",
102 replacement => "'for'";
103 throws_like 'undef', X::Obsolete,
104 old => rx/<<undef>>/;
105
a97dc2a @moritz track change of error class names; add some more redeclaration tests
moritz authored
106 throws_like 'my $a::::b', X::Syntax::Name::Null;
d1f0fd8 @moritz tests for typed syntax errors
moritz authored
107 throws_like 'unless 1 { } else { }', X::Syntax::UnlessElse;
108 throws_like 'for my $x (1, 2, 3) { }', X::Syntax::P5;
109 throws_like ':!foo(3)', X::Syntax::NegatedPair;
110 throws_like 'my $0', X::Syntax::Variable::Numeric;
b291479 @moritz test parameter variant of X::Syntax::Variable::Numeric
moritz authored
111 throws_like 'my sub f($0) { }', X::Syntax::Variable::Numeric, what => 'parameter';
d1f0fd8 @moritz tests for typed syntax errors
moritz authored
112 throws_like 'my $<a>', X::Syntax::Variable::Match;
33e961e @moritz RT #69230, my $!foo
moritz authored
113 throws_like 'my class A { my $!foo }', X::Syntax::Variable::Twigil, twigil => '!', scope => 'my';
277e13d @moritz more tests for typed syntax errors
moritz authored
114 throws_like 'my $::("foo")', X::Syntax::Variable::IndirectDeclaration;
171ad0a @moritz test X::Undeclared
moritz authored
115 throws_like '@a', X::Undeclared, symbol => '@a';
277e13d @moritz more tests for typed syntax errors
moritz authored
116 throws_like 'augment class Any { }', X::Syntax::Augment::WithoutMonkeyTyping;
117 throws_like 'use MONKEY_TYPING; augment role Positional { }', X::Syntax::Augment::Role;
296a529 @timo test for X::Does::TypeObject raising for 'does' operator.
timo authored
118 throws_like 'my $foo does &Int', X::Does::TypeObject;
119 throws_like 'my $foo does &Int, &Bool', X::Does::TypeObject;
277e13d @moritz more tests for typed syntax errors
moritz authored
120
121 throws_like 'sub f($a?, $b) { }', X::Parameter::WrongOrder,
122 misplaced => 'required',
123 after => 'optional';
124 throws_like 'sub f(*@a, $b) { }', X::Parameter::WrongOrder,
125 misplaced => 'required',
126 after => 'variadic';
127 throws_like 'sub f(*@a, $b?) { }', X::Parameter::WrongOrder,
128 misplaced => 'optional positional',
129 after => 'variadic';
03faea7 @moritz X::Syntax::Name::NotNull
moritz authored
130
2002a2f @moritz more typed exceptions
moritz authored
131 #?rakudo skip 'parsing regression'
132 throws_like '#`', X::Syntax::Comment::Embedded;
1af5f7a @moritz RT #71814, =begin without identifier
moritz authored
133 # RT #71814
134 throws_like "=begin\n", X::Syntax::Pod::BeginWithoutIdentifier, line => 1, filename => rx/eval/;
2002a2f @moritz more typed exceptions
moritz authored
135
136 throws_like '@', X::Syntax::SigilWithoutName;
137 throws_like '1∞', X::Syntax::Confused;
e4dee67 @moritz X::Syntax::Missing
moritz authored
138 throws_like 'for 1, 2', X::Syntax::Missing, what => 'block';
a78af8c @moritz X::Syntax::Reserved
moritz authored
139 throws_like 'my @a()', X::Syntax::Reserved, reserved => /shape/ & /array/;
140 throws_like 'my &a()', X::Syntax::Reserved, instead => /':()'/;
2002a2f @moritz more typed exceptions
moritz authored
141
59f3f35 @moritz test exception type for stuff that must be known at compile time
moritz authored
142 throws_like 'm:i(@*ARGS[0])/foo/', X::Value::Dynamic;
143 throws_like 'my enum Foo (:x(@*ARGS[0]))', X::Value::Dynamic;
144
5c0802d @moritz more exception tests
moritz authored
145 throws_like 'self', X::Syntax::Self::WithoutObject;
478ed38 @moritz more tests for availabilty-of-self related errors
moritz authored
146 throws_like 'class { has $.x = $.y }', X::Syntax::VirtualCall, call => '$.y';
147 throws_like '$.a', X::Syntax::NoSelf, variable => '$.a';
5c0802d @moritz more exception tests
moritz authored
148
6467df2 @moritz test X::Attribute::Package and X::Attribute::NoPackage
moritz authored
149 throws_like 'has $.x', X::Attribute::NoPackage;
150 throws_like 'my module A { has $.x }', X::Attribute::Package, package-type => 'module';
151
46f9b17 @moritz track exception name change, and add another test
moritz authored
152 throws_like 'has sub a() { }', X::Declaration::Scope, scope => 'has', declaration => 'sub';
153 throws_like 'has package a { }', X::Declaration::Scope, scope => 'has', declaration => 'package';
eb6edd6 @moritz test X::Declaration::Scope::Multi
moritz authored
154 throws_like 'our multi a() { }', X::Declaration::Scope::Multi, scope => 'our';
a2b6fc2 @moritz more typed exceptions
moritz authored
155 throws_like 'multi sub () { }', X::Anon::Multi, multiness => 'multi';
156 throws_like 'proto sub () { }', X::Anon::Multi, multiness => 'proto';
3b9a604 @moritz more typed exceptions
moritz authored
157 throws_like 'class { multi method () { }}', X::Anon::Multi, routine-type => 'method';
5065e35 @moritz test X::Anon::Augment
moritz authored
158 throws_like 'use MONKEY_TYPING; augment class { }', X::Anon::Augment, package-type => 'class';
3804629 @moritz test X::Augment::NoSuchType
moritz authored
159 throws_like 'use MONKEY_TYPING; augment class NoSuchClass { }', X::Augment::NoSuchType,
160 package-type => 'class',
161 package => 'NoSuchClass';
3b9a604 @moritz more typed exceptions
moritz authored
162
163 throws_like ':45<abcd>', X::Syntax::Number::RadixOutOfRange, radix => 45;
dc3de2d @moritz X::Syntax::Regex::Adverb
moritz authored
164 throws_like 'rx:g/a/', X::Syntax::Regex::Adverb, adverb => 'g', construct => 'rx';
26f6834 @moritz X::Syntax::Signature::InvocantMarker
moritz authored
165 throws_like 'my sub f($x, $y:) { }', X::Syntax::Signature::InvocantMarker;
a2b6fc2 @moritz more typed exceptions
moritz authored
166
1bf4f1d @moritz X::OutOfRange
moritz authored
167 throws_like 'Date.new("2012-02-30")', X::OutOfRange,
168 range => Range, message => rx/<<1\.\.29>>/;
169
52f6db6 @moritz test for X::Routine::Unwrap
moritz authored
170 throws_like 'sub f() { }; &f.unwrap("foo")', X::Routine::Unwrap;
44d0aa3 @moritz test X::Constructor::Positional
moritz authored
171 throws_like 'Mu.new(1)', X::Constructor::Positional;
39b3eb7 @Siddhant add test for X::Hash::Store::Oddnumber
Siddhant authored
172 throws_like 'my %h = 1', X::Hash::Store::OddNumber;
52f6db6 @moritz test for X::Routine::Unwrap
moritz authored
173
fcd1668 @moritz Test for proper error of "sub foo;", RT #68710 and RT #111492
moritz authored
174 # TOOD: might be X::Syntax::Malformed too...
175 throws_like 'sub foo;', X::Syntax::Missing, what => 'block';
157d7b7 @moritz more tests for "constant" parse errors
moritz authored
176 throws_like 'constant foo;', X::Syntax::Missing, what => /initializer/;
177 throws_like 'constant * = 3;', X::Syntax::Missing, what => /constant/;
fcd1668 @moritz Test for proper error of "sub foo;", RT #68710 and RT #111492
moritz authored
178
ddc21bf @moritz test X::Package::Stubbed
moritz authored
179 throws_like 'class A {...}; grammar B { ... }', X::Package::Stubbed, packages => <A B>;
180
9831233 @moritz track phaser error changes in rakudo
moritz authored
181 throws_like 'my sub a { PRE 0 }; a()', X::Phaser::PrePost, phaser => 'PRE', condition => /0/;
182 throws_like 'my sub a { POST 0 }; a()', X::Phaser::PrePost, phaser => 'POST', condition => /0/;
efa9610 @moritz typed exceptions for PRE/POST failures
moritz authored
183
d720afa @moritz test that Str numification error message survives through infix:<+>
moritz authored
184 throws_like 'use fatal; my $x = "5 foo" + 8;', X::Str::Numeric, source => '5 foo', pos => 1,
185 reason => /trailing/;
186
3d3f9a2 @moritz initial tests for typed exceptions; very unstructured for now
moritz authored
187 done;
Something went wrong with that request. Please try again.