示例#1
0
 def test(c):
     m = Mocker()
     menu = m.mock(ak.NSMenu)
     ctl = TextCommandController("<history>")
     for command in c.commands:
         ctl.add_command(command, None, menu)
     eq_(ctl.lookup(c.lookup), c.result)
示例#2
0
 def test(c):
     m = Mocker()
     menu = m.mock(ak.NSMenu)
     ctl = TextCommandController("<history>")
     for command in c.commands:
         ctl.add_command(command, None, menu)
         menu.insertItem_atIndex_(ANY, ANY)
     eq_(ctl.lookup_full_command(c.lookup), c.result)
示例#3
0
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)
示例#4
0
def test_TextCommandController_init():
    m = Mocker()
    menu = m.mock(ak.NSMenu)
    with m:
        ctl = TextCommandController("<history>")
        eq_(ctl.history, "<history>")
        eq_(ctl.commands, {})
        eq_(ctl.commands_by_path, {})
        eq_(ctl.input_handlers, {})
        eq_(ctl.editems, {})
示例#5
0
    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)
示例#6
0
 def __init__(self, profile=None):
     if profile is None:
         profile = self.default_profile()
     self.profile_path = os.path.expanduser(profile)
     assert os.path.isabs(self.profile_path), \
         'profile path cannot be relative (%s)' % self.profile_path
     self._setup_profile = set()
     self.editors = []
     self.path_opener = None
     self.config = Config(self.profile_path)
     self.context = ContextMap()
     self.syntax_factory = None
     state_dir = os.path.join(self.profile_path, const.STATE_DIR)
     command_history = CommandHistory(state_dir)
     self.text_commander = TextCommandController(command_history)
     register_value_transformers()
示例#7
0
 def test(c):
     m = Mocker()
     lg = m.replace("editxt.textcommand.log")
     mi = m.mock(ak.NSMenuItem)
     tv = m.mock(ak.NSTextView)
     tc = m.mock()
     tcc = TextCommandController("<history>")
     cmds = m.replace(tcc, 'commands')
     cmds.get(mi.tag() >> 42) >> (tc if c.has_command else None)
     if c.has_command:
         tc(tv, mi, None)
         if c.error:
             m.throw(Exception)
             lg.error("%s.execute failed", ANY, exc_info=True)
     with m:
         tcc.do_textview_command(tv, mi)
示例#8
0
 def test(c):
     m = Mocker()
     lg = m.replace("editxt.textcommand.log")
     tv = m.mock(ak.NSTextView)
     tcc = TextCommandController("<history>")
     sel = "<selector>"
     callback = m.mock()
     handlers = m.replace(tcc, 'input_handlers')
     cmd = handlers.get(sel) >> (callback if c.has_selector else None)
     if c.has_selector:
         callback(tv, None, None)
         if c.error:
             m.throw(Exception)
             lg.error("%s failed", callback, exc_info=True)
     with m:
         result = tcc.do_textview_command_by_selector(tv, sel)
         eq_(result, c.result)
示例#9
0
 def test(c):
     m = Mocker()
     lg = m.replace("editxt.textcommand.log")
     mi = m.mock(ak.NSMenuItem)
     tv = m.mock(ak.NSTextView)
     tc = m.mock()
     tcc = TextCommandController("<history>")
     cmds = m.replace(tcc, 'commands')
     cmds.get(mi.tag() >> 42) >> (tc if c.has_command else None)
     if c.has_command:
         if c.error:
             expect(tc.is_enabled(tv, mi)).throw(Exception)
             lg.error("%s.is_enabled failed", ANY, exc_info=True)
         else:
             tc.is_enabled(tv, mi) >> c.enabled
     with m:
         result = tcc.is_textview_command_enabled(tv, mi)
         eq_(result, c.enabled)
示例#10
0
 def test(c):
     m = Mocker()
     menu = m.mock(ak.NSMenu)
     ctl = TextCommandController("<history>")
     handlers = ctl.input_handlers = m.mock(dict)
     add = m.method(ctl.add_command)
     mod = m.mock(dict)
     m.method(ctl.iter_command_modules)() >> [("<path>", mod)]
     cmds = []
     mod.get("text_menu_commands", []) >> cmds
     for i in range(c.commands):
         cmd = "<command %s>" % i
         add(cmd, "<path>", menu)
         cmds.append(cmd)
     hnds = mod.get("input_handlers", {}) >> {}
     for i in range(c.handlers):
         hnds["handle%s" % i] = "<handle %s>" % i
     handlers.update(hnds)
     with m:
         ctl.load_commands(menu)
示例#11
0
 def test(c):
     m = Mocker()
     menu = m.mock(ak.NSMenu)
     mi_class = m.replace(ak, 'NSMenuItem')
     ctl = TextCommandController("<history>")
     handlers = m.replace(ctl, 'input_handlers')
     validate = m.method(ctl.validate_hotkey)
     cmd = m.mock()
     cmd.names >> []
     cmd.lookup_with_arg_parser >> False
     tag = cmd._TextCommandController__tag = next(ctl.tagger) + 1
     validate(cmd.hotkey >> "<hotkey>") >> ("<hotkey>", "<keymask>")
     mi = mi_class.alloc() >> m.mock(ak.NSMenuItem)
     (cmd.title << "<title>").count(2)
     mi.initWithTitle_action_keyEquivalent_(
         '<title>', "performTextCommand:", "<hotkey>") >> mi
     mi.setKeyEquivalentModifierMask_("<keymask>")
     mi.setTag_(tag)
     menu.insertItem_atIndex_(mi, tag)
     with m:
         ctl.add_command(cmd, None, menu)
         assert ctl.commands[tag] is cmd, (ctl.commands[tag], cmd)
示例#12
0
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"])
示例#13
0
def test_TextCommandController_validate_hotkey():
    tc = TextCommandController("<history>")
    eq_(tc.validate_hotkey(None), ("", 0))
    eq_(tc.validate_hotkey(("a", 1)), ("a", 1))
    assert_raises(AssertionError, tc.validate_hotkey, ("a", "b", "c"))