OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
3 # for details. All rights reserved. Use of this source code is governed by a | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 """Tests for emitter module.""" | |
7 | |
8 import logging.config | |
9 import unittest | |
10 import emitter | |
11 | |
12 | |
13 class EmitterTestCase(unittest.TestCase): | |
14 | |
15 def setUp(self): | |
16 pass | |
17 | |
18 def tearDown(self): | |
19 pass | |
20 | |
21 def check(self, e, expected): | |
22 self.assertEquals(''.join(e.Fragments()), expected) | |
23 | |
24 def testExample(self): | |
25 e = emitter.Emitter() | |
26 body = e.Emit('$TYPE $NAME() {\n' | |
27 ' $!BODY\n' | |
28 '}\n', | |
29 TYPE='int', NAME='foo') | |
30 body.Emit('return $VALUE;', VALUE='100') | |
31 self.check(e, | |
32 'int foo() {\n' | |
33 ' return 100;\n' | |
34 '}\n') | |
35 | |
36 def testTemplateErrorDuplicate(self): | |
37 try: | |
38 e = emitter.Emitter() | |
39 b = e.Emit('$(A)$(!B)$(A)$(!B)') # $(!B) is duplicated | |
40 except RuntimeError, ex: | |
41 return | |
42 raise AssertionError('Expected error') | |
43 | |
44 def testTemplate1(self): | |
45 e = emitter.Emitter() | |
46 e.Emit('-$A+$B-$A+$B-', A='1', B='2') | |
47 self.check(e, '-1+2-1+2-') | |
48 | |
49 def testTemplate2(self): | |
50 e = emitter.Emitter() | |
51 r = e.Emit('1$(A)2$(B)3$(A)4$(B)5', A='x', B='y') | |
52 self.assertEquals(None, r) | |
53 self.check(e, '1x2y3x4y5') | |
54 | |
55 def testTemplate3(self): | |
56 e = emitter.Emitter() | |
57 b = e.Emit('1$(A)2$(!B)3$(A)4$(B)5', A='x') | |
58 b.Emit('y') | |
59 self.check(e, '1x2y3x4y5') | |
60 self.check(b, 'y') | |
61 | |
62 def testTemplate4(self): | |
63 e = emitter.Emitter() | |
64 (a, b) = e.Emit('$!A$!B$A$B') # pair of holes. | |
65 a.Emit('x') | |
66 b.Emit('y') | |
67 self.check(e, 'xyxy') | |
68 | |
69 def testMissing(self): | |
70 # Behaviour of Undefined parameters depends on form. | |
71 e = emitter.Emitter(); | |
72 e.Emit('$A $?B $(C) $(?D)') | |
73 self.check(e, '$A $(C) ') | |
74 | |
75 def testHoleScopes(self): | |
76 e = emitter.Emitter() | |
77 # Holes have scope. They remember the bindings of the template application | |
78 # in which they are created. Create two holes which inherit bindings for C | |
79 # and D. | |
80 (a, b) = e.Emit('[$!A][$!B]$C$D$E', C='1', D='2') | |
81 e.Emit(' $A$B$C$D') # Bindings are local to the Emit | |
82 self.check(e, '[][]12$E $A$B$C$D') | |
83 | |
84 # Holes are not bound within holes. That would too easily lead to infinite | |
85 # expansions. | |
86 a.Emit('$A$C$D') # $A12 | |
87 b.Emit('$D$C$B') # 21$B | |
88 self.check(e, '[$A12][21$B]12$E $A$B$C$D') | |
89 # EmitRaw avoids interpolation. | |
90 a.EmitRaw('$C$D') | |
91 b.EmitRaw('$D$C') | |
92 self.check(e, '[$A12$C$D][21$B$D$C]12$E $A$B$C$D') | |
93 | |
94 def testFormat(self): | |
95 self.assertEquals(emitter.Format('$A$B', A=1, B=2), '12') | |
96 | |
97 if __name__ == '__main__': | |
98 logging.config.fileConfig('logging.conf') | |
99 if __name__ == '__main__': | |
100 unittest.main() | |
OLD | NEW |