Skip to content
Newer
Older
100644 74 lines (57 sloc) 1.76 KB
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
1 use v6;
5b8dbb5 @lizmat "use MONKEY_TYPING" -> "use MONKEY-TYPING"
lizmat authored Mar 13, 2015
2 use MONKEY-TYPING;
db4bde7 [t/spec] add 'use MONKEY_TYPING' where appropriate
lwall authored Mar 13, 2010
3
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
4 use Test;
a9d04f4 @pmurias Test that adverbs are illegal when augmenting a class and only :auth/…
pmurias authored Nov 19, 2015
5 plan 10;
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
6
7 # L<S12/Open vs Closed Classes>
8
9 class Something {
10 has $.attribute;
11 method in_Something { 'a' ~ $.attribute };
12 }
13
14 my $x = Something.new(attribute => 'b');
15
16 is $x.in_Something, 'ab', 'basic OO sanity';
17
18 # although we use curlies here to be better fudge-able, remeber
95ea915 [t/spec] remove rest of "is also" on classes
moritz authored May 10, 2009
19 # that 'augment' class extensions are *not* lexically scoped
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
20 {
95ea915 [t/spec] remove rest of "is also" on classes
moritz authored May 10, 2009
21 augment class Something {
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
22 method later_added {
23 'later'
24 }
25 method uses-other-methods {
26 'blubb|' ~ self.in_Something;
27
28 }
29 }
30
31 my $y = Something.new(attribute => 'c');
32 is $y.later_added, 'later', 'can call method that was later added';
33 is $y.uses-other-methods, 'blubb|ac', 'can call new method that calls other methods';
34
35 is $x.later_added, 'later', 'can call method on object that was instantiated earlier';
36 is $x.uses-other-methods, 'blubb|ab', 'works with other method too';
37 }
38
39 # now try to extend "core" types
4c37943 @felliott mark tests for RT #75114 (augment class (Str|Int))
felliott authored Jan 22, 2011
40 # RT #75114
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
41 {
95ea915 [t/spec] remove rest of "is also" on classes
moritz authored May 10, 2009
42 augment class Str {
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
43 method mydouble {
44 self.uc ~ self.lc;
45 }
46 }
47
48 is 'aBc'.mydouble, 'ABCabc', 'can extend Str';
49 }
50
4c37943 @felliott mark tests for RT #75114 (augment class (Str|Int))
felliott authored Jan 22, 2011
51 # RT #75114
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
52 {
95ea915 [t/spec] remove rest of "is also" on classes
moritz authored May 10, 2009
53 augment class Int {
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
54 method triple { self * 3 }
55 }
56 is 3.triple, 9, 'can extend Int';
57 }
58
59 {
95ea915 [t/spec] remove rest of "is also" on classes
moritz authored May 10, 2009
60 augment class Array {
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
61 method last-and-first {
62 self[self - 1] ~ self[0]
63 }
64 }
65
66 my @a = 1, 3, 7, 0;
67 is @a.last-and-first, '01', 'can extend class Array';
68 }
69
a9d04f4 @pmurias Test that adverbs are illegal when augmenting a class and only :auth/…
pmurias authored Nov 19, 2015
70 throws-like 'use MONKEY-TYPING; class RT124017_A {}; augment class RT124017_A:D {}', X::Syntax::Augment::Adverb, "can't pass :D when augmenting class";
71 throws-like 'use MONKEY-TYPING; class RT124017_B {}; augment class RT124017_B:auth<random_auth> {}', X::Syntax::Augment::Adverb, "can't pass :auth when augmenting class";
72
09f6fa3 [t/spec] add tests for 'is also' on classes
moritz authored Nov 23, 2008
73 # vim: ft=perl6
Something went wrong with that request. Please try again.