Example #1
0
    def test_dictionary_collection(self):
        dc = StenoDictionaryCollection()
        d1 = StenoDictionary()
        d1[('S', )] = 'a'
        d1[('T', )] = 'b'
        d2 = StenoDictionary()
        d2[('S', )] = 'c'
        d2[('W', )] = 'd'
        dc.set_dicts([d1, d2])
        self.assertEqual(dc.lookup(('S', )), 'c')
        self.assertEqual(dc.lookup(('W', )), 'd')
        self.assertEqual(dc.lookup(('T', )), 'b')
        f = lambda k, v: v == 'c'
        dc.add_filter(f)
        self.assertIsNone(dc.lookup(('S', )))
        self.assertEqual(dc.raw_lookup(('S', )), 'c')
        self.assertEqual(dc.lookup(('W', )), 'd')
        self.assertEqual(dc.lookup(('T', )), 'b')
        self.assertEqual(dc.reverse_lookup('c'), [('S', )])

        dc.remove_filter(f)
        self.assertEqual(dc.lookup(('S', )), 'c')
        self.assertEqual(dc.lookup(('W', )), 'd')
        self.assertEqual(dc.lookup(('T', )), 'b')

        self.assertEqual(dc.reverse_lookup('c'), [('S', )])

        dc.set(('S', ), 'e')
        self.assertEqual(dc.lookup(('S', )), 'e')
        self.assertEqual(d2[('S', )], 'e')
Example #2
0
 def test_translation(self):
     d = StenoDictionary()
     d[('S', 'T')] = 'translation'
     t = Translation([Stroke('S'), Stroke('T')], d)
     self.assertEqual(t.strokes, [Stroke('S'), Stroke('T')])
     self.assertEqual(t.rtfcre, ('S', 'T'))
     self.assertEqual(t.english, 'translation')
 def setUp(self):
     self.t = Translator()
     self.s = type(self).FakeState()
     self.t._state = self.s
     self.d = StenoDictionary()
     self.dc = StenoDictionaryCollection()
     self.dc.set_dicts([self.d])
     self.t.set_dictionary(self.dc)
Example #4
0
    def test_dictionary(self):
        notifications = []

        def listener(longest_key):
            notifications.append(longest_key)

        d = StenoDictionary()
        self.assertEqual(d.longest_key, 0)

        d.add_longest_key_listener(listener)
        d[('S', )] = 'a'
        self.assertEqual(d.longest_key, 1)
        self.assertEqual(notifications, [1])
        d[('S', 'S', 'S', 'S')] = 'b'
        self.assertEqual(d.longest_key, 4)
        self.assertEqual(notifications, [1, 4])
        d[('S', 'S')] = 'c'
        self.assertEqual(d.longest_key, 4)
        self.assertEqual(d[('S', 'S')], 'c')
        self.assertEqual(notifications, [1, 4])
        del d[('S', 'S', 'S', 'S')]
        self.assertEqual(d.longest_key, 2)
        self.assertEqual(notifications, [1, 4, 2])
        del d[('S', )]
        self.assertEqual(d.longest_key, 2)
        self.assertEqual(notifications, [1, 4, 2])
        d.clear()
        self.assertEqual(d.longest_key, 0)
        self.assertEqual(notifications, [1, 4, 2, 0])

        d.remove_longest_key_listener(listener)
        d[('S', 'S')] = 'c'
        self.assertEqual(d.longest_key, 2)
        self.assertEqual(notifications, [1, 4, 2, 0])

        self.assertEqual(StenoDictionary([('a', 'b')]).items(), [('a', 'b')])
        self.assertEqual(StenoDictionary(a='b').items(), [('a', 'b')])
Example #5
0
    def test_listeners(self):
        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], StenoDictionary())
        expected_output = [([], [tr], tr)]

        t.translate(s)

        t.add_listener(listener1)
        t.translate(s)
        self.assertEqual(output1, expected_output)

        del output1[:]
        t.add_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, expected_output)
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.add_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, expected_output)
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.remove_listener(listener1)
        t.translate(s)
        self.assertEqual(output1, [])
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.remove_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, [])
        self.assertEqual(output2, [])
Example #6
0
    def test_changing_state(self):
        output = []

        def listener(undo, do, prev):
            output.append((undo, do, prev))

        d = StenoDictionary()
        d[('S', 'P')] = 'hi'
        dc = StenoDictionaryCollection()
        dc.set_dicts([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'))
        self.assertEqual(output, expected)

        del output[:]
        t.set_state(s)
        t.translate(stroke('P'))
        self.assertEqual(output, expected)

        del output[:]
        t.clear_state()
        t.translate(stroke('P'))
        self.assertEqual(output,
                         [([], [Translation([stroke('P')], None)], None)])

        del output[:]
        t.set_state(s)
        t.translate(stroke('P'))
        self.assertEqual(output,
                         [([], [Translation([stroke('P')], None)],
                           Translation([stroke('S'), stroke('P')], 'hi'))])
Example #7
0
 def test_dictionary(self):
     notifications = []
     def listener(longest_key):
         notifications.append(longest_key)
     
     d = StenoDictionary()
     self.assertEqual(d.longest_key, 0)
     
     d.add_longest_key_listener(listener)
     d[('S',)] = 'a'
     self.assertEqual(d.longest_key, 1)
     self.assertEqual(notifications, [1])
     d[('S', 'S', 'S', 'S')] = 'b'
     self.assertEqual(d.longest_key, 4)
     self.assertEqual(notifications, [1, 4])
     d[('S', 'S')] = 'c'
     self.assertEqual(d.longest_key, 4)
     self.assertEqual(d[('S', 'S')], 'c')
     self.assertEqual(notifications, [1, 4])
     del d[('S', 'S', 'S', 'S')]
     self.assertEqual(d.longest_key, 2)
     self.assertEqual(notifications, [1, 4, 2])
     del d[('S',)]
     self.assertEqual(d.longest_key, 2)
     self.assertEqual(notifications, [1, 4, 2])
     d.clear()
     self.assertEqual(d.longest_key, 0)
     self.assertEqual(notifications, [1, 4, 2, 0])
     
     d.remove_longest_key_listener(listener)
     d[('S', 'S')] = 'c'
     self.assertEqual(d.longest_key, 2)
     self.assertEqual(notifications, [1, 4, 2, 0])
     
     self.assertEqual(StenoDictionary([('a', 'b')]).items(), [('a', 'b')])
     self.assertEqual(StenoDictionary(a='b').items(), [('a', 'b')])
Example #8
0
 def setUp(self):
     self.t = Translator()
     self.s = type(self).FakeState()
     self.t._state = self.s
     self.d = StenoDictionary()
     self.t.set_dictionary(self.d)
Example #9
0
 def setUp(self):
     self.d = StenoDictionary()
     self.s = _State()
     self.o = type(self).CaptureOutput()
Example #10
0
 def setUp(self):
     d = StenoDictionary()
     self.a = Translation([Stroke('S')], d)
     self.b = Translation([Stroke('T'), Stroke('-D')], d)
     self.c = Translation([Stroke('-Z'), Stroke('P'), Stroke('T*')], d)
Example #11
0
 def test_no_translation(self):
     d = StenoDictionary()
     t = Translation([Stroke('S'), Stroke('T')], d)
     self.assertEqual(t.strokes, [Stroke('S'), Stroke('T')])
     self.assertEqual(t.rtfcre, ('S', 'T'))
     self.assertIsNone(t.english)
Example #12
0
    def test_translator(self):

        # 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(object):
            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()
        t.set_dictionary(d)
        t.add_listener(out.write)

        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 'S')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 'S T')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 'S')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 'S')  # Undo buffer ran out.

        t.set_min_undo_length(3)
        out.clear()
        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 'S')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 'S T')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 'S')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), '')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), '')  # Undo buffer ran out.

        out.clear()
        d[('S', )] = 't1'
        d[('T', )] = 't2'
        d[('S', 'T')] = 't3'

        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't1')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 't3')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't3 t2 t1')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't3')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't1')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), '')

        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't1')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 't3')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 't3 t2')

        d[('S', 'T', 'T')] = 't4'
        d[('S', 'T', 'T', 'S')] = 't5'

        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't5')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't3')
        t.translate(Stroke('T'))
        self.assertEqual(out.get(), 't4')
        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't5')
        t.translate(Stroke('S'))
        self.assertEqual(out.get(), 't5 t1')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't5')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't4')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't3')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 't1')
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), '')

        d.clear()

        t.translate(Stroke('S'))
        t.translate(Stroke('S'))
        t.translate(Stroke('S'))
        t.translate(Stroke('S'))
        t.translate(Stroke('*', True))
        t.translate(Stroke('*', True))
        t.translate(Stroke('*', True))
        t.translate(Stroke('*', True))
        self.assertEqual(out.get(), 'S')  # Not enough undo to clear output.

        out.clear()
        t.remove_listener(out.write)
        t.translate(Stroke('S'))
        self.assertEqual(out.get(), '')
Example #13
0
    def test_translator(self):

        # 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(object):
            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()
        dc.set_dicts([d])
        t.set_dictionary(dc)
        t.add_listener(out.write)
        
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 'S')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 'S T')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 'S')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 'S')  # Undo buffer ran out.
        
        t.set_min_undo_length(3)
        out.clear()
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 'S')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 'S T')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 'S')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), '')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), '')  # Undo buffer ran out.
        
        out.clear()
        d[('S',)] = 't1'
        d[('T',)] = 't2'
        d[('S', 'T')] = 't3'
        
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't1')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 't3')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't3 t2 t1')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't3')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't1')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), '')
        
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't1')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 't3')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 't3 t2')

        d[('S', 'T', 'T')] = 't4'
        d[('S', 'T', 'T', 'S')] = 't5'
        
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't5')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't3 t2')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't3')
        t.translate(stroke('T'))
        self.assertEqual(out.get(), 't4')
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't5')
        t.translate(stroke('S'))
        self.assertEqual(out.get(), 't5 t1')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't5')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't4')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't3')
        t.translate(stroke('*'))
        self.assertEqual(out.get(), 't1')
        t.translate(stroke('*'))
        self.assertEqual(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)
        self.assertEqual(out.get(), 'S')  # Not enough undo to clear output.
        
        out.clear()
        t.remove_listener(out.write)
        t.translate(stroke('S'))
        self.assertEqual(out.get(), '')
 def setUp(self):
     self.d = StenoDictionary()
     self.dc = StenoDictionaryCollection()
     self.dc.set_dicts([self.d])
     self.s = _State()
     self.o = type(self).CaptureOutput()
Example #15
0
 def __init__(self):
     self._undo_length = 0
     self._dictionary = None
     self.set_dictionary(StenoDictionary())
     self._listeners = set()
     self._state = _State()