def test_CommandBar_get_history_concurrently(): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) editor = type("FakeEditor", (object, ), {})() commander = TextCommandController(history) bar1 = mod.CommandBar(editor, commander) bar2 = mod.CommandBar(editor, commander) bar3 = mod.CommandBar(editor, commander) bar4 = mod.CommandBar(editor, commander) eq_(bar1.get_history("x"), "a") eq_(bar2.get_history(""), "a") eq_(bar2.get_history("y"), "b") eq_(bar3.get_history(""), "a") eq_(bar3.get_history("a"), "b") eq_(bar3.get_history("z"), "c") # <-- "z" will move to 0 (with "b") history.append("b") # current index "a", "x" in new command buffer eq_(bar1.get_history("a"), "c") eq_(bar1.get_history("c"), None) eq_(bar1.get_history("c", True), "a") eq_(bar1.get_history("a", True), "b") eq_(bar1.get_history("b", True), "x") eq_(bar1.get_history("x", True), None) # current index "b", "y" at 0 eq_(bar2.get_history("B"), "y") # <-- "B" now at 0 eq_(bar2.get_history("y"), "c") eq_(bar2.get_history("c"), None) eq_(bar2.get_history("c", True), "y") eq_(bar2.get_history("y", True), "B") eq_(bar2.get_history("B", True), "") eq_(bar2.get_history("", True), None) # current index "c", "z" at 1 eq_(bar3.get_history("c"), None) eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a"), "C") eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a", True), "z") eq_(bar3.get_history("z", True), "") # <-- "z" moved to 0 eq_(bar3.get_history("", True), None) eq_(bar4.get_history("A", True), None) eq_(bar4.get_history("A"), "b") eq_(bar4.get_history("b"), "a") eq_(bar4.get_history("a"), "c") eq_(bar4.get_history("c"), None) eq_(bar4.get_history("c", True), "a") eq_(bar4.get_history("a", True), "b") eq_(bar4.get_history("b", True), "A") eq_(bar4.get_history("A", True), None)
def test_CommandBar_reset(): with tempdir() as tmp: editor = type("Editor", (object, ), {})() history = mod.CommandHistory(tmp) commander = mod.TextCommandController(history) bar = mod.CommandBar(editor, commander) eq_(bar.get_history(""), None) assert bar.history_view is not None assert bar.history_view in history.views bar.reset() eq_(bar.history_view, None) assert bar.history_view not in history.views
def test_CommandBar_editor(): editor = type('Editor', (object, ), {})() text_commander = type('TextCommandController', (object, ), {})() cmd = mod.CommandBar(editor, text_commander) eq_(cmd.editor, editor) eq_(cmd.text_commander, text_commander) # NOTE the following depends on CPython weakref behavior del editor, text_commander with assert_raises(AttributeError): cmd.editor with assert_raises(AttributeError): cmd.text_commander
def test(nav): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) editor = type("FakeEditor", (object, ), {})() commander = TextCommandController(history) bar = mod.CommandBar(editor, commander) for input, direction, history in nav: dirchar = "v" if direction else "A" print("{}({!r}, {!r})".format(dirchar, input, history)) eq_(bar.get_history(input, forward=direction), history)
def test(c): m = Mocker() editor = m.mock() beep = m.replace(ak, 'NSBeep') commander = m.mock(TextCommandController) bar = mod.CommandBar(editor, commander) message = m.replace(bar, "message") args = c.text.split() if args and not c.current: editor.current_view >> None beep() elif args: view = editor.current_view >> m.mock(TextDocumentView) command = m.mock() if c.lookup == 'first': commander.lookup(args[0]) >> command if isinstance(c.args, Exception): expect(command.arg_parser.parse(c.argstr)).throw(c.args) else: command.arg_parser.parse(c.argstr) >> c.args elif c.lookup == 'full': commander.lookup(args[0]) >> None if c.args is None: commander.lookup_full_command(c.text) >> (None, None) else: commander.lookup_full_command(c.text) >> (command, c.args) else: assert c.lookup == None, c.lookup if c.args is None or isinstance(c.args, Exception): if isinstance(c.args, Exception): kw = {"exc_info": True} else: kw = {} message(c.msg, **kw) else: view.text_view >> '<view>' res = command('<view>', bar, '<args>') if c.error: expect(res).throw(Exception('bang!')) message(ANY, exc_info=True) elif not c.text.startswith(" "): res >> c.msg history = commander.history >> m.mock(mod.CommandHistory) history.append(c.text) if c.msg: message(c.msg, msg_type=const.INFO) with m: bar.execute(c.text)
def test(c): m = Mocker() editor = m.mock() commander = m.mock(TextCommandController) sys_exc_info = m.replace(mod.sys, "exc_info") format_exc = m.replace(mod.traceback, "format_exception") bar = mod.CommandBar(editor, commander) view = editor.current_view >> m.mock(TextDocumentView) kw = {} if c.exc_info is not None: kw["exc_info"] = c.exc_info exc_info = sys_exc_info() >> ("<type>", "<exc>", "<tb>") format_exc(*exc_info) >> ["Traceback", "...", "Error!"] view.message(c.msg, msg_type=const.ERROR) with m: bar.message(c.text, **kw)
def __init__(self, *commands, **kw): class menu: @staticmethod def insertItem_atIndex_(item, tag): pass class editor: class current_view: text_view = kw.pop("textview", None) def message(msg, msg_type=const.INFO): if isinstance(msg, Exception): raise msg raise AssertionError(msg) commander = textcommand.TextCommandController(kw.pop("history", [])) for command in commands: commander.add_command(command, None, menu) self.bar = textcommand.CommandBar(kw.pop("editor", editor), commander) # keep references (CommandBar uses weakref) self.refs = (editor, commander)
def test_CommandBar_history_reset_on_execute(): from editxt.document import TextDocumentView from editxt.textcommand import CommandHistory, TextCommandController with tempdir() as tmp: m = Mocker() editor = m.mock() history = CommandHistory(tmp) commander = TextCommandController(history) bar = mod.CommandBar(editor, commander) args = ["cmd"] view = editor.current_view >> m.mock(TextDocumentView) view.text_view >> '<view>' @command def cmd(textview, sender, args): pass commander.add_command(cmd, None, None) with m: bar.get_history("cmd") bar.execute("cmd") eq_(bar.history_view, None) eq_(list(history), ["cmd"])