Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 85 lines (63 sloc) 2.201 kb
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
1 use v6;
2
3 use Test;
4
d4308c8 [t/spec] another test for ro-ness of arguments.
moritz authored
5 plan 14;
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
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 #
97fdbb2 [t/spec] don't rely on a lexicals working in eval() when testing ro-ness...
moritz authored
23 #?rakudo todo 'TODO: catch modification of subroutine arg'
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
24 eval_dies_ok '
97fdbb2 [t/spec] don't rely on a lexicals working in eval() when testing ro-ness...
moritz authored
25 my $tmp = 1;
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
26 sub mods_param ($x) { $x++; }
97fdbb2 [t/spec] don't rely on a lexicals working in eval() when testing ro-ness...
moritz authored
27 mods_param($tmp)
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
28 ',
29 'can\'t modify parameter, constant by default';
30
d4308c8 [t/spec] another test for ro-ness of arguments.
moritz authored
31 eval_dies_ok '
32 my $tmp = 1;
33 sub mods_param ($x) { $x = 1; }
34 mods_param($tmp)
35 ',
36 'can\'t modify parameter, constant by default';
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
37
38 # is readonly
39 eval_dies_ok 'sub mods_param_constant ($x is readonly) { $x++; };
40 mods_param_constant($foo);' ,
41 'can\'t modify constant parameter, constant by default';
42
43 sub mods_param_rw ($x is rw) { $x++; }
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
44 #?rakudo todo "'is rw' can't modify constants"
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
45 dies_ok { mods_param_rw(1) }, 'can\'t modify constant even if we claim it\'s rw';
46 sub mods_param_rw_does_nothing ($x is rw) { $x; }
47 lives_ok { mods_param_rw_does_nothing(1) }, 'is rw with non-lvalue should autovivify';
48
49 lives_ok { mods_param_rw($foo) }, 'pass by "is rw" doesn\'t die';
50 is($foo, 2, 'pass by reference works');
51
52 #icopy
53 $foo=1;
54 sub mods_param_copy ($x is copy) {$x++;}
55 lives_ok { mods_param_copy($foo) }, 'is copy';
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
56 #?rakudo todo 'is copy'
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
57 is($foo, 1, 'pass by value works');
58
26e3caa [t] merge xx-uncategorized/optional_copy.t into S06-traits/misc.t, and m...
moritz authored
59 # same test with default value
60 sub boom ($arg is copy = 0) { $arg++ }
61
62 lives_ok { boom(42) }, "can modify a copy";
63
64
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
65 # is ref
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
66 #?rakudo skip 'is ref'
67 {
68 $foo=1;
69 sub mods_param_ref ($x is ref) { $x++; }
70 dies_ok { mods_param_ref(1); }, 'is ref with non-lvalue';
71 lives_ok { mods_param_ref($foo); }, 'is ref with non-lvalue', :todo;
72 is($foo, 2, 'is ref works', :todo);
73 }
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
74
75 # is context
76 # Doesn't even compile, which is lucky, because I don't understand it well
77 # enough to write an actual test...
1b911a1 [t/spec] fudged S06-traits/misc.t for rakudo
moritz authored
78 #?rakudo skip 'is context'
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
79 ok(eval('sub my_format (*@data is context(Item)) { }; 1'), "is context - compile check");
80
26e3caa [t] merge xx-uncategorized/optional_copy.t into S06-traits/misc.t, and m...
moritz authored
81
82
4ae127a [t] moved sub_parameter_traits to spec/
moritz authored
83 # To do - check that is context actually works
84 # vim: ft=perl6
Something went wrong with that request. Please try again.