Exemplo n.º 1
0
def test_create_editor():
    with test_app("project") as app:
        project = app.windows[0].projects[0]
        eq_(len(project.editors), 0)
        project.create_editor()
        eq_(len(project.editors), 1)
        eq_(test_app(app).state, "window project editor[untitled 0]")
Exemplo n.º 2
0
 def test(app, recent_after, path="/file.txt"):
     tapp = test_app(app)
     project = app.windows[0].projects[0]
     project.recent.append(Recent(tapp.temp_path("/file.txt")))
     project.create_editor(tapp.temp_path(path))
     eq_([tapp.pretty_path(r) for r in project.recent], recent_after)
     eq_(test_app(app).state, "window project editor[%s 0]" % path)
Exemplo n.º 3
0
def test_test_app_context_manager():
    from editxt.window import Window
    with test_app("editor(1)") as app:
        assert app is not None
        window = Window(app)
        app.windows.append(window)
    eq_(test_app(app).state, "window project editor(1) window[0]")
Exemplo n.º 4
0
def test_serialize_project_with_errlog():
    with test_app("project") as app:
        project = app.windows[0].projects[0]
        project.create_editor_with_state({"internal": "errlog"})
        serial = project.serialize()
    print(serial)
    with test_app("window") as app:
        window = app.windows[0]
        project = Project(window, serial=serial)
        eq_(project.editors[0].document, app.errlog.document)
Exemplo n.º 5
0
 def test(command, expected="", error=None, config="", project_path="/"):
     project_config = "({})".format(project_path) if project_path else ""
     if "*" not in config:
         project_config += "*"
     base_config = "window project{} ".format(project_config)
     with test_app(base_config + config) as app:
         editor = app.windows[0].current_editor
         CommandTester(mod.open_, editor=editor, error=error)(command)
         if not error:
             base_config = base_config.replace("*", "")
         eq_(test_app(app).state, (base_config + expected).strip())
Exemplo n.º 6
0
 def test(recent_after, recent_before=(), path="/file.txt", config="project"):
     with test_app(config) as app:
         tapp = test_app(app)
         project = app.windows[0].projects[0]
         doc = tapp.document_with_path(path)
         project.recent.extend(
             Recent(tapp.temp_path(p)) for p in recent_before)
         editor = project.insert_items([doc], action=const.COPY)[1]
         project.remove(editor)
         print(test_app(app).state)
         eq_([tapp.pretty_path(r) for r in project.recent], recent_after)
Exemplo n.º 7
0
def test_saved_errlog_serial():
    with test_app("project") as app:
        project = app.windows[0].projects[0]
        editor = project.create_editor_with_state({"internal": "errlog"})
        editor.file_path = test_app(app).temp_path("error.log")
        editor.document.save()
        eq_(editor.document, app.errlog.document)
        state = editor.edit_state
        assert "internal" not in state, state
        assert "path" in state, state
        assert editor.document.file_exists(), editor.file_path
    assert not editor.document.file_exists(), editor.file_path
Exemplo n.º 8
0
 def do(link, config="", expect=True, goto=None, meta=False, *, base_config, get_subject):
     with test_app(base_config) as app:
         m = Mocker()
         subject = get_subject(app)
         goto_line = m.replace(Editor, "goto_line")
         if goto is not None:
             goto_line(goto)
         with m:
             eq_(subject.handle_link(link, meta), expect)
         if config:
             if "*" in config:
                 base_config = base_config.replace("*", "")
             eq_(test_app(app).state, base_config + config)
Exemplo n.º 9
0
 def test(config, expected_calls=[]):
     with test_app(config) as app:
         for item in config.split():
             if "(" in item:
                 editor = test_app(app).get(item)
                 if "/doc.save" in editor.file_path:
                     test_app(app).set_content(editor)
                 make_dirty(editor.document)
         calls = []
         def callback(ok):
             calls.append(ok)
         answer = app.should_terminate(callback)
         eq_(answer, (callback if calls else True))
         eq_(calls, expected_calls)
Exemplo n.º 10
0
 def test(original, config="editor"):
     diffed = []
     text_name = "file.txt"
     rm = []
     def diff_stub(path1, path2, diff_program, remove=None):
         for path in remove:
             os.remove(path)
         eq_(path1, path_1)
         eq_(path2, path_2)
         eq_(diff_program, "opendiff")
         diffed.append(1)
     with tempdir() as tmp, test_app(config) as app, \
             replattr(mod, "external_diff", diff_stub):
         window = app.windows[0]
         editor = text_editor = window.projects[0].editors[0]
         if config == "editor":
             path = editor.file_path = join(tmp, "file.txt")
             with open(path, mode="w", encoding="utf8") as fh:
                 fh.write("abc")
         m = Mocker()
         text_view = editor.text_view = m.mock(TextView)
         path_1 = editor.file_path
         if original:
             make_dirty(editor.document)
             name_ext = splitext(test_app(app).name(editor))
             path_2 = Regex(r"/file-.*\.txt$")
             args = None
             text_view.string() >> "def"
         elif len(window.selected_items) == 2:
             editor2 = window.selected_items[1]
             editor2.text_view = m.mock(TextView)
             name_ext = splitext(test_app(app).name(editor)[7:-1])
             path_1 = Regex(r"/{}-.*{}$".format(*name_ext))
             name_ext = splitext(test_app(app).name(editor2)[7:-1])
             path_2 = Regex(r"/{}-.*{}$".format(*name_ext))
             args = None
             text_view.string() >> "def"
             editor2.text_view.string() >> "ghi"
         else:
             path_1 = join(tmp, "other.txt")
             path_2 = editor.file_path
             with open(path_1, mode="w", encoding="utf8") as fh:
                 fh.write("other")
             assert " " not in path_1, path_1
             args = mod.diff.arg_parser.parse(path_1)
         with m:
             mod.diff(editor, args)
             assert diffed
Exemplo n.º 11
0
 def test(argstr=None, value=None):
     with test_app() as app:
         history = app.text_commander.history
         if argstr:
             history.append(argstr)
         options = mod.load_options(dummy_command, history)
         eq_(options, Options(value=value))
Exemplo n.º 12
0
 def test(c):
     with test_app() as app:
         m = Mocker()
         fc = FindController(app)
         fc.options = make_options(c)
         beep = m.replace(mod, "beep")
         sheet = m.replace(ak, "NSBeginAlertSheet")
         gui = m.replace(fc, "gui")
         ftext = m.mock(ak.NSTextField)
         if c.search != mod.LITERAL:
             if c.ftext is None:
                 gui.find_text >> None
             else:
                 (gui.find_text << ftext).count(1, None)
                 ftext.stringValue() >> c.ftext
             if not c.expect:
                 beep()
                 sheet(
                     ANY,
                     "OK", None, None,
                     gui.window >> "<window>", None, None, None, 0,
                     ANY,
                 );
         with m:
             result = fc.validate_expression()
             eq_(result, c.expect)
Exemplo n.º 13
0
 def test(command, token):
     with test_app("editor*") as app:
         editor = app.windows[0].current_editor
         editor.text_view = fake_text_view
         do = CommandTester(mod.set_variable, editor=editor)
         do(command)
         eq_(editor.document.comment_token, token)
Exemplo n.º 14
0
 def test(config, prompt=[], dirty=False, close=True):
     calls = []
     def callback():
         calls.append(True)
     with test_app(config) as app:
         window = app.windows[0]
         editor = window.current_editor
         if "/doc.save" in editor.file_path:
             test_app(app).set_content(editor)
         if dirty:
             make_dirty(editor.document)
         editor.interactive_close(callback)
         eq_(calls, [True] if close else [])
         eq_(window.wc.prompts, prompt)
         post_config = "window project " + config
         eq_(test_app(app).state, post_config)
Exemplo n.º 15
0
    def test(text, fail=True, beep=False, config="editor*", message=None, call=0, history=False):
        @command(arg_parser=CommandParser(Choice(("action", None), "cmd_err", "error", "message")))
        def cmd(editor, args):
            nonlocal calls
            calls += 1
            if args.action == "cmd_err":
                raise mod.CommandError("cmd_err")
            if args.action == "error":
                raise Exception("error")
            if args.action == "message":
                return args.action

        calls = 0
        messages = []
        with test_app(config) as app:
            bar = mod.CommandBar(app.windows[0], app.text_commander)
            app.text_commander.add_command(cmd, None, None)

            def bar_message(msg, *args, exc_info=False, **kw):
                if exc_info:
                    log.info(str(msg), exc_info=True)
                messages.append(str(msg))

            bar.message = bar_message
            with expect_beep(beep):
                bar.execute(text)
            eq_(messages, [message] if message else [])
            eq_(bar.failed_command, text if fail else None)
            eq_(calls, call)
            eq_(bar.get_history(""), text if history else None)
            eq_(bar._cached_parser, (None, None, None))
Exemplo n.º 16
0
def property_value_util(c, doc=None):
    with test_app() as app:
        if doc is None:
            doc = TextDocument(app)
        eq_(getattr(doc, c.attr), c.default)
        setattr(doc, c.attr, c.value)
        eq_(getattr(doc, c.attr), c.value)
Exemplo n.º 17
0
def test_CommandManager_load_shortcuts():
    from editxt.config import config_schema

    @command
    def doc(editor, opts):
        pass

    shorts = config_schema()["shortcuts"]
    menu = const.Constant("menu")
    expect = []
    tags = {doc: doc.name}
    key = lambda kv: kv[1]["rank"].default
    for i, (hotkey, value) in enumerate(sorted(shorts.items(), key=key)):
        hkey = mod.parse_hotkey(hotkey)
        title = value["name"].default
        expect.append((menu, title) + hkey)
        tags[title] = i
    items = []

    def add_menu_item(menu, title, hotkey, modifiers):
        items.append((menu, title, hotkey, modifiers))
        return tags[title]

    with test_app() as app:
        ctl = CommandManager("<history>", app=app)
        ctl.add_command(doc, None, menu)
        ctl.add_menu_item = add_menu_item
        ctl.load_shortcuts(menu)
        eq_(items, expect)
        eq_(set(ctl.commands), set(tags.values()))
Exemplo n.º 18
0
 def test(c):
     with test_app() as app:
         m = Mocker()
         fc = FindController(app)
         flog = m.replace("editxt.command.find.log")
         beep = m.replace(mod, "beep")
         get_editor = m.method(fc.get_editor)
         sender = m.mock()
         (sender.tag() << c.tag).count(1, 2)
         func = None
         for tag, meth in list(fc.action_registry.items()):
             fc.action_registry[tag] = temp = m.mock(meth)
             if tag == c.tag:
                 func = temp
         if c.fail:
             flog.info(ANY, c.tag)
         else:
             if c.error:
                 err = mod.CommandError("error!")
                 expect(func(sender)).throw(err)
                 beep()
                 editor = get_editor() >> (m.mock() if c.target else None)
                 if c.target:
                     editor.message("error!", msg_type=const.ERROR)
                 else:
                     flog.warn(err)
             else:
                 func(sender)
         with m:
             fc.perform_action(sender)
Exemplo n.º 19
0
def test_window_will_close_after_app_will_terminate():
    with test_app("editor") as app:
        window = app.windows[0]
        app.will_terminate()
        app.discard_window(window)
        closed_path = join(app.profile_path, const.STATE_DIR, const.CLOSED_DIR)
        eq_(os.listdir(closed_path), [])
Exemplo n.º 20
0
def test_Editor_highlight_selection_after_close(app):
    m = Mocker()
    editor = test_app(app).get("editor(a)")
    editor.document.text = "abcdef"
    text = editor.line_numbers.text
    editor.close()
    editor.highlight_selection(text, (0, 3))  # should not raise
Exemplo n.º 21
0
 def test(c):
     with test_app(c.app) as app:
         m = Mocker()
         teardown_main_view = m.replace(mod, 'teardown_main_view')
         window = app.windows[0]
         editor = window.projects[0].editors[0]
         make_dirty(editor.document)
         assert window.is_dirty
         editor.text_view = None if c.tv_is_none else m.mock(ak.NSTextView)
         doc = editor.document
         undo = editor.undo_manager
         if c.ts_is_none:
             doc.text_storage = None
         else:
             with replattr(doc, 'reset_text_attributes', lambda *a: None, sigcheck=False):
                 text_storage = doc.text_storage
             #text_storage.setDelegate_(doc)
             remove_layout = m.method(text_storage.removeLayoutManager_)
         if not (c.tv_is_none or c.ts_is_none):
             lm = editor.text_view.layoutManager() >> m.mock(ak.NSLayoutManager)
             remove_layout(lm)
         if c.main_is_none:
             editor.main_view = None
         else:
             editor.main_view = m.mock()
             teardown_main_view(editor.main_view)
         assert editor.on_dirty_status_changed in undo.callbacks.items
         with m:
             editor.close()
         if next(window.iter_editors_of_document(doc), None) is not None:
             assert window.is_dirty
         else:
             assert not window.is_dirty
         eq_(editor.command_view, None)
         eq_(editor.scroll_view, None)
         eq_(editor.text_view, None)
         eq_(editor.document, None)
         eq_(editor.proxy, None)
         assert editor.on_dirty_status_changed not in undo.callbacks.items
         if c.close_doc:
             eq_(doc.text_storage, None)
             #if not c.ts_is_none:
                 #eq_(text_storage.delegate(), None)
         else:
             assert doc.text_storage is not None
         eq_(test_app(app).state, c.end)
Exemplo n.º 22
0
def test_PanelController_shared_controller():
    with test_app() as app:
        cx = PanelController.shared_controller(app)
        c1 = OtherController.shared_controller(app)
        assert cx is not c1, c1
        assert isinstance(c1, OtherController), c1
        c2 = OtherController.shared_controller(app)
        assert c1 is c2, (c1, c2)
Exemplo n.º 23
0
def test_WrapLinesController_default_options():
    with test_app() as app:
        editor = base.Options(app=app)
        ctl = WrapLinesController(editor)
        eq_(ctl.options._target, mod.Options(
            wrap_column=const.DEFAULT_WRAP_COLUMN,
            indent=True,
        ))
Exemplo n.º 24
0
def test_CommandController_save_options():
    with test_app() as app:
        history = app.text_commander.history
        eq_(next(iter(history), None), None)
        slc = FakeController(app)
        slc.options = Options(value=42)
        slc.save_options()
        eq_(next(iter(history)), "42")
Exemplo n.º 25
0
def test_CommandController_options():
    with test_app() as app:
        ctl = FakeController(app)
        eq_(ctl.history, app.text_commander.history)
        assert ctl.options is ctl.gui.options()
        obj = object()
        ctl.gui.setOptions_(obj)
        eq_(ctl.options, obj)
Exemplo n.º 26
0
def test_create_editor_with_state():
    with test_app() as app:
        window = TestConfig(app=app)
        project = Project(window)
        state = {"path": "Untitled"}
        result = project.create_editor_with_state(state)
        eq_(result.project, project)
        assert result in project.editors, project.editors
Exemplo n.º 27
0
 def test(command, size, mode):
     with test_app("editor*") as app:
         editor = app.windows[0].current_editor
         editor.text_view = fake_text_view
         do = CommandTester(mod.set_variable, editor=editor)
         do(command)
         eq_(editor.indent_size, size)
         eq_(editor.indent_mode, mode)
Exemplo n.º 28
0
 def test(hist, expect):
     with test_app() as app:
         history = app.text_commander.history
         ctl = FakeController(app)
         eq_(ctl.history, history)
         if hist:
             history.append(hist)
         ctl.load_options()
         eq_(ctl.options.__dict__["_target"], expect)
Exemplo n.º 29
0
 def test(command, attr, value, *value_before):
     with test_app("project editor*") as app:
         project = app.windows[0].projects[0]
         do = CommandTester(mod.set_variable, editor=project.editors[0])
         if value_before:
             eq_(getattr(project, attr), *value_before)
             eq_(len(value_before), 1, value_before)
         do(command)
         eq_(getattr(project, attr), value)
Exemplo n.º 30
0
def test_Editor_project():
    with test_app("""
            window(A)
                project(0)
                    editor(a)*
                    editor(b)
            window(B)
                project(1)*
        """) as app:
        print(test_app(app).state)
        win_A, win_B = app.windows
        proj_0, = win_A.projects
        proj_1, = win_B.projects
        editor_a, editor_b = proj_0.editors
        editor_a.project = proj_1
        eq_(editor_a.project, proj_1)
        eq_(test_app(app).state,
            "window(A) project(0) editor(b)* window(B) project(1)*")