File size: 3,139 Bytes
a5fd608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from dataclasses import dataclass
from pipeline.base.logging_config_utils import format_config_value, INDENT


def test_format_string():
    """测试字符串格式化"""
    assert format_config_value("hello") == "hello"


def test_format_int():
    """测试整数格式化"""
    assert format_config_value(42) == "42"


def test_format_none():
    """测试 None 格式化"""
    assert format_config_value(None) == "None"


def test_format_function():
    """测试函数格式化 - 显示函数名"""

    def foo():
        pass

    assert format_config_value(foo) == "foo"


def test_format_lambda():
    """测试 lambda 格式化"""
    func = lambda x: x
    assert format_config_value(func) == "<lambda>"


class TestFormatDataclass:
    """测试 dataclass 格式化"""

    def test_simple_dataclass(self):
        """测试简单 dataclass"""

        @dataclass
        class Simple:
            x: int = 1
            y: str = "hello"

        obj = Simple(x=10, y="world")
        result = format_config_value(obj)

        assert result.startswith("Simple(")
        assert "x=10" in result
        assert "y=world" in result
        assert result.endswith(")")

    def test_nested_dataclass(self):
        """测试嵌套 dataclass"""

        @dataclass
        class Inner:
            value: int = 0

        @dataclass
        class Outer:
            name: str = "outer"
            inner: Inner = None

        inner = Inner(value=42)
        outer = Outer(name="test", inner=inner)
        result = format_config_value(outer)

        expected = f"""Outer(
{INDENT}name=test
{INDENT}inner=Inner(
{INDENT}{INDENT}value=42
{INDENT})
)"""
        assert result == expected

    def test_empty_dataclass(self):
        """测试空 dataclass(没有字段)"""

        @dataclass
        class Empty:
            pass

        obj = Empty()
        result = format_config_value(obj)

        assert result == "Empty()"

    def test_dataclass_with_callable(self):
        """测试包含 callable 的 dataclass"""

        @dataclass
        class WithCallable:
            name: str = "test"
            processor: callable = None

        def my_processor():
            pass

        obj = WithCallable(name="doc", processor=my_processor)
        result = format_config_value(obj)

        assert "WithCallable(" in result
        assert "name=doc" in result
        assert "processor=my_processor" in result

    def test_dataclass_with_indent(self):
        """测试带缩进的格式化"""

        @dataclass
        class Simple:
            x: int = 1

        obj = Simple(x=5)
        result = format_config_value(obj, indent=1)

        # 验证有缩进
        assert f"{INDENT}Simple(" in result
        lines = result.split("\n")
        field_line = [l for l in lines if "x=5" in l][0]
        assert field_line.startswith(INDENT * 2)  # 2层缩进


def test_format_arbitrary_object():
    """测试任意对象的格式化"""

    class MyClass:
        def __str__(self):
            return "my_custom_object"

    obj = MyClass()
    assert format_config_value(obj) == "my_custom_object"