1 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 r"""Parses section-based test.
12 lines: list of \n-terminated strings.
15 List of string pairs (field name, field content) in order. Field content is
16 concatenation of \n-terminated lines, so it's either empty or ends with \n.
23 if line.startswith(' '):
24 assert current_field is not None, line
25 field_data[current_field].append(line[2:])
27 match = re.match('@(\S+):$', line)
29 raise Exception('Bad line: %r' % line)
30 current_field = match.group(1)
31 assert current_field not in field_data, current_field
32 field_data[current_field] = []
33 fields.append(current_field)
35 return [(field, ''.join(field_data[field])) for field in fields]
38 def SplitLines(lines, separator_regex):
39 """Split sequence of lines into sequence of list of lines.
42 lines: sequence of strings.
43 separator_regex: separator regex.
46 Nonempty sequence of (possibly empty) lists of strings. Separator lines
51 if re.match(separator_regex, line):
59 def LoadTestFile(filename):
60 r"""Loads and parses .test file.
66 List of tests (see ParseTest).
68 with open(filename) as file_in:
69 return map(ParseTest, SplitLines(file_in, r'-{3,}\s*$'))
72 def UnparseTest(items_list):
73 """Convert test to sequence of \n-terminated strings
76 items_list: list of string pairs (see ParseTest).
79 Sequence of \n-terminated strings.
81 for field, content in items_list:
82 yield '@%s:\n' % field
86 assert content.endswith('\n')
87 content = content[:-1]
89 for line in content.split('\n'):
93 def SaveTestFile(tests, filename):
97 tests: list of tests (see ParseTest).
102 with open(filename, 'w') as file_out:
106 file_out.write('-' * 70 + '\n')
108 for line in UnparseTest(test):