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
|
#!/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'")
def testArrayAsSeq(self):
"""Handle array as YAML block sequence."""
definitions = '''
root:
a: [ a, 'b', 0xcc, "\td\n\\"'" ]
'''
root = yaml.parse_from_text(definitions)
found = root.find_first_by_path('/root/a')
self.assertIsNone(found.value)
self.assertEqual(len(found.children.nodes), 4)
self.assertEqual(found.children.nodes[0].key, 'a')
self.assertEqual(found.children.nodes[1].key, 'b')
self.assertEqual(found.children.nodes[2].key, '0xcc')
self.assertEqual(found.children.nodes[3].key, "\td \"'")
self.assertEqual(found.aggregate_value(), '[ a, \'b\', 0xcc, " d \"\'" ]')
|