From 969d1d393e75a229523c234203059fb570d28ed1 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 17 Sep 2019 18:31:24 +0200 Subject: Initial import --- lulua/test_writer.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 lulua/test_writer.py (limited to 'lulua/test_writer.py') diff --git a/lulua/test_writer.py b/lulua/test_writer.py new file mode 100644 index 0000000..bc02a7e --- /dev/null +++ b/lulua/test_writer.py @@ -0,0 +1,118 @@ +# Copyright (c) 2019 lulua contributors +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from io import StringIO + +import pytest + +from .writer import Writer, SkipEvent +from .layout import * +from .keyboard import defaultKeyboards + +def toButtonComb (keyboard, data): + lookupButton = lambda x: keyboard.find (x) + return ButtonCombination (*map (lambda y: frozenset (lookupButton (z) for z in y), data)) + +def test_writer (): + keyboard = defaultKeyboards['ibmpc105'] + layout = defaultLayouts['ar-linux'].specialize (keyboard) + w = Writer (layout) + + f = w[LEFT][RING] + assert f.number == RING + assert f.hand.position == LEFT + +typeData = [ + ('شسضص', [ + ('ش', (tuple (), ('Dl1', ))), + ('س', (tuple (), ('Dl2', ))), + ('ض', (tuple (), ('Cl1', ))), + ('ص', (tuple (), ('Cl2', ))), + ]), + ('aصb', [ + (None, SkipEvent ('a')), + ('ص', (tuple (), ('Cl2', ))), + (None, SkipEvent ('b')), + ]), + ] + +@pytest.mark.parametrize("s, expect", typeData) +def test_writer_type (s, expect): + keyboard = defaultKeyboards['ibmpc105'] + layout = defaultLayouts['ar-linux'].specialize (keyboard) + w = Writer (layout) + + data = StringIO (s) + result = list (w.type (data)) + + newExpect = [] + for char, comb in expect: + if isinstance (comb, SkipEvent): + newExpect.append ((char, comb)) + else: + newExpect.append ((char, toButtonComb (keyboard, comb))) + expect = newExpect + assert result == expect + +testCombs = [ + ([ + (('El_shift', ), ('Dr7', )), + (('Er_shift', ), ('Dr7', )), + ], 0, None + ), ([ + (('El_shift', ), ('Dl5', )), + (('Er_shift', ), ('Dl5', )), + ], 1, None + ), ([ + (tuple (), ('Fl_space', )), + (tuple (), ('Fr_space', )), + ], 0, (tuple (), ('Dr7', )) + ), ([ + (tuple (), ('Fl_space', )), + (tuple (), ('Fr_space', )), + ], 1, (tuple (), ('Dl5', )) + ), ([ + (tuple (), ('Fl_space', )), + (tuple (), ('Fr_space', )), + ], 0, (('El_shift', ), ('Dr7', )) + ), ([ + (tuple (), ('Fl_space', )), + (tuple (), ('Fr_space', )), + ], 0, (('Er_shift', ), ('Dl5', )) + ), ([ + # choose the shortest combination if there’s two available + (tuple (), ('CD_ret', )), + (('Er_shift', ), ('CD_ret', )), + (('El_shift', ), ('CD_ret', )), + ], 0, None), + ] + +@pytest.mark.parametrize("combs, expect, prev", testCombs) +def test_writer_chooseComb (combs, expect, prev): + keyboard = defaultKeyboards['ibmpc105'] + layout = defaultLayouts['ar-linux'].specialize (keyboard) + w = Writer (layout) + + if prev: + prev = toButtonComb (keyboard, prev) + w.press (prev) + combs = [toButtonComb (keyboard, x) for x in combs] + assert w.chooseCombination (combs) == combs[expect] + -- cgit v1.2.3