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(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() fc = FindController.shared_controller() sender = m.mock() if "do" in c: c.do(m, c, fc, sender) else: m.method(fc, c.real)(*c.args) with m: getattr(fc, c.meth)(sender)
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 SELECTION_REQUIRED_ACTIONS: tv.selectedRange().length >> c.sel with m: result = fc.validate_action(c.tag) eq_(result, c.result)
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(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): m = Mocker() fc = FindController.shared_controller() app = m.replace("editxt.app", passthrough=False) x = expect(app.iter_editors().next()) if c.has_ed: ed = m.mock(Editor) x.result(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.throw(StopIteration) 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(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 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 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