Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (79 sloc) 2.802 kb
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
1 use v6;
2
3 use Test;
4
5547d3f [t/spec] Test for RT #60966
kyle authored
5 plan 20;
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
6
7 =begin description
8
9 Testing parameter traits for subroutines
10
11 L<S06/"Parameter traits">
12
13 =end description
14
15 my $foo=1;
16
17 # note: many of these errors can be detected at compile time, so need
db93e89 [t/spec] unmark some passing rakudo tests due to container/value refacto...
particle authored
18 # eval_dies_ok instead of dies_ok
d4308c8 [t/spec] another test for ro-ness of arguments.
moritz authored
19 #
20 # test twice, once with assignment and once with increment, rakudo
21 # used to catch the first but not the latter.
22 #
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
23 eval_dies_ok '
97fdbb2 [t/spec] don't rely on a lexicals working in eval() when testing ro-ness...
moritz authored
24 my $tmp = 1;
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
25 sub mods_param ($x) { $x++; }
97fdbb2 [t/spec] don't rely on a lexicals working in eval() when testing ro-ness...
moritz authored
26 mods_param($tmp)
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
27 ',
28 'can\'t modify parameter, constant by default';
db93e89 [t/spec] unmark some passing rakudo tests due to container/value refacto...
particle authored
29
d4308c8 [t/spec] another test for ro-ness of arguments.
moritz authored
30 eval_dies_ok '
31 my $tmp = 1;
32 sub mods_param ($x) { $x = 1; }
33 mods_param($tmp)
34 ',
35 'can\'t modify parameter, constant by default';
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
36
37 # is readonly
db93e89 [t/spec] unmark some passing rakudo tests due to container/value refacto...
particle authored
38 eval_dies_ok 'sub mods_param_constant ($x is readonly) { $x++; };
39 mods_param_constant($foo);' ,
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
40 'can\'t modify constant parameter, constant by default';
41
42 sub mods_param_rw ($x is rw) { $x++; }
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
43 #?rakudo todo "'is rw' can't modify constants"
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
44 dies_ok { mods_param_rw(1) }, 'can\'t modify constant even if we claim it\'s rw';
45 sub mods_param_rw_does_nothing ($x is rw) { $x; }
46 lives_ok { mods_param_rw_does_nothing(1) }, 'is rw with non-lvalue should autovivify';
47
48 lives_ok { mods_param_rw($foo) }, 'pass by "is rw" doesn\'t die';
49 is($foo, 2, 'pass by reference works');
50
51 #icopy
52 $foo=1;
53 sub mods_param_copy ($x is copy) {$x++;}
54 lives_ok { mods_param_copy($foo) }, 'is copy';
55 is($foo, 1, 'pass by value works');
56
26e3caa [t] merge xx-uncategorized/optional_copy.t into S06-traits/misc.t, and m...
moritz authored
57 # same test with default value
58 sub boom ($arg is copy = 0) { $arg++ }
59
60 lives_ok { boom(42) }, "can modify a copy";
61
62
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
63 # is ref
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
64 #?rakudo skip 'is ref'
65 {
66 $foo=1;
67 sub mods_param_ref ($x is ref) { $x++; }
68 dies_ok { mods_param_ref(1); }, 'is ref with non-lvalue';
1c0341c [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
69 #?pugs todo
70 lives_ok { mods_param_ref($foo); }, 'is ref with non-lvalue';
71 #?pugs todo
72 is($foo, 2, 'is ref works');
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
73 }
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
74
3b8f4d2 [t/spec] Some more tests to exercise <->.
jnthn authored
75
76 # with <-> we should still obey readonly traits
77 {
78 my $anon1 = <-> $a is readonly, $b { $b++ };
79 my $anon2 = <-> $a is readonly, $b { $a++ };
80 my $x = 1;
81 $anon1($x, $x);
82 is($x, 2, '<-> does not override explicit traints (sanity)');
83 dies_ok({ $anon2($x, $x) }, '<-> does not override explicit traints');
84 is($x, 2, '<-> does not override explicit traints (sanity)');
85 }
86
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
87 # is context
88 # Doesn't even compile, which is lucky, because I don't understand it well
89 # enough to write an actual test...
90 ok(eval('sub my_format (*@data is context(Item)) { }; 1'), "is context - compile check");
91
5547d3f [t/spec] Test for RT #60966
kyle authored
92 # To do - check that is context actually works
26e3caa [t] merge xx-uncategorized/optional_copy.t into S06-traits/misc.t, and m...
moritz authored
93
5547d3f [t/spec] Test for RT #60966
kyle authored
94 #?rakudo todo 'RT 60966'
95 {
96 eval 'sub oh_noes( $gack is nonesuch ) { }';
97
98 ok $! ~~ Exception, "Can't use an unknown trait";
99 ok "$!" ~~ /trait/, 'error message mentions trait';
100 ok "$!" ~~ /nonesuch/, 'error message mentions the name of the trait';
101 }
26e3caa [t] merge xx-uncategorized/optional_copy.t into S06-traits/misc.t, and m...
moritz authored
102
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
103 # vim: ft=perl6
Something went wrong with that request. Please try again.