diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2019-11-03 22:56:52 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2019-11-03 22:56:52 (GMT) |
commit | 459b345d69532825f21bdcd3e4f92009b0a046dc (patch) | |
tree | e0bc3d9089f0b5452e77be0f5d37fc0522f78c4a /tests/plugins | |
parent | 33b5dc9af0404eabeb0e60245ab1ca1dc3713a17 (diff) |
Handled sequences with the Yaml reader in an improved way.
Diffstat (limited to 'tests/plugins')
-rw-r--r-- | tests/plugins/yamlrdr.py | 174 |
1 files changed, 138 insertions, 36 deletions
diff --git a/tests/plugins/yamlrdr.py b/tests/plugins/yamlrdr.py index 7f65624..103d6e8 100644 --- a/tests/plugins/yamlrdr.py +++ b/tests/plugins/yamlrdr.py @@ -16,26 +16,54 @@ class TestYamlReader(ChrysalideTestCase): super(TestYamlReader, cls).setUpClass() - cls._simple = tempfile.NamedTemporaryFile() - - cls.simple_data = b''' -meta: - id: java_class - endian: be - file-extension: class - xref: - justsolve: Java - pronom: x-fmt/415 - 'wiki"data': Q2193155 - license: CC0-1.0 + cls._simple_map = tempfile.NamedTemporaryFile() + + cls._simple_map_data = b''' +a: av +b: bv +c: cv + +''' + + cls._simple_seq = tempfile.NamedTemporaryFile() + + cls._simple_seq_data = b''' +- a: av +- b: bv +- c: cv ''' - cls._simple.write(cls.simple_data) + cls._nested = tempfile.NamedTemporaryFile() + + cls._nested_data = b''' +root: + a: v0 + b: v1 + c: v2 + sub: + aa: v00 + bb: v01 + cc: v02 + - i: w + - j: x + - k: c + d: v3 - cls._simple.flush() +''' - cls.log('Using temporary file "%s"' % cls._simple.name) + tmp = [ + [ cls._simple_map, cls._simple_map_data ], + [ cls._simple_seq, cls._simple_seq_data ], + [ cls._nested, cls._nested_data ] + ] + + for f, d in tmp: + + f.write(d) + f.flush() + + cls.log('Using temporary file "%s"' % f.name) @classmethod @@ -43,52 +71,126 @@ meta: super(TestYamlReader, cls).tearDownClass() - cls.log('Delete file "%s"' % cls._simple.name) + tmp = [ + cls._simple_map, + cls._simple_seq, + cls._nested, + ] - cls._simple.close() + for f in tmp: + cls.log('Delete file "%s"' % f.name) - def testSimpleYamlContent(self): - """Validate simple Yaml content reader.""" + f.close() - reader = YamlReader.new_from_path(self._simple.name) - self.assertIsNotNone(reader) + def testSimpleYamlContent(self): + """Validate Yaml content readers.""" def _build_node_desc(node, left): line = node.yaml_line - desc = left + line.key + ':' + (' ' + line.value if line.value else '') + '\n' + if line: + prefix = '- ' if line.is_list_item else '' + desc = left + prefix + line.key + ':' + (' ' + line.value if line.value else '') + '\n' + indent = ' ' + else: + desc = '' + indent = '' - for child in node.children: - desc += _build_node_desc(child, left + ' ') + if node.collection: + for child in node.collection.nodes: + desc += _build_node_desc(child, left + indent) return desc - fulldesc = '' + reader = YamlReader.new_from_path(self._simple_map.name) + self.assertIsNotNone(reader) + self.assertIsNotNone(reader.tree) + + fulldesc = _build_node_desc(reader.tree.root, '') + + self.assertEqual('\n' + fulldesc + '\n', self._simple_map_data.decode('ascii')) + + reader = YamlReader.new_from_path(self._simple_seq.name) + self.assertIsNotNone(reader) + self.assertIsNotNone(reader.tree) + + fulldesc = _build_node_desc(reader.tree.root, '') + + self.assertEqual('\n' + fulldesc + '\n', self._simple_seq_data.decode('ascii')) + + reader = YamlReader.new_from_path(self._nested.name) + self.assertIsNotNone(reader) + self.assertIsNotNone(reader.tree) - for rn in reader.tree.root_nodes: - fulldesc += _build_node_desc(rn, '') + fulldesc = _build_node_desc(reader.tree.root, '') - self.assertEqual('\n' + fulldesc + '\n', self.simple_data.decode('ascii')) + self.assertEqual('\n' + fulldesc + '\n', self._nested_data.decode('ascii')) def testSimpleYamlContentFinder(self): - """Validate simple Yaml content search.""" + """Validate Yaml nested content search.""" - reader = YamlReader.new_from_path(self._simple.name) + reader = YamlReader.new_from_path(self._nested.name) self.assertIsNotNone(reader) - found = reader.tree.find_node_by_path('/meta/xref') + found = reader.tree.find_by_path('/root/sub') + + self.assertEqual(len(found), 1) + + if len(found) == 1: + self.assertEqual(found[0].yaml_line.key, 'sub') + + found = reader.tree.find_by_path('/root/sub/') + + self.assertEqual(len(found), 3) + + found = reader.tree.find_by_path('/root/sub/xx') + + self.assertEqual(len(found), 0) + + found = reader.tree.find_by_path('/root/sub/cc/i') + + self.assertEqual(len(found), 1) + + if len(found) == 1: + self.assertEqual(found[0].yaml_line.key, 'i') + self.assertEqual(found[0].yaml_line.is_list_item, True) + + found = reader.tree.find_by_path('/root/sub/cc') + + self.assertEqual(len(found), 1) + + if len(found) == 1: + + root = found[0] + + found = root.find_by_path('cc/i') + + self.assertEqual(len(found), 1) + + if len(found) == 1: + + self.assertEqual(found[0].yaml_line.key, 'i') + self.assertEqual(found[0].yaml_line.is_list_item, True) + + found = root.find_by_path('/cc/i') + + self.assertEqual(len(found), 1) + + if len(found) == 1: - self.assertEqual(found.yaml_line.key, 'xref') + self.assertEqual(found[0].yaml_line.key, 'i') + self.assertEqual(found[0].yaml_line.is_list_item, True) - found = reader.tree.find_node_by_path('/meta/xref/') + found = root.find_by_path('//i') - self.assertEqual(found.yaml_line.key, 'xref') + self.assertEqual(len(found), 1) - found = reader.tree.find_node_by_path('/meta/xref/aa') + if len(found) == 1: - self.assertIsNone(found) + self.assertEqual(found[0].yaml_line.key, 'i') + self.assertEqual(found[0].yaml_line.is_list_item, True) |