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
|
# -*- coding: utf-8 -*-
import unittest
from typing import Optional
from funcparserlib.parser import NoParseError
from funcparserlib.lexer import LexerError
from . import json
class JsonTest(unittest.TestCase):
def t(self, data: str, expected: Optional[object] = None) -> None:
self.assertEqual(json.loads(data), expected)
def test_1_array(self) -> None:
self.t("[1]", [1])
def test_1_object(self) -> None:
self.t('{"foo": "bar"}', {"foo": "bar"})
def test_bool_and_null(self) -> None:
self.t("[null, true, false]", [None, True, False])
def test_empty_array(self) -> None:
self.t("[]", [])
def test_empty_object(self) -> None:
self.t("{}", {})
def test_many_array(self) -> None:
self.t("[1, 2, [3, 4, 5], 6]", [1, 2, [3, 4, 5], 6])
def test_many_object(self) -> None:
# noinspection SpellCheckingInspection
self.t(
"""
{
"foo": 1,
"bar":
{
"baz": 2,
"quux": [true, false],
"{}": {}
},
"spam": "eggs"
}
""",
{
"foo": 1,
"bar": {
"baz": 2,
"quux": [True, False],
"{}": {},
},
"spam": "eggs",
},
)
def test_null(self) -> None:
try:
self.t("")
except NoParseError:
pass
else:
self.fail("must raise NoParseError")
def test_numbers(self) -> None:
self.t(
"""\
[
0, 1, -1, 14, -14, 65536,
0.0, 3.14, -3.14, -123.456,
6.67428e-11, -1.602176e-19, 6.67428E-11
]
""",
[
0,
1,
-1,
14,
-14,
65536,
0.0,
3.14,
-3.14,
-123.456,
6.67428e-11,
-1.602176e-19,
6.67428e-11,
],
)
def test_strings(self) -> None:
# noinspection SpellCheckingInspection
self.t(
r"""
[
["", "hello", "hello world!"],
["привет, мир!", "λx.x"],
["\"", "\\", "\/", "\b", "\f", "\n", "\r", "\t"],
["\u0000", "\u03bb", "\uffff", "\uFFFF"],
["вот функция идентичности:\nλx.x\nили так:\n\u03bbx.x"]
]
""",
[
["", "hello", "hello world!"],
["привет, мир!", "λx.x"],
['"', "\\", "/", "\x08", "\x0c", "\n", "\r", "\t"],
["\u0000", "\u03bb", "\uffff", "\uffff"],
["вот функция идентичности:\nλx.x\nили так:\n\u03bbx.x"],
],
)
def test_toplevel_string(self) -> None:
try:
self.t("неправильно")
except LexerError:
pass
else:
self.fail("must raise LexerError")
|