- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathgenerics.t
55 lines (47 loc) · 1.96 KB
/
generics.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
usev6.e.PREVIEW;
useTest;
usenqp;
plan1;
subtest "Nominalizable generic"=> {
plan6;
my \r =myroleR[::T] {
mypackageG {
classAisArray[T] {}
}
my T $v.=new;
has@.ais G::A;
methodt-nominalizables { T:D, T:U, T(), T:D() }
methoda-nominalizables { G::A:D, G::A:U, G::A(), G::A:D() }
methodvv { self.^name~":"~$v.raku }
}
myclassCIntdoes R[Int] { }
myclassCStrdoes R[Str] { }
is-deeply CInt.t-nominalizables, (Int:D, Int:U, Int(), Int:D()), "instantiated";
is-deeply CStr.t-nominalizables, (Str:D, Str:U, Str(), Str:D()), "instantiating over different type";
is CInt.a-nominalizables.map(*.^name).List,
("R::G::A[Int]:D", "R::G::A[Int]:U", "R::G::A[Int](Any)", "R::G::A[Int]:D(Any)"),
"generic class instantiates";
is CStr.a-nominalizables.map(*.^name).List,
("R::G::A[Str]:D", "R::G::A[Str]:U", "R::G::A[Str](Any)", "R::G::A[Str]:D(Any)"),
"generic class instantiates over different type";
subtest-assign(Mu \type, @good-data, @bad-data) is test-assertion {
subtest "Typecheck for "~ type.^name, {
plan5;
my$obj;
lives-ok
{ $obj= type.new(a =>@good-data) },
"positional attribute can be initialized with correct value types";
is-deeply$obj.a.List, @good-data.List, "initialization is successfull";
my@good-reversed:=@good-data.reverse.List;
lives-ok { $obj.a =@good-reversed }, "can assign with correct value types";
is-deeply$obj.a.List, @good-reversed, "assignment is successfull";
throws-like
{ type.new(a =>@bad-data); },
X::TypeCheck::Assignment,
"wrong value types result in exception";
}
}
test-assign CInt, (1,10,20,42), <A B C>;
test-assign CStr, <A B C D>, (13, 666);
}
# vim: expandtab shiftwidth=4 ft=raku