Skip to content

test_def_file_encoder

Functions

encoder

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

encoder_bool_labels

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

test_bool

test_bool(encoder)
Source code in tests/test_def_file_encoder.py
16
17
18
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_def_file_encoder.py
21
22
23
24
25
26
27
def test_bool_labels(encoder_bool_labels):
    # Test other labels for true/false values
    # These changes are local to this test, and will not affect other tests
    encoder_bool_labels._truthy_value = "y"
    encoder_bool_labels._falsey_value = "n"
    assert encoder_bool_labels.encode(True) == "y"
    assert encoder_bool_labels.encode(False) == "n"

test_dict

test_dict(encoder)
Source code in tests/test_def_file_encoder.py
30
31
32
33
34
35
36
37
38
def test_dict(encoder):
    assert encoder.encode({"p": 1}) == "p = 1"
    assert encoder.encode({"p": 1, "q": 2}) == "p = 1\nq = 2"
    assert (
        encoder.encode({"INCLUDEDEF": ["m1", "m2"], "p": 1})
        == "INCLUDEDEF = m1\nINCLUDEDEF = m2\np = 1"
    )
    with pytest.raises(TypeError, match="cannot be of type: dict"):
        encoder.encode({"p": {"q": 2}})

test_float

test_float(encoder)
Source code in tests/test_def_file_encoder.py
41
42
43
44
45
46
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_def_file_encoder.py
49
50
51
52
def test_int(encoder):
    assert encoder.encode(-1) == "-1"
    assert encoder.encode(+1) == "1"
    assert encoder.encode(int(5e4)) == "50000"

test_list

test_list(encoder)
Source code in tests/test_def_file_encoder.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def test_list(encoder):
    assert encoder.encode([1, 2]) == "1, 2"
    assert encoder.encode([1, 2, "a"]) == "1, 2, a"
    with pytest.raises(ValueError, match="at least two elements"):
        assert encoder.encode([])
    with pytest.raises(ValueError, match="at least two elements"):
        assert encoder.encode([1])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, {}])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, {"p": 2}])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, ()])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, (2, 3)])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, []])
    with pytest.raises(TypeError, match="must be simple scalars"):
        assert encoder.encode([1, [2, 3]])

test_tuple

test_tuple(encoder)
Source code in tests/test_def_file_encoder.py
 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
def test_tuple(encoder):
    assert encoder.encode(("_AUTO_", None)) == "_AUTO_"
    assert encoder.encode(("_AUTOBLOCKER_", None)) == "_AUTOBLOCKER_"
    assert encoder.encode(("_AUTO_", True)) == "_AUTO_: DEFAULT=true"
    assert encoder.encode(("_AUTO_", 3)) == "_AUTO_: DEFAULT=3"
    assert encoder.encode(("_AUTO_", 3.14)) == "_AUTO_: DEFAULT=3.14"
    assert encoder.encode(("_AUTO_", "text")) == "_AUTO_: DEFAULT=text"
    assert encoder.encode(("_AUTO_", "'text'")) == "_AUTO_: DEFAULT='text'"
    assert encoder.encode(("_AUTO_", '"text"')) == '_AUTO_: DEFAULT="text"'
    with pytest.raises(ValueError, match="len=2"):
        assert encoder.encode(())
    with pytest.raises(ValueError, match="len=2"):
        assert encoder.encode(("_AUTO_",))
    with pytest.raises(ValueError, match="len=2"):
        assert encoder.encode(("_AUTO_", None, None))
    with pytest.raises(ValueError, match="First element"):
        assert encoder.encode(("AUTO", None))
    with pytest.raises(ValueError, match="First element"):
        assert encoder.encode((None, None))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", ()))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", (1, 2, 3)))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", {}))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", {"k1": 1, "k2": 2, "k3": 3}))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", []))
    with pytest.raises(TypeError, match="Second element"):
        assert encoder.encode(("_AUTO_", [1, 2, 3]))