-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_gcl.py
829 lines (681 loc) · 21.1 KB
/
test_gcl.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
import unittest
from os import path
import gcl
from gcl import exceptions
import traceback
def parse(s, env=None, implicit_tuple=False, loader=None):
return (gcl.reads(s, implicit_tuple=implicit_tuple, loader=loader)
.eval(gcl.default_env.extend(env)))
def parse_tuple(s, env=None, loader=None):
return parse(s, env, implicit_tuple=True, loader=loader)
class TestBasics(unittest.TestCase):
def testInteger(self):
self.assertEquals(3, parse('3'))
def testNegativeInteger(self):
self.assertEquals(-3, parse('-3'))
def testFloat(self):
self.assertEquals(3.14, parse('3.14'))
self.assertEquals(0.14, parse('.14'))
def testSingleQuotedString(self):
self.assertEquals("foo", parse("'foo'"))
def testDoubleQuotedString(self):
self.assertEquals("foo", parse('"foo"'))
def testEscapedString(self):
self.assertEquals("Master \"Bob\" Foo", parse('"Master \\"Bob\\" Foo"'))
def testStringWithNewlines(self):
self.assertEquals("Beep\nBoop", parse('"Beep\\nBoop"'))
def testStringWithCR(self):
self.assertEquals("Beep\rBoop", parse('"Beep\\rBoop"'))
def testStringWithTab(self):
self.assertEquals("Beep\tBoop", parse('"Beep\\tBoop"'))
def testNull(self):
self.assertEquals(None, parse('null'))
def testBool(self):
self.assertEquals(True, parse('true'))
self.assertEquals(False, parse('false'))
def testComments(self):
self.assertEquals(3, parse("""
# comment
3
"""))
def testSingleCharacterComment(self):
self.assertEquals(3, parse("#\n3"))
def testLeaveTabsAlone(self):
obj = parse('{ x = "\t" }')
self.assertEquals('\t', obj['x'])
def testComments2(self):
self.assertEquals(3, parse("""
3
# comment"""))
def testImplicitTupleBasic(self):
self.assertEquals([('foo', 3)], parse('foo=3;', implicit_tuple=True).items())
def testImplicitTupleEmpty(self):
self.assertEquals([], parse('', implicit_tuple=True).items())
def testIdentifierWithDash(self):
x = parse('hello-world', env={'hello-world': 1})
self.assertEquals(1, x)
def testIdentifierWithColon(self):
x = parse('hello:world', env={'hello:world': 1})
self.assertEquals(1, x)
class TestList(unittest.TestCase):
def testEmptyList(self):
self.assertEquals([], parse('[]'))
def testSingletonList(self):
self.assertEquals([1], parse('[1]'))
def testTrailingComma(self):
self.assertEquals([1], parse('[1, ]'))
def testPair(self):
self.assertEquals([1, 2], parse('[1, 2]'))
def testNestedList(self):
self.assertEquals([1, [2]], parse('[1, [2]]'))
class TestVariable(unittest.TestCase):
def testVariableWithEnv(self):
self.assertEquals('bar', parse('foo', env={'foo': 'bar'}))
class TestTuple(unittest.TestCase):
def testEmptyTuple(self):
self.assertEquals([], parse('{}').items())
def testBoundIdentifiers(self):
self.assertEquals([('foo', 'bar')], parse('{ foo = "bar" }').items())
def testUnboundIdentifiers(self):
t = parse('{ foo; }')
try:
print(t['foo'])
self.fail('Should have thrown')
except exceptions.EvaluationError:
pass # Expected
def testIndirectUnbound(self):
t = parse('{ foo; bar = foo + 3; }')
try:
print(t['bar'])
self.fail('Should have thrown')
except exceptions.EvaluationError:
pass # Expected
def testVariableInSameScope(self):
t = parse('{ foo = 3; bar = foo; }')
self.assertEquals(3, t['bar'])
def testVariableFromParentScope(self):
t = parse('{ foo = global; bar = foo; }', { 'global': 3 })
self.assertEquals(3, t['bar'])
def testShadowGlobalScope(self):
t = parse('{ foo = 3; bar = foo; }', { 'foo': 2 })
self.assertEquals(3, t['bar'])
def testDereferencing(self):
t = parse("""{
obj = {
attr = 1;
};
one = obj.attr;
}""");
self.assertEquals(1, t['one'])
def testDereferencingFromEnvironment(self):
x = parse('obj.attr', env={ 'obj': { 'attr' : 1 }})
self.assertEquals(1, x)
def testDoubleDeclarationIsError(self):
with self.assertRaises(gcl.ParseError):
parse('{ x = 3; x = 4 }')
def testCanHaveColonAtTheEndWhenQuoting(self):
obj = parse_tuple('`what:` = 3')
self.assertEquals('what:', list(obj.keys())[0])
def testCanHaveSubtractionInAnIdentifier(self):
obj = parse_tuple('he110-1orld = 3')
self.assertEquals(3, obj['he110-1orld'])
<<<<<<< HEAD
def testSelfReference(self):
obj = parse_tuple('''
x = 1;
y = 2;
z = [k for k in self];
''')
self.assertSetEqual(set(['x', 'y', 'z']), set(obj['z']))
def testSelfReferenceInCompositeTuple(self):
obj = parse_tuple('''
x = {
param;
z = [k for k in self];
bla = self.param;
};
y = x {
w = [k for k in self];
param = 'something';
};
''')
self.assertSetEqual(set(['param', 'z', 'bla']), set(obj['x']['z']))
self.assertSetEqual(set(['param', 'z', 'bla', 'w']), set(obj['y']['w']))
self.assertEquals('something', obj['y']['bla'])
def testTupleHasValues(self):
obj = parse_tuple('x = { y = 1 }')
print(obj.values())
def testCompositeTupleHasValues(self):
obj = parse_tuple('x = { z = 2 } { y = 1 }')
print(obj.values())
class TestDocComments(unittest.TestCase):
def testParseDocComment(self):
obj = parse_tuple('''
#. This is a doc comment
member = 'value';
''')
self.assertEquals(['This is a doc comment'], obj._ast_node.members[0].comment.lines)
def testParseMultiLineDocComment(self):
obj = parse_tuple('''
#. This is a doc comment
#.
#. Containing multiple lines
member = 'value';
''')
self.assertEquals(['This is a doc comment', '', 'Containing multiple lines'], obj._ast_node.members[0].comment.lines)
self.assertEquals('This is a doc comment', obj._ast_node.members[0].comment.title())
self.assertEquals(['Containing multiple lines'], obj._ast_node.members[0].comment.body_lines())
def testTextAndTags(self):
obj = parse_tuple('''
#. Comment
#. @tag
member = 'value';
''')
self.assertEquals('', obj._ast_node.members[0].comment.tag('tag'))
self.assertEquals(None, obj._ast_node.members[0].comment.tag('other_tag'))
def testOnlyTag(self):
obj = parse_tuple('''
#. @tag
member = 'value';
''')
self.assertEquals('', obj._ast_node.members[0].comment.tag('tag'))
self.assertEquals('', obj._ast_node.members[0].comment.title())
class TestApplication(unittest.TestCase):
def setUp(self):
self.env = {}
self.env['inc'] = lambda x: x + 1
self.env['foo'] = lambda: lambda: 3
self.env['add'] = lambda x, y: x + y
self.env['curry_add'] = lambda x: lambda y: x + y
self.env['mk_obj'] = lambda x: { 'attr': x }
def testFunctionApplication(self):
self.assertEquals(3, parse('inc(2)', env=self.env))
def testFunctionApplicationMultiArgs(self):
self.assertEquals(5, parse('add(2, 3)', env=self.env))
def testRepeatedFunctionCalls(self):
self.assertEquals(3, parse('foo()()', env=self.env))
def testNestedFunctionCalls(self):
self.assertEquals(4, parse('inc(inc(2))', env=self.env))
def testFunctionApplicationWithoutParens(self):
self.assertEquals(3, parse('inc 2', env=self.env))
def testChainedApplicationWithoutParens(self):
self.assertEquals(5, parse('curry_add 2 3', env=self.env))
def testTupleComposition(self):
t = parse('{ foo = 1 } { bar = 2 }')
self.assertEquals([
('bar', 2),
('foo', 1),
], sorted(t.items()))
def testIndirectTupleComposition(self):
t = parse("""
{
base = {
name;
hello = 'hello ' + name;
};
mine = base { name = 'Johnny' }
}
""")
self.assertEquals('hello Johnny', t['mine']['hello'])
def testIndirectTupleCompositionWithDefault(self):
t = parse("""
{
base = {
name = 'Barry';
hello = 'hello ' + name;
};
mine = base { name = 'Johnny' }
}
""")
self.assertEquals('hello Johnny', t['mine']['hello'])
def testBaseDerefenceInRightTuple(self):
# We also need this to EXTEND default tuples
x = parse_tuple("""
left = { a = 3 };
right = left { a = base.a + 1 }
""")
self.assertEquals(4, x['right']['a'])
def testBaseDereferenceInDoubleComposite(self):
x = parse("""
{ one = 1 } { two = 2 } { too = base.two; wan = base.one }
""")
self.assertEquals(1, x['wan'])
self.assertEquals(2, x['too'])
def testBaseWithReferencesToVoid(self):
x = parse_tuple("""
one = {
param;
foo = 'something ' + param;
};
two = {
param = 'yay';
foo = base.foo + ' world';
};
three = one two;
""")
self.assertEquals('something yay world', x['three']['foo']);
def testBaseWithReferencesToValue(self):
x = parse_tuple("""
one = {
param = 'dontcare';
foo = 'something ' + param;
};
two = {
param = 'yay';
foo = base.foo + ' world';
};
three = one two;
""")
self.assertEquals('something yay world', x['three']['foo']);
def testDereferencingFromFunctionCall1(self):
self.assertEquals(10, parse('mk_obj(10).attr', env=self.env))
def testDereferencingFromFunctionCall2(self):
self.assertEquals(10, parse('(mk_obj 10).attr', env=self.env))
def testTupleApplicationAndDerefPrecedence(self):
x = parse_tuple("""
lib = {
parent = { x = 1 }
};
composed = lib.parent { y = 2; }
""")
self.assertEquals(1, x['composed']['x'])
self.assertEquals(2, x['composed']['y'])
def testTripleApplication(self):
t = parse("""
{ x = 1 } { y = 2 } { z = 3 }
""")
self.assertEquals(1, t['x'])
self.assertEquals(2, t['y'])
self.assertEquals(3, t['z'])
def testApplyingTupleToString(self):
# Applying the tuple to a string should index the tuple
x = parse_tuple("""
tuple = { x = 3 };
y = tuple 'x';
z = tuple('x')
""")
self.assertEquals(3, x['y'])
self.assertEquals(3, x['z'])
def testApplyingVariableToString(self):
x = parse_tuple("""
tuple = { x = 3 };
var = 'x';
y = tuple var;
""")
self.assertEquals(3, x['y'])
def applyIntegerToList(self):
x = parse_tuple("""
list = [ 'a', 'b', 'c' ];
y = list(0);
z = list 1;
""")
self.assertEquals('a', x['y'])
self.assertEquals('b', x['z'])
def testCompositionWithDict(self):
env = {}
env['fn'] = lambda arg : {'b': arg}
x = parse_tuple('''
x = { a = 'a' };
y = x (fn 'b');
''', env=gcl.Environment(env))
self.assertEquals('b', x['y']['b'])
class TestExpressions(unittest.TestCase):
def testAdd(self):
self.assertEquals(5, parse('2 + 3'))
def testAddStrings(self):
self.assertEquals('foobar', parse('"foo" + "bar"'))
def testMul(self):
self.assertEquals(6, parse('2 * 3'))
def testPrecedence(self):
self.assertEqual(10, parse('2 * 3 + 4'))
def testNot(self):
self.assertEqual(False, parse('not true'))
def testLogicalOpsAndPrecedence(self):
self.assertEquals(True, parse('0 < 5 and 2 <= 2'))
def testConditional(self):
self.assertEquals(1, parse('if 0 < 5 then 1 else 2'))
self.assertEquals(2, parse('if 5 < 0 then 1 else 2'))
def testListComprehension(self):
self.assertEquals([2, 4], parse('[x * 2 for x in [1, 2]]'))
def testListComprehensionWithIf(self):
self.assertEquals([4], parse('[x * 2 for x in [1, 2] if x % 2 == 0]'))
def testListComprehensionWithFunctionCall(self):
self.assertEquals([2, 4, 6], parse('[double x for x in [1, 2] + [3]]', {
'double': lambda x: x * 2
}))
def testListComprehensionOverTuple(self):
x = parse('''{
x = { a = 1; b = 2; c = 3 };
y = sum([x(k) for k in x ]);
}''')
self.assertEquals(6, x['y'])
def testAndIsLazy(self):
x = gcl.loads('''
x = false and y;
''')
self.assertEquals(False, x['x'])
def testInOperatorPos(self):
self.assertEquals(True, parse('"a" in ["a", "b"]'))
def testInOperatorNeg(self):
self.assertEquals(False, parse('"c" in ["a", "b"]'))
class TestScoping(unittest.TestCase):
def testOuterValueAvailableInInnerOne(self):
t = parse_tuple("""
x = 3;
y = {
z = x;
};
""")
self.assertEquals(3, t['y']['z'])
def testCompositedScopeNotAvailable(self):
# Why is this? Well, it makes sense if there's also an x in the outer
# scope. See next test.
t = parse_tuple("""
base = {
x = 3;
};
y = base {
z = x;
}
""")
self.assertRaises(exceptions.EvaluationError, lambda: t['y']['z'])
def testVariableCantBeOverridenByContentsOfTuple(self):
t = parse_tuple("""
x = 1;
base = {
x = 2;
};
y = base {
z = x;
}
""")
# z should be 1, not 2 (even though we composite it with base, which also
# has an x)
# FIXME: To implement this: composition of tuples is not a single tuple,
# but a construct that behaves like a tuple but has 2 lookups.
self.assertEquals(1, t['y']['z'])
def testCompositedScopeAvailableIfDeclared(self):
t = parse_tuple("""
base = {
x = 3;
};
y = base {
x;
z = x;
}
""")
self.assertEquals(3, t['y']['z'])
def testCompositedValueInSubExpression(self):
t = parse_tuple("""
base = {
x = 3;
};
y = base {
x; # <-- only if I put this here
sub = {
z = x;
}
}
""")
self.assertEquals(3, t['y']['sub']['z']);
def testTwoLocalreferences(self):
t = parse_tuple("""
base = {
x = 3;
w = x
};
y = base {
x = 4;
z = x;
}
""")
self.assertEquals(3, t['base']['w'])
self.assertEquals(4, t['y']['z'])
def testLocalValueOverridesOuterOne(self):
t = parse_tuple("""
x = 3;
y = {
x = 2;
z = x;
}
""")
self.assertEquals(2, t['y']['z'])
def testLocalValueOverridesOuterOne_EvenIfUnbound(self):
t = parse_tuple("""
x = 3;
y = {
x;
z = x;
}
""")
try:
t['y']['z']
self.fail('Should have thrown')
except exceptions.EvaluationError as e:
pass # Expected
def testRelativeImportWithDeclaration(self):
t = parse("""
{
x = 1;
y = x;
}
{
y;
x = 2;
z = y;
}
""")
# At this point, what should be the value of z?
# To be locally analyzable, the value should be 1.
# But to allow default values for 'declared tuples', which is
# also desirable, the answer is the right declaration of 'x'
# should override the left one, and the answer should be 2.
self.assertEquals(2, t['z'])
def testInheriting(self):
# We need inheriting because we can't write
# z = { x = x }
# (That would lead to infinite recursion)
t = parse_tuple("""
x = 1;
y = 2;
z = { inherit x y; w = 1 }
""")
self.assertEquals(1, t['z']['x'])
self.assertEquals(2, t['z']['y'])
def testInheritingAndCompositingLeft(self):
x = parse_tuple("""
parent = {
foo = 1;
child = {
inherit foo;
}
};
composed = parent { };
""")
self.assertEquals(1, x['composed']['child']['foo']);
def testInheritingAndCompositingRight(self):
x = parse_tuple("""
parent = {
foo;
added = foo + 1;
};
foo = 1;
composed = parent { inherit foo };
""")
self.assertEquals(1, x['composed']['foo']);
def testDoubleInheriting(self):
x = parse_tuple("""
upper = {
foo;
sub = {
inherit foo;
}
};
final = upper { foo = 3; sub; lower = sub };
""")
self.assertEquals(3, x['final']['lower']['foo']);
def testDoubleScopeAndInheriting(self):
x = parse_tuple("""
first = {};
scope = {
outer_thing = 'something';
second = {
copy = outer_thing;
};
};
foo = first scope.second { }
""")
self.assertEquals('something', x['foo']['copy'])
def testScopeGoodError(self):
x = parse_tuple("""
one = { foo = 3 };
two = { };
final = one two { };
""")
try:
self.assertEquals(3, x['two']['moo'])
except exceptions.EvaluationError as e:
self.assertTrue('Unknown' in str(e))
def testDoubleApplicationAndResolutionGivesGoodError(self):
x = parse_tuple("""
one = { sub = { bar = foo }};
two = { foo = boo };
final = one two { sub; bla = sub.bar };
""")
try:
self.assertEquals(3, x['final']['bla'])
except exceptions.EvaluationError as e:
self.assertTrue('Unbound' in str(e))
def testRecursionDetection(self):
x = parse_tuple("""
x = { z = y.z };
y = { z = x.z };
""")
try:
print(x['y']['z'])
self.fail('Should have thrown')
except exceptions.EvaluationError as e:
pass
class TestPrecedence(unittest.TestCase):
def testMinus(self):
x = gcl.reads('3 - 2', implicit_tuple=False)
self.assertEquals(1, parse('3 - 2'))
def testMinusWithVariable(self):
# This basically says: arithmetic binds stronger than juxtaposition
obj = gcl.loads('''
x = 3;
y = x - 2;
''')
self.assertEquals(1, obj['y'])
def testMinusWithMoreOperations(self):
obj = gcl.loads('''
x = -6;
y = x / -2;
''')
self.assertEquals(3, obj['y'])
def testFunctionToNegativeNumber(self):
obj = gcl.loads('''
y = x(-2);
''', env={'x': lambda x: x + 3})
self.assertEquals(1, obj['y'])
def testUnaryOperator(self):
obj = gcl.loads('''
y = -x;
''', env={'x': -1})
self.assertEquals(1, obj['y'])
def testComparisonBindsWeakerThanJuxtaposition(self):
obj = gcl.loads('''
X = { a = 3 };
y = 1 < X 'a';
''')
self.assertEquals(True, obj['y'])
def testLogicalBindsWeakerThanJuxtaposition(self):
obj = gcl.loads('''
X = { a = true };
y = true or X 'a';
''')
self.assertEquals(True, obj['y'])
class TestStandardLibrary(unittest.TestCase):
def testPathJoin(self):
self.assertEquals('a/b/c', parse("path_join('a', 'b', 'c')"))
class TestIncludes(unittest.TestCase):
def parse(self, fname, s):
return gcl.loads(s, filename=fname, loader=self.loader,
env=gcl.Environment({'thing': 'yes'}))
def loader(self, base, rel, env=None):
target_path = gcl.find_relative(path.dirname(base), rel)
return gcl.loads('loaded_from = %r; z = thing' % target_path, env=env)
def testRelativeInclude(self):
t = self.parse('/home/me/file', 'inc = include "other_file"')
self.assertEquals('/home/me/other_file', t['inc']['loaded_from'])
self.assertEquals('yes', t['inc']['z'])
def testEnvPropagation(self):
t = self.parse('/home/me/file', 'thing = "no"; z = "no"; inc = include "other_file"')
self.assertEquals('/home/me/other_file', t['inc']['loaded_from'])
self.assertEquals('no', t['thing'])
self.assertEquals('no', t['z'])
self.assertEquals('yes', t['inc']['z'])
def testRelativeIncludeUp(self):
t = self.parse('/home/me/file', 'inc = include "../other_file"')
self.assertEquals('/home/other_file', t['inc']['loaded_from'])
def testAbsoluteInclude(self):
t = self.parse('/home/me/file', 'inc = include "/other_file"')
self.assertEquals('/other_file', t['inc']['loaded_from'])
def testIncludeWithApplyPrecedence(self):
x = self.parse('/home/me/file', 'inc = include "other_file.gcl" { foo = 3 };')
self.assertEquals(3, x['inc']['foo']);
class TestErrorMessages(unittest.TestCase):
def testKeyExceptionContainsInfo(self):
x = parse('{ x = { boo = "bah" }}')
try:
print(x['foo'])
self.fail('Should have thrown')
except Exception as e:
self.assertTrue('boo' in str(e))
def testKeyExceptionContainsInfoForCompositeTuple(self):
x = parse('{ x = { boo = "bah" } { bie = "bye" } }')
try:
print(x['foo'])
self.fail('Should have thrown')
except Exception as e:
self.assertTrue('boo' in str(e))
def testKeyExceptionContainsInfoForBaseReferenceInCompositeTuple(self):
x = parse('{ x = { boo = "bah" } { foo = base.bye } }')
try:
print(x['foo'])
self.fail('Should have thrown')
except Exception as e:
self.assertTrue('boo' in str(e))
def testNameOfVoidVariableIsMentioned(self):
x = parse('{ the_var; }')
try:
print(x['the_var'])
self.fail('Should have thrown')
except Exception as e:
print(str(e))
self.assertTrue('\'the_var\'' in str(e))
def testParseErrorIndicatesCorrectLocation(self):
try:
x = parse("""
bar = {
+ woop;
};
""".strip(), implicit_tuple=True)
self.fail('Should have thrown')
except gcl.ParseError as e:
print(e.sourcelocation.line)
self.assertTrue('woop' in e.sourcelocation.line)
class TestRuntimeCaps(unittest.TestCase):
def testComposeTupleWithDict(self):
x = parse_tuple("""
single;
double = single + single;
""")
self.assertEquals('toottoot', x.compose({ 'single': 'toot' })['double'])
def testDoubleComposeTupleWithDict(self):
x = parse_tuple("""
single;
double = single + single;
""")
self.assertEquals('wootwoot',
x.compose({ 'single': 'toot' }).compose({'single': 'woot'})['double'])