File: test_json_dict.py

package info (click to toggle)
plover 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,356 kB
  • sloc: python: 21,589; sh: 682; ansic: 25; makefile: 11
file content (88 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# Copyright (c) 2013 Hesky Fisher
# See LICENSE.txt for details.

"""Unit tests for json.py."""

from plover.dictionary.json_dict import JsonDictionary

from plover_build_utils.testing import dictionary_test


def json_load_test(contents, expected):
    if isinstance(contents, str):
        contents = contents.encode("utf-8")
    return contents, expected


JSON_LOAD_TESTS = (
    lambda: json_load_test('{"S": "a"}', '"S": "a"'),
    # Default encoding is utf-8.
    lambda: json_load_test('{"S": "café"}', '"S": "café"'),
    # But if that fails, the implementation
    # must automatically retry with latin-1.
    lambda: json_load_test('{"S": "café"}'.encode("latin-1"), '"S": "café"'),
    # Invalid JSON.
    lambda: json_load_test('{"foo", "bar",}', ValueError),
    # Invalid JSON.
    lambda: json_load_test("foo", ValueError),
    # Cannot convert to dict.
    lambda: json_load_test('"foo"', ValueError),
    # Ditto.
    lambda: json_load_test("4.2", TypeError),
)


def json_save_test(entries, expected):
    return entries, expected.encode("utf-8")


JSON_SAVE_TESTS = (
    # Simple test.
    lambda: json_save_test(
        """
        'S': 'a',
        """,
        '{\n"S": "a"\n}\n',
    ),
    # Check strokes format: '/' separated.
    lambda: json_save_test(
        """
        'SAPL/-PL': 'sample',
        """,
        '{\n"SAPL/-PL": "sample"\n}\n',
    ),
    # Contents should be saved as UTF-8, no escaping.
    lambda: json_save_test(
        """
        'S': 'café',
        """,
        '{\n"S": "café"\n}\n',
    ),
    # Check escaping of special characters.
    lambda: json_save_test(
        r"""
        'S': '{^"\n\t"^}',
        """,
        '{\n"S": "' + r"{^\"\n\t\"^}" + '"\n}\n',
    ),
    # Keys are sorted on save.
    lambda: json_save_test(
        """
        'T': 'bravo',
        'S': 'alpha',
        'R': 'charlie',
        """,
        '{\n"S": "alpha",\n"T": "bravo",\n"R": "charlie"\n}\n',
    ),
)


@dictionary_test
class TestJsonDictionary:
    DICT_CLASS = JsonDictionary
    DICT_EXTENSION = "json"
    DICT_REGISTERED = True
    DICT_LOAD_TESTS = JSON_LOAD_TESTS
    DICT_SAVE_TESTS = JSON_SAVE_TESTS
    DICT_SAMPLE = b"{}"