Example #1
0
 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)
Example #2
0
 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)
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, c.real)(*c.args)
     with m:
         getattr(fc, c.meth)(sender)
Example #4
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 SELECTION_REQUIRED_ACTIONS:
                 tv.selectedRange().length >> c.sel
     with m:
         result = fc.validate_action(c.tag)
         eq_(result, c.result)
Example #5
0
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)
Example #6
0
 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)
Example #7
0
    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>")
Example #8
0
 def test(c):
     m = Mocker()
     fc = FindController.create()
     flog = m.replace("editxt.findpanel.log", passthrough=False)
     sender = m.mock()
     (sender.tag() << c.tag).count(1, 2)
     func = None
     for tag, meth in list(fc.action_registry.items()):
         if tag == c.tag:
             func = m.replace(meth, passthrough=False)
         else:
             fc.action_registry[tag] = m.mock(meth)
     if c.fail:
         flog.info(ANY, c.tag)
     else:
         func(sender)
     with m:
         fc.perform_action(sender)
Example #9
0
 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)
Example #10
0
 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)
Example #11
0
 def test(c):
     m = Mocker()
     fc = FindController.create()
     fc.opts = opts = FindOptions()
     opts.find_text = c.astate.get("find_text", u"")
     nsud = m.replace(NSUserDefaults)
     nspb = m.replace(NSPasteboard)
     if "find_text" in c.astate:
         pboard = nspb.pasteboardWithName_(NSFindPboard)
         pboard.declareTypes_owner_([NSStringPboardType], None)
         pboard.setString_forType_(c.astate["find_text"], NSStringPboardType)
     zstate = {}
     for k, v in FindController.OPTIONS_DEFAULTS.iteritems():
         setattr(opts, k, c.astate.get(k, v))
         zstate[k] = c.zstate.get(k, v)
     defaults = nsud.standardUserDefaults() >> m.mock(NSUserDefaults)
     def do(state, key):
         eq_(state, zstate)
         return True
     expect(defaults.setObject_forKey_(ANY, const.FIND_PANEL_OPTIONS_KEY)).call(do)
     with m:
         fc.save_options()
Example #12
0
 def test(c):
     m = Mocker()
     nsud = m.replace(NSUserDefaults, passthrough=False)
     nspb = m.replace(NSPasteboard, passthrough=False)
     defaults = nsud.standardUserDefaults() >> m.mock(NSUserDefaults)
     defaults.dictionaryForKey_(const.FIND_PANEL_OPTIONS_KEY) >> c.state
     pboard = nspb.pasteboardWithName_(NSFindPboard)
     pboard.availableTypeFromArray_([NSStringPboardType]) >> c.has_ftext
     if c.has_ftext:
         pboard.stringForType_(NSStringPboardType) >> c.ftext
     with m:
         fc = FindController.create() # calls load_options()
         opts = fc.opts
         if c.state is not None:
             for k, v in FindController.OPTIONS_DEFAULTS.iteritems():
                 eq_(getattr(opts, k), c.state.get(k, v), k)
         assert isinstance(opts.recent_finds, NSMutableArray)
         assert isinstance(opts.recent_replaces, NSMutableArray)
         if c.has_ftext:
             eq_(opts.find_text, c.ftext)
         else:
             eq_(opts.find_text, u"")
Example #13
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 #14
0
 def performFindPanelAction_(self, sender):
     FindController.shared_controller().perform_action(sender)
Example #15
0
def test_FindController_shared_controller():
    fc = FindController.shared_controller()
    assert isinstance(fc, FindController), fc
    f2 = FindController.shared_controller()
    assert fc is f2