-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_functions.py
114 lines (93 loc) · 2.82 KB
/
test_functions.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import unittest
from os import path
import gcl
class TestStringInterpolation(unittest.TestCase):
def testStringInterpolation(self):
x = gcl.loads("""
things = { foo = 'FOO'; bar = 'BAR' };
y = fmt('Hey {foo}, are you calling me a {bar}?', things)
""")
self.assertEquals('Hey FOO, are you calling me a BAR?', x['y'])
def testLazyStringInterpolation(self):
x = gcl.loads("""
things = { foo = 'FOO'; boom; };
y = fmt('Hi {foo}', things)
""")
self.assertEquals('Hi FOO', x['y'])
def testSubInterpolation(self):
x = gcl.loads("""
things = { sub = { foo = 'FOO'; boom } };
y = fmt('Hi {sub.foo}', things)
""")
self.assertEquals('Hi FOO', x['y'])
def testImplicitScope(self):
x = gcl.loads("""
things = { foo = 'FOO'; boom };
y = fmt 'Hi {things.foo}'
""")
self.assertEquals('Hi FOO', x['y'])
def testJoin(self):
x = gcl.loads("""
things = join(['a', 'b', 'c']);
""")
self.assertEquals('a b c', x['things'])
def testComposeAll(self):
x = gcl.loads('''
empty = compose_all([]);
combined = compose_all([{ a = 'a' }, { b = 'b' }]);
''')
self.assertEquals([], list(x['empty'].keys()))
self.assertEquals(['a', 'b'], sorted(x['combined'].keys()))
def testSorted(self):
x = gcl.loads('''
tup = { a = 1; b = 2; c = 3; d = 4 };
keys = sorted([k for k in tup ])
''')
self.assertEquals(['a', 'b', 'c', 'd'], x['keys'])
def testSplit(self):
x = gcl.loads('''
list1 = split "one two three";
list2 = split("one/two/three", "/");
''')
self.assertEquals(['one', 'two', 'three'], x['list1'])
self.assertEquals(['one', 'two', 'three'], x['list2'])
def testHasKey(self):
x = gcl.loads('''
X = { one; two = 2; };
has_one = has(X, 'one');
has_two = has(X, 'two');
has_three = has(X, 'three');
''')
self.assertEquals(False, x['has_one'])
self.assertEquals(True, x['has_two'])
self.assertEquals(False, x['has_three'])
def testHasKeyCompound(self):
x = gcl.loads('''
X = { key };
Y = X { key = 3 };
x_key = has(X, 'key');
y_key = has(Y, 'key');
''')
self.assertEquals(False, x['x_key'])
self.assertEquals(True, x['y_key'])
def testFlatten(self):
x = gcl.loads('''
list = flatten [[1], [2], [], [3, 4, 5], [[6]]];
''')
self.assertEquals([1, 2, 3, 4, 5, [6]], x['list'])
def testFlattenNotAccidentallyOnStrings(self):
x = gcl.loads('''
list = flatten ["abc"]
''')
with self.assertRaises(gcl.EvaluationError):
print(x['list'])
def testFunctionErrorsAreTraced(self):
x = gcl.loads('''
list = flatten ["abc"]
''')
try:
print(x['list'])
self.fail('Should have thrown')
except Exception as e:
print(str(e))
self.assertTrue('flatten ["abc"]' in str(e))