2019-07-10 07:00:44 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from tutor import serialize
|
|
|
|
|
|
|
|
|
|
|
|
class SerializeTests(unittest.TestCase):
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_str(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertEqual("abcd", serialize.parse("abcd"))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_int(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertEqual(1, serialize.parse("1"))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_bool(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertEqual(True, serialize.parse("true"))
|
|
|
|
self.assertEqual(False, serialize.parse("false"))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_null(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertIsNone(serialize.parse("null"))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_invalid_format(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertEqual('["abcd"', serialize.parse('["abcd"'))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_list(self) -> None:
|
2019-07-10 07:00:44 +00:00
|
|
|
self.assertEqual(["abcd"], serialize.parse('["abcd"]'))
|
2019-07-11 02:31:23 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_weird_chars(self) -> None:
|
2019-07-11 02:31:23 +00:00
|
|
|
self.assertEqual("*@google.com", serialize.parse("*@google.com"))
|
2020-01-21 16:09:58 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_parse_empty_string(self) -> None:
|
2020-01-21 16:09:58 +00:00
|
|
|
self.assertEqual("", serialize.parse("''"))
|
|
|
|
|
2022-11-17 13:14:49 +00:00
|
|
|
def test_parse_key_value(self) -> None:
|
|
|
|
self.assertEqual(("name", True), serialize.parse_key_value("name=true"))
|
|
|
|
self.assertEqual(("name", "abcd"), serialize.parse_key_value("name=abcd"))
|
|
|
|
self.assertEqual(("name", ""), serialize.parse_key_value("name="))
|
|
|
|
self.assertIsNone(serialize.parse_key_value("name"))
|
|
|
|
self.assertEqual(("x", "a=bcd"), serialize.parse_key_value("x=a=bcd"))
|
2021-04-23 13:53:20 +00:00
|
|
|
self.assertEqual(
|
|
|
|
("x", {"key1": {"subkey": "value"}, "key2": {"subkey": "value"}}),
|
2022-11-17 13:14:49 +00:00
|
|
|
serialize.parse_key_value(
|
|
|
|
"x=key1:\n subkey: value\nkey2:\n subkey: value"
|
2021-04-23 13:53:20 +00:00
|
|
|
),
|
|
|
|
)
|
2023-05-16 07:17:37 +00:00
|
|
|
|
|
|
|
def test_str_format(self) -> None:
|
|
|
|
self.assertEqual("true", serialize.str_format(True))
|
|
|
|
self.assertEqual("false", serialize.str_format(False))
|
|
|
|
self.assertEqual("null", serialize.str_format(None))
|
|
|
|
self.assertEqual("éü©", serialize.str_format("éü©"))
|
|
|
|
self.assertEqual("""[1, 'abcd']""", serialize.str_format([1, "abcd"]))
|
|
|
|
|
|
|
|
def test_load_str_format(self) -> None:
|
|
|
|
self.assertEqual(True, serialize.load(serialize.str_format(True)))
|
|
|
|
self.assertEqual(False, serialize.load(serialize.str_format(False)))
|
|
|
|
self.assertEqual(None, serialize.load(serialize.str_format(None)))
|
|
|
|
self.assertEqual("éü©", serialize.load(serialize.str_format("éü©")))
|
|
|
|
self.assertEqual([1, "abcd"], serialize.load(serialize.str_format([1, "abcd"])))
|