Serde: fixup & tests after changed serialization

This commit is contained in:
Johannes Kirschbauer
2024-10-18 11:52:27 +02:00
parent f05268cda8
commit 4b0af71b9d
3 changed files with 17 additions and 20 deletions

View File

@@ -30,7 +30,6 @@ Note: This module assumes the presence of other modules and classes such as `Cla
"""
import dataclasses
import json
from dataclasses import dataclass, fields, is_dataclass
from enum import Enum
from pathlib import Path
@@ -162,7 +161,7 @@ def construct_value(
msg = f"Expected string, got {field_value}"
raise ClanError(msg, location=f"{loc}")
return json.loads(f'"{field_value}"')
return field_value
if t is int and not isinstance(field_value, str):
return int(field_value) # type: ignore

View File

@@ -217,8 +217,8 @@ def test_none_or_string() -> None:
def test_roundtrip_escape() -> None:
assert from_dict(str, "\\n") == "\n"
assert dataclass_to_dict("\n") == "\\n"
assert from_dict(str, "\n") == "\n"
assert dataclass_to_dict("\n") == "\n"
# Test that the functions are inverses of each other
# f(g(x)) == x

View File

@@ -11,27 +11,25 @@ from clan_cli.api import (
def test_sanitize_string() -> None:
# Simple strings
assert sanitize_string("Hello World") == "Hello World"
assert sanitize_string("Hello\nWorld") == "Hello\\nWorld"
assert sanitize_string("Hello\tWorld") == "Hello\\tWorld"
assert sanitize_string("Hello\rWorld") == "Hello\\rWorld"
assert sanitize_string("Hello\fWorld") == "Hello\\fWorld"
assert sanitize_string("Hello\vWorld") == "Hello\\u000bWorld"
assert sanitize_string("Hello\bWorld") == "Hello\\bWorld"
assert sanitize_string("Hello\\World") == "Hello\\\\World"
assert sanitize_string('Hello"World') == 'Hello\\"World'
assert sanitize_string("Hello\nWorld") == "Hello\nWorld"
assert sanitize_string("Hello\tWorld") == "Hello\tWorld"
assert sanitize_string("Hello\rWorld") == "Hello\rWorld"
assert sanitize_string("Hello\fWorld") == "Hello\fWorld"
assert sanitize_string("Hello\vWorld") == "Hello\u000bWorld"
assert sanitize_string("Hello\bWorld") == "Hello\bWorld"
assert sanitize_string("Hello\\World") == "Hello\\World"
assert sanitize_string('Hello"World') == 'Hello"World'
assert sanitize_string("Hello'World") == "Hello'World"
assert sanitize_string("Hello\0World") == "Hello\\u0000World"
assert sanitize_string("Hello\0World") == "Hello\x00World"
# Console escape characters
assert sanitize_string("\033[1mBold\033[0m") == "\\u001b[1mBold\\u001b[0m" # Red
assert sanitize_string("\033[31mRed\033[0m") == "\\u001b[31mRed\\u001b[0m" # Blue
assert (
sanitize_string("\033[42mGreen\033[0m") == "\\u001b[42mGreen\\u001b[0m"
) # Green
assert sanitize_string("\033[4mUnderline\033[0m") == "\\u001b[4mUnderline\\u001b[0m"
assert sanitize_string("\033[1mBold\033[0m") == "\033[1mBold\033[0m" # Red
assert sanitize_string("\033[31mRed\033[0m") == "\033[31mRed\033[0m" # Blue
assert sanitize_string("\033[42mGreen\033[0m") == "\033[42mGreen\033[0m" # Green
assert sanitize_string("\033[4mUnderline\033[0m") == "\033[4mUnderline\033[0m"
assert (
sanitize_string("\033[91m\033[1mBold Red\033[0m")
== "\\u001b[91m\\u001b[1mBold Red\\u001b[0m"
== "\033[91m\033[1mBold Red\033[0m"
)