def test(c): m = Mocker() regundo = m.replace(mod, 'register_undo_callback') convert = m.replace(mod, 'change_indentation') doc = m.mock(TextDocument) with m.off_the_record(): dv = Editor(None, document=doc) tv = dv.text_view = m.mock(ak.NSTextView) if c.convert: old_indent = "\t" if c.oldm is TAB else (" " * c.olds) new_indent = "\t" if c.newm is TAB else (" " * c.news) convert(tv, old_indent, new_indent, c.news) if c.oldm != c.newm: doc.props.indent_mode = c.newm if c.olds != c.news: doc.props.indent_size = c.news if c.convert or c.convert is None: undo_change = m.mock(); undo_change(c.newm, c.news, c.oldm, c.olds, None) def _undo(undoman, undo): dv.change_indentation = undo_change undo() undoman = doc.undo_manager >> m.mock(fn.NSUndoManager) expect(regundo(undoman, ANY)).call(_undo) with m: dv.change_indentation(c.oldm, c.olds, c.newm, c.news, c.convert)
def test(app, c): m = Mocker() doc = TextDocument(app) with m.off_the_record(): doc.text_storage = ts = m.mock(ak.NSTextStorage) app.syntax_factory = m.mock(SyntaxFactory) m.property(doc, "syntaxdef") m.property(doc, "props") syn = doc.syntaxer = m.mock(Highlighter) color_text = m.method(doc.color_text) syn.filename >> "<filename %s>" % ("0" if c.namechange else "1") new = doc.file_path = "<filename 1>" colored = False if c.namechange: syn.filename = new sdef = app.syntax_factory.get_definition(new) >> m.mock(SyntaxDefinition) doc.syntaxdef >> (None if c.newdef else sdef) if c.newdef: doc.props.syntaxdef = sdef color_text() colored = True doc.syntax_needs_color = c.needs_color if c.needs_color and not colored: color_text() with m: doc.update_syntaxer() eq_(doc.syntax_needs_color, False)
def test(app, has_text_storage, event=None): INDENT_SIZE = 42 m = Mocker() ps_class = m.replace(ak, 'NSParagraphStyle') doc = TextDocument(app) if has_text_storage: with m.off_the_record(): ts = doc.text_storage = m.mock(ak.NSTextStorage) else: ts = doc.text_storage = None font = app.default_font.font tabw = ak.NSString.stringWithString_("8" * INDENT_SIZE) \ .sizeWithAttributes_({ak.NSFontAttributeName: font}).width ps = ps_class.defaultParagraphStyle().mutableCopy() >> m.mock() ps.setTabStops_([]) ps.setDefaultTabInterval_(tabw) attrs = { ak.NSFontAttributeName: font, ak.NSParagraphStyleAttributeName: ps, ak.NSForegroundColorAttributeName: app.theme.text_color, } if has_text_storage: no_color = {k: v for k, v in attrs.items() if k != ak.NSForegroundColorAttributeName} ts.addAttributes_range_(no_color, fn.NSMakeRange(0, ts.length() >> 20)) ts.setFont_(doc.font.font) editors = [m.mock(Editor), m.mock(Editor)] m.method(app.iter_editors_of_document)(doc) >> editors for editor in editors: editor.set_text_attributes(attrs) with m: doc.reset_text_attributes(INDENT_SIZE, event) eq_(doc.default_text_attributes(), attrs) eq_(doc.syntax_needs_color, has_text_storage and event and event.theme_changed)
def test_TextDocument_color_text_with_null_text_storage(app): m = Mocker() doc = TextDocument(app) with m.off_the_record(): doc.text_storage = None syn = doc.syntaxer = m.mock() range = (0, 20) with m: doc.color_text(range)
def test(c): def end(): with m: doc.reload_document() eq_(doc.text, text) text = "edit" config = " ".join("project" for x in c.view_state) with make_file(content="disk") as path, test_app(config) as app: if c.url_is_none: path = "not-saved" elif not c.exists: path += "-not-found" m = Mocker() call_later = m.replace(mod, "call_later") doc = TextDocument(app, path) reset_text_attributes = m.method(doc.reset_text_attributes) doc.text = text if c.url_is_none or not c.exists: return end() undo = doc.undo_manager = m.mock(UndoManager) if not c.read2_success: os.remove(path) os.mkdir(path) return end() text = "disk" tv = m.mock(TextView) for i, text_view_exists in enumerate(c.view_state): project = app.windows[0].projects[i] with m.off_the_record(): editor = Editor(project, document=doc) editor.text_view = (tv if text_view_exists else None) project.editors.append(editor) reset_text_attributes() if not any(c.view_state): # TODO reload without undo #doc_ts.replaceCharactersInRange_withString_(range, text) undo.removeAllActions() return end() range = fn.NSRange(0, 4) tv.shouldChangeTextInRange_replacementString_(range, text) >> True # TODO get edit_state of each document view # TODO diff the two text blocks and change chunks of text # throughout the document (in a single undo group if possible) # also adjust edit states if possible # see http://www.python.org/doc/2.5.2/lib/module-difflib.html #doc_ts.replaceCharactersInRange_withString_(range, text) tv.didChangeText() tv.breakUndoCoalescing() # TODO restore edit states # HACK use timed invocation to allow didChangeText notification # to update change count before _clearUndo is invoked call_later(0, doc.clear_dirty) tv.select((0, 0)) reset_text_attributes() m.method(doc.update_syntaxer)() end()
def test(app, state=None, ts_len=0): m = Mocker() editor = app.windows[0].projects[0].editors[0] proxy_prop = m.property(editor, "proxy") soft_wrap_prop = m.property(editor, "soft_wrap") text_prop = m.property(editor.document, "text_storage") if state is None: eq_state = state = {"soft_wrap": const.WRAP_NONE} else: editor.line_numbers = m.mock(mod.LineNumbers) m.off_the_record(editor.line_numbers) eq_state = dict(state) eq_state.setdefault("selection", [0, 0]) eq_state.setdefault("scrollpoint", (0, 0)) eq_state.setdefault("soft_wrap", const.WRAP_NONE) point = eq_state["scrollpoint"] sel = eq_state["selection"] editor.text_view = m.mock(ak.NSTextView) editor.scroll_view = m.mock(ak.NSScrollView) soft_wrap_prop.value = eq_state["soft_wrap"] editor.text_view.layoutManager() \ .characterIndexForPoint_inTextContainer_fractionOfDistanceBetweenInsertionPoints_( ANY, ANY, ANY) >> (1, 0) editor.text_view.bounds().size.height >> 10 editor.text_view.textContainer() editor.scroll_view.verticalRulerView().invalidateRuleThickness() editor.text_view.scrollPoint_(fn.NSPoint(*point)) text = text_prop.value >> m.mock(ak.NSTextStorage) text.length() >> ts_len if sel[0] > ts_len: sel = (ts_len, 0) elif sel[0] + sel[1] > ts_len: sel = (sel[0], ts_len - sel[0]) editor.text_view.setSelectedRange_(sel) if "updates_path_on_file_move" in state: proxy = proxy_prop.value >> m.mock(Editor) proxy.updates_path_on_file_move = state["updates_path_on_file_move"] with m: editor.edit_state = state if not isinstance(state, dict): eq_(state, eq_state)
def test_TextDocument_on_text_edit(app): from editxt.syntax import Highlighter m = Mocker() doc = TextDocument(app) with m.off_the_record(): ts = doc.text_storage = m.mock(ak.NSTextStorage) ts.editedMask() >> -1 syn = doc.syntaxer = m.mock(Highlighter) range = (0, 20) syn.color_text(ts, range, timeout=0.05) with m: doc.on_text_edit(range)
def test(kw): m = Mocker() doc = m.mock(TextDocument) if "state" in kw: path = kw["state"]["path"] elif "path" in kw: path = kw["path"] else: path = None kw["document"] = doc proj = m.mock(Project) doc_class = m.replace(editxt.document, 'TextDocument') default_state = m.replace(Editor, 'edit_state') if path is not None: doc = proj.window.app.document_with_path(path) >> doc m.off_the_record(doc.props) m.off_the_record(doc.text_storage) doc.undo_manager.on(ANY) with m: result = Editor(proj, **kw) eq_(result.project, proj) eq_(result.document, doc) eq_(result.edit_state, kw.get("state", default_state))
def test(c): m = Mocker() regundo = m.replace(mod, 'register_undo_callback') repnl = m.replace(mod, 'replace_newlines') doc = m.mock(TextDocument) with m.off_the_record(): dv = Editor(None, document=doc) proxy = m.property(dv, 'proxy') with m.order(): (getattr(doc, c.attr) << c.default).count(2 if c.value != c.default else 3) if c.value != c.default: c.do(TestConfig(**locals())) getattr(doc, c.attr) >> c.value with m: property_value_util(c, dv)
def test(app, success): m = Mocker() data = "<data>" doc = TextDocument(app) doc.document_attrs = INIT_ATTRS = {"attr": 0, ak.NSCharacterEncodingDocumentAttribute: "<encoding>"} with m.off_the_record(): ts = doc.text_storage = m.mock(ak.NSTextStorage) analyze = m.method(doc.analyze_content) m.method(doc.default_text_attributes)() >> "<text attributes>" (ts.readFromData_options_documentAttributes_error_(data, ANY, None, None) << (success, "<attrs>", None)).count(1 if success else 2) with m: result = doc.read_from_data(data) eq_(result, (success, None)) eq_(doc.document_attrs, ("<attrs>" if success else INIT_ATTRS))
def test(app, c): if c.eol_char != "\n": c.text = c.text.replace("\n", eol_char) m = Mocker() doc = TextDocument(app) eq_((doc.newline_mode, doc.indent_mode, doc.indent_size), (const.NEWLINE_MODE_UNIX, const.INDENT_MODE_SPACE, 4), 'invalid initial state') with m.off_the_record(): doc.text_storage = ts = m.mock(ak.NSTextStorage) ts.string() >> fn.NSString.stringWithString_(c.text) with m: doc.analyze_content() eq_((doc.newline_mode, doc.indent_mode, doc.indent_size), ( c._get("eol", const.NEWLINE_MODE_UNIX), c._get("imode", const.INDENT_MODE_SPACE), c._get("isize", 4), ))
def test(app, c): m = Mocker() doc = m.mock(TextDocument) project = app.windows[0].projects[0] with m.off_the_record(): dv = Editor(project, document=doc) m.property(dv, "soft_wrap") m.property(dv, "file_path") doc.updates_path_on_file_move >> True if c.tv_is_none: if c.set_state: dv.edit_state = state = dict(key="value") else: state = {} state["soft_wrap"] = const.WRAP_NONE else: dv.text_view = m.mock(ak.NSTextView) dv.scroll_view = m.mock(ak.NSScrollView) sel = m.mock(fn.NSRange) sp = m.mock(fn.NSPoint) dv.text_view.selectedRange() >> sel dv.scroll_view.documentVisibleRect().origin >> sp sel.location >> "<sel.location>" sel.length >> "<sel.length>" sp.x >> "<sp.x>" sp.y >> "<sp.y>" dv.soft_wrap >> c.soft_wrap state = dict( selection=["<sel.location>", "<sel.length>"], scrollpoint=["<sp.x>", "<sp.y>"], soft_wrap=c.soft_wrap, ) (dv.file_path << "<path>").count(1, 2) state["path"] = "<path>" with m: result = dv.edit_state eq_(result, state) if c.tv_is_none and c.set_state: assert result is not state, \ "identity check should fail: must be a new (mutable) dict"