Skip to content
Newer
Older
100644 128 lines (101 sloc) 3.27 KB
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
1 use v6;
2
3 use Test;
4
5 plan 17;
6
7 # L<S02/Names/"Which class am I in?">
8
9 class Foo {
10 method get_self_normal() { self }
11 method get_class_normal() { $?CLASS }
12 method get_package_normal() { $?PACKAGE }
13
14 method get_class_pvar() { ::?CLASS }
15 method get_package_pvar() { ::?PACKAGE }
16
17 method dummy() { 42 }
18 }
19
20 role Bar {
21 method get_self_normal() { self }
22 method get_class_normal() { $?CLASS }
23 method get_role_normal() { $?ROLE }
24 method get_package_normal() { $?PACKAGE }
25
26 method get_class_pvar() { ::?CLASS }
27 method get_role_pvar() { ::?ROLE }
28 method get_package_pvar() { ::?PACKAGE }
29
30 method dummy() { 42 }
31 }
32
33 class SimpleClass does Bar {}
34
35 {
36 my $foo_obj = Foo.new;
37 my $class = $foo_obj.get_class_normal;
38 my $package = $foo_obj.get_package_normal;
3e278d8 @jnthn Corrections and updates.
jnthn authored Jan 15, 2012
39 is( $package.gist, Foo.gist, '$?PACKAGE should be the package name' );
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
40
41 ok( $class ~~ Foo, 'the thing returned by $?CLASS in our class smartmatches against our class' );
42 my $forty_two;
43 lives_ok { my $obj = $class.new; $forty_two = $obj.dummy },
44 'the class returned by $?CLASS in our class was really our class (1)';
45 is $forty_two, 42, 'the class returned by $?CLASS in our class way really our class (2)';
46 }
47
48 {
49 my $foo1 = Foo.new;
50 my $foo2 = $foo1.get_self_normal;
51
52 ok $foo1 === $foo2, 'self in classes works';
53 }
54
55 {
56 my $bar = SimpleClass.new;
57 my $class = $bar.get_class_normal;
58 my $package = $bar.get_package_normal;
59
3e278d8 @jnthn Corrections and updates.
jnthn authored Jan 15, 2012
60 is( $package.gist, Bar.gist, '$?PACKAGE should be the role package name - it is not generic like $?CLASS');
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
61
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored Jun 19, 2009
62 #?pugs todo 'bug'
3e278d8 @jnthn Corrections and updates.
jnthn authored Jan 15, 2012
63 ok $class ~~ ::SimpleClass, 'the thing returned by $?CLASS in our role smartmatches against our class';
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
64 my $forty_two;
65 lives_ok { my $obj = $class.new; $forty_two = $obj.dummy },
66 'the class returned by $?CLASS in our role way really our class (1)';
67 is $forty_two, 42, 'the class returned by $?CLASS in our role way really our class (2)';
68 }
69
70 {
71 my $bar1 = SimpleClass.new;
72 my $bar2 = $bar1.get_self_normal;
73
74 ok $bar1 === $bar2, 'self in roles works';
75 }
76
77 {
78 my $bar = SimpleClass.new;
79 my $role = $bar.get_role_normal;
80
3e278d8 @jnthn Corrections and updates.
jnthn authored Jan 15, 2012
81 ok $role ~~ Bar, 'the returned by $?ROLE smartmatches against our role';
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
82 }
83
84 # Now the same with type vars
85 #?pugs todo 'oo'
86 {
87 ok Foo.new.get_class_pvar === ::Foo,
88 "::?CLASS in classes works";
89 ok SimpleClass.new.get_class_pvar === ::SimpleClass,
90 "::?CLASS in roles works";
91 ok SimpleClass.new.get_role_pvar === ::Bar,
92 "::?ROLE in roles works";
93 }
94
95 # Per L<"http://www.nntp.perl.org/group/perl.perl6.language/23541">:
96 # On Sat, Oct 15, 2005 at 07:39:36PM +0300, wolverian wrote:
97 # : On Sat, Oct 15, 2005 at 08:25:15AM -0700, Larry Wall wrote:
98 # : > [snip]
99 # : >
100 # : > Of course, there's never been any controversy here about what to call
101 # : > "self", oh no... :-)
102 # :
103 # : IMHO just call it "self" (by default) and be done with it. :)
104 #
105 # Let it be so.
106 #
107 # Larry
108 {
109 class Grtz {
110 method get_self1 { self }
111 method get_self2 ($self:) { $self }
112 method foo { 42 }
113 method run_foo { self.foo }
114 }
115 my $grtz = Grtz.new;
116
117 ok $grtz.get_self1 === $grtz.get_self2,
118 'self is an alias for $self (1)';
119 is $grtz.run_foo, 42,
120 'self is an alias for $self (2)';
121 }
122
123 {
124 eval_dies_ok 'self' , "there is no self outside of a method";
125 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored Aug 8, 2009
126
127 # vim: ft=perl6
Something went wrong with that request. Please try again.