def do(m, c, fc, sender): beep = m.replace(ak, 'NSBeep') dobeep = True tv = m.replace(fc.finder, 'find_target')() >> (m.mock(TextView) if c.has_tv else None) if c.has_tv: options = m.replace(fc.finder, "options") rtext = options.replace_text >> "abc" options.regular_expression >> c.regex FoundRange = make_found_range_factory( FindOptions(regular_expression=False)) if c.regex: if c.rfr: tv._Finder__recently_found_range >> FoundRange(None) elif c.rfr is None: expect( tv._Finder__recently_found_range).throw(AttributeError) else: tv._Finder__recently_found_range >> None range = tv.selectedRange() >> m.mock() tv.shouldChangeTextInRange_replacementString_(range, rtext) >> c.act if c.act: tv.textStorage().replaceCharactersInRange_withString_( range, rtext) tv.didChangeText() tv.setNeedsDisplay_(True) dobeep = False if dobeep: beep()
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 finder(self): try: finder = self._finder except Exception: finder = self._finder = Finder( (lambda: self.text_view), FindOptions(ignore_case=False, wrap_around=False), ) return finder
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): options = FindOptions() setattr(options, c.att.name, c.att.before) for dep in c.deps: setattr(options, dep.name, dep.before) eq_(getattr(options, c.att.name), c.att.before, c.att.name) for dep in c.deps: eq_(getattr(options, dep.name), dep.before, dep.name) # make the change, which fires the dependent change setattr(options, c.att.name, c.att.after) eq_(getattr(options, c.att.name), c.att.after, c.att.name) for dep in c.deps: eq_(getattr(options, dep.name), dep.after, dep.name)
def make_options(config): return FindOptions( **{ opt: config[key] for key, opt in { "find": "find_text", "replace": "replace_text", "action": "action", "search": "search_type", "ignore_case": "ignore_case", "wrap": "wrap_around", }.items() if key in config })
def test_FindController_recentFindSelected_(): Config = TestConfig 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) yield test, "/abc", FindOptions(find_text="abc") yield test, "/abc// unknown-action", FindOptions() yield test, "/abc/def/i all word no-wrap", FindOptions( find_text="abc", replace_text="def", action="find_next", ignore_case=True, search_type=mod.WORD, wrap_around=False, )
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() 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)