forked from jbeder/yaml-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-emitter-tests.py
223 lines (196 loc) · 7.17 KB
/
create-emitter-tests.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
import sys
import yaml
import hashlib
DEFINE = 'YAML_GEN_TESTS'
EVENT_COUNT = 5
def encode_stream(line):
for c in line:
if c == '\n':
yield '\\n'
elif c == '"':
yield '\\"'
elif c == '\t':
yield '\\t'
elif ord(c) < 0x20:
yield '\\x' + hex(ord(c))
else:
yield c
def encode(line):
return ''.join(encode_stream(line))
def doc_start(implicit=False):
if implicit:
return {'emit': '', 'handle': 'OnDocumentStart(_)'}
else:
return {'emit': 'BeginDoc', 'handle': 'OnDocumentStart(_)'}
def doc_end(implicit=False):
if implicit:
return {'emit': '', 'handle': 'OnDocumentEnd()'}
else:
return {'emit': 'EndDoc', 'handle': 'OnDocumentEnd()'}
def scalar(value, tag='', anchor='', anchor_id=0):
emit = []
handle = []
if tag:
emit += ['VerbatimTag("%s")' % encode(tag)]
if anchor:
emit += ['Anchor("%s")' % encode(anchor)]
handle += ['OnAnchor(_, "%s")' % encode(anchor)]
if tag:
out_tag = encode(tag)
else:
if value == encode(value):
out_tag = '?'
else:
out_tag = '!'
emit += ['"%s"' % encode(value)]
handle += ['OnScalar(_, "%s", %s, "%s")' % (out_tag, anchor_id, encode(value))]
return {'emit': emit, 'handle': handle}
def comment(value):
return {'emit': 'Comment("%s")' % value, 'handle': ''}
def seq_start(tag='', anchor='', anchor_id=0, style='_'):
emit = []
handle = []
if tag:
emit += ['VerbatimTag("%s")' % encode(tag)]
if anchor:
emit += ['Anchor("%s")' % encode(anchor)]
handle += ['OnAnchor(_, "%s")' % encode(anchor)]
if tag:
out_tag = encode(tag)
else:
out_tag = '?'
emit += ['BeginSeq']
handle += ['OnSequenceStart(_, "%s", %s, %s)' % (out_tag, anchor_id, style)]
return {'emit': emit, 'handle': handle}
def seq_end():
return {'emit': 'EndSeq', 'handle': 'OnSequenceEnd()'}
def map_start(tag='', anchor='', anchor_id=0, style='_'):
emit = []
handle = []
if tag:
emit += ['VerbatimTag("%s")' % encode(tag)]
if anchor:
emit += ['Anchor("%s")' % encode(anchor)]
handle += ['OnAnchor(_, "%s")' % encode(anchor)]
if tag:
out_tag = encode(tag)
else:
out_tag = '?'
emit += ['BeginMap']
handle += ['OnMapStart(_, "%s", %s, %s)' % (out_tag, anchor_id, style)]
return {'emit': emit, 'handle': handle}
def map_end():
return {'emit': 'EndMap', 'handle': 'OnMapEnd()'}
def gen_templates():
yield [[doc_start(), doc_start(True)],
[scalar('foo'), scalar('foo\n'), scalar('foo', 'tag'), scalar('foo', '', 'anchor', 1)],
[doc_end(), doc_end(True)]]
yield [[doc_start(), doc_start(True)],
[seq_start()],
[[], [scalar('foo')], [scalar('foo', 'tag')], [scalar('foo', '', 'anchor', 1)], [scalar('foo', 'tag', 'anchor', 1)], [scalar('foo'), scalar('bar')], [scalar('foo', 'tag', 'anchor', 1), scalar('bar', 'tag', 'other', 2)]],
[seq_end()],
[doc_end(), doc_end(True)]]
yield [[doc_start(), doc_start(True)],
[map_start()],
[[], [scalar('foo'), scalar('bar')], [scalar('foo', 'tag', 'anchor', 1), scalar('bar', 'tag', 'other', 2)]],
[map_end()],
[doc_end(), doc_end(True)]]
yield [[doc_start(True)],
[map_start()],
[[scalar('foo')], [seq_start(), scalar('foo'), seq_end()], [map_start(), scalar('foo'), scalar('bar'), map_end()]],
[[scalar('foo')], [seq_start(), scalar('foo'), seq_end()], [map_start(), scalar('foo'), scalar('bar'), map_end()]],
[map_end()],
[doc_end(True)]]
yield [[doc_start(True)],
[seq_start()],
[[scalar('foo')], [seq_start(), scalar('foo'), seq_end()], [map_start(), scalar('foo'), scalar('bar'), map_end()]],
[[scalar('foo')], [seq_start(), scalar('foo'), seq_end()], [map_start(), scalar('foo'), scalar('bar'), map_end()]],
[seq_end()],
[doc_end(True)]]
def expand(template):
if len(template) == 0:
pass
elif len(template) == 1:
for item in template[0]:
if isinstance(item, list):
yield item
else:
yield [item]
else:
for car in expand(template[:1]):
for cdr in expand(template[1:]):
yield car + cdr
def gen_events():
for template in gen_templates():
for events in expand(template):
base = list(events)
for i in range(0, len(base)+1):
cpy = list(base)
cpy.insert(i, comment('comment'))
yield cpy
def gen_tests():
for events in gen_events():
name = 'test' + hashlib.sha1(''.join(yaml.dump(event) for event in events)).hexdigest()[:20]
yield {'name': name, 'events': events}
class Writer(object):
def __init__(self, out):
self.out = out
self.indent = 0
def writeln(self, s):
self.out.write('%s%s\n' % (' ' * self.indent, s))
class Scope(object):
def __init__(self, writer, name, indent):
self.writer = writer
self.name = name
self.indent = indent
def __enter__(self):
self.writer.writeln('%s {' % self.name)
self.writer.indent += self.indent
def __exit__(self, type, value, traceback):
self.writer.indent -= self.indent
self.writer.writeln('}')
def create_emitter_tests(out):
out = Writer(out)
includes = [
'handler_test.h',
'yaml-cpp/yaml.h',
'gmock/gmock.h',
'gtest/gtest.h',
]
for include in includes:
out.writeln('#include "%s"' % include)
out.writeln('')
usings = [
'::testing::_',
]
for using in usings:
out.writeln('using %s;' % using)
out.writeln('')
with Scope(out, 'namespace YAML', 0) as _:
with Scope(out, 'namespace', 0) as _:
out.writeln('')
out.writeln('typedef HandlerTest GenEmitterTest;')
out.writeln('')
tests = list(gen_tests())
for test in tests:
with Scope(out, 'TEST_F(%s, %s)' % ('GenEmitterTest', test['name']), 2) as _:
out.writeln('Emitter out;')
for event in test['events']:
emit = event['emit']
if isinstance(emit, list):
for e in emit:
out.writeln('out << %s;' % e)
elif emit:
out.writeln('out << %s;' % emit)
out.writeln('')
for event in test['events']:
handle = event['handle']
if isinstance(handle, list):
for e in handle:
out.writeln('EXPECT_CALL(handler, %s);' % e)
elif handle:
out.writeln('EXPECT_CALL(handler, %s);' % handle)
out.writeln('Parse(out.c_str());')
out.writeln('')
if __name__ == '__main__':
create_emitter_tests(sys.stdout)