Flutter Engine
The Flutter Engine
emitter_test.py
Go to the documentation of this file.
1#!/usr/bin/env python3
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"""Tests for emitter module."""
6
7import logging.config
8import unittest
9import emitter
10
11
12class EmitterTestCase(unittest.TestCase):
13
14 def setUp(self):
15 pass
16
17 def tearDown(self):
18 pass
19
20 def check(self, e, expected):
21 self.assertEqual(''.join(e.Fragments()), expected)
22
23 def testExample(self):
24 e = emitter.Emitter()
25 body = e.Emit('$TYPE $NAME() {\n'
26 ' $!BODY\n'
27 '}\n',
28 TYPE='int',
29 NAME='foo')
30 body.Emit('return $VALUE;', VALUE='100')
31 self.check(e, 'int foo() {\n' ' return 100;\n' '}\n')
32
34 try:
35 e = emitter.Emitter()
36 b = e.Emit('$(A)$(!B)$(A)$(!B)') # $(!B) is duplicated
37 except RuntimeError as ex:
38 return
39 raise AssertionError('Expected error')
40
41 def testTemplate1(self):
42 e = emitter.Emitter()
43 e.Emit('-$A+$B-$A+$B-', A='1', B='2')
44 self.check(e, '-1+2-1+2-')
45
46 def testTemplate2(self):
47 e = emitter.Emitter()
48 r = e.Emit('1$(A)2$(B)3$(A)4$(B)5', A='x', B='y')
49 self.assertEqual(None, r)
50 self.check(e, '1x2y3x4y5')
51
52 def testTemplate3(self):
53 e = emitter.Emitter()
54 b = e.Emit('1$(A)2$(!B)3$(A)4$(B)5', A='x')
55 b.Emit('y')
56 self.check(e, '1x2y3x4y5')
57 self.check(b, 'y')
58
59 def testTemplate4(self):
60 e = emitter.Emitter()
61 (a, b) = e.Emit('$!A$!B$A$B') # pair of holes.
62 a.Emit('x')
63 b.Emit('y')
64 self.check(e, 'xyxy')
65
66 def testMissing(self):
67 # Behaviour of Undefined parameters depends on form.
68 e = emitter.Emitter()
69 e.Emit('$A $?B $(C) $(?D)')
70 self.check(e, '$A $(C) ')
71
72 def testHoleScopes(self):
73 e = emitter.Emitter()
74 # Holes have scope. They remember the bindings of the template application
75 # in which they are created. Create two holes which inherit bindings for C
76 # and D.
77 (a, b) = e.Emit('[$!A][$!B]$C$D$E', C='1', D='2')
78 e.Emit(' $A$B$C$D') # Bindings are local to the Emit
79 self.check(e, '[][]12$E $A$B$C$D')
80
81 # Holes are not bound within holes. That would too easily lead to infinite
82 # expansions.
83 a.Emit('$A$C$D') # $A12
84 b.Emit('$D$C$B') # 21$B
85 self.check(e, '[$A12][21$B]12$E $A$B$C$D')
86 # EmitRaw avoids interpolation.
87 a.EmitRaw('$C$D')
88 b.EmitRaw('$D$C')
89 self.check(e, '[$A12$C$D][21$B$D$C]12$E $A$B$C$D')
90
92 e = emitter.Emitter()
93 e.Emit('-$#A(-)-', A=True)
94 self.check(e, '---')
95
97 e = emitter.Emitter()
98 e.Emit('-$#A( $#B(-$C-) )-', A=True, B=True, C='1')
99 self.check(e, '- -1- -')
100
102 e = emitter.Emitter()
103 e.Emit('-$#A( $B )-', A=False, B='1')
104 self.check(e, '--')
105
107 e = emitter.Emitter()
108 e.Emit('-$#A( $#B(-$C-) )-', A=False, B=True, C='1')
109 self.check(e, '--')
110
112 e = emitter.Emitter()
113 e.Emit('-$#A( $#B(-$C-) )-', A=True, B=False, C='1')
114 self.check(e, '- -')
115
117 e = emitter.Emitter()
118 e.Emit('-$#A(-$#B(-$C-)-)-', C='1')
119 self.check(e, '-$#A(-$#B(-1-)-)-')
120
122 e = emitter.Emitter()
123 e.Emit('-$#A(-$B-', A=True, B='1')
124 self.check(e, '-$#A(-1-')
125
127 try:
128 e = emitter.Emitter()
129 e.Emit('$#A(-)', A='Invalid')
130 e.Fragments()
131 except RuntimeError as ex:
132 return
133 raise AssertionError('Expected error')
134
135 def testFormat(self):
136 self.assertEqual(emitter.Format('$A$B', A=1, B=2), '12')
137
138
139if __name__ == '__main__':
140 logging.config.fileConfig('logging.conf')
141 if __name__ == '__main__':
142 unittest.main()
def check(self, e, expected)
Definition: emitter_test.py:20
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741