File: test_016_encoding.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (34 lines) | stat: -rw-r--r-- 955 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2016-2020, Manfred Moitzi
# License: MIT License
import pytest
import codecs
from ezdxf.lldxf.encoding import dxf_backslash_replace, encode
from pathlib import Path

# setup DXF unicode encoder -> '\U+nnnn'
codecs.register_error("dxfreplace", dxf_backslash_replace)


def test_ascii_encoding():
    assert b"123\\U+6539" == encode("123改", "ascii")


@pytest.mark.parametrize(
    ["s", "e"],
    [
        ("300\n\udcb7\udc9e\udcff\n", b"300\n\xb7\x9e\xff\n"),
        ("123改", b"123\\U+6539"),
    ],
)
def test_surrogate_escape_support_in_dxf_replace_encoder(s, e):
    assert e == encode(s, "ascii")


@pytest.mark.parametrize("n", [0, 1, 2])
def test_XRECORD_handling_of_dxf_replace_encoder(n):
    XRECORD = Path(__file__).parent / f"XRECORD_{n}.bin"
    with open(XRECORD, "rb") as f:
        data = f.read()
    s = data.decode("utf8", errors="surrogateescape")
    result = encode(s, encoding="utf8")
    assert data == result