Skip to content

test_card_file_decoder

Unit tests of CardFileDecoder

Functions:

decoder

decoder()

Initialize classical decoder

Source code in tests/test_card_file_decoder.py
 9
10
11
12
@pytest.fixture
def decoder():
    """Initialize classical decoder"""
    return CardFileDecoder()

test_float

test_float(decoder)

Test decoding float numbers

Source code in tests/test_card_file_decoder.py
84
85
86
87
88
89
90
91
def test_float(decoder):
    """Test decoding float numbers"""
    assert decoder.decode("a=3.1415") == {"a": 3.1415}
    assert decoder.decode("a=3.14e2") == {"a": 314}
    assert decoder.decode("a=3.14e-2") == {"a": 0.0314}
    assert decoder.decode("a=-3.1415") == {"a": -3.1415}
    assert decoder.decode("a=-3.14e2") == {"a": -314}
    assert decoder.decode("a=-3.14e-2") == {"a": -0.0314}

test_integer

test_integer(decoder)

Test decoding integer numbers

Source code in tests/test_card_file_decoder.py
75
76
77
78
79
80
81
def test_integer(decoder):
    """Test decoding integer numbers"""
    assert decoder.decode("a=1") == {"a": 1}
    assert decoder.decode("a=-1") == {"a": -1}
    assert decoder.decode("a=-0") == {"a": 0}
    assert decoder.decode("a=+0") == {"a": 0}
    assert decoder.decode("a=6123") == {"a": 6123}

test_invalid

test_invalid(decoder)

Test decoding invalid files

Source code in tests/test_card_file_decoder.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def test_invalid(decoder):
    """Test decoding invalid files"""
    # Unnamed section
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("[]")
    # Missing key and value
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("=")
    # Missing value
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("=1")
    # Literal without a key
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("1")
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("'1'")
    # Missing `,` in the nested list
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("a=()\\()")
    # Missing `\` in the nested list
    with pytest.raises(UnexpectedCharacters):
        decoder.decode("a=(),()")

test_list

test_list(decoder)

Test decoding of lists

Source code in tests/test_card_file_decoder.py
41
42
43
44
45
46
def test_list(decoder):
    """Test decoding of lists"""
    assert decoder.decode("a = ()") == {"a": []}
    assert decoder.decode("a = (1,2)") == {"a": [1, 2]}
    assert decoder.decode("a = (1,2,3)") == {"a": [1, 2, 3]}
    assert decoder.decode("a = (a, 2, y)") == {"a": ["a", 2, True]}

test_literal

test_literal(decoder)

Test decoding standard key-value entries

Source code in tests/test_card_file_decoder.py
30
31
32
33
34
35
36
37
38
def test_literal(decoder):
    """Test decoding standard key-value entries"""
    assert decoder.decode("a=") == {"a": None}
    assert decoder.decode("a=3") == {"a": 3}
    assert decoder.decode("a=text\nb='text'\nc=\"text\"") == {
        "a": "text",
        "b": "'text'",
        "c": '"text"',
    }

test_logical

test_logical(decoder)

Test decoding logical values

Source code in tests/test_card_file_decoder.py
 94
 95
 96
 97
 98
 99
100
101
def test_logical(decoder):
    """Test decoding logical values"""
    assert decoder.decode("a = true\nb=y\nc=TRUE") == {"a": True, "b": True, "c": True}
    assert decoder.decode("a = false\nb=n\nc=FALSE") == {
        "a": False,
        "b": False,
        "c": False,
    }

test_nested_list

test_nested_list(decoder)

Test decoding of nested lists (lists of lists)

Source code in tests/test_card_file_decoder.py
49
50
51
52
53
54
55
56
def test_nested_list(decoder):
    """Test decoding of nested lists (lists of lists)"""
    assert decoder.decode("a = (),\\()") == {"a": [[], []]}
    assert decoder.decode("a = (1,2),\\(3,4)") == {"a": [[1, 2], [3, 4]]}
    assert decoder.decode("a = (1,2,3), \\(a,b)") == {"a": [[1, 2, 3], ["a", "b"]]}
    assert decoder.decode("a = (y),\\(n),\\(y),\\(n)") == {
        "a": [[True], [False], [True], [False]]
    }

test_range

test_range(decoder)

Test decoding of ranges in the form START:STEP:END

Source code in tests/test_card_file_decoder.py
59
60
61
62
def test_range(decoder):
    """Test decoding of ranges in the form START:STEP:END"""
    assert decoder.decode("a=1:2:3") == {"a": (1, 2, 3)}
    assert decoder.decode("a=-100:-5:-200") == {"a": (-100, -5, -200)}

test_section

test_section(decoder)

Test decoding sections

Source code in tests/test_card_file_decoder.py
15
16
17
18
19
20
21
22
23
24
25
26
27
def test_section(decoder):
    """Test decoding sections"""
    assert decoder.decode("[section1][section2]") == {"section1": {}, "section2": {}}
    assert decoder.decode("[section1]\n[section2]") == {"section1": {}, "section2": {}}
    assert decoder.decode("[section1]\na=3\nb=(1,2,3)[section2]c='some text'") == {
        "section1": {"a": 3, "b": [1, 2, 3]},
        "section2": {"c": "'some text'"},
    }
    assert decoder.decode("a=1\n[section1]b=1\n[section2]\nc=3") == {
        "a": 1,
        "section1": {"b": 1},
        "section2": {"c": 3},
    }

test_string

test_string(decoder)

Test decoding strings (quoted and unqoted)

Source code in tests/test_card_file_decoder.py
65
66
67
68
69
70
71
72
def test_string(decoder):
    """Test decoding strings (quoted and unqoted)"""
    assert decoder.decode("a=text") == {"a": "text"}
    assert decoder.decode("a='text'") == {"a": "'text'"}
    assert decoder.decode('a="text"') == {"a": '"text"'}
    assert decoder.decode("file1=${SUBMIT_DIR}/../${year}/some_file.ext") == {
        "file1": "${SUBMIT_DIR}/../${year}/some_file.ext"
    }