summaryrefslogtreecommitdiff
path: root/tests/unit/keyinput/test_bindingtrie.py
blob: 388be0f4201303d3c62d82c8c2c876145cedbf51 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2019-2021 Jay Kamat <jaygkamat@gmail.com>:
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""Tests for the BindingTrie."""

import string
import itertools
import textwrap

import pytest

from qutebrowser.qt import QtGui

from qutebrowser.keyinput import basekeyparser
from qutebrowser.keyinput import keyutils
from unit.keyinput import test_keyutils


@pytest.mark.parametrize('entered, configured, match_type',
                         test_keyutils.TestKeySequence.MATCH_TESTS)
def test_matches_single(entered, configured, match_type):
    entered = keyutils.KeySequence.parse(entered)
    configured = keyutils.KeySequence.parse(configured)
    trie = basekeyparser.BindingTrie()
    trie[configured] = "eeloo"
    command = "eeloo" if match_type == QtGui.QKeySequence.ExactMatch else None
    result = basekeyparser.MatchResult(match_type=match_type,
                                       command=command,
                                       sequence=entered)
    assert trie.matches(entered) == result


def test_str():
    bindings = {
        keyutils.KeySequence.parse('a'): 'cmd-a',
        keyutils.KeySequence.parse('ba'): 'cmd-ba',
        keyutils.KeySequence.parse('bb'): 'cmd-bb',
        keyutils.KeySequence.parse('cax'): 'cmd-cax',
        keyutils.KeySequence.parse('cby'): 'cmd-cby',
    }
    trie = basekeyparser.BindingTrie()
    trie.update(bindings)

    expected = """
        a:
          => cmd-a

        b:
          a:
            => cmd-ba
          b:
            => cmd-bb

        c:
          a:
            x:
              => cmd-cax
          b:
            y:
              => cmd-cby
    """

    assert str(trie) == textwrap.dedent(expected).lstrip('\n')


@pytest.mark.parametrize(
    'configured, expected',
    [
        (
            [],
            # null match
            [('a', QtGui.QKeySequence.NoMatch), ('', QtGui.QKeySequence.NoMatch)],
        ),
        (
            ['abcd'],
            [
                ('abcd', QtGui.QKeySequence.ExactMatch),
                ('abc', QtGui.QKeySequence.PartialMatch),
            ],
        ),
        (
            ['aa', 'ab', 'ac', 'ad'],
            [
                ('ac', QtGui.QKeySequence.ExactMatch),
                ('a', QtGui.QKeySequence.PartialMatch),
                ('f', QtGui.QKeySequence.NoMatch),
                ('acd', QtGui.QKeySequence.NoMatch),
            ],
        ),
        (
            ['aaaaaaab', 'aaaaaaac', 'aaaaaaad'],
            [
                ('aaaaaaab', QtGui.QKeySequence.ExactMatch),
                ('z', QtGui.QKeySequence.NoMatch),
            ],
        ),
        (
            string.ascii_letters,
            [('a', QtGui.QKeySequence.ExactMatch), ('!', QtGui.QKeySequence.NoMatch)],
        ),
    ],
)
def test_matches_tree(configured, expected, benchmark):
    trie = basekeyparser.BindingTrie()
    trie.update({keyutils.KeySequence.parse(keys): "eeloo"
                 for keys in configured})

    def run():
        for entered, match_type in expected:
            sequence = keyutils.KeySequence.parse(entered)
            command = "eeloo" if match_type == QtGui.QKeySequence.ExactMatch else None
            result = basekeyparser.MatchResult(
                match_type=match_type, command=command, sequence=sequence
            )
            assert trie.matches(sequence) == result

    benchmark(run)


@pytest.mark.parametrize('configured', [
    ['a'],
    itertools.permutations('asdfghjkl', 3)
])
def test_bench_create(configured, benchmark):
    bindings = {keyutils.KeySequence.parse(keys): "dres"
                for keys in configured}

    def run():
        trie = basekeyparser.BindingTrie()
        trie.update(bindings)

    benchmark(run)