-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestFunction.sc
71 lines (57 loc) · 1.61 KB
/
TestFunction.sc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
TestFunction : UnitTest {
test_function_scope {
var generator, list, result, a;
generator = {
var x = 0;
{ |y| a = x = x + y }
};
list = 10.collect(generator);
result = list.collect { |each, i| each.value(i) };
this.assert(result == (0..9), "lexical function scope should be independent");
this.assert(a == 9, "outer lexical function scope should recieve side-effect");
}
test_classmethod_scope {
var obj, func;
obj = CommonTestClass;
obj.x = 5;
func = obj.returnLexicalScopeGetterFunc;
this.assert(func.value == [5, 7], "function scope should be able to access class variables");
func = obj.returnLexicalScopeSetterFunc;
func.value(42);
this.assert(obj.x == 42, "function should be able to set class variable");
this.assert(obj.y == 42, "function should be able to set class variable that has no setter");
}
test_instancemethod_scope {
var obj, func;
obj = CommonTestClass.new;
obj.a = 5;
func = obj.returnLexicalScopeGetterFunc;
this.assert(func.value == [5, 3], "function scope should be able to access instance variables");
func = obj.returnLexicalScopeSetterFunc;
func.value(42);
this.assert(obj.a == 42, "function should be able to set instance variable");
this.assert(obj.b == 42, "function should be able to set instance variable that has no setter");
}
}
CommonTestClass {
var <>a, <b = 3;
classvar <>x, <y = 7;
*returnLexicalScopeGetterFunc {
^{ [x, y] }
}
*returnLexicalScopeSetterFunc {
^{ |val|
x = val;
y = val;
}
}
returnLexicalScopeGetterFunc {
^{ [a, b] }
}
returnLexicalScopeSetterFunc {
^{ |val|
a = val;
b = val;
}
}
}