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]")
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)
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]")
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)
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())
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)
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
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)
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)
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
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))
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)
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)
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)
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))
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)
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()))
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)
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), [])
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
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)
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)
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, ))
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")
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)
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
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)
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)
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)
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)*")