# coding=utf-8 # Copyright (C) 2009, 2010 Roman Zimbelmann # # This program 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. # # This program 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 this program. If not, see . if __name__ == '__main__': from __init__ import init; init() from unittest import TestCase, main from test import TODO from ranger.ext.tree import Tree from ranger.container.keymap import * from ranger.container.keybuffer import KeyBuffer from ranger.ext.keybinding_parser import parse_keybinding import sys def simulate_press(self, string): for char in parse_keybinding(string): self.add(char) if self.done: return self.command if self.failure: break return self.command class PressTestCase(TestCase): """Some useful methods for the actual test""" def _mkpress(self, keybuffer, _=0): def press(keys): keybuffer.clear() match = simulate_press(keybuffer, keys) self.assertFalse(keybuffer.failure, "parsing keys '"+keys+"' did fail!") self.assertTrue(keybuffer.done, "parsing keys '"+keys+"' did not complete!") arg = CommandArgs(None, None, keybuffer) self.assert_(match.function, "No function found! " + \ str(match.__dict__)) return match.function(arg) return press def assertPressFails(self, kb, keys): kb.clear() simulate_press(kb, keys) self.assertTrue(kb.failure, "Keypress did not fail as expected") kb.clear() def assertPressIncomplete(self, kb, keys): kb.clear() simulate_press(kb, keys) self.assertFalse(kb.failure, "Keypress failed, expected incomplete") self.assertFalse(kb.done, "Keypress done which was unexpected") kb.clear() class Test(PressTestCase): """The test cases""" def test_passive_action(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) def n(value): """return n or value""" def fnc(arg=None): if arg is None or arg.n is None: return value return arg.n return fnc km.map('ppp', n(5)) km.map('pp', n(8)) km.map('pp', n(2)) directions.map('j', dir=Direction(down=1)) press = self._mkpress(kb, km) self.assertEqual(5, press('ppp')) self.assertEqual(3, press('3ppp')) self.assertEqual(2, press('ppj')) kb.clear() match = simulate_press(kb, 'pp') args = CommandArgs(0, 0, kb) self.assert_(match) self.assert_(match.function) self.assertEqual(8, match.function(args)) def test_translate_keys(self): def test(string, *args): if not args: args = (string, ) self.assertEqual(ordtuple(*args), tuple(parse_keybinding(string))) def ordtuple(*args): lst = [] for arg in args: if isinstance(arg, str): lst.extend(ord(c) for c in arg) else: lst.append(arg) return tuple(lst) # 1 argument means: assume nothing is translated. test('k') test('kj') test('k', 'k', DIRKEY) test('kz', 'k', ANYKEY, 'z', ANYKEY) test('kz', 'k', ANYKEY, 'z', DIRKEY) test('', "\n") test('', "\t\t\n") test('<') test('>') test('', 1) test('', 2) for i in range(1, 26): test('', i) test('', 27, ord('x')) test('', 27, ord('o')) test('k') test('k') test('k') test('knz>') test('>nz>') def test_alias(self): def add_dirs(arg): return sum(dir.down() for dir in arg.directions) def return5(_): return 5 directions = KeyMap() directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) directions.map('', alias='j') directions.map('@', alias='') base = KeyMap() base.map('a', add_dirs) base.map('b', add_dirs) base.map('xx', add_dirs) base.map('f', return5) base.map('yy', alias='y') base.map('!', alias='!') other = KeyMap() other.map('bb
; reads lines, prints them back when you hit 'enter'
; dies if you wait a while, because so far we never free memory
(function main [
  (default-space:space-address <- new space:literal 30:literal)
  (cursor-mode) ;? 1
  ; hook up stdin
  (stdin:channel-address <- init-channel 1:literal)
  (fork-helper send-keys-to-stdin:fn nil:literal/globals nil:literal/limit nil:literal/keyboard stdin:channel-address)
  ; buffer stdin
  (buffered-stdin:channel-address <- init-channel 1:literal)
  (fork-helper buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
  { begin
    ; now read characters from the buffer until 'enter' is typed
    (s:string-address <- new "? ")