Debian Codegraph @

terps/tads/tads3/test/data/setmethod.t

raw
1 #include <tads.h>
2 #include <dynfunc.h>
3
4 obj1: object
5 name = 'obj1'
6
7 ctxTests()
8 {
9 local i = 1;
10
11 obj3.setMethod(
12 &a, {x: "shared ctx: x=<<x>>, self=<<self.name>>,
13 defobj=<<definingobj.name>>\n" });
14 obj3.setMethod(
15 &b, new method(x) { "unshared ctx: x=<<x>>, self=<<self.name>>,
16 defobj=<<definingobj.name>>\n"; });
17
18 obj3.setMethod(
19 &c, new method(x) { "unshared ctx 2: x=<<x>>, i=<<i++>>,
20 self=<<self.name>>, defobj=<<definingobj.name>>\n"; });
21
22 obj3.a(11);
23 obj3.b(12);
24 obj3.c(13);
25 obj4.a(14);
26 obj4.b(15);
27 obj4.c(16);
28 obj5.a(17);
29 obj5.b(18);
30 obj5.c(19);
31 }
32 ;
33
34 obj2: object
35 name = 'obj2'
36 d(n) { "This is <<name>>.d - n=<<n>>\n"; }
37 e = "This is e"
38 location = obj1
39 ;
40
41 obj3: object
42 name = 'obj3'
43 location = obj2
44 ;
45
46 obj4: obj3
47 name = 'obj4'
48 ;
49
50 obj5: obj4
51 name = 'obj5'
52 ;
53
54
55 property a, b, c, d, e;
56
57 method m_topLoc()
58 {
59 return location == nil ? self : location;
60 }
61 method m_getLoc() { return location; }
62
63 fopt(a, b, c = 0)
64 {
65 return a + b + c;
66 }
67
68 testConcat([args])
69 {
70 local ret = '';
71 for (local s in args)
72 ret += s;
73 return ret;
74 }
75
76 main(args)
77 {
78 local i = 100;
79
80 obj1.setMethod(&a, { x : "This is a - x=<<x>>, i=<<i>>\n" });
81 obj1.setMethod(&b, { y : "This is b - y=<<y>>\n" });
82 obj1.setMethod(&c, 'This is c\n');
83 obj1.setMethod(&d, obj2.getMethod(&d));
84 obj1.setMethod(&e, testConcat('This is e: ', makeString('Test... ', 5),
85 '\n'));
86
87 obj1.a(200);
88 obj1.b(300);
89 obj1.c;
90 obj1.d(400);
91 obj1.e;
92 "\b";
93
94 local s = obj2.getMethod(&e);
95 "obj2.s = [<<s>>], dataType(s) = <<dataType(s)>>\n";
96
97 obj3.setMethod(&a, obj1.getMethod(&a));
98 obj3.setMethod(&b, obj1.getMethod(&b));
99 obj3.setMethod(&c, obj1.getMethod(&c));
100 obj3.setMethod(&d, obj1.getMethod(&d));
101
102 obj3.a(2000);
103 obj3.b(3000);
104 obj3.c;
105 obj3.d(4000);
106 "\b";
107
108 foreach (local x in [obj1, obj2, obj3])
109 x.setMethod(&topLoc, m_topLoc), x.setMethod(&getLoc, m_getLoc);
110 "obj1.topLoc=<<obj1.topLoc.name>>\n";
111 "obj2.getLoc=<<obj2.getLoc.name>>, topLoc=<<obj2.topLoc.name>>\n";
112 "obj3.getLoc=<<obj3.getLoc.name>>, topLoc=<<obj3.topLoc.name>>\n";
113 "\b";
114
115 obj1.setMethod(&a, fopt);
116 obj1.setMethod(&b, { a, b?, c? : a+b+c });
117 obj1.setMethod(&c, { a?, ... : a+i });
118 obj1.setMethod(
119 &d, Compiler.compile('function(a, b, c?) { return a; }'));
120
121 "fopt <<descIfc(getFuncParams(fopt))>>\n";
122 "obj1.a <<descMethod(obj1, &a)>>\n";
123 "obj1.b <<descMethod(obj1, &b)>>\n";
124 "obj1.c <<descMethod(obj1, &c)>>\n";
125 "obj1.d <<descMethod(obj1, &d)>>\n";
126 "\b";
127
128 obj1.setMethod(&a, &t3GetNamedArg);
129 obj1.setMethod(&b, &inputDialog);
130 obj1.setMethod(&c, &systemInfo);
131 obj1.setMethod(&d, &rexSearch);
132 "obj1.a <<descMethod(obj1, &a)>>\n";
133 "obj1.b <<descMethod(obj1, &b)>>\n";
134 "obj1.c <<descMethod(obj1, &c)>>\n";
135 "obj1.d <<descMethod(obj1, &d)>>\n";
136 "obj1.d call = <<obj1.d('<alpha>+', '123 test 456', 1)[3]>>\n";
137 "\b";
138
139 obj1.ctxTests();
140 }
141
142 descIfc(ifc)
143 {
144 "interface = (minArgc=<<ifc[1]>>, optArgc=<<ifc[2]>>, varargs=<<
145 ifc[3] ? 'yes' : 'no'>>)";
146 }
147
148 descMethod(obj, prop)
149 {
150 descIfc(obj.getPropParams(prop));
151 }
152