Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (85 sloc) 2.698 kb
297e783 [t] and [t/spec]
moritz authored
1 use v6;
2
3 use Test;
4
9b8425a @timo tests for native attribute BUILDPLAN.
timo authored
5 plan 18;
297e783 [t] and [t/spec]
moritz authored
6
7 # L<S12/"Construction and Initialization">
8
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
9 class OwnConstr {
297e783 [t] and [t/spec]
moritz authored
10 has $.x = 13;
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
11 my $in_own = 0;
12 method own() {
297e783 [t] and [t/spec]
moritz authored
13 $in_own++;
ae4af99 [t/spec] Spec change means bless always needs a candidate now; update co...
jnthn authored
14 return self.bless(self.CREATE(), :x(42));
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
15 }
16 method in_own {
17 $in_own;
297e783 [t] and [t/spec]
moritz authored
18 }
19 }
20 ok OwnConstr.new ~~ OwnConstr, "basic class instantiation";
21 is OwnConstr.new.x, 13, "basic attribute access";
22 # As usual, is instead of todo_is to suppress unexpected succeedings
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
23 is OwnConstr.in_own, 0, "own constructor was not called";
f70e129 [t/spec] Test for RT #64116
kyle authored
24
297e783 [t] and [t/spec]
moritz authored
25 ok OwnConstr.own ~~ OwnConstr, "own construction instantiated its class";
26 is OwnConstr.own.x, 42, "attribute was set from our constructor";
14975bd @jnthn Correct a test and re-fudge construction.t (only one we lost is related ...
jnthn authored
27 is OwnConstr.in_own, 2, "own constructor was actually called";
297e783 [t] and [t/spec]
moritz authored
28
29
30 # L<"http://www.mail-archive.com/perl6-language@perl.org/msg20241.html">
31 # provide constructor for single positional argument
32
33 class Foo {
34 has $.a;
35
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
36 method new ($self: Str $string) {
d2a9e62 @moritz remove * from bless
moritz authored
37 $self.bless(a => $string);
297e783 [t] and [t/spec]
moritz authored
38 }
39 }
40
41
42 ok Foo.new("a string") ~~ Foo, '... our Foo instance was created';
43
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
44 is Foo.new("a string").a, 'a string', "our own 'new' was called";
297e783 [t] and [t/spec]
moritz authored
45
46
47 # Using ".=" to create an object
48 {
49 class Bar { has $.attr }
50 my Bar $bar .= new(:attr(42));
51 is $bar.attr, 42, "instantiating an object using .= worked (1)";
52 }
53 # Using ".=()" to create an object
54 {
55 class Fooz { has $.x }
0634426 [t/spec] fix small error in construction.t
moritz authored
56 my Fooz $f .= new(:x(1));
297e783 [t] and [t/spec]
moritz authored
57 is $f.x, 1, "instantiating an object using .=() worked";
58 }
59
60 {
61 class Baz { has @.x is rw }
62 my Baz $foo .= new(:x(1,2,3));
63 lives_ok -> { $foo.x[0] = 3 }, "Array initialized in auto-constructor is not unwritable...";
64 is $foo.x[0], 3, "... and keeps its value properly."
65 }
f70e129 [t/spec] Test for RT #64116
kyle authored
66
67 # RT #64116
63cff50 @colomon Fudge for niecza.
colomon authored
68 #?niecza skip 'System.NullReferenceException: Object reference not set to an instance of an object'
f70e129 [t/spec] Test for RT #64116
kyle authored
69 {
70 class RT64116 { has %.env is rw };
71
ea4d235 [t/spec] begin cleansing of eval and todos that should skip
kyle authored
72 my $a = RT64116.CREATE;
f70e129 [t/spec] Test for RT #64116
kyle authored
73
6620326 @lizmat Fix %h = { a => 1 } confusion
lizmat authored
74 lives_ok { $a.env = foo => "bar" }, 'assign to attr of .CREATEd class';
f70e129 [t/spec] Test for RT #64116
kyle authored
75 is $a.env<foo>, 'bar', 'assignment works';
76 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
77
166331d [t/spec] test for RT #76476, monkey-typed method new is not called
moritz authored
78 # RT #76476
79 {
5b8dbb5 @lizmat "use MONKEY_TYPING" -> "use MONKEY-TYPING"
lizmat authored
80 use MONKEY-TYPING;
166331d [t/spec] test for RT #76476, monkey-typed method new is not called
moritz authored
81 class MonkeyNew { has $.x is rw };
82 augment class MonkeyNew {
83 method new() {
d2a9e62 @moritz remove * from bless
moritz authored
84 self.bless(:x('called'));
166331d [t/spec] test for RT #76476, monkey-typed method new is not called
moritz authored
85 }
86 };
87 is MonkeyNew.new().x, 'called', 'monkey-typed .new() method is called';
88 }
89
9c3354e @colomon Fudge for Niecza.
colomon authored
90 #?niecza skip "Malformed has (int NYI, I think)"
9b8425a @timo tests for native attribute BUILDPLAN.
timo authored
91 {
92 class NativeInt {
93 has int $.attr;
94 }
95 lives_ok -> { NativeInt.new(:attr(123)) }, ".new with a native int attribute";
96 class NativeNum {
97 has num $.attr;
98 }
99 lives_ok -> { NativeNum.new(:attr(0e0)) }, ".new with a native num attribute";
100 class NativeStr {
101 has str $.attr;
102 }
103 lives_ok -> { NativeStr.new(:attr<foo>) }, ".new with a native str attribute";
104 }
105
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
106 # vim: ft=perl6
Something went wrong with that request. Please try again.