Beispiel #1
0
def test_objects_keymap():

    bindings = {
        'refresh': ['a', 0x12, '<LF>', '<KEY_UP>'],
        'exit': [],
        Command('UPVOTE'): ['b', '<KEY_F5>']
    }

    keymap = KeyMap(bindings)
    assert keymap.get(Command('REFRESH')) == ['a', 0x12, '<LF>', '<KEY_UP>']
    assert keymap.get(Command('exit')) == []
    assert keymap.get('upvote') == ['b', '<KEY_F5>']
    with pytest.raises(exceptions.ConfigError) as e:
        keymap.get('downvote')
    assert 'DOWNVOTE' in six.text_type(e)

    # Updating the bindings wipes out the old ones
    bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
    keymap.set_bindings(bindings)
    assert keymap.get('refresh')
    with pytest.raises(exceptions.ConfigError) as e:
        keymap.get('upvote')
    assert 'UPVOTE' in six.text_type(e)

    # Strings should be parsed correctly into keys
    assert KeyMap.parse('a') == 97
    assert KeyMap.parse(0x12) == 18
    assert KeyMap.parse('<LF>') == 10
    assert KeyMap.parse('<KEY_UP>') == 259
    assert KeyMap.parse('<KEY_F5>') == 269
    for key in ('', None, '<lf>', '<DNS>', '<KEY_UD>', '♬'):
        with pytest.raises(exceptions.ConfigError) as e:
            keymap.parse(key)
        assert six.text_type(key) in six.text_type(e)
Beispiel #2
0
def test_objects_controller_command():
    class ControllerA(Controller):
        character_map = {}

    class ControllerB(ControllerA):
        character_map = {}

    @ControllerA.register(Command('REFRESH'))
    def call_page(_):
        return 'a1'

    @ControllerA.register(Command('UPVOTE'))
    def call_page(_):
        return 'a2'

    @ControllerB.register(Command('REFRESH'))
    def call_page(_):
        return 'b1'

    # Two commands aren't allowed to share keys
    keymap = KeyMap({'REFRESH': [0x10, 0x11], 'UPVOTE': [0x11, 0x12]})
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerA(None, keymap=keymap)
    assert 'ControllerA' in six.text_type(e)

    # Reset the character map
    ControllerA.character_map = {Command('REFRESH'): 0, Command('UPVOTE'): 0}

    # All commands must be defined in the keymap
    keymap = KeyMap({'REFRESH': [0x10]})
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerB(None, keymap=keymap)
    assert 'UPVOTE' in six.text_type(e)
Beispiel #3
0
def test_objects_controller_double_press():

    class ControllerA(Controller):
        character_map = {}

    @ControllerA.register(Command('F1'))
    def call_page(_):
        return '1'

    @ControllerA.register(Command('F2'))
    def call_page(_):
        return '2'

    keymap = KeyMap({'F1': ['gg'], 'F2': ['a']})
    controller_a = ControllerA(None, keymap=keymap)

    # Double press
    controller_a.last_char = None
    assert controller_a.trigger('g') is None
    assert controller_a.trigger('g') == '1'
    assert controller_a.trigger('g') is None

    # Executing another command cancels the chain
    controller_a.last_char = None
    assert controller_a.trigger('g') is None
    assert controller_a.trigger('a') == '2'
    assert controller_a.trigger('g') is None

    # Pressing an invalid key cancels the chain
    controller_a.last_char = None
    assert controller_a.trigger('g') is None
    assert controller_a.trigger('b') is None
    assert controller_a.trigger('g') is None
Beispiel #4
0
def test_objects_command():

    c1 = Command("REFRESH")
    c2 = Command("refresh")
    c3 = Command("EXIT")

    assert c1 == c2
    assert c1 != c3

    keymap = {c1: None, c2: None, c3: None}
    assert len(keymap) == 2
    assert c1 in keymap
    assert c2 in keymap
    assert c3 in keymap
Beispiel #5
0
def test_objects_keymap():

    bindings = {
        'refresh': ['a', 0x12, '<LF>', '<KEY_UP>'],
        'exit': [],
        Command('UPVOTE'): ['b', '<KEY_F5>'],
        Command('PAGE_TOP'): ['gg']
    }

    keymap = KeyMap(bindings)
    assert keymap.get(Command('REFRESH')) == ['a', 0x12, '<LF>', '<KEY_UP>']
    assert keymap.get(Command('exit')) == []
    assert keymap.get(Command('PAGE_TOP')) == ['gg']
    assert keymap.get('upvote') == ['b', '<KEY_F5>']
    with pytest.raises(exceptions.ConfigError) as e:
        keymap.get('downvote')
    assert 'DOWNVOTE' in six.text_type(e)

    bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
    bindings2 = {'upvote': ['b', 0x13, '<KEY_DOWN>']}
    keymap._keymap = {}
    keymap.set_bindings(bindings)
    assert keymap.get('refresh')
    with pytest.raises(exceptions.ConfigError) as e:
        keymap.get('upvote')
    assert 'UPVOTE' in six.text_type(e)
    keymap.set_bindings(bindings2)
    assert keymap.get('refresh')
    assert keymap.get('upvote')

    # Strings should be parsed correctly into keys
    assert KeyMap.parse('a') == 97
    assert KeyMap.parse(0x12) == 18
    assert KeyMap.parse('<LF>') == 10
    assert KeyMap.parse('<KEY_UP>') == 259
    assert KeyMap.parse('<KEY_F5>') == 269
    assert KeyMap.parse('gg') == (103, 103)
    for key in ('', None, '<lf>', '<DNS>', '<KEY_UD>', '♬', 'ggg'):
        with pytest.raises(exceptions.ConfigError) as e:
            keymap.parse(key)
        assert six.text_type(key) in six.text_type(e)
Beispiel #6
0
def test_objects_controller_command():

    class ControllerA(Controller):
        character_map = {}

    class ControllerB(ControllerA):
        character_map = {}

    @ControllerA.register(Command('REFRESH'))
    def call_page(_):
        return 'a1'

    @ControllerA.register(Command('UPVOTE'))
    def call_page(_):
        return 'a2'

    @ControllerB.register(Command('REFRESH'))
    def call_page(_):
        return 'b1'

    # Two commands aren't allowed to share keys
    keymap = KeyMap({'REFRESH': [0x10, 0x11], 'UPVOTE': [0x11, 0x12]})
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerA(None, keymap=keymap)
    assert 'ControllerA' in six.text_type(e)

    # Reset the character map
    ControllerA.character_map = {Command('REFRESH'): 0, Command('UPVOTE'): 0}

    # A double command can't share the first key with a single comand
    keymap = KeyMap(OrderedDict([('REFRESH', ['gg']), ('UPVOTE', ['g'])]))
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerA(None, keymap=keymap)
    assert 'ControllerA' in six.text_type(e)

    # It doesn't matter which order they were entered
    keymap = KeyMap(OrderedDict([('UPVOTE', ['g']), ('REFRESH', ['gg'])]))
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerA(None, keymap=keymap)
    assert 'ControllerA' in six.text_type(e)

    # Reset the character map
    ControllerA.character_map = {Command('REFRESH'): 0, Command('UPVOTE'): 0}

    # All commands must be defined in the keymap
    keymap = KeyMap({'REFRESH': [0x10]})
    with pytest.raises(exceptions.ConfigError) as e:
        ControllerB(None, keymap=keymap)
    assert 'UPVOTE' in six.text_type(e)