Skip to content

Commit

Permalink
test(XKB): Add extra mapping test
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Apr 8, 2024
1 parent 4fa8b2f commit c866103
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
54 changes: 42 additions & 12 deletions tests/test_serializer_xkb.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
from textwrap import dedent
from typing import Dict

from kalamine import KeyboardLayout
from kalamine.generators.xkb import xkb_table

from .util import get_layout_dict


def load_layout(filename: str) -> KeyboardLayout:
return KeyboardLayout(get_layout_dict(filename))
def load_layout(
filename: str, extraMapping: Dict[str, Dict[str, str]]
) -> KeyboardLayout:
return KeyboardLayout(get_layout_dict(filename, extraMapping))


def split(multiline_str: str):
return dedent(multiline_str).lstrip().rstrip().splitlines()


def test_ansi():
layout = load_layout("ansi")
layout = load_layout("ansi", {})

expected = split(
"""
Expand Down Expand Up @@ -91,8 +94,6 @@ def test_ansi():


def test_intl():
layout = load_layout("intl")

expected = split(
"""
// Digits
Expand Down Expand Up @@ -158,17 +159,46 @@ def test_intl():
"""
)

xkbcomp = xkb_table(layout, xkbcomp=True)
assert len(xkbcomp) == len(expected)
assert xkbcomp == expected
extraMapping = {
# NOTE: redefine level
"ae01": {"shift": "?"},
# NOTE: test case variants and ODK alias
"menu": {"base": "a", "sHiFt": "A", "1dk": "æ", "ODk_shiFt": "Æ"},
# NOTE: clone level
"esc": {"base": "(ae11)"},
# NOTE: clone key
"i172": "(lsgt)",
}

extraSymbols = [
"",
"// System",
"key <ESC> {[ minus , VoidSymbol , VoidSymbol , VoidSymbol ]}; // -",
"key <MENU> {[ a , A , ae , AE ]}; // a A æ Æ",
"",
"// Miscellaneous",
"key <I172> {[ backslash , bar , VoidSymbol , VoidSymbol ]}; // \\ |",
]

extraExpected = expected + extraSymbols
extraExpected[1] = (
"key <AE01> {[ 1 , question , VoidSymbol , VoidSymbol ]}; // 1 ?"
)

xkbpatch = xkb_table(layout, xkbcomp=False)
assert len(xkbpatch) == len(expected)
assert xkbpatch == expected
for mapping, expectedʹ in (({}, expected), (extraMapping, extraExpected)):
layout = load_layout("intl", mapping)

xkbcomp = xkb_table(layout, xkbcomp=True)
assert len(xkbcomp) == len(expectedʹ)
assert xkbcomp == expectedʹ

xkbpatch = xkb_table(layout, xkbcomp=False)
assert len(xkbpatch) == len(expectedʹ)
assert xkbpatch == expectedʹ


def test_prog():
layout = load_layout("prog")
layout = load_layout("prog", {})

expected = split(
"""
Expand Down
11 changes: 8 additions & 3 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Some util functions for tests."""

from pathlib import Path
from typing import Dict
from typing import Dict, Optional

import tomli


def get_layout_dict(filename: str) -> Dict:
def get_layout_dict(
filename: str, extraMapping: Optional[Dict[str, Dict[str, str]]] = None
) -> Dict:
"""Return the layout directory path."""

file_path = Path(__file__).parent.parent / f"layouts/{filename}.toml"
with file_path.open(mode="rb") as file:
return tomli.load(file)
layout = tomli.load(file)
if extraMapping:
layout.update({"mapping": extraMapping})
return layout

0 comments on commit c866103

Please sign in to comment.