summaryrefslogtreecommitdiff
path: root/tests/plugins
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-04-27 21:27:52 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-04-27 21:27:52 (GMT)
commit25bac01127581767639a5bd9024c41eb803388fa (patch)
treea67cd5cdecbf001e788935e756fc722b7c552b9b /tests/plugins
parenta1ff7646ce6f829dd192e2e2246def2ea583e93b (diff)
Replace the old YAML parser by another one relying on the external libyaml library.
Diffstat (limited to 'tests/plugins')
-rw-r--r--tests/plugins/yaml.py148
-rw-r--r--tests/plugins/yamlrdr.py261
2 files changed, 148 insertions, 261 deletions
diff --git a/tests/plugins/yaml.py b/tests/plugins/yaml.py
new file mode 100644
index 0000000..2fbb0bd
--- /dev/null
+++ b/tests/plugins/yaml.py
@@ -0,0 +1,148 @@
+#!/usr/bin/python3-dbg
+# -*- coding: utf-8 -*-
+
+
+from chrysacase import ChrysalideTestCase
+from pychrysalide.plugins import yaml
+
+
+class TestYamlSupport(ChrysalideTestCase):
+ """TestCase for the YAML support."""
+
+
+ def testParseSimpleYamlContent(self):
+ """Parse basic YAML content."""
+
+ definitions = '''
+a: av
+b: bv
+c: cv
+'''
+
+ root = yaml.parse_from_text(definitions)
+
+ self.assertFalse(root.is_sequence)
+
+ self.assertEqual(root.nodes[0].key, 'a')
+ self.assertEqual(root.nodes[1].key, 'b')
+ self.assertEqual(root.nodes[2].key, 'c')
+
+ self.assertEqual(root.nodes[0].value, 'av')
+ self.assertEqual(root.nodes[1].value, 'bv')
+ self.assertEqual(root.nodes[2].value, 'cv')
+
+ definitions = '''
+- a: av
+- b: bv
+- c: cv
+'''
+
+ root = yaml.parse_from_text(definitions)
+
+ self.assertTrue(root.is_sequence)
+
+ self.assertEqual(root.nodes[0].nodes[0].key, 'a')
+ self.assertEqual(root.nodes[1].nodes[0].key, 'b')
+ self.assertEqual(root.nodes[2].nodes[0].key, 'c')
+
+ self.assertEqual(root.nodes[0].nodes[0].value, 'av')
+ self.assertEqual(root.nodes[1].nodes[0].value, 'bv')
+ self.assertEqual(root.nodes[2].nodes[0].value, 'cv')
+
+
+ def testSearchYamlNodes(self):
+ """Search YAML nodes related to paths."""
+
+ definitions = '''
+root:
+ a: v0
+ b: v1
+ c: v2
+ sub:
+ aa: v00
+ bb: v01
+ cc:
+ - i: w
+ - j: x
+ - k: c
+ d: v3
+'''
+
+ root = yaml.parse_from_text(definitions)
+
+ found = root.find_first_by_path('/root/a')
+
+ self.assertEqual(found.value, 'v0')
+
+ found = root.find_first_by_path('/root/sub')
+
+ self.assertEqual(found.value, None)
+
+ found = root.find_first_by_path('/root/sub/cc')
+
+ self.assertEqual(found.value, None)
+
+ found = root.find_first_by_path('/root/sub/cc/j')
+
+ self.assertEqual(found.value, 'x')
+
+ found = root.find_first_by_path('/root/d')
+
+ self.assertEqual(found.value, 'v3')
+
+
+ definitions = '''
+root:
+ - a: av
+ aa: aav
+ ab: abv
+ - b: bv
+ ba: bav
+ bb: bbv
+'''
+
+ root = yaml.parse_from_text(definitions)
+
+ found = root.find_first_by_path('/root/ba')
+
+ self.assertEqual(found.value, 'bav')
+
+ found = root.find_first_by_path('/root/b')
+
+ self.assertEqual(found.value, 'bv')
+
+ found = root.find_first_by_path('/root/')
+
+ self.assertTrue(found.is_sequence)
+ self.assertFalse(found.nodes[0].is_sequence)
+ self.assertEqual(found.nodes[0].nodes[0].value, 'av')
+
+
+ def testComplexYamlContent(self):
+ """Process more complex YAML content."""
+
+ definitions = '''
+root:
+ a: 'v0'
+ b: 'v1 ? 1 : 2'
+ c: v2 # final comment
+ d: "'xx::xx'"
+'''
+
+ root = yaml.parse_from_text(definitions)
+
+ found = root.find_first_by_path('/root/a')
+
+ self.assertEqual(found.value, 'v0')
+
+ found = root.find_first_by_path('/root/b')
+
+ self.assertEqual(found.value, 'v1 ? 1 : 2')
+
+ found = root.find_first_by_path('/root/c')
+
+ self.assertEqual(found.value, 'v2')
+
+ found = root.find_first_by_path('/root/d')
+
+ self.assertEqual(found.value, "'xx::xx'")
diff --git a/tests/plugins/yamlrdr.py b/tests/plugins/yamlrdr.py
deleted file mode 100644
index dbd9651..0000000
--- a/tests/plugins/yamlrdr.py
+++ /dev/null
@@ -1,261 +0,0 @@
-#!/usr/bin/python3-dbg
-# -*- coding: utf-8 -*-
-
-
-from chrysacase import ChrysalideTestCase
-from pychrysalide.plugins.yaml import YamlReader
-
-
-class TestYamlReader(ChrysalideTestCase):
- """TestCase for the Yaml reader."""
-
-
- @classmethod
- def setUpClass(cls):
-
- super(TestYamlReader, cls).setUpClass()
-
- cls._simple_map_data = '''
-a: av
-b: bv
-c: cv
-
-'''
-
- cls._simple_seq_data = '''
-- a: av
-- b: bv
-- c: cv
-
-'''
-
- cls._nested_data = '''
-root:
- a: v0
- b: v1
- c: v2
- sub:
- aa: v00
- bb: v01
- cc: v02
- - i: w
- - j: x
- - k: c
- d: v3
-
-'''
-
- cls._mixed_data = '''
-root:
- - a: av
- aa: aav
- ab: abv
- - b: bv
- ba: bav
- bb: bbv
-
-'''
-
-
- def testSimpleYamlContent(self):
- """Validate Yaml content readers."""
-
- def _build_node_desc(node, left, extra = ''):
-
- if hasattr(node, 'key'):
-
- line = node.yaml_line
-
- prefix = '- ' if line.is_list_item else extra
- desc = left + prefix + line.key + ':' + (' ' + line.value if line.value else '') + '\n'
- indent = ' '
-
- collec = node.collection
-
- else:
-
- desc = ''
- indent = ''
-
- if hasattr(node, 'nodes'):
- collec = node
-
- if collec:
-
- if collec.is_sequence:
- extra = ' '
-
- for child in collec.nodes:
- desc += _build_node_desc(child, left + indent, extra)
-
- return desc
-
-
- reader = YamlReader(text=self._simple_map_data)
- self.assertIsNotNone(reader)
- self.assertIsNotNone(reader.tree)
-
- fulldesc = _build_node_desc(reader.tree.root, '')
-
- self.assertEqual('\n' + fulldesc + '\n', self._simple_map_data)
-
- reader = YamlReader(text=self._simple_seq_data)
- self.assertIsNotNone(reader)
- self.assertIsNotNone(reader.tree)
-
- fulldesc = _build_node_desc(reader.tree.root, '')
-
- self.assertEqual('\n' + fulldesc + '\n', self._simple_seq_data)
-
- reader = YamlReader(text=self._nested_data)
- self.assertIsNotNone(reader)
- self.assertIsNotNone(reader.tree)
-
- fulldesc = _build_node_desc(reader.tree.root, '')
-
- self.assertEqual('\n' + fulldesc + '\n', self._nested_data)
-
- reader = YamlReader(text=self._mixed_data)
- self.assertIsNotNone(reader)
- self.assertIsNotNone(reader.tree)
-
- fulldesc = _build_node_desc(reader.tree.root, '')
-
- self.assertEqual('\n' + fulldesc + '\n', self._mixed_data)
-
-
- def testSimpleYamlContentFinder(self):
- """Validate Yaml nested content search."""
-
- reader = YamlReader(text=self._nested_data)
- self.assertIsNotNone(reader)
-
- found = reader.tree.find_by_path('/root/sub')
-
- self.assertEqual(len(found), 1)
-
- if len(found) == 1:
- self.assertEqual(found[0].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].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].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[0].key, 'i')
- self.assertEqual(found[0].yaml_line.is_list_item, True)
-
- found = root.find_by_path('//i')
-
- self.assertEqual(len(found), 1)
-
- if len(found) == 1:
-
- self.assertEqual(found[0].key, 'i')
- self.assertEqual(found[0].yaml_line.is_list_item, True)
-
-
- def testMixedYamlContentFinder(self):
- """Validate Yaml mixed content search."""
-
- reader = YamlReader(text=self._mixed_data)
- self.assertIsNotNone(reader)
-
- found = reader.tree.find_by_path('/root')
-
- self.assertEqual(len(found), 1)
-
- if len(found) == 1:
- self.assertEqual(found[0].key, 'root')
-
- found = reader.tree.find_by_path('/root/', True)
-
- self.assertEqual(len(found), 1)
-
- found = reader.tree.find_one_by_path('/root/', True)
-
- self.assertIsNotNone(found)
-
- if found:
-
- sub = found.find_one_by_path('/a')
- self.assertIsNotNone(sub)
- self.assertEqual(sub.key, 'a')
-
- sub = found.find_one_by_path('/aa')
- self.assertIsNotNone(sub)
- self.assertEqual(sub.key, 'aa')
-
- found = reader.tree.find_by_path('/root/')
-
- self.assertEqual(len(found), 2)
-
- if len(found) == 2:
-
- sub = found[0].find_one_by_path('/a')
- self.assertIsNotNone(sub)
- self.assertEqual(sub.key, 'a')
-
- sub = found[0].find_one_by_path('/aa')
- self.assertIsNotNone(sub)
- self.assertEqual(sub.key, 'aa')
-
-
- def testItemsWithoutValue(self):
- """Find items without values."""
-
- from pychrysalide import core
- core.set_verbosity(5)
-
- data = '''
-vals:
- - 1
- - 2
- - 3
-
-'''
-
- reader = YamlReader(text=data)
- self.assertIsNotNone(reader)
-
- found = reader.tree.find_by_path('/vals/')
-
- self.assertEqual(len(found), 3)
-
- self.assertEqual(found[0].nodes[0].key, '1')
- self.assertEqual(found[1].nodes[0].key, '2')
- self.assertEqual(found[2].nodes[0].key, '3')