File: test_steno_dictionary.py

package info (click to toggle)
plover 5.0.0-2
  • 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 (250 lines) | stat: -rw-r--r-- 6,928 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright (c) 2013 Hesky Fisher
# See LICENSE.txt for details.

"""Unit tests for steno_dictionary.py."""

import pytest

from plover.steno_dictionary import StenoDictionary, StenoDictionaryCollection

from plover_build_utils.testing import dictionary_test


def test_dictionary_collection():
    d1 = StenoDictionary()
    d1[("S",)] = "a"
    d1[("T",)] = "b"
    d1[("H",)] = "{PLOVER:deleted}"
    d1[("R",)] = "e"
    d1.path = "d1"
    d2 = StenoDictionary()
    d2[("S",)] = "c"
    d2[("W",)] = "d"
    d2[("H",)] = "f"
    d2[("R",)] = "{plover:deleted}"
    d2.path = "d2"
    dc = StenoDictionaryCollection([d2, d1])
    assert dc.lookup(("S",)) == "c"
    assert dc.lookup(("W",)) == "d"
    assert dc.lookup(("T",)) == "b"
    assert dc.lookup(("H",)) == "f"
    assert dc.lookup(("R",)) == None
    assert dc.lookup_from_all(("S",)) == [("c", d2), ("a", d1)]
    assert dc.lookup_from_all(("W",)) == [("d", d2)]
    assert dc.lookup_from_all(("T",)) == [("b", d1)]
    assert dc.lookup_from_all(("H",)) == [("f", d2), ("{PLOVER:deleted}", d1)]
    assert dc.lookup_from_all(("R",)) == [("{plover:deleted}", d2), ("e", d1)]
    f = lambda k, v: v == "c"
    dc.add_filter(f)
    assert dc.lookup(("S",)) == "a"
    assert dc.raw_lookup(("S",)) == "c"
    assert dc.lookup_from_all(("S",)) == [("a", d1)]
    g = lambda k, v: v == "a"
    dc.add_filter(g)
    assert dc.lookup(("S",)) is None
    assert dc.lookup_from_all(("S",)) == []
    assert dc.raw_lookup(("S",)) == "c"
    assert dc.lookup(("W",)) == "d"
    assert dc.lookup(("T",)) == "b"
    assert dc.raw_lookup(("W",)) == "d"
    assert dc.raw_lookup(("T",)) == "b"
    assert dc.raw_lookup_from_all(("S",)) == [("c", d2), ("a", d1)]
    assert dc.raw_lookup_from_all(("W",)) == [("d", d2)]
    assert dc.raw_lookup_from_all(("T",)) == [("b", d1)]
    assert dc.reverse_lookup("c") == {("S",)}

    dc.remove_filter(f)
    dc.remove_filter(g)
    assert dc.lookup(("S",)) == "c"
    assert dc.lookup(("W",)) == "d"
    assert dc.lookup(("T",)) == "b"
    assert dc.lookup(("H",)) == "f"
    assert dc.lookup(("R",)) == None
    assert dc.lookup_from_all(("S",)) == [("c", d2), ("a", d1)]
    assert dc.lookup_from_all(("W",)) == [("d", d2)]
    assert dc.lookup_from_all(("T",)) == [("b", d1)]
    assert dc.lookup_from_all(("H",)) == [("f", d2), ("{PLOVER:deleted}", d1)]
    assert dc.lookup_from_all(("R",)) == [("{plover:deleted}", d2), ("e", d1)]

    assert dc.reverse_lookup("c") == {("S",)}

    dc.set(("S",), "e")
    assert dc.lookup(("S",)) == "e"
    assert d2[("S",)] == "e"

    dc.set(("S",), "f", path="d1")
    assert dc.lookup(("S",)) == "e"
    assert d1[("S",)] == "f"
    assert d2[("S",)] == "e"

    # Iterating on a StenoDictionaryCollection is
    # the same as iterating on its dictionaries' paths.
    assert list(dc) == ["d2", "d1"]

    # Test get and [].
    assert dc.get("d1") == d1
    assert dc["d1"] == d1
    assert dc.get("invalid") is None
    with pytest.raises(KeyError):
        dc["invalid"]


def test_dictionary_collection_writeable():
    d1 = StenoDictionary()
    d1[("S",)] = "a"
    d1[("T",)] = "b"
    d2 = StenoDictionary()
    d2[("S",)] = "c"
    d2[("W",)] = "d"
    d2.readonly = True
    dc = StenoDictionaryCollection([d2, d1])
    assert dc.first_writable() == d1
    dc.set(("S",), "A")
    assert d1[("S",)] == "A"
    assert d2[("S",)] == "c"


def test_dictionary_collection_longest_key():
    k1 = ("S",)
    k2 = ("S", "T")
    k3 = ("S", "T", "R")

    dc = StenoDictionaryCollection()
    assert dc.longest_key == 0

    d1 = StenoDictionary()
    d1.path = "d1"
    d1[k1] = "a"

    dc.set_dicts([d1])
    assert dc.longest_key == 1

    d1[k2] = "a"
    assert dc.longest_key == 2

    d2 = StenoDictionary()
    d2.path = "d2"
    d2[k3] = "c"

    dc.set_dicts([d2, d1])
    assert dc.longest_key == 3

    del d1[k2]
    assert dc.longest_key == 3

    d2.enabled = False
    assert dc.longest_key == 1

    d1.enabled = False
    assert dc.longest_key == 0

    d2.enabled = True
    assert dc.longest_key == 3

    d1.enabled = True
    assert dc.longest_key == 3

    dc.set_dicts([d1])
    assert dc.longest_key == 1

    dc.set_dicts([])
    assert dc.longest_key == 0


def test_casereverse_lookup():
    dc = StenoDictionaryCollection()

    d1 = StenoDictionary()
    d1[("PWAOUFL",)] = "beautiful"
    d1[("WAOUFL",)] = "beAuTIFul"

    d2 = StenoDictionary()
    d2[("PW-FL",)] = "BEAUTIFUL"

    d3 = StenoDictionary()
    d3[("WAOUFL",)] = "not beautiful"

    dc.set_dicts([d1, d2, d3])

    assert dc.casereverse_lookup("beautiful") == {"beautiful", "BEAUTIFUL", "beAuTIFul"}


def test_reverse_lookup():
    dc = StenoDictionaryCollection()

    d1 = StenoDictionary()
    d1[("PWAOUFL",)] = "beautiful"
    d1[("WAOUFL",)] = "beautiful"

    d2 = StenoDictionary()
    d2[("PW-FL",)] = "beautiful"

    d3 = StenoDictionary()
    d3[("WAOUFL",)] = "not beautiful"

    # Simple test.
    dc.set_dicts([d1])
    assert dc.reverse_lookup("beautiful") == {("PWAOUFL",), ("WAOUFL",)}

    # No duplicates.
    d2_copy = StenoDictionary()
    d2_copy.update(d2)
    dc.set_dicts([d2_copy, d2])
    assert dc.reverse_lookup("beautiful") == {("PW-FL",)}

    # Don't stop at the first dictionary with matches.
    dc.set_dicts([d2, d1])
    assert dc.reverse_lookup("beautiful") == {("PW-FL",), ("PWAOUFL",), ("WAOUFL",)}

    # Ignore keys overridden by a higher precedence dictionary.
    dc.set_dicts([d3, d2, d1])
    assert dc.reverse_lookup("beautiful") == {("PW-FL",), ("PWAOUFL",)}


def test_dictionary_enabled():
    dc = StenoDictionaryCollection()
    d1 = StenoDictionary()
    d1.path = "d1"
    d1[("TEFT",)] = "test1"
    d1[("TEFGT",)] = "Testing"
    d2 = StenoDictionary()
    d2[("TEFT",)] = "test2"
    d2[("TEFT", "-G")] = "Testing"
    d2.path = "d2"
    dc.set_dicts([d2, d1])
    assert dc.lookup(("TEFT",)) == "test2"
    assert dc.raw_lookup(("TEFT",)) == "test2"
    assert dc.casereverse_lookup("testing") == {"Testing"}
    assert dc.reverse_lookup("Testing") == {("TEFT", "-G"), ("TEFGT",)}
    d2.enabled = False
    assert dc.lookup(("TEFT",)) == "test1"
    assert dc.raw_lookup(("TEFT",)) == "test1"
    assert dc.casereverse_lookup("testing") == {"Testing"}
    assert dc.reverse_lookup("Testing") == {("TEFGT",)}
    d1.enabled = False
    assert dc.lookup(("TEST",)) is None
    assert dc.raw_lookup(("TEFT",)) is None
    assert dc.casereverse_lookup("testing") == set()
    assert dc.reverse_lookup("Testing") == set()


@dictionary_test
class TestStenoDictionary:
    class DICT_CLASS(StenoDictionary):
        def _load(self, filename):
            pass

    DICT_EXTENSION = "dict"
    DICT_SAMPLE = b""


@dictionary_test
class TestReadOnlyStenoDictionary:
    class DICT_CLASS(StenoDictionary):
        readonly = True

        def _load(self, filename):
            pass

    DICT_EXTENSION = "dict"
    DICT_SAMPLE = b""