Skip to content

test_card_file_encoder

Functions:

encoder

encoder()
Source code in tests/test_card_file_encoder.py
6
7
8
@pytest.fixture
def encoder():
    return CardFileEncoder()

encoder_bool_labels

encoder_bool_labels()
Source code in tests/test_card_file_encoder.py
11
12
13
@pytest.fixture
def encoder_bool_labels():
    return CardFileEncoder(truthy_value="y", falsey_value="n")

encoder_explicit_kv_none

encoder_explicit_kv_none()
Source code in tests/test_card_file_encoder.py
16
17
18
@pytest.fixture
def encoder_explicit_kv_none():
    return CardFileEncoder(encode_none_in_kv=True)

test_bool

test_bool(encoder)
Source code in tests/test_card_file_encoder.py
81
82
83
def test_bool(encoder):
    assert encoder.encode(True) == "true"
    assert encoder.encode(False) == "false"

test_bool_labels

test_bool_labels(encoder_bool_labels)
Source code in tests/test_card_file_encoder.py
86
87
88
def test_bool_labels(encoder_bool_labels):
    assert encoder_bool_labels.encode(True) == "y"
    assert encoder_bool_labels.encode(False) == "n"

test_float

test_float(encoder)
Source code in tests/test_card_file_encoder.py
91
92
93
94
95
96
def test_float(encoder):
    assert encoder.encode(-1.0) == "-1.0"
    assert encoder.encode(+1.0) == "1.0"
    assert encoder.encode(5e4) == "50000.0"
    assert encoder.encode(3.14e2) == "314.0"
    assert encoder.encode(3.14e-2) == "0.0314"

test_int

test_int(encoder)
Source code in tests/test_card_file_encoder.py
 99
100
101
102
def test_int(encoder):
    assert encoder.encode(-1) == "-1"
    assert encoder.encode(+1) == "1"
    assert encoder.encode(int(5e4)) == "50000"

test_invalid

test_invalid(encoder)
Source code in tests/test_card_file_encoder.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def test_invalid(encoder):
    with pytest.raises(ValueError, match="Section name can't be empty"):
        encoder.encode({"": {}})
    with pytest.raises(ValueError, match="can't contain double-nested dicts"):
        encoder.encode({"section": {"k": {}}})
    with pytest.raises(ValueError, match="can't contain dicts"):
        encoder.encode([1, {}])
    with pytest.raises(ValueError, match="Single nested list .* is prohibited"):
        encoder.encode([[]])
    with pytest.raises(ValueError, match="Range must contain 3 values"):
        encoder.encode(tuple())
    with pytest.raises(ValueError, match="Range must contain 3 values"):
        encoder.encode((1,))
    with pytest.raises(ValueError, match="Range must contain 3 values"):
        encoder.encode((1, 2))
    with pytest.raises(ValueError, match="Range must contain 3 values"):
        encoder.encode((1, 2, 3, 4))
    with pytest.raises(ValueError, match="Range must contain 3 ints"):
        encoder.encode((1, 2, 3.14))
    with pytest.raises(ValueError, match="Range must contain 3 ints"):
        encoder.encode((True, "text", 3.14))

test_kv

test_kv(encoder)
Source code in tests/test_card_file_encoder.py
54
55
56
57
def test_kv(encoder):
    assert encoder.encode({"p": 1}) == "p=1"
    assert encoder.encode({"p": 1, "q": 2}) == "p=1\nq=2"
    assert encoder.encode({"p": True, "q": 2.12}) == "p=true\nq=2.12"

test_list

test_list(encoder)
Source code in tests/test_card_file_encoder.py
60
61
62
63
64
65
66
67
def test_list(encoder):
    assert encoder.encode({"a": []}) == "a=()"
    assert encoder.encode({"a": [True]}) == "a=(true)"
    assert encoder.encode({"a": [1, 2, 3]}) == "a=(1, 2, 3)"
    assert (
        encoder.encode({"a": [3.14, False, "text", "'more text'"]})
        == "a=(3.14, false, text, 'more text')"
    )

test_nested_list

test_nested_list(encoder)
Source code in tests/test_card_file_encoder.py
70
71
72
73
def test_nested_list(encoder):
    assert encoder.encode({"a": [[], []]}) == "a=(), \\\n  ()"
    assert encoder.encode({"a": [[1], []]}) == "a=(1), \\\n  ()"
    assert encoder.encode({"a": [[1, 2, 3], ["text"]]}) == "a=(1, 2, 3), \\\n  (text)"

test_none

test_none(encoder, encoder_explicit_kv_none)
Source code in tests/test_card_file_encoder.py
43
44
45
46
47
48
49
50
51
def test_none(encoder, encoder_explicit_kv_none):
    assert encoder.encode({"p": None}) == "p="
    assert encoder.encode({"p": []}) == "p=()"
    assert encoder.encode({"p": [None]}) == "p=(NONE)"
    assert encoder.encode({"p": [None, None]}) == "p=(NONE, NONE)"
    assert encoder_explicit_kv_none.encode({"p": None}) == "p=NONE"
    assert encoder_explicit_kv_none.encode({"p": []}) == "p=()"
    assert encoder_explicit_kv_none.encode({"p": [None]}) == "p=(NONE)"
    assert encoder_explicit_kv_none.encode({"p": [None, None]}) == "p=(NONE, NONE)"

test_range

test_range(encoder)
Source code in tests/test_card_file_encoder.py
76
77
78
def test_range(encoder):
    assert encoder.encode((1, 2, 3)) == "1:2:3"
    assert encoder.encode((-100, -5, 300)) == "-100:-5:300"

test_section

test_section(encoder)
Source code in tests/test_card_file_encoder.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def test_section(encoder):
    assert encoder.encode({"section1": {}}) == "[section1]"
    assert encoder.encode({"section1": {}, "section2": {}}) == "[section1]\n[section2]"
    assert (
        encoder.encode({"section1": {"a": 1}, "section2": {}})
        == "[section1]\na=1\n[section2]"
    )
    assert (
        encoder.encode({"section1": {}, "section2": {"a": 1}})
        == "[section1]\n[section2]\na=1"
    )
    assert (
        encoder.encode({"section1": {"a": 1}, "section2": {"a": 1}})
        == "[section1]\na=1\n[section2]\na=1"
    )
    # Top-level kv are placed before any section
    assert (
        encoder.encode({"section1": {}, "a": 1, "section2": {}})
        == "a=1\n[section1]\n[section2]"
    )