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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
import unittest
from unittest.mock import patch
from trubar import jaml
from trubar.messages import MsgNode
class JamlReaderTest(unittest.TestCase):
def test_read(self):
text = """
# jaml file
class `A`:
foo: bar
boo: null
# This function contains nothing but another function
def `f`:
def `g`:
# I think that vaz is just
# mistyped baz
vaz: true
baz: false
"quoted {dict:03}": "v narekovajih {slovar:04}"
'quoted {dict:05}': 'v narekovajih {slovar:04}'
'quoted {dict:06}': v narekovajih {slovar:04}
"This is so: so": to je tako: tako
# Yet another class
class `B`:
nothing: nič
"""
msgs = jaml.read(text)
self.assertEqual(
msgs,
{'class `A`': MsgNode(comments=["# jaml file"], value={
'foo': MsgNode(value='bar'),
'boo': MsgNode(value=None),
'def `f`': MsgNode(
comments=['# This function contains nothing '
'but another function'],
value={
'def `g`': MsgNode(value={
'vaz': MsgNode(
comments=['# I think that vaz is just',
'# mistyped baz'],
value=True),
'baz': MsgNode(value=False),
'quoted {dict:03}': MsgNode(
value='v narekovajih {slovar:04}'),
'quoted {dict:05}': MsgNode(
value='v narekovajih {slovar:04}'),
'quoted {dict:06}': MsgNode(
value='v narekovajih {slovar:04}'),
'This is so: so': MsgNode(
value='to je tako: tako')
})
}
)
}),
'class `B`': MsgNode(
comments=['# Yet another class'],
value={'nothing': MsgNode(value='nič')
})
})
def test_read_empty_file(self):
self.assertRaisesRegex(jaml.JamlError, "unexpected end", jaml.read, "")
def test_read_quoted_blocks(self):
self.assertEqual(jaml.read('''a/b.py:
def `f`:
"a
b
c": abc
abc: "
a
''' + " " * 5 + '''
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
"
foo: false
def: "a
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
x/y.py: null
'''),
{"a/b.py":
MsgNode(
value={"def `f`": MsgNode({
"a\nb\nc": MsgNode("abc"),
"abc": MsgNode(
"\n a\n \n " + "b" * 100 + "\n"),
"foo": MsgNode(False),
"def": MsgNode("a\n" + "b" * 100),
})}),
"x/y.py": MsgNode(None)
}
)
def test_read_quotes_in_values(self):
text = '''
foo1: "bar"
foo2: """bar"
foo4: "ba""r"
foo5: "bar"""
foo7: 'bar'
foo8: 'ba''r'
foo9: 'ba"r'
foo12: "bar''"
'''
msgs = jaml.read(text)
self.assertEqual(
[node.value for node in msgs.values()],
["bar", "\"bar", "ba\"r", "bar\"", "bar", "ba'r", 'ba"r', "bar''"])
def test_read_quotes_in_keys(self):
text = """
1bar": foo2
"2bar": foo3
"3ba""r": foo4
4bar': foo6
'5bar': foo7
'6ba''r': foo8
"7ba""r": foo12
'8ba''r': foo13
"""
msgs = jaml.read(text)
self.assertEqual(
list(msgs),
['1bar"', '2bar', '3ba"r', "4bar'", "5bar", "6ba'r", '7ba"r', "8ba'r"])
def test_read_colons_in_keys(self):
text = """
bar:baz: foo1
bar: baz: foo2
"bar:baz: boz": foo3
"bar"": baz: boz": foo4
"""
msgs = jaml.read(text)
self.assertEqual(
list(msgs),
["bar:baz", "bar", "bar:baz: boz", "bar\": baz: boz"]
)
def test_stray_comments(self):
self.assertRaisesRegex(jaml.JamlError, "5", jaml.read, """
class `A`:
def `f`:
sth: sth
# stray comment
def `g`:
sth:stg
""")
self.assertRaisesRegex(jaml.JamlError, "4", jaml.read, """
class `A`:
def `f`:
# stray comment""")
self.assertRaisesRegex(jaml.JamlError, "4", jaml.read, """
class `A`:
def `f`:
# stray comment
""")
self.assertRaisesRegex(jaml.JamlError, "4", jaml.read, """
class `A`:
def `f`:
# stray comment
""")
def test_indentation_errors(self):
# This function checks for exact error messages. While this is not
# a good practice in general, it makes these tests more readable
self.assertRaisesRegex(
jaml.JamlError, "Line 4: unexpected indent", jaml.read, """
abc:
def: fgh
ghi: jkl
jkl: mno
prs:
tuv: bdf""",
)
self.assertRaisesRegex(
jaml.JamlError, "Line 4: unindent does not match any outer level",
jaml.read, """
abc:
def: fgh
ghi: jkl
jkl: mno
prs:
tuv: bdf""",
)
self.assertRaisesRegex(
jaml.JamlError, "Line 4: unindent does not match any outer level",
jaml.read, """
abc:
def: fgh
ghi: jkl
jkl: mno
prs:
tuv: bdf""",
)
self.assertRaisesRegex(
jaml.JamlError, "Line 4: unindent does not match any outer level",
jaml.read, """
abc:
def: fgh
ghi: jkl
jkl: mno
prs:
tuv:""",
)
self.assertRaisesRegex(
jaml.JamlError, "Line 9: unexpected end of file", jaml.read, """
abc:
def: fgh
ghi: jkl
jkl: mno
prs:
tuv:
""",
)
def test_syntax_errors(self):
self.assertRaisesRegex(
jaml.JamlError, "Line 1: file ends.*", jaml.read, "'''x: y")
self.assertRaisesRegex(
jaml.JamlError, "Line 1: file ends.*", jaml.read, "'x: y")
self.assertRaisesRegex(
jaml.JamlError, "Line 2: quoted key must be followed .*",
jaml.read, '"x\ny"\na:b')
self.assertRaisesRegex(
jaml.JamlError, "Line 2: quoted value must be followed .*",
jaml.read, 'x: "\na": b')
self.assertRaisesRegex(
jaml.JamlError,
"Line 1: colon at the end of the key should be "
"followed by a space or a new line", jaml.read, "x:y")
def test_format_errors(self):
# This function checks for exact error messages. While this is not
# a good practice in general, it makes these tests more readable
self.assertRaisesRegex(
jaml.JamlError, "Line 3: key followed by colon expected", jaml.read, """
abc:
def
ghi: jkl
jkl: mno
prs:
tuv: bdf""",
)
self.assertRaisesRegex(
jaml.JamlError, "Line 4: file ends", jaml.read, """
abc:
def:
"ghi: jkl
jkl: mno
prs:
tuv: bdf""",
)
@patch("trubar.jaml.read")
def test_readfile(self, read):
jaml.readfile(jaml.__file__)
with open(jaml.__file__, encoding="utf-8") as f:
read.assert_called_with(f.read())
class JamlDumperTest(unittest.TestCase):
def test_dump(self):
tree = {"a/b.py":
MsgNode(comments=["# a few", "# initial comments"],
value={ "def `f`":
MsgNode(comments=["# a function!"],
value={"foo": MsgNode("bar"),
"baz": MsgNode(None, ["# eh"])}),
"yada": MsgNode(comments=["# bada", "# boom"],
value=True),
"": MsgNode(""),
}),
"class `A`":
MsgNode(value=False)}
self.assertEqual(
jaml.dump(tree),
"""
# a few
# initial comments
a/b.py:
# a function!
def `f`:
foo: bar
# eh
baz: null
# bada
# boom
yada: true
'': ""
class `A`: false
"""[1:])
def test_backslashes(self):
self.assertEqual(jaml.dump({r"a\nb": MsgNode(r"c\nd")}).strip(),
r"a\nb: c\nd")
def test_dump_quotes(self):
self.assertEqual(jaml.dump({"'foo'": MsgNode("'asdf'")}),
""""'foo'": "'asdf'"\n""")
self.assertEqual(jaml.dump({'"foo"': MsgNode('"asdf"')}),
"""'"foo"': '"asdf"'\n""")
self.assertEqual(jaml.dump({"'foo": MsgNode("asdf'")}),
"""\"'foo": asdf'\n""")
def test_dump_spaces_in_value(self):
self.assertEqual(jaml.dump({"foo": MsgNode("bar ")}),
"foo: 'bar '\n")
self.assertEqual(jaml.dump({"foo": MsgNode(" bar")}),
"foo: ' bar'\n")
def test_quoting_keys(self):
self.assertEqual(jaml.dump({"| ": MsgNode(True)}),
"'| ': true\n")
self.assertEqual(jaml.dump({"# ": MsgNode(True)}),
"'# ': true\n")
self.assertEqual(jaml.dump({" x": MsgNode(True)}),
"' x': true\n")
self.assertEqual(jaml.dump({"x ": MsgNode(True)}),
"'x ': true\n")
self.assertEqual(jaml.dump({" x: y": MsgNode(True)}),
"' x: y': true\n")
self.assertEqual(jaml.dump({"x:y": MsgNode(True)}),
"x:y: true\n")
def test_dump_blocks(self):
tree = {"a/b.py":
MsgNode(
value={ "def `f`": MsgNode({
"a\nb\nc": MsgNode("abc"),
"abc": MsgNode("\n a\n \n " + "b" * 100 + "\n"),
"foo": MsgNode(False),
"def": MsgNode("a\n" + "b" * 100),
})}),
"x/y.py": MsgNode(None)
}
self.assertEqual(jaml.dump(tree), '''a/b.py:
def `f`:
'a
b
c': abc
abc: '
a
''' + " " * 5 + '''
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
'
foo: false
def: 'a
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
x/y.py: null
''')
if __name__ == "__main__":
unittest.main()
|