test_escape.py

Overview

`test_escape.py` is a test suite focused on verifying the correct serialization (JSON encoding) of various special and control Unicode characters by the `orjson` library. `orjson` is a fast, correct JSON library for Python that produces UTF-8 encoded JSON output.

This file ensures that specific whitespace, control characters, and escape sequences are correctly escaped according to JSON standards when serialized by `orjson.dumps()`. The tests primarily check that characters like newline, carriage return, backspace, tab, and others are transformed into their proper escaped representations, ensuring compatibility and correctness of JSON output.


Detailed Explanation

Imports

import orjson

Test Functions

Each test function asserts that serializing a single Unicode character results in the correct escaped byte string output.

General Pattern

def test_xxx():
    assert orjson.dumps("<unicode_char>") == b'"<expected_escaped_string>"'

Examples

1. test_issue565
def test_issue565():
    assert (
        orjson.dumps("\n\r\u000b\f\u001c\u001d\u001e")
        == b'"\\n\\r\\u000b\\f\\u001c\\u001d\\u001e"'
    )
2. test_0x00 through test_0x1a

Each function tests a specific control character in the Unicode range U+0000 to U+001A, verifying the escaping behavior.

Example:

def test_0x00():
    assert orjson.dumps("\u0000") == b'"\\u0000"'

Some characters have special escape sequences, e.g.,

def test_0x08():
    assert orjson.dumps("\u0008") == b'"\\b"'
3. test_backslash
def test_backslash():
    assert orjson.dumps("\\") == b'"\\\\"'
4. test_quote
def test_quote():
    assert orjson.dumps('"') == b'"\\""'

Important Implementation Details


Interaction with Other Parts of the System


Summary


Visual Diagram: Flowchart of Test Functions and Relationships

flowchart TD
    A[Start: Run test_escape.py] --> B{Test Functions}
    
    B --> C[test_issue565]
    B --> D[test_0x00_to_0x1A]
    B --> E[test_backslash]
    B --> F[test_quote]

    D --> D1[test_0x00]
    D --> D2[test_0x01]
    D --> D3[test_0x02]
    D --> D4[test_0x03]
    D --> D5[test_0x04]
    D --> D6[test_0x05]
    D --> D7[test_0x06]
    D --> D8[test_0x07]
    D --> D9[test_0x08]
    D --> D10[test_0x09]
    D --> D11[test_0x0a]
    D --> D12[test_0x0b]
    D --> D13[test_0x0c]
    D --> D14[test_0x0d]
    D --> D15[test_0x0e]
    D --> D16[test_0x0f]
    D --> D17[test_0x10]
    D --> D18[test_0x11]
    D --> D19[test_0x12]
    D --> D20[test_0x13]
    D --> D21[test_0x14]
    D --> D22[test_0x15]
    D --> D23[test_0x16]
    D --> D24[test_0x17]
    D --> D25[test_0x18]
    D --> D26[test_0x19]
    D --> D27[test_0x1a]

    style B fill:#f9f,stroke:#333,stroke-width:2px
    style D fill:#bbf,stroke:#333,stroke-width:1px

End of Documentation for test_escape.py