Пример #1
0
def test_encoding_simple():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1, 2], b=[3]))

    result = charset.encode("acacb", EncodingParams(per_page=3))
    assert result.result == [[("a", 1), ("", 2), ("a", 1)], [("", 2),
                                                             ("b", 3)]]
    assert result.unknown == {"c"}
    assert result.unknown_count == 2
Пример #2
0
def test_register_switching():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1], b=[2]))
    charset.add_characters(dict(c=[3], d=[4]), activation=[50])
    charset.add_characters(dict(e=[5], f=[6]), activation=[51])

    result = charset.encode("accdabecf", EncodingParams(per_page=200))
    assert len(result.result) == 1

    expected_codes = [1, 50, 3, 3, 4, 1, 2, 51, 5, 50, 3, 51, 6]
    assert [x[1] for x in result.result[0]] == expected_codes
    expected_chars = [
        "a", "", "c", "c", "d", "a", "b", "", "e", "", "c", "", "f"
    ]
    assert [x[0] for x in result.result[0]] == expected_chars
Пример #3
0
def test_creating():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1, 2, 3], b=[4, 6]))
    charset.add_characters(dict(c=[7]), activation=[15])

    assert charset.name == "Charset"
    assert charset.type == EncodingType.PUNCHCARD

    assert charset["a"].codes == [1, 2, 3]
    assert not charset["a"].needs_activation

    assert charset["c"].codes == [7]
    assert charset["c"].needs_activation
    assert charset["c"].activation == [15]
Пример #4
0
def test_does_not_break_with_newline():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters({"a": [1], "\n": [2]})
    result = charset.encode("aaa\naaaa\naaaaaa", EncodingParams(per_page=50))
    assert [len(x) for x in result.result] == [15]
Пример #5
0
def test_breaks_with_newline():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1]))
    result = charset.encode("aaa\naaaa\naaaaaa",
                            EncodingParams(per_page=50, break_with_line=True))
    assert [len(x) for x in result.result] == [3, 4, 6]
Пример #6
0
def test_too_many_pages():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1]))
    with pytest.raises(TooManyPagesError):
        charset.encode("aaaaaaaaaaaaaaaaaa",
                       EncodingParams(per_page=5, max_pages=3))
Пример #7
0
def test_message_too_long_ignores_unknown():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1]))
    charset.encode("abbbbbbba", EncodingParams(per_page=100, max_length=5))
Пример #8
0
def test_message_too_long():
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD)
    charset.add_characters(dict(a=[1]))
    with pytest.raises(MessageTooLongError):
        charset.encode("aaaaaaaaa", EncodingParams(per_page=100, max_length=5))
Пример #9
0
def test_fix_string(original: str, expected: str):
    charset = CharacterSet("Charset", EncodingType.PUNCHCARD, {
        "-": "+",
        "a": "b"
    })
    assert charset.fix_string(original) == expected