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(c): m = Mocker() fc = FindController.shared_controller() regexfind = m.method(fc.regexfinditer) simplefind = m.method(fc.simplefinditer) rfr = m.property(fc, "recently_found_range") sel = NSMakeRange(1, 2) direction = "<direction>" opts = m.property(fc, "opts").value >> m.mock(FindOptions) ftext = u"<find>" if opts.regular_expression >> c.regex: finditer = regexfind elif opts.match_entire_word >> c.mword: finditer = regexfind ftext = u"\\b" + re.escape(ftext) + u"\\b" else: finditer = simplefind range = NSMakeRange(sel.location, 0) items = [] rng = None for i, r in enumerate(c.matches): if r is WRAPTOKEN: items.append(r) continue found = FoundRange(NSMakeRange(*r)) items.append(found) if i == 0 and found.range == sel: continue rfr.value = found rng = found.range finditer(u"<text>", ftext, range, direction, True) >> items with m: result = fc._find(u"<text>", u"<find>", sel, direction) eq_(result, rng)
def test_FindController_show_find_panel(): m = Mocker() fc = FindController.shared_controller() sender = m.mock() m.property(fc, "opts") m.property(fc, "find_text") #fc.opts.load() m.method(fc.showWindow_)(fc) #fc.find_text.setStringValue_(fc.opts.find_text >> "abc") fc.find_text.selectText_(sender) with m: fc.show_find_panel(sender)
def test_FindController_show_find_panel(): m = Mocker() fc = FindController.shared_controller() sender = m.mock() opts = m.property(fc, "options").value opts.willChangeValueForKey_("recent_finds") m.method(fc.load_options)() opts.didChangeValueForKey_("recent_finds") m.property(fc, "find_text") m.method(fc.gui.showWindow_)(fc) fc.find_text.selectText_(sender) with m: fc.show_find_panel(sender)
def test_FindController_show_find_panel(): m = Mocker() fc = FindController.shared_controller() sender = m.mock() opts = m.property(fc, "options").value opts.willChangeValueForKey_("recent_finds") m.method(fc.load_options)() opts.didChangeValueForKey_("recent_finds") m.property(fc, "find_text") m.method(fc.gui.showWindow_)(fc) fc.find_text.selectText_(sender) with m: fc.show_find_panel(sender)
def test_FindController_show_find_panel(): with test_app() as app: m = Mocker() fc = FindController(app) sender = m.mock() opts = m.property(fc, "options").value opts.willChangeValueForKey_("recent_finds") m.method(fc.load_options)() opts.didChangeValueForKey_("recent_finds") m.property(fc, "find_text") m.method(fc.gui.show)() with m: fc.show_find_panel(sender)
def test(c): m = Mocker() fc = FindController.shared_controller() beep = m.replace(NSBeep, passthrough=False) dobeep = True tv = m.method(fc.find_target)() >> (m.mock(TextView) if c.has_tv else None) ftext = m.property(fc, "find_value").value >> c.ftext range = (tv.selectedRange() >> NSRange(*c.sel)) if c.has_tv else None if c.has_tv and c.ftext and ((c.sel_only and c.sel[1] > 0) or not c.sel_only): text = tv.string() >> c.text opts = m.property(fc, "opts").value >> m.mock() if not c.sel_only: if (opts.wrap_around >> c.wrap): range = NSMakeRange(0, 0) else: range = NSMakeRange(range[0], len(text) - range[0]) if opts.regular_expression >> c.regex: finditer = m.method(fc.regexfinditer) elif opts.match_entire_word >> c.mword: ftext = u"\\b" + re.escape(ftext) + u"\\b" finditer = m.method(fc.regexfinditer) else: finditer = m.method(fc.simplefinditer) rtext = m.property(fc, "replace_value").value >> c.rtext found = None ranges = [] rtexts = [] items = [] for r in c.ranges: found = FoundRange(NSMakeRange(*r)) if ranges: rtexts.append(text[sum(ranges[-1]):r[0]]) ranges.append(found.range) rtexts.append(rtext) items.append(found) finditer(text, ftext, range, FORWARD, False) >> items if ranges: start = c.ranges[0][0] range = NSMakeRange(start, sum(c.ranges[-1]) - start) value = "".join(rtexts) if tv.shouldChangeTextInRange_replacementString_(range, value) >> c.replace: ts = tv.textStorage() >> m.mock(NSTextStorage) ts.replaceCharactersInRange_withString_(range, value) tv.didChangeText() tv.setNeedsDisplay_(True) dobeep = False eq_(dobeep, c.beep) if dobeep: beep() with m: fc._replace_all(c.sel_only)
def test(c): m = Mocker() fc = FindController.shared_controller() field = m.property(fc, c.name).value >> (m.mock(NSTextField) if c.has_field else None) if c.has_field: field.stringValue() >> "<value>" else: opts = m.property(fc, "opts").value >> m.mock(FindOptions) # (m.property(fc, "opts").value << opts).count(2) # opts.load() getattr(opts, c.name) >> "<value>" with m: result = fc.value_of_field(c.name) eq_(result, "<value>")
def test(c): app = Application() m = Mocker() opc_class = m.replace(OpenPathController, passthrough=False) opc = m.mock(OpenPathController) m.property(app, "path_opener").value >> (opc if c.exists else None) if c.exists: app.path_opener.window().makeKeyAndOrderFront_(app) else: (opc_class.alloc() >> opc).initWithWindowNibName_("OpenPath") >> opc app.path_opener = opc opc.showWindow_(app) app.path_opener.populateWithClipboard() with m: app.open_path_dialog()
def test(app, _state_exists): m = Mocker() editor = app.windows[0].projects[0].editors[0] soft_wrap_property = m.property(editor, "soft_wrap") edit_state_property = m.property(editor, "edit_state") _state = m.mock(name="state") if _state_exists: editor._state = _state edit_state_property.value = _state else: assert getattr(editor, "_state", None) is None soft_wrap_property.value = app.config["soft_wrap"] with m: editor.reset_edit_state() assert getattr(editor, "_state", None) is None
def test(c): app = Application() m = Mocker() opc_class = m.replace(mod, 'OpenPathController') opc = m.mock(mod.OpenPathController) m.property(app, "path_opener").value >> (opc if c.exists else None) if c.exists: app.path_opener.window().makeKeyAndOrderFront_(app) else: opc_class(app) >> opc app.path_opener = opc opc.showWindow_(app) app.path_opener.populateWithClipboard() with m: app.open_path_dialog()
def test(c): app = Application() m = Mocker() opc_class = m.replace(mod, 'OpenPathController') opc = m.mock(mod.OpenPathController) m.property(app, "path_opener").value >> (opc if c.exists else None) if c.exists: app.path_opener.window().makeKeyAndOrderFront_(app) else: (opc_class.alloc() >> opc).initWithWindowNibName_("OpenPath") >> opc app.path_opener = opc opc.showWindow_(app) app.path_opener.populateWithClipboard() with m: app.open_path_dialog()
def test(c): m = Mocker() beep = m.replace(NSBeep, passthrough=False) fc = FindController.shared_controller() flash = m.method(fc.flash_status_text) regexfind = m.method(fc.regexfinditer) simplefind = m.method(fc.simplefinditer) tv = m.method(fc.find_target)() >> (m.mock(TextView) if c.has_tv else None) if c.has_tv: ftext = u"<find>" text = tv.string() >> NSString.stringWithString_("<text>") opts = m.property(fc, "opts").value >> m.mock(FindOptions) range = NSMakeRange(0, text.length()) if c.allow_regex and (opts.regular_expression >> c.regex): finditer = regexfind elif c.allow_regex and (opts.match_entire_word >> c.mword): finditer = regexfind ftext = u"\\b" + re.escape(ftext) + u"\\b" else: finditer = simplefind finditer(text, ftext, range, FORWARD, False) >> xrange(c.cnt) if c.cnt: flash("%i occurrences" % c.cnt) else: flash("Not found") else: beep() with m: fc.count_occurrences(u"<find>", c.allow_regex)
def test(c): m = Mocker() app = m.replace(editxt, 'app') opc = OpenPathController.alloc().init() paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView) paths.textStorage().string() >> c.text app.open_documents_with_paths(c.paths) (m.method(opc.window)() >> m.mock(ak.NSWindow)).orderOut_(opc) with m: opc.open_(None)
def test(c): m = Mocker() app = m.replace(editxt, 'app') opc = OpenPathController.alloc().init() paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView) paths.textStorage().string() >> c.text app.open_documents_with_paths(c.paths) (m.method(opc.window)() >> m.mock(ak.NSWindow)).orderOut_(opc) with m: opc.open_(None)
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(file_exists=True): m = Mocker() app = Application() view = m.mock(TextDocumentView) m.method(app.open_documents_with_paths)([app.config.path]) >> [view] default_config = m.property(app.config, "default_config") m.replace("os.path.exists")(app.config.path) >> file_exists if not file_exists: default_config.value >> "# config" view.document.text = "# config" with m: app.open_config_file()
def test(file_exists=True): m = Mocker() app = Application() view = m.mock(TextDocumentView) m.method(app.open_documents_with_paths)([app.config.path]) >> [view] default_config = m.property(app.config, "default_config") m.replace("os.path.exists")(app.config.path) >> file_exists if not file_exists: default_config.value >> "# config" view.document.text = "# config" with m: app.open_config_file()
def test(c): m = Mocker() opc = OpenPathController.alloc().init() paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView) with m.order(): ts = paths.textStorage() >> m.mock(ak.NSTextStorage) #ts.deleteCharactersInRange_((0, ts.string().length() >> c.len0)) paths.setSelectedRange_((0, ts.string().length() >> c.len0)) paths.pasteAsPlainText_(opc) paths.setSelectedRange_((0, ts.string().length() >> c.len1)) with m: opc.populateWithClipboard()
def test(c): m = Mocker() fc = FindController.shared_controller() beep = m.replace(NSBeep, passthrough=False) dobeep = True direction = "<direction>" _find = m.method(fc._find) tv = m.method(fc.find_target)() >> (m.mock(TextView) if c.has_tv else None) m.property(fc, "find_value").value >> c.ftext if c.has_tv and c.ftext: text = tv.string() >> "<text>" sel = tv.selectedRange() >> (1, 2) range = _find(text, c.ftext, sel, direction) >> ("<range>" if c.found else None) if c.found: tv.setSelectedRange_(range) tv.scrollRangeToVisible_(range) dobeep = False if dobeep: beep() with m: fc.find(direction)
def test(c): m = Mocker() opc = OpenPathController(None) paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView) with m.order(): ts = paths.textStorage() >> m.mock(ak.NSTextStorage) #ts.deleteCharactersInRange_((0, ts.string().length() >> c.len0)) paths.setSelectedRange_((0, ts.string().length() >> c.len0)) paths.pasteAsPlainText_(opc) paths.setSelectedRange_((0, ts.string().length() >> c.len1)) with m: opc.populateWithClipboard()
def test(c): m = Mocker() app = m.mock(Application) opc = OpenPathController(app) paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView) paths.textStorage().string() >> c.text def check_paths(paths): eq_(c.paths, list(paths)) expect(app.open_documents_with_paths(ANY)).call(check_paths) (m.method(opc.window)() >> m.mock(ak.NSWindow)).orderOut_(opc) with m: opc.open_(None)
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"
def test_OpenPathController_windowDidLoad(): m = Mocker() opc = OpenPathController.alloc().init() tv = m.property(opc, "paths").value >> m.mock(ak.NSTextView) tc = tv.textContainer() >> m.mock(ak.NSTextContainer) tc.setContainerSize_(fn.NSMakeSize(const.LARGE_NUMBER_FOR_TEXT, const.LARGE_NUMBER_FOR_TEXT)) tc.setWidthTracksTextView_(False) tv.setHorizontallyResizable_(True) tv.setAutoresizingMask_(ak.NSViewNotSizable) tv.setFieldEditor_(True) tv.setFont_(ANY) with m: opc.windowDidLoad()
def test(c): m = Mocker() hbc = HoverButtonCell.alloc().init() frame = m.mock(NSRect) view = m.mock(NSOutlineView) point, pressed = hbc.hover_info = c.info if point is not None: m.replace(NSPointInRect)(point, frame) >> (point == "in") row = view.rowAtPoint_(frame.origin >> (1, 1)) >> 2 dgt = m.property(hbc, "delegate").value >> m.mock(EditorWindowController) image = dgt.hoverButtonCell_imageForState_row_(hbc, c.state, row) >> "<img>" with m: eq_(hbc.buttonImageForFrame_inView_(frame, view), image)
def test(c): m = Mocker() app = Application() doc = m.mock(TextDocument) m.method(app.open_documents_with_paths)([app.config.path]) >> [doc] (doc.file_path << app.config.path).count(1, None) default_config = m.property(app.config, "default_config") m.replace("os.path.exists")(app.config.path) >> c.exists if not c.exists: doc.document.text >> c.text if not c.text: doc.document.text = default_config.value >> "# config" with m: app.open_config_file()
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(c): m = Mocker() hbc = HoverButtonCell.alloc().init() frame = m.mock(fn.NSRect) view = m.mock(ak.NSOutlineView) point, pressed = hbc.hover_info = c.info if point is not None: m.replace(fn, 'NSPointInRect')(point, frame) >> (point == "in") row = view.rowAtPoint_(frame.origin >> (1, 1)) >> 2 dgt = m.property(hbc, "delegate").value >> m.mock(EditorWindowController) image = dgt.hoverButtonCell_imageForState_row_(hbc, c.state, row) >> "<img>" with m: eq_(hbc.buttonImageForFrame_inView_(frame, view), image)
def test_OpenPathController_windowDidLoad(): m = Mocker() opc = OpenPathController.alloc().init() tv = m.property(opc, "paths").value >> m.mock(ak.NSTextView) tc = tv.textContainer() >> m.mock(ak.NSTextContainer) tc.setContainerSize_( fn.NSMakeSize(const.LARGE_NUMBER_FOR_TEXT, const.LARGE_NUMBER_FOR_TEXT)) tc.setWidthTracksTextView_(False) tv.setHorizontallyResizable_(True) tv.setAutoresizingMask_(ak.NSViewNotSizable) tv.setFieldEditor_(True) tv.setFont_(ANY) with m: opc.windowDidLoad()
def test(c): m = Mocker() ed = m.mock(Window) editor = m.mock(Editor) app = Application() with app.logger() as errlog: err = m.property(app.errlog, "document").value >> m.mock(TextDocument) if c.is_open: idocs = iter([editor]) m.method(app.set_current_editor)(editor) else: idocs = iter([]) m.method(app.current_window)() >> (ed if c.has_window else None) if not c.has_window: m.method(app.create_window)() >> ed ed.insert_items([err], focus=True) m.method(app.iter_editors_of_document)(err) >> idocs with m: app.open_error_log()
def test(c): m = Mocker() hbc = HoverButtonCell.alloc().init() hbc.hover_info = ("initial", None) frame = m.mock(fn.NSRect) point = c.info[0] pir = m.replace(fn, 'NSPointInRect') if c.method.startswith("mouseUp"): if c.inside is None: hbc.hover_info = (None, None) elif pir("initial", frame) >> c.inside[0] \ and pir(point, frame) >> c.inside[1]: row = (m.method(hbc, "controlView")() >> m.mock(ak.NSOutlineView)) \ .rowAtPoint_(point) >> 2 (m.property(hbc, "delegate").value >> m.mock(EditorWindowController)) \ .hoverButton_rowClicked_(hbc, row) with m: assert getattr(hbc, c.method)(point, frame) eq_(hbc.hover_info, c.info)
def test(c): m = Mocker() hbc = HoverButtonCell.alloc().init() hbc.hover_info = ("initial", None) frame = m.mock(NSRect) point = c.info[0] pir = m.replace(NSPointInRect, passthrough=False) if c.method.startswith("mouseUp"): if c.inside is None: hbc.hover_info = (None, None) elif pir("initial", frame) >> c.inside[0] \ and pir(point, frame) >> c.inside[1]: row = (m.method(hbc, "controlView")() >> m.mock(NSOutlineView)) \ .rowAtPoint_(point) >> 2 (m.property(hbc, "delegate").value >> m.mock(EditorWindowController)) \ .hoverButton_rowClicked_(hbc, row) with m: assert getattr(hbc, c.method)(point, frame) eq_(hbc.hover_info, c.info)
def test(c): m = Mocker() ed = m.mock(Editor) dv = m.mock(TextDocumentView) dv_class = m.replace(edoc, 'TextDocumentView') app = Application() err = m.property(mod.errlog, "document").value >> m.mock(TextDocument) if c.is_open: idocs = iter([dv]) m.method(app.set_current_document_view)(dv) else: idocs = iter([]) m.method(app.current_editor)() >> (ed if c.has_editor else None) if not c.has_editor: m.method(app.create_editor)() >> ed dv_class.create_with_document(err) >> dv ed.add_document_view(dv) ed.current_view = dv m.method(app.iter_views_of_document)(err) >> idocs with m: app.open_error_log()
def test(c): m = Mocker() ed = m.mock(Editor) dv = m.mock(TextDocumentView) dv_class = m.replace(edoc, 'TextDocumentView') app = Application() err = m.property(mod.errlog, "document").value >> m.mock(TextDocument) if c.is_open: idocs = iter([dv]) m.method(app.set_current_document_view)(dv) else: idocs = iter([]) m.method(app.current_editor)() >> (ed if c.has_editor else None) if not c.has_editor: m.method(app.create_editor)() >> ed dv_class.create_with_document(err) >> dv ed.add_document_view(dv) ed.current_view = dv m.method(app.iter_views_of_document)(err) >> idocs with m: app.open_error_log()
def test(c): m = Mocker() fc = FindController.shared_controller() tv = m.mock(TextView) regexfind = m.method(fc.finder.regexfinditer) simplefind = m.method(fc.finder.simplefinditer) sel = fn.NSMakeRange(1, 2) direction = "<direction>" options = m.property(fc.finder, "options").value >> m.mock(FindOptions) ftext = "<find>" if options.regular_expression >> c.regex: finditer = regexfind elif options.match_entire_word >> c.mword: finditer = regexfind ftext = "\\b" + re.escape(ftext) + "\\b" else: finditer = simplefind range = fn.NSMakeRange(sel.location, 0) items = [] rng = None tv.string() >> "<text>" FoundRange = make_found_range_factory( FindOptions(regular_expression=c.regex, match_entire_word=c.mword)) for i, r in enumerate(c.matches): if r is mod.WRAPTOKEN: items.append(r) continue found = FoundRange(fn.NSMakeRange(*r)) items.append(found) if i == 0 and found.range == sel: continue tv._Finder__recently_found_range = found rng = found.range finditer("<text>", ftext, range, direction, True) >> items with m: result = fc.finder._find(tv, "<find>", sel, direction) eq_(result, rng)
def test(c): m = Mocker() fc = FindController.shared_controller() tv = m.mock(TextView) regexfind = m.method(fc.finder.regexfinditer) simplefind = m.method(fc.finder.simplefinditer) sel = fn.NSMakeRange(1, 2) direction = "<direction>" options = m.property(fc.finder, "options").value >> m.mock(FindOptions) ftext = "<find>" if options.regular_expression >> c.regex: finditer = regexfind elif options.match_entire_word >> c.mword: finditer = regexfind ftext = "\\b" + re.escape(ftext) + "\\b" else: finditer = simplefind range = fn.NSMakeRange(sel.location, 0) items = [] rng = None tv.string() >> "<text>" FoundRange = make_found_range_factory( FindOptions(regular_expression=c.regex, match_entire_word=c.mword)) for i, r in enumerate(c.matches): if r is mod.WRAPTOKEN: items.append(r) continue found = FoundRange(fn.NSMakeRange(*r)) items.append(found) if i == 0 and found.range == sel: continue tv._Finder__recently_found_range = found rng = found.range finditer("<text>", ftext, range, direction, True) >> items with m: result = fc.finder._find(tv, "<find>", sel, direction) eq_(result, rng)