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(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(command, options): m = Mocker() nspb = m.replace(ak, 'NSPasteboard') pboard = nspb.pasteboardWithName_(ak.NSFindPboard) pboard.availableTypeFromArray_([ak.NSStringPboardType]) >> None with m, replace_history() as history: fc = FindController() sender = Config(selectedItem=lambda: Config(title=lambda: command)) fc.recentFindSelected_(sender) eq_(fc.options._target, options)
def test(command, options): m = Mocker() nspb = m.replace(ak, 'NSPasteboard') pboard = nspb.pasteboardWithName_(ak.NSFindPboard) pboard.availableTypeFromArray_([ak.NSStringPboardType]) >> None with m, test_app() as app: fc = FindController(app) sender = Config(selectedItem=lambda:Config(title=lambda:command)) fc.recentFindSelected_(sender) eq_(fc.options._target, options)
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): with test_app() as app: m = Mocker() fc = FindController(app) if c.tag in fc.action_registry: editor = (m.mock(Editor) if c.has_target else None) m.method(fc.get_editor)() >> editor if c.has_target: if c.tag in mod.SELECTION_REQUIRED_ACTIONS: editor.selection >> (0, c.sel) with m, replattr(fc, 'options', FindOptions()): result = fc.validate_action(c.tag) eq_(result, c.result)
def validateUserInterfaceItem_(self, item): if item.action() == "performFindPanelAction:": return FindController.shared_controller().validate_action( item.tag()) elif item.action() == "performTextCommand:": return app.text_commander.is_textview_command_enabled(self, item) return super(TextView, self).validateUserInterfaceItem_(item)
def test(c): m = Mocker() fc = FindController() flog = m.replace("editxt.command.find.log") 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: func(sender) with m: fc.perform_action(sender)
def test(c): m = Mocker() with replace_history() as history: fc = FindController() # calls load_options() fc.options.find_text = "" # clear value from real pasteboard fc.options.ignore_case = False nspb = m.replace(ak, 'NSPasteboard') if "find_text" in c.opts: pboard = nspb.pasteboardWithName_(ak.NSFindPboard) pboard.declareTypes_owner_([ak.NSStringPboardType], None) pboard.setString_forType_(c.opts["find_text"], ak.NSStringPboardType) with m: for k, v in c.opts.items(): setattr(fc.options, k, v) print(fc.options._target) eq_(fc.save_options(), c.res, fc.options._target) eq_(list(history), [] if c.hist is None else [c.hist])
def test(c): m = Mocker() fc = FindController.shared_controller() beep = m.replace(ak, 'NSBeep') dobeep = True tv = m.replace(fc.finder, 'find_target')() >> (m.mock(TextView) if c.has_tv else None) options = m.replace(fc.finder, "options") ftext = options.find_text >> c.ftext range = ( tv.selectedRange() >> fn.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 if not c.sel_only: if (options.wrap_around >> c.wrap): range = fn.NSMakeRange(0, 0) else: range = fn.NSMakeRange(range[0], len(text) - range[0]) if options.regular_expression >> c.regex: finditer = m.method(fc.finder.regexfinditer) elif options.match_entire_word >> c.mword: ftext = "\\b" + re.escape(ftext) + "\\b" finditer = m.method(fc.finder.regexfinditer) else: finditer = m.method(fc.finder.simplefinditer) rtext = options.replace_text >> c.rtext found = None ranges = [] rtexts = [] items = [] FoundRange = make_found_range_factory( FindOptions(regular_expression=c.regex, match_entire_word=c.mword)) for r in c.ranges: found = FoundRange(fn.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 = fn.NSMakeRange(start, sum(c.ranges[-1]) - start) value = "".join(rtexts) if tv.shouldChangeTextInRange_replacementString_( range, value) >> c.replace: ts = tv.textStorage() >> m.mock(ak.NSTextStorage) ts.replaceCharactersInRange_withString_(range, value) tv.didChangeText() tv.setNeedsDisplay_(True) dobeep = False eq_(dobeep, c.beep) if dobeep: beep() with m: fc.finder._replace_all(c.sel_only)
def test(c): m = Mocker() fc = FindController.shared_controller() sender = m.mock() if "do" in c: c.do(m, c, fc, sender) else: m.method(fc.finder, c.real)(*c.args) with m: getattr(fc, c.meth)(sender)
def test(c): with test_app() as app: m = Mocker() fc = FindController(app) iter_windows = m.method(app.iter_windows) if c.has_window: window = m.mock(Window) x = iter([window]) window.current_editor >> (m.mock(Editor) if c.has_editor else None) else: x = iter([]) iter_windows() >> x assert fc.app is not None with m: result = fc.get_editor() if c.has_editor: assert result is not None else: eq_(result, None)
def test(c): with test_app() as app: m = Mocker() beep = m.replace(mod, 'beep') fc = FindController(app) flash = m.method(fc.flash_status_text) mark = m.method(fc.finder.mark_occurrences) m.method(fc.get_editor)() >> (m.mock(Editor) if c.has_tv else None) if c.has_tv: ftext = "<find>" mark(ftext, c.regex, timeout=30) >> c.cnt if c.cnt: flash("%i occurrences" % c.cnt) else: flash("Not found") else: beep() with m: fc.count_occurrences("<find>", c.regex)
def test(c): m = Mocker() fc = FindController.shared_controller() if c.tag in fc.action_registry: tv = (m.mock(TextView) if c.has_target else None) m.method(fc.find_target)() >> tv if c.has_target: if c.tag in mod.SELECTION_REQUIRED_ACTIONS: tv.selectedRange().length >> c.sel with m, replattr(fc, 'options', FindOptions()): result = fc.validate_action(c.tag) eq_(result, c.result)
def test(c): m = Mocker() fc = FindController.shared_controller() beep = m.replace(ak, 'NSBeep') dobeep = True tv = m.replace(fc.finder, 'find_target')() >> (m.mock(TextView) if c.has_tv else None) options = m.replace(fc.finder, "options") ftext = options.find_text >> c.ftext range = (tv.selectedRange() >> fn.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 if not c.sel_only: if (options.wrap_around >> c.wrap): range = fn.NSMakeRange(0, 0) else: range = fn.NSMakeRange(range[0], len(text) - range[0]) if options.regular_expression >> c.regex: finditer = m.method(fc.finder.regexfinditer) elif options.match_entire_word >> c.mword: ftext = "\\b" + re.escape(ftext) + "\\b" finditer = m.method(fc.finder.regexfinditer) else: finditer = m.method(fc.finder.simplefinditer) rtext = options.replace_text >> c.rtext found = None ranges = [] rtexts = [] items = [] FoundRange = make_found_range_factory( FindOptions(regular_expression=c.regex, match_entire_word=c.mword)) for r in c.ranges: found = FoundRange(fn.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 = fn.NSMakeRange(start, sum(c.ranges[-1]) - start) value = "".join(rtexts) if tv.shouldChangeTextInRange_replacementString_(range, value) >> c.replace: ts = tv.textStorage() >> m.mock(ak.NSTextStorage) ts.replaceCharactersInRange_withString_(range, value) tv.didChangeText() tv.setNeedsDisplay_(True) dobeep = False eq_(dobeep, c.beep) if dobeep: beep() with m: fc.finder._replace_all(c.sel_only)
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(c): m = Mocker() nspb = m.replace(ak, 'NSPasteboard') pboard = nspb.pasteboardWithName_(ak.NSFindPboard) pboard.availableTypeFromArray_([ak.NSStringPboardType ]) >> (c.ftext is not None) if c.ftext is not None: pboard.stringForType_(ak.NSStringPboardType) >> c.ftext with replace_history() as history: if c.hist is not None: history.append(c.hist) with m: fc = FindController() # calls load_options() eq_(fc.options._target, FindOptions(**c.opts)) eq_(fc.options.recent_finds, [] if c.hist is None else [c.hist])
def test(c): m = Mocker() beep = m.replace(ak, 'NSBeep') fc = FindController.shared_controller() flash = m.method(fc.flash_status_text) mark = m.method(fc.finder.mark_occurrences) tv = m.method(fc.find_target)() >> (m.mock(TextView) if c.has_tv else None) if c.has_tv: ftext = "<find>" mark(ftext, c.regex) >> c.cnt if c.cnt: flash("%i occurrences" % c.cnt) else: flash("Not found") else: beep() with m: fc.count_occurrences("<find>", c.regex)
def test(c): m = Mocker() beep = m.replace(ak, 'NSBeep') fc = FindController.shared_controller() flash = m.method(fc.flash_status_text) mark = m.method(fc.finder.mark_occurrences) tv = m.method( fc.find_target)() >> (m.mock(TextView) if c.has_tv else None) if c.has_tv: ftext = "<find>" mark(ftext, c.regex) >> c.cnt if c.cnt: flash("%i occurrences" % c.cnt) else: flash("Not found") else: beep() with m: fc.count_occurrences("<find>", c.regex)
def test(c): m = Mocker() fc = FindController.shared_controller() app = m.replace(editxt, "app") if c.has_ed: ed = m.mock(Editor) x = iter([ed]) dv = ed.current_view >> (m.mock(TextDocumentView) if c.has_dv else None) if c.has_dv: dv.text_view >> (m.mock(TextView) if c.has_tv else None) else: x = iter([]) app.iter_editors() >> x with m: result = fc.find_target() if c.has_ed and c.has_tv: assert result is not None else: eq_(result, None)
def test(c): m = Mocker() fc = FindController.shared_controller() beep = m.replace(ak, 'NSBeep') dobeep = True direction = "<direction>" _find = m.method(fc.finder._find) tv = m.replace(fc.finder, 'find_target')() >> (m.mock(TextView) if c.has_tv else None) m.replace(fc.finder, "options").find_text >> c.ftext if c.has_tv and c.ftext: sel = tv.selectedRange() >> (1, 2) range = _find(tv, 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.finder.find(direction)
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 performFindPanelAction_(self, sender): FindController.shared_controller().perform_action(sender)
def test_FindController_shared_controller(): fc = FindController.shared_controller() assert isinstance(fc, FindController), fc f2 = FindController.shared_controller() assert fc is f2
def test_FindController_shared_controller(): with test_app() as app: fc = FindController.shared_controller(app) assert isinstance(fc, FindController), fc f2 = FindController.shared_controller(app) assert fc is f2
def _find_controller(self): from editxt.command.find import FindController return FindController.shared_controller(self.app)
def validateUserInterfaceItem_(self, item): if item.action() == "performFindPanelAction:": return FindController.shared_controller().validate_action(item.tag()) elif item.action() == "performTextCommand:": return app.text_commander.is_textview_command_enabled(self, item) return super(TextView, self).validateUserInterfaceItem_(item)