Example #1
0
 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)
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
 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)
Example #11
0
 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)
Example #12
0
 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)
Example #13
0
 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)
Example #14
0
 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)
Example #15
0
 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)
Example #16
0
 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)
Example #17
0
 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)
Example #18
0
 def performFindPanelAction_(self, sender):
     FindController.shared_controller().perform_action(sender)
Example #19
0
 def performFindPanelAction_(self, sender):
     FindController.shared_controller().perform_action(sender)
Example #20
0
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
Example #21
0
 def _find_controller(self):
     from editxt.command.find import FindController
     return FindController.shared_controller(self.app)
Example #22
0
def test_FindController_shared_controller():
    fc = FindController.shared_controller()
    assert isinstance(fc, FindController), fc
    f2 = FindController.shared_controller()
    assert fc is f2
Example #23
0
 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)
Example #24
0
def test_FindController_shared_controller():
    fc = FindController.shared_controller()
    assert isinstance(fc, FindController), fc
    f2 = FindController.shared_controller()
    assert fc is f2