def test_dictionary_update_shrink(self): self.d[('S', 'T', 'P', '-Z', '-D')] = '1' self.t.translate(stroke('T-')) self._check_size_call(5) self.clear() self.d[('A', 'P')] = '2' self.t.translate(stroke('T-')) self._check_size_call(5) self.clear() del self.d[('S', 'T', 'P', '-Z', '-D')] self.t.translate(stroke('T-')) self._check_size_call(2)
def test_dictionary_update_no_grow(self): self.t.set_min_undo_length(4) self._check_size_call(4) self.clear() self.d[('S', 'T')] = 'nothing' self.t.translate(stroke('T-')) self._check_size_call(4)
def test_listeners(): output1 = [] def listener1(undo, do, prev): output1.append((undo, do, prev)) output2 = [] def listener2(undo, do, prev): output2.append((undo, do, prev)) t = Translator() s = stroke('S') tr = Translation([s], None) expected_output = [([], [tr], [tr])] t.translate(s) t.add_listener(listener1) t.translate(s) assert output1 == expected_output del output1[:] t.add_listener(listener2) t.translate(s) assert output1 == expected_output assert output2 == expected_output del output1[:] del output2[:] t.add_listener(listener2) t.translate(s) assert output1 == expected_output assert output2 == expected_output del output1[:] del output2[:] t.remove_listener(listener1) t.translate(s) assert output1 == [] assert output2 == expected_output del output1[:] del output2[:] t.remove_listener(listener2) t.translate(s) assert output1 == [] assert output2 == []
def test_listeners(): output1 = [] def listener1(undo, do, prev): output1.append((undo, do, prev)) output2 = [] def listener2(undo, do, prev): output2.append((undo, do, prev)) t = Translator() s = stroke('S') tr = Translation([s], None) expected_output = [([], [tr], [tr])] t.translate(s) t.add_listener(listener1) t.translate(s) assert output1 == expected_output del output1[:] t.add_listener(listener2) t.translate(s) assert output1 == expected_output assert output2 == expected_output del output1[:] del output2[:] t.add_listener(listener2) t.translate(s) assert output1 == expected_output assert output2 == expected_output del output1[:] del output2[:] t.remove_listener(listener1) t.translate(s) assert output1 == [] assert output2 == expected_output del output1[:] del output2[:] t.remove_listener(listener2) t.translate(s) assert output1 == [] assert output2 == []
def test_translator(): # It's not clear that this test is needed anymore. There are separate # tests for _translate_stroke and test_translate_calls_translate_stroke # makes sure that translate calls it properly. But since I already wrote # this test I'm going to keep it. class Output: def __init__(self): self._output = [] def write(self, undo, do, prev): for t in undo: self._output.pop() for t in do: if t.english: self._output.append(t.english) else: self._output.append('/'.join(t.rtfcre)) def get(self): return ' '.join(self._output) def clear(self): del self._output[:] d = StenoDictionary() out = Output() t = Translator() dc = StenoDictionaryCollection([d]) t.set_dictionary(dc) t.add_listener(out.write) t.translate(stroke('S')) assert out.get() == 'S' t.translate(stroke('T')) assert out.get() == 'S T' t.translate(stroke('*')) assert out.get() == 'S' t.translate(stroke('*')) # Undo buffer ran out assert out.get() == 'S ' + BACK_STRING t.set_min_undo_length(3) out.clear() t.translate(stroke('S')) assert out.get() == 'S' t.translate(stroke('T')) assert out.get() == 'S T' t.translate(stroke('*')) assert out.get() == 'S' t.translate(stroke('*')) assert out.get() == '' out.clear() d[('S',)] = 't1' d[('T',)] = 't2' d[('S', 'T')] = 't3' t.translate(stroke('S')) assert out.get() == 't1' t.translate(stroke('T')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't3 t2' t.translate(stroke('S')) assert out.get() == 't3 t2 t1' t.translate(stroke('*')) assert out.get() == 't3 t2' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('*')) assert out.get() == 't1' t.translate(stroke('*')) assert out.get() == '' t.translate(stroke('S')) assert out.get() == 't1' t.translate(stroke('T')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't3 t2' d[('S', 'T', 'T')] = 't4' d[('S', 'T', 'T', 'S')] = 't5' t.translate(stroke('S')) assert out.get() == 't5' t.translate(stroke('*')) assert out.get() == 't3 t2' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't4' t.translate(stroke('S')) assert out.get() == 't5' t.translate(stroke('S')) assert out.get() == 't5 t1' t.translate(stroke('*')) assert out.get() == 't5' t.translate(stroke('*')) assert out.get() == 't4' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('*')) assert out.get() == 't1' t.translate(stroke('*')) assert out.get() == '' d.clear() s = stroke('S') t.translate(s) t.translate(s) t.translate(s) t.translate(s) s = stroke('*') t.translate(s) t.translate(s) t.translate(s) t.translate(s) # Not enough undo to clear output. assert out.get() == 'S ' + BACK_STRING out.clear() t.remove_listener(out.write) t.translate(stroke('S')) assert out.get() == ''
def test_changing_state(): output = [] def listener(undo, do, prev): prev = list(prev) if prev else None output.append((undo, do, prev)) d = StenoDictionary() d[('S', 'P')] = 'hi' dc = StenoDictionaryCollection([d]) t = Translator() t.set_dictionary(dc) t.translate(stroke('T')) t.translate(stroke('S')) s = copy.deepcopy(t.get_state()) t.add_listener(listener) expected = [([Translation([stroke('S')], None)], [Translation([stroke('S'), stroke('P')], 'hi')], [Translation([stroke('T')], None)])] t.translate(stroke('P')) assert output == expected del output[:] t.set_state(s) t.translate(stroke('P')) assert output == expected del output[:] t.clear_state() t.translate(stroke('P')) assert output == [([], [Translation([stroke('P')], None)], None)] del output[:] t.set_state(s) t.translate(stroke('P')) assert output == [([], [Translation([stroke('P')], None)], [Translation([stroke('S'), stroke('P')], 'hi')])]
def translate(self, steno): self.tlor.translate(stroke(steno))
def setup_method(self): self.a = Translation([stroke('S')], None) self.b = Translation([stroke('T'), stroke('-D')], None) self.c = Translation([stroke('-Z'), stroke('P'), stroke('T*')], None)
def setup_method(self): self.a = Translation([stroke('S')], None) self.b = Translation([stroke('T'), stroke('-D')], None) self.c = Translation([stroke('-Z'), stroke('P'), stroke('T*')], None)
def test_translation(): t = Translation([stroke('S'), stroke('T')], 'translation') assert t.strokes == [stroke('S'), stroke('T')] assert t.rtfcre == ('S', 'T') assert t.english == 'translation'
def test_no_translation(): t = Translation([stroke('S'), stroke('T')], None) assert t.strokes == [stroke('S'), stroke('T')] assert t.rtfcre == ('S', 'T') assert t.english is None
def test_translator(): # It's not clear that this test is needed anymore. There are separate # tests for _translate_stroke and test_translate_calls_translate_stroke # makes sure that translate calls it properly. But since I already wrote # this test I'm going to keep it. class Output: def __init__(self): self._output = [] def write(self, undo, do, prev): for t in undo: self._output.pop() for t in do: if t.english: self._output.append(t.english) else: self._output.append('/'.join(t.rtfcre)) def get(self): return ' '.join(self._output) def clear(self): del self._output[:] d = StenoDictionary() out = Output() t = Translator() dc = StenoDictionaryCollection([d]) t.set_dictionary(dc) t.add_listener(out.write) t.translate(stroke('S')) assert out.get() == 'S' t.translate(stroke('T')) assert out.get() == 'S T' t.translate(stroke('*')) assert out.get() == 'S' t.translate(stroke('*')) # Undo buffer ran out assert out.get() == 'S ' + BACK_STRING t.set_min_undo_length(3) out.clear() t.translate(stroke('S')) assert out.get() == 'S' t.translate(stroke('T')) assert out.get() == 'S T' t.translate(stroke('*')) assert out.get() == 'S' t.translate(stroke('*')) assert out.get() == '' out.clear() d[('S', )] = 't1' d[('T', )] = 't2' d[('S', 'T')] = 't3' t.translate(stroke('S')) assert out.get() == 't1' t.translate(stroke('T')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't3 t2' t.translate(stroke('S')) assert out.get() == 't3 t2 t1' t.translate(stroke('*')) assert out.get() == 't3 t2' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('*')) assert out.get() == 't1' t.translate(stroke('*')) assert out.get() == '' t.translate(stroke('S')) assert out.get() == 't1' t.translate(stroke('T')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't3 t2' d[('S', 'T', 'T')] = 't4' d[('S', 'T', 'T', 'S')] = 't5' t.translate(stroke('S')) assert out.get() == 't5' t.translate(stroke('*')) assert out.get() == 't3 t2' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('T')) assert out.get() == 't4' t.translate(stroke('S')) assert out.get() == 't5' t.translate(stroke('S')) assert out.get() == 't5 t1' t.translate(stroke('*')) assert out.get() == 't5' t.translate(stroke('*')) assert out.get() == 't4' t.translate(stroke('*')) assert out.get() == 't3' t.translate(stroke('*')) assert out.get() == 't1' t.translate(stroke('*')) assert out.get() == '' d.clear() s = stroke('S') t.translate(s) t.translate(s) t.translate(s) t.translate(s) s = stroke('*') t.translate(s) t.translate(s) t.translate(s) t.translate(s) # Not enough undo to clear output. assert out.get() == 'S ' + BACK_STRING out.clear() t.remove_listener(out.write) t.translate(stroke('S')) assert out.get() == ''
def test_changing_state(): output = [] def listener(undo, do, prev): prev = list(prev) if prev else None output.append((undo, do, prev)) d = StenoDictionary() d[('S', 'P')] = 'hi' dc = StenoDictionaryCollection([d]) t = Translator() t.set_dictionary(dc) t.translate(stroke('T')) t.translate(stroke('S')) s = copy.deepcopy(t.get_state()) t.add_listener(listener) expected = [([Translation([stroke('S')], None)], [Translation([stroke('S'), stroke('P')], 'hi')], [Translation([stroke('T')], None)])] t.translate(stroke('P')) assert output == expected del output[:] t.set_state(s) t.translate(stroke('P')) assert output == expected del output[:] t.clear_state() t.translate(stroke('P')) assert output == [([], [Translation([stroke('P')], None)], None)] del output[:] t.set_state(s) t.translate(stroke('P')) assert output == [([], [Translation([stroke('P')], None)], [Translation([stroke('S'), stroke('P')], 'hi')])]
def test_no_translation(): t = Translation([stroke('S'), stroke('T')], None) assert t.strokes == [stroke('S'), stroke('T')] assert t.rtfcre == ('S', 'T') assert t.english is None
def t(self, strokes): """A quick way to make a translation.""" strokes = [stroke(x) for x in strokes.split('/')] key = tuple(s.rtfcre for s in strokes) translation = self.dc.lookup(key) return Translation(strokes, translation)
def test_translation(): t = Translation([stroke('S'), stroke('T')], 'translation') assert t.strokes == [stroke('S'), stroke('T')] assert t.rtfcre == ('S', 'T') assert t.english == 'translation'
def translate(self, steno): self.tlor.translate(stroke(steno))
def t(self, strokes): """A quick way to make a translation.""" strokes = [stroke(x) for x in strokes.split('/')] key = tuple(s.rtfcre for s in strokes) translation = self.dc.lookup(key) return Translation(strokes, translation)
def test_translation_calls_restrict(self): self.t.translate(stroke('S')) self._check_size_call(0)
def test_translation_calls_restrict(self): self.t.translate(stroke('S')) self._check_size_call(0)
def test_dictionary_update_grows_size(self, key): self.d[key] = 'key' self.t.translate(stroke('T-')) self._check_size_call(len(key))