summaryrefslogtreecommitdiff
path: root/lulua/test_writer.py
blob: bc02a7e87be17b582a182bbe5d536bbe303f1613 (plain)
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
# 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]