Exemple #1
0
 def do_test(windows_template):
     app = Application()
     m = Mocker()
     seen = set()
     dirty_docs = []
     eds = []
     for ecfg in windows_template:
         projects = []
         for pcfg in ecfg:
             proj = m.mock(Project)
             projects.append(proj)
             editors = []
             has_dirty = False
             for doc_id in pcfg:
                 editor = m.mock(Editor)
                 editors.append(editor)
                 (editor.document.id << doc_id).count(1, 2)
                 if doc_id not in seen:
                     seen.add(doc_id)
                     dirty_docs.append(editor)
                     has_dirty = True
             proj.dirty_editors() >> editors
             if has_dirty:
                 dirty_docs.append(proj)
         ed = m.mock(Window)
         ed.projects >> projects
         eds.append(ed)
     m.method(app.iter_windows)() >> eds
     with m:
         result = list(app.iter_dirty_editors())
         eq_(result, dirty_docs)
Exemple #2
0
 def test(eds_config):
     app = Application()
     m = Mocker()
     create_window = m.method(app.create_window)
     open_error_log = m.method(app.open_error_log)
     nsapp = m.mock(ak.NSApplication)
     ud_class = m.replace(fn, 'NSUserDefaults')
     m.method(app.iter_saved_window_states)() >> iter(eds_config)
     tc = m.replace(app, 'text_commander', spec=CommandManager)
     dc = m.mock(DocumentController)
     tc.load_commands(dc.textMenu >> m.mock(ak.NSMenu))
     tc.load_shortcuts(dc.shortcutsMenu >> m.mock(ak.NSMenu))
     if eds_config:
         error = False
         for ed_config in eds_config:
             if isinstance(ed_config, mod.StateLoadFailure):
                 error = True
             else:
                 create_window(ed_config)
         if error:
             open_error_log(set_current=False)
     else:
         create_window()
     with m:
         app.application_will_finish_launching(nsapp, dc)
         eq_(app.text_commander, tc)
Exemple #3
0
 def test(app, has_text_storage, event=None):
     INDENT_SIZE = 42
     m = Mocker()
     ps_class = m.replace(ak, 'NSParagraphStyle')
     doc = TextDocument(app)
     if has_text_storage:
         with m.off_the_record():
             ts = doc.text_storage = m.mock(ak.NSTextStorage)
     else:
         ts = doc.text_storage = None
     font = app.default_font.font
     tabw = ak.NSString.stringWithString_("8" * INDENT_SIZE) \
              .sizeWithAttributes_({ak.NSFontAttributeName: font}).width
     ps = ps_class.defaultParagraphStyle().mutableCopy() >> m.mock()
     ps.setTabStops_([])
     ps.setDefaultTabInterval_(tabw)
     attrs = {
         ak.NSFontAttributeName: font,
         ak.NSParagraphStyleAttributeName: ps,
         ak.NSForegroundColorAttributeName: app.theme.text_color,
     }
     if has_text_storage:
         no_color = {k: v for k, v in attrs.items()
                     if k != ak.NSForegroundColorAttributeName}
         ts.addAttributes_range_(no_color, fn.NSMakeRange(0, ts.length() >> 20))
         ts.setFont_(doc.font.font)
     editors = [m.mock(Editor), m.mock(Editor)]
     m.method(app.iter_editors_of_document)(doc) >> editors
     for editor in editors:
         editor.set_text_attributes(attrs)
     with m:
         doc.reset_text_attributes(INDENT_SIZE, event)
         eq_(doc.default_text_attributes(), attrs)
         eq_(doc.syntax_needs_color,
             has_text_storage and event and event.theme_changed)
Exemple #4
0
 def do_test(editors_template):
     app = Application()
     m = Mocker()
     seen = set()
     dirty_docs = []
     eds = []
     for ecfg in editors_template:
         projects = []
         for pcfg in ecfg:
             proj = m.mock(Project)
             projects.append(proj)
             documents = []
             has_dirty = False
             for doc_id in pcfg:
                 dv = m.mock(TextDocumentView)
                 documents.append(dv)
                 (dv.document.id << doc_id).count(1, 2)
                 if doc_id not in seen:
                     seen.add(doc_id)
                     dirty_docs.append(dv)
                     has_dirty = True
             proj.dirty_documents() >> documents
             if has_dirty:
                 dirty_docs.append(proj)
         ed = m.mock(Editor)
         ed.projects >> projects
         eds.append(ed)
     m.method(app.iter_editors)() >> eds
     with m:
         result = list(app.iter_dirty_documents())
         eq_(result, dirty_docs)
Exemple #5
0
 def test(serial):
     m = Mocker()
     proj = Project.create()
     log = m.replace("editxt.project.log", passthrough=False)
     nsdat = m.replace(NSData, passthrough=False)
     nspls = m.replace(NSPropertyListSerialization, passthrough=False)
     create_document_view_with_state = m.method(Project.create_document_view_with_state)
     create_document_view = m.method(Project.create_document_view)
     proj._documents = docs = m.mock(KVOList)
     if "path" in serial:
         data = nsdat.dataWithContentsOfFile_(serial["path"]) >> m.mock()
         serial_, format, error = nspls. \
             propertyListFromData_mutabilityOption_format_errorDescription_( \
                 data, NSPropertyListImmutable, None, None) >> ({}, m.mock(), None)
     else:
         serial_ = serial
     docs_ = serial_.get("documents", [])
     for item in docs_:
         create_document_view_with_state(item)
         if item == "doc_not_found":
             m.throw(Exception("document not found"))
             log.warn("cannot open document: %r" % item)
         #proj._is_dirty = True
     bool(docs); m.result(bool(docs_))
     if not docs_:
         create_document_view()
         #proj._is_dirty = True
     with m:
         proj.deserialize(serial)
         if "path" in serial:
             eq_(proj.path, serial["path"])
             assert "name" not in serial
         else:
             eq_(proj.name, serial.get("name", const.UNTITLED_PROJECT_NAME))
             eq_(proj.expanded, serial.get("expanded", True))
Exemple #6
0
 def test(ed_config):
     ac = Application()
     m = Mocker()
     df_class = m.replace("editxt.application.NSUserDefaults")
     iter_editors = m.method(ac.iter_editors)
     save_open_projects = m.method(ac.save_open_projects)
     discard_editor = m.method(ac.discard_editor)
     nsapp = m.mock()
     ac.editors = eds = []
     all_settings = []
     for i in ed_config:
         ed = m.mock(Editor)
         eds.append(ed)
         ed.window_settings_loaded >> (i != 2)
         if i != 2:
             settings = "<settings %s>" % i
             all_settings.append(settings)
             ed.window_settings >> settings
     iter_editors(nsapp) >> reversed(eds)
     defaults = df_class.standardUserDefaults() >> MockUserDefaults()
     save_open_projects(defaults)
     with m:
         ac.app_will_terminate(nsapp)
     result = defaults.arrayForKey_(const.WINDOW_SETTINGS_DEFAULTS_KEY)
     eq_(result, all_settings)
     assert defaults.synced
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
 def test(app, path, prompt=False):
     prompt = (True,) if prompt else ()
     initial_content = None if "missing" in path else "initial"
     with make_file(path, content=initial_content) as real_path:
         m = Mocker()
         window = app.windows[0]
         editor = window.projects[0].editors[0]
         document = editor.document
         save_document_as = m.method(window.save_document_as)
         prompt_to_overwrite = m.method(window.prompt_to_overwrite)
         has_path_changed = m.method(document.file_changed_since_save)
         if os.path.isabs(path):
             document.file_path = path = real_path
         elif path:
             document.file_path = path
         document.text = "saved text"
         def save_prompt(document, callback):
             if "nodir" in path:
                 os.mkdir(os.path.dirname(real_path))
             print("saving as", real_path)
             callback(real_path)
         if prompt or path != real_path or "nodir" in path or "missing" in path:
             expect(save_document_as(editor, ANY)).call(save_prompt)
         elif has_path_changed() >> ("moved" in path):
             expect(prompt_to_overwrite(editor, ANY)).call(save_prompt)
         calls = []
         def callback(saved):
             calls.append(saved)
         with m:
             editor.save(*prompt, callback=callback)
             eq_(get_content(real_path), "saved text")
             eq_(calls, [True])
Exemple #10
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)
Exemple #11
0
def test_set_syntaxdef(app):
    from editxt.syntax import SyntaxDefinition
    m = Mocker()
    sd = m.mock(SyntaxDefinition)
    doc = TextDocument(app)
    m.method(doc.color_text)()
    with m:
        doc.syntaxdef = sd
        eq_(doc.syntaxer.syntaxdef, sd)
Exemple #12
0
 def test(c):
     def end():
         with m:
             doc.reload_document()
             eq_(doc.text, text)
     text = "edit"
     config = " ".join("project" for x in c.view_state)
     with make_file(content="disk") as path, test_app(config) as app:
         if c.url_is_none:
             path = "not-saved"
         elif not c.exists:
             path += "-not-found"
         m = Mocker()
         call_later = m.replace(mod, "call_later")
         doc = TextDocument(app, path)
         reset_text_attributes = m.method(doc.reset_text_attributes)
         doc.text = text
         if c.url_is_none or not c.exists:
             return end()
         undo = doc.undo_manager = m.mock(UndoManager)
         if not c.read2_success:
             os.remove(path)
             os.mkdir(path)
             return end()
         text = "disk"
         tv = m.mock(TextView)
         for i, text_view_exists in enumerate(c.view_state):
             project = app.windows[0].projects[i]
             with m.off_the_record():
                 editor = Editor(project, document=doc)
             editor.text_view = (tv if text_view_exists else None)
             project.editors.append(editor)
         reset_text_attributes()
         if not any(c.view_state):
             # TODO reload without undo
             #doc_ts.replaceCharactersInRange_withString_(range, text)
             undo.removeAllActions()
             return end()
         range = fn.NSRange(0, 4)
         tv.shouldChangeTextInRange_replacementString_(range, text) >> True
         # TODO get edit_state of each document view
         # TODO diff the two text blocks and change chunks of text
         # throughout the document (in a single undo group if possible)
         # also adjust edit states if possible
         # see http://www.python.org/doc/2.5.2/lib/module-difflib.html
         #doc_ts.replaceCharactersInRange_withString_(range, text)
         tv.didChangeText()
         tv.breakUndoCoalescing()
         # TODO restore edit states
         # HACK use timed invocation to allow didChangeText notification
         # to update change count before _clearUndo is invoked
         call_later(0, doc.clear_dirty)
         tv.select((0, 0))
         reset_text_attributes()
         m.method(doc.update_syntaxer)()
         end()
Exemple #13
0
def test_SortLinesController_sort_():
    m = Mocker()
    sort = m.replace(sortlines, passthrough=False)
    tv = m.mock(TextView)
    slc = SortLinesController.create_with_textview(tv)
    sort(tv, slc.opts)
    m.method(slc.save_options)()
    m.method(slc.cancel_)(None)
    with m:
        slc.sort_(None)
Exemple #14
0
def test_WrapLinesController_wrap_():
    m = Mocker()
    cmd = m.replace(mod, 'wrap_selected_lines')
    tv = m.mock(TextView)
    ctl = WrapLinesController(tv)
    cmd(tv, ctl.options)
    m.method(ctl.save_options)()
    m.method(ctl.cancel_)(None)
    with m:
        ctl.wrap_(None)
Exemple #15
0
def test_WrapLinesController_wrap_():
    m = Mocker()
    cmd = m.replace(mod, 'wrap_selected_lines')
    tv = m.mock(TextView)
    ctl = WrapLinesController(tv)
    cmd(tv, ctl.options)
    m.method(ctl.save_options)()
    m.method(ctl.cancel_)(None)
    with m:
        ctl.wrap_(None)
Exemple #16
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)
Exemple #17
0
def test_init_with_serial():
    m = Mocker()
    kvo_class = m.replace(mod, 'KVOList')
    deserialize = m.method(Project.deserialize)
    reset_cache = m.method(Project.reset_serial_cache)
    docs = kvo_class.alloc().init() >> []
    deserialize("<serial>")
    reset_cache(); m.count(2)
    with m:
        proj = Project.alloc().init_with_serial("<serial>")
Exemple #18
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)
Exemple #19
0
 def test(c):
     with test_app() as app:
         m = Mocker()
         fc = FindController(app)
         beep = m.replace(mod, 'beep')
         dobeep = True
         editor = m.replace(fc.finder, 'get_editor')() >> (m.mock(Editor) if c.has_tv else None)
         options = m.replace(fc.finder, "options")
         ftext = options.find_text >> c.ftext
         range = (editor.selection >> 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 = editor.text >> Text(c.text)
             if not c.sel_only:
                 if (options.wrap_around >> c.wrap):
                     range = (0, 0)
                 else:
                     range = (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 rng in c.ranges:
                 found = FoundRange(rng)
                 if ranges:
                     rtexts.append(text[sum(ranges[-1]):rng[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 = (start, sum(c.ranges[-1]) - start)
                 value = "".join(rtexts)
                 tv = editor.text_view >> m.mock(TextView)
                 if tv.shouldChangeTextInRange_replacementString_(range, value) >> c.replace:
                     tv.didChangeText()
                     tv.setNeedsDisplay_(True)
                     dobeep = False
         eq_(dobeep, c.beep)
         if dobeep:
             beep()
         with m:
             fc.finder._replace_all(c.sel_only)
             if c.ranges and c.replace:
                 eq_(text[:], "<XEXX>")
Exemple #20
0
 def test(c):
     with test_app() as app:
         m = Mocker()
         fc = FindController(app)
         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)
Exemple #21
0
def test_SortLinesController_sort_():
    m = Mocker()
    sort = m.replace(mod, 'sortlines')
    with test_app() as app:
        editor = base.Options(app=app)
        slc = SortLinesController(editor)
        sort(editor, slc.options)
        m.method(slc.save_options)()
        m.method(slc.cancel_)(None)
        with m:
            slc.sort_(None)
Exemple #22
0
 def test(c):
     m = Mocker()
     app = Application()
     create_window = m.method(app.create_window)
     window = m.mock(Window)
     m.method(app.current_window)() >> (window if c.has_window else None)
     if not c.has_window:
         create_window() >> window
     items = window.open_paths("<paths>") >> m.mock()
     with m:
         app.open_documents_with_paths("<paths>")
Exemple #23
0
def test_WrapLinesController_wrap_():
    with test_app() as app:
        m = Mocker()
        editor = base.Options(app=app)
        cmd = m.replace(mod, 'wrap_selected_lines')
        ctl = WrapLinesController(editor)
        cmd(editor, ctl.options)
        m.method(ctl.save_options)()
        m.method(ctl.cancel_)(None)
        with m:
            ctl.wrap_(None)
Exemple #24
0
def test_init_with_serial():
    m = Mocker()
    kvo_class = m.replace(mod, 'KVOList')
    deserialize = m.method(Project.deserialize)
    reset_cache = m.method(Project.reset_serial_cache)
    docs = kvo_class.alloc().init() >> []
    deserialize("<serial>")
    reset_cache()
    m.count(2)
    with m:
        proj = Project.alloc().init_with_serial("<serial>")
Exemple #25
0
def test_SortLinesController_sort_():
    m = Mocker()
    sort = m.replace(mod, 'sortlines')
    tv = m.mock(TextView)
    with replace_history() as history:
        slc = SortLinesController(tv)
        sort(tv, slc.options)
        m.method(slc.save_options)()
        m.method(slc.cancel_)(None)
        with m:
            slc.sort_(None)
Exemple #26
0
def test_SortLinesController_sort_():
    m = Mocker()
    sort = m.replace(mod, 'sortlines')
    tv = m.mock(TextView)
    with replace_history() as history:
        slc = SortLinesController(tv)
        sort(tv, slc.options)
        m.method(slc.save_options)()
        m.method(slc.cancel_)(None)
        with m:
            slc.sort_(None)
Exemple #27
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)
Exemple #28
0
    def test(c):
        with test_app() as app:
            m = Mocker()
            fc = FindController(app)
            beep = m.replace(mod, 'beep')
            editor = (m.mock(Editor) if c.has_tv else None)
            m.replace(fc.finder, 'get_editor')() >> editor
            options = m.replace(fc.finder, "options")
            ftext = options.find_text >> c.ftext
            range = (editor.selection >> 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 = editor.text >> Text(c.text)
                if not c.sel_only:
                    if (options.wrap_around >> c.wrap):
                        range = (0, 0)
                    else:
                        range = (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 rng in c.ranges:
                    found = FoundRange(rng)
                    if ranges:
                        rtexts.append(text[sum(ranges[-1]):rng[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 = (start, sum(c.ranges[-1]) - start)
                    value = "".join(rtexts)

                    def put(val, rng, select=False):
                        text[rng] = val
                    (editor.put(value, range) << c.replace).call(put)
            if c.beep:
                beep()
            with m:
                fc.finder._replace_all(c.sel_only)
                if c.ranges and c.replace:
                    eq_(text[:], "<XEXX>")
Exemple #29
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)
Exemple #30
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)
Exemple #31
0
 def test(c):
     app = Application()
     m = Mocker()
     ed = m.mock(Editor) if c.has_editor else None
     m.method(app.current_editor)() >> ed
     if c.has_editor:
         view = m.mock(TextDocumentView) if c.has_view else None
         ed.current_view >> view
         if c.has_view:
             view.perform_close(ed)
     with m:
         app.close_current_document()
Exemple #32
0
 def test(file_exists=True):
     m = Mocker()
     app = Application()
     view = m.mock(TextDocumentView)
     m.method(app.open_documents_with_paths)([app.config.path]) >> [view]
     default_config = m.property(app.config, "default_config")
     m.replace("os.path.exists")(app.config.path) >> file_exists
     if not file_exists:
         default_config.value >> "# config"
         view.document.text = "# config"
     with m:
         app.open_config_file()
Exemple #33
0
 def test(file_exists=True):
     m = Mocker()
     app = Application()
     view = m.mock(TextDocumentView)
     m.method(app.open_documents_with_paths)([app.config.path]) >> [view]
     default_config = m.property(app.config, "default_config")
     m.replace("os.path.exists")(app.config.path) >> file_exists
     if not file_exists:
         default_config.value >> "# config"
         view.document.text = "# config"
     with m:
         app.open_config_file()
Exemple #34
0
 def test(c):
     m = Mocker()
     sv = mod.ThinSplitView.alloc().init()
     view = m.method(sv.subviews)()[c.index] >> m.mock(ak.NSView)
     if m.method(sv.isVertical)() >> c.vertical:
         view.bounds().size.width >> c.thick
     else:
         view.bounds().size.height >> c.thick
     with m:
         result = sv.is_view_visible(c.index, c.minthick)
     eq_(result, c.thick > c.minthick)
     eq_(result, c.vis)
Exemple #35
0
 def test(c):
     app = Application()
     m = Mocker()
     window = m.mock(Window) if c.has_window else None
     m.method(app.current_window)() >> window
     if c.has_window:
         editor = m.mock(Editor) if c.has_editor else None
         window.current_editor >> editor
         if c.has_editor:
             window.close_item(editor)
     with m:
         app.close_current_document()
Exemple #36
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)
Exemple #37
0
 def test(c):
     app = Application()
     m = Mocker()
     ed = m.mock(Editor) if c.has_editor else None
     m.method(app.current_editor)() >> ed
     if c.has_editor:
         view = m.mock(TextDocumentView) if c.has_view else None
         ed.current_view >> view
         if c.has_view:
             view.perform_close(ed)
     with m:
         app.close_current_document()
Exemple #38
0
def test_ChangeIndentationController_execute_():
    m = Mocker()
    tv = m.mock(TextView)
    dv = m.mock(TextDocumentView)
    (tv.doc_view << dv).count(2)
    mode = dv.indent_mode >> "m"
    size = dv.indent_size >> "s"
    dv.change_indentation("m", "s", "m", "s", True)
    m.method(ChangeIndentationController.save_options)()
    m.method(ChangeIndentationController.cancel_)(None)
    with m:
        ctl = ChangeIndentationController.create_with_textview(tv)
        ctl.execute_(None)
Exemple #39
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)
Exemple #40
0
def test_ChangeIndentationController_execute_():
    m = Mocker()
    tv = m.mock(TextView)
    dv = m.mock(TextDocumentView)
    (tv.doc_view << dv).count(2)
    mode = dv.indent_mode >> "m"
    size = dv.indent_size >> "s"
    dv.change_indentation("m", "s", "m", "s", True)
    m.method(ChangeIndentationController.save_options)()
    m.method(ChangeIndentationController.cancel_)(None)
    with m:
        ctl = ChangeIndentationController(tv)
        ctl.execute_(None)
Exemple #41
0
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)
Exemple #42
0
 def test(c):
     m = Mocker()
     tv = TextView.alloc().initWithFrame_(fn.NSMakeRect(0, 0, 100, 100)) # x, y, w, h
     tc = m.method(tv.textContainer)() >> (m.mock(ak.NSTextContainer) if c.setup else None)
     lm = m.method(tv.layoutManager)() >> (m.mock(ak.NSLayoutManager) if c.setup else None)
     sv = m.method(tv.enclosingScrollView)() >> (m.mock(ak.NSScrollView) if c.setup else None)
     height = 100
     if c.setup:
         lm.usedRectForTextContainer_(tc) >> fn.NSMakeRect(0, 0, 100, c.content_height)
         sv.contentSize() >> fn.NSMakeSize(100, 100) # w, h
         if c.content_height + 75 > 100:
             height = c.content_height + 75
     with m:
         tv.setFrameSize_(fn.NSMakeSize(100, height))
         eq_(tv.frameSize(), fn.NSMakeSize(100, c.final_height))
Exemple #43
0
 def test(c):
     m = Mocker()
     nsapp = m.replace(ak, 'NSApp', spec=False)
     opc = OpenPathController.alloc().init()
     tv = m.mock(ak.NSTextView)
     if c.sel == "insertNewline:":
         nsapp().currentEvent().modifierFlags() >> c.mod
         if c.mod & ak.NSCommandKeyMask or c.mod & ak.NSShiftKeyMask:
             tv.insertNewlineIgnoringFieldEditor_(opc)
         else:
             m.method(opc.open_)(opc)
         # check for shift or command (tv.insertTabIgnoringFieldEditor_(self))
         # otherwise open file
     with m:
         eq_(opc.textView_doCommandBySelector_(tv, c.sel), c.res)
Exemple #44
0
 def test(config):
     ac = Application()
     m = Mocker()
     ac.iter_editors = iwc = m.method(ac.iter_editors)
     iwc() >> iter(config)
     with m:
         result = ac.current_editor()
         eq_(result, (config[0] if config else None))
Exemple #45
0
 def test(c):
     m = Mocker()
     tv = m.mock(TextView)
     lnv = create_lnv(tv)
     estimate_line_count = m.method(lnv.estimate_line_count)
     ruleThickness = m.method(lnv.ruleThickness)
     lines = []
     font = None if c.font_is_none else m.mock(ak.NSFont)
     (tv.textStorage() >> m.mock(ak.NSTextStorage)).font() >> font
     if c.font_is_none:
         ruleThickness() >> c.result
     else:
         estimate_line_count(font) >> c.numlines
         cw = font.advancementForGlyph_(ord("0")).width >> 15
     with m:
         result = lnv.calculate_thickness()
         eq_(result, c.result)
         eq_(lnv.lines, lines)
Exemple #46
0
def test_undo_manager():
    from editxt.editor import EditorWindowController
    m = Mocker()
    ew = EditorWindow.alloc().init()
    wc = m.method(ew.windowController)() >> m.mock(EditorWindowController)
    wc.undo_manager() >> "<undo_manager>"
    with m:
        result = ew.undoManager()
        eq_(result, "<undo_manager>")
Exemple #47
0
 def test(c):
     m = Mocker()
     sv = mod.ThinSplitView.alloc().init()
     view = m.method(sv.subviews)()[c.index] >> m.mock(ak.NSView)
     rect = view.frame() >> m.mock(fn.NSRect)
     if m.method(sv.isVertical)() >> c.vertical:
         dim, org = "width", "x"
     else:
         dim, org = "height", "y"
     getattr(rect.size, dim) >> c.vwthick
     if c.vwthick > 0:
         if c.index == 1:
             getattr(rect.origin, org) >> 0
             setattr(rect.origin, org, c.vwthick)
         setattr(rect.size, dim, 0.0)
         m.method(sv._animate_view)(view, rect)
     with m:
         sv.hide_view(c.index)
Exemple #48
0
def test_set_current_document_view():
    ac = Application()
    m = Mocker()
    dv = m.mock(TextDocumentView)
    ac.find_editor_with_document_view = m.method(
        ac.find_editor_with_document_view)
    ed = ac.find_editor_with_document_view(dv) >> m.mock(Editor)
    ed.current_view = dv
    with m:
        ac.set_current_document_view(dv)
Exemple #49
0
 def test(proj_has_path, is_changed):
     m = Mocker()
     proj = Project.create()
     app = m.replace(mod, 'app')
     save_with_path = m.method(proj.save_with_path)
     reset_cache = m.method(proj.reset_serial_cache)
     m.method(proj.serialize_full)() >> "<serial>"
     if proj_has_path:
         proj.path = "test/proj.tmp"
     if is_changed:
         proj.serial_cache = "<invalid-cache>"
         if proj_has_path:
             save_with_path(proj.path)
         app.save_editor_states()
         reset_cache()
     else:
         proj.serial_cache = "<serial>"
     with m:
         proj.save()
Exemple #50
0
 def test(c):
     m = Mocker()
     app = m.replace(editxt, 'app')
     opc = OpenPathController.alloc().init()
     paths = m.property(opc, "paths").value >> m.mock(ak.NSTextView)
     paths.textStorage().string() >> c.text
     app.open_documents_with_paths(c.paths)
     (m.method(opc.window)() >> m.mock(ak.NSWindow)).orderOut_(opc)
     with m:
         opc.open_(None)
Exemple #51
0
 def test(c):
     m = Mocker()
     app = Application()
     exists = lambda path: True
     alog = m.replace(mod, 'log')
     ed = m.mock(Editor)
     dv_class = m.replace(edoc, 'TextDocumentView')
     m.method(app.current_editor)() >> (ed if c.has_editor else None)
     if not c.has_editor:
         m.method(app.create_editor)() >> ed
     focus = None
     for p in c.paths:
         exists(p.path) >> p.exists
         dv = dv_class.create_with_path(p.path) >> m.mock(TextDocumentView)
         focus = ed.add_document_view(dv) >> dv
     if focus is not None:
         ed.current_view = dv
     with replattr(os.path, 'isfile', exists), m:
         app.open_documents_with_paths([p.path for p in c.paths])
Exemple #52
0
 def test(c):
     m = Mocker()
     cell = mod.ImageAndTextCell.alloc().init()
     img = cell._image = m.mock(ak.NSImage) if c.image else None
     frame = fn.NSMakeRect(0, 0, 20, 100)
     view = m.mock(ak.NSView)
     draws = m.method(mod.ImageAndTextCell.drawsBackground)
     color = m.method(mod.ImageAndTextCell.backgroundColor)
     fill = m.replace(ak, 'NSRectFill')
     if c.image:
         img.size() >> fn.NSSize(20, 20)
         if draws() >> c.draws:
             color().set()
             fill(ANY)
         view.isFlipped() >> c.flipped
         img.compositeToPoint_operation_(ANY, ak.NSCompositeSourceOver)
     m.method(ak.NSTextFieldCell.drawWithFrame_inView_)(frame, view)
     with m:
         cell.drawWithFrame_inView_(frame, view)
Exemple #53
0
    def test(states):
        with tempdir() as tmp:
            state_path = os.path.join(tmp, const.STATE_DIR)
            if states:
                # setup previous state
                m = Mocker()
                app = Application(tmp)

                def iter_editors():
                    for ident in states:
                        yield TestConfig(state=[ident])

                m.method(app.iter_editors)() >> iter_editors()
                with m:
                    app.save_editor_states()
                assert os.listdir(state_path), state_path
            app = Application(tmp)
            result = list(app.iter_saved_editor_states())
            eq_(result, [[id] for id in states])
Exemple #54
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)
Exemple #55
0
 def test(eds_config):
     app = Application()
     m = Mocker()
     create_editor = m.method(app.create_editor)
     nsapp = m.mock(ak.NSApplication)
     ud_class = m.replace(fn, 'NSUserDefaults')
     m.method(app.iter_saved_editor_states)() >> iter(eds_config)
     tc = m.replace(app, 'text_commander', spec=TextCommandController)
     dc = m.mock(DocumentController)
     menu = dc.textMenu >> m.mock(ak.NSMenu)
     m.method(app.init_syntax_definitions)()
     tc.load_commands(menu)
     if eds_config:
         for ed_config in eds_config:
             create_editor(ed_config)
     else:
         create_editor()
     with m:
         app.application_will_finish_launching(nsapp, dc)
         eq_(app.text_commander, tc)
Exemple #56
0
 def test(config):
     ac = Application()
     m = Mocker()
     doc = m.mock(TextDocument)
     views = []
     total_views = 0
     #ac.editors = eds = []
     eds = []
     for view_count in config:
         ed = m.mock(Editor)
         eds.append(ed)
         total_views += view_count
         vws = [m.mock(TextDocumentView) for i in range(view_count)]
         ed.iter_views_of_document(doc) >> vws
         views.extend(vws)
     m.method(ac.iter_editors)() >> eds
     with m:
         result = list(ac.iter_views_of_document(doc))
         eq_(result, views)
         eq_(len(result), total_views)
Exemple #57
0
 def test(c):
     m = Mocker()
     menu = m.mock(ak.NSMenu)
     ctl = TextCommandController("<history>")
     handlers = ctl.input_handlers = m.mock(dict)
     add = m.method(ctl.add_command)
     mod = m.mock(dict)
     m.method(ctl.iter_command_modules)() >> [("<path>", mod)]
     cmds = []
     mod.get("text_menu_commands", []) >> cmds
     for i in range(c.commands):
         cmd = "<command %s>" % i
         add(cmd, "<path>", menu)
         cmds.append(cmd)
     hnds = mod.get("input_handlers", {}) >> {}
     for i in range(c.handlers):
         hnds["handle%s" % i] = "<handle %s>" % i
     handlers.update(hnds)
     with m:
         ctl.load_commands(menu)
Exemple #58
0
 def test(has_current):
     m = Mocker()
     ac = Application()
     ac.current_editor = m.method(ac.current_editor)
     if has_current:
         ed = m.mock(Editor)
         proj = (ac.current_editor() >> ed).new_project() >> m.mock(Project)
     else:
         ac.current_editor() >> None
         proj = None
     with m:
         result = ac.new_project()
         eq_(result, proj)
Exemple #59
0
def test_init_syntax_definitions():
    import editxt.syntax as syntax
    m = Mocker()
    app = Application(profile='/editxtdev')
    rsrc_path = m.method(app.resource_path)() >> "/tmp/resources"
    SyntaxFactory = m.replace(syntax, 'SyntaxFactory', spec=False)
    sf = SyntaxFactory() >> m.mock(syntax.SyntaxFactory)
    app_log = m.replace("editxt.application.log")
    for path, info in [(rsrc_path, False), ('/editxtdev', True)]:
        sf.load_definitions(os.path.join(path, const.SYNTAX_DEFS_DIR), info)
    sf.index_definitions()
    with m:
        app.init_syntax_definitions()