Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (91 sloc) 3.026 kB
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
1 use v6;
2
3 use Test;
4
428e7b0 @sorear Fix(?) S06/lvalue-subroutines, fudge Proxy tests for niecza
sorear authored
5 plan 14;
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
6
7 =begin description
8
9 Testing lvalue-returning subroutines
10
11 =end description
12
eda6db1 [t/spec] move smartlinks from POD to ordinary comments
moritz authored
13 # L<S06/"Lvalue subroutines">
14
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
15 # Lvalue subrefs
16 {
17 my $var1 = 1;
18 my $var2 = 2;
19
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
20 my $lastvar = sub () is rw { $var2 };
21 my $prevvar = sub () is rw { $lastvar() };
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
22
23 $lastvar() = 3;
24 is $var2, 3, "lvalue subroutine references work (simple)";
25
26 $prevvar() = 4;
27 is $var2, 4, "lvalue subroutine references work (nested)";
28 }
29
30 {
31 my $var = 42;
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
32 my $notlvalue = sub () { $var };
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
33
f25f151 [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
34 #?pugs 2 todo 'bug'
428e7b0 @sorear Fix(?) S06/lvalue-subroutines, fudge Proxy tests for niecza
sorear authored
35 #?niecza 2 todo 'rw checking'
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
36 dies_ok { $notlvalue() = 23 },
f25f151 [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
37 "assigning to non-rw subrefs should die";
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
38 is $var, 42,
f25f151 [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
39 "assigning to non-rw subrefs shouldn't modify the original variable";
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
40 }
41
42 my $var1 = 1;
43 my $var2 = 2;
44
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
45 sub lastvar is rw { $var2; }
46 sub prevvar is rw { lastvar(); }
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
47
48 lastvar() = 3;
49 is($var2, 3, "lvalue subroutines work (simple)");
50
51 prevvar() = 4;
52 is($var2, 4, "lvalue subroutines work (nested)");
53
54 {
55 my $var = 42;
56
57 # S6 says that lvalue subroutines are marked out by 'is rw'
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
58 sub notlvalue { $var; } # without rw
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
59
428e7b0 @sorear Fix(?) S06/lvalue-subroutines, fudge Proxy tests for niecza
sorear authored
60 #?niecza 2 todo 'rw checking'
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
61 dies_ok { notlvalue() = 5 },
62 "assigning to non-rw subs should die";
63 is $var, 42,
64 "assigning to non-rw subs shouldn't modify the original variable";
65 }
66
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
67 sub check ($passwd) { $passwd eq "fish"; };
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
68
69 sub checklastval ($passwd) is rw {
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
70 Proxy.new(
71 FETCH => sub ($self) { #OK not used
72 lastvar();
73 },
74 STORE => sub ($self, $val) { #OK not used
75 die "wrong password" unless check($passwd);
76 lastvar() = $val;
77 }
78 );
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
79 };
80
64ca263 @masak [S06-routine-modifiers/lvalue-subroutines.t] fudged for Rakudo
masak authored
81 #?rakudo skip 'segfaults, probably due to [RT #114134]'
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
82 dies_ok {checklastval("octopus") = 10 }, 'checklastval STORE can die';
83
84 # Above test may well die for the wrong reason, if the Proxy stuff didn't
85 # parse OK, it will complain that it couldn't find the desired subroutine
66b3e5c @moritz fix rakudo fudging of lvalue-subroutines.t
moritz authored
86 #?rakudo skip 'maximum recursion depth exceeded'
428e7b0 @sorear Fix(?) S06/lvalue-subroutines, fudge Proxy tests for niecza
sorear authored
87 is((try { checklastval("fish") = 12 }), 12, 'proxy lvalue subroutine STORE works');
66b3e5c @moritz fix rakudo fudging of lvalue-subroutines.t
moritz authored
88 #?rakudo emit #
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
89 my $resultval = checklastval("fish");
66b3e5c @moritz fix rakudo fudging of lvalue-subroutines.t
moritz authored
90 #?rakudo skip 'maximum recursion depth exceeded'
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
91 is($resultval, 12, 'proxy lvalue subroutine FETCH works');
92
93 my $realvar = "foo";
94 sub proxyvar ($prefix) is rw {
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
95 Proxy.new(
96 FETCH => method () { $prefix ~ lc($realvar) },
97 STORE => method ($val) { lc($realvar = $val) },
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
98 );
99 }
100 is proxyvar("PRE"), 'PREfoo', 'proxy lvalue subroutine FETCH works';
101 # Return value of assignments of Proxy objects is decided now.
102 # See thread "Assigning Proxy objects" on p6l,
103 # L<"http://www.nntp.perl.org/group/perl.perl6.language/21838">.
104 # Quoting Larry:
105 # The intention is that lvalue subs behave in all respects as if they
106 # were variables.  So consider what
107 #
108 # say $nonproxy = 40;
109 #
110 # should do.
62db446 @moritz try to bring some sanity to lvalue-subroutines.t
moritz authored
111 is (proxyvar("PRE") = "BAR"), 'PREbar',
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
112 'proxy lvalue subroutine STORE works and returns the correct value';
f25f151 [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
113 #?pugs todo 'feature'
114 is $realvar, 'BAR', 'variable was modified';
fd515aa [t] clean up lvalue_subroutines.t and move to spec/
moritz authored
115
116 # vim: ft=perl6
Something went wrong with that request. Please try again.