Example #1
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])
Example #2
0
 def test(f_exists):
     with make_file() as path:
         if f_exists:
             stat = os.stat(path)
             res = (stat.st_size, stat.st_mtime)
         else:
             path += "-not-found"
             res = None
         eq_(filestat(path), res)
Example #3
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()
Example #4
0
 def test(app, expected, actions=(), create=True):
     with make_file(content="" if create else None) as path:
         m = Mocker()
         doc = app.document_with_path(path)
         for action in actions:
             if action == "move":
                 new_path = path + ".moved"
                 os.rename(path, new_path)
                 path = doc.file_path = new_path
             elif action == "save":
                 doc.save()
             else:
                 assert action == "modify", action
                 with open(path, "a") as file:
                     file.write("modified")
         result = doc.file_changed_since_save()
         eq_(result, expected, (doc.file_path, doc.persistent_path))
Example #5
0
    def test(c):
        """check if this document has been externally modified

        is_externally_modified returns True, False or None. If a file exists at the
        path of this document then return True if the document has been modified by
        another program else False. However, if there is no file at the path of
        this document this function will return None.
        """
        with make_file() as path, test_app() as app:
            if not c.exists:
                path += "-not-found"
            doc = TextDocument(app, (None if c.url_is_none else path))
            if c.exists:
                if c.change == "modify":
                    stat = filestat(path)
                    with open(path, "a", encoding="utf-8") as file:
                        file.write("more data")
                    assert stat != filestat(path)
                elif c.change == "remove":
                    os.remove(path)
            eq_(doc.is_externally_modified(), c.rval)
Example #6
0
 def test(path_type):
     # rules:
     # - relative path means new file, not yet saved (do not read, start with blank file)
     # - absolute path means file should be read from disk
     #   - if file exists, attempt to read it
     #   - if not exists, start with empty file
     content = "test content"
     with make_file(content=content) as path, test_app() as app:
         if path_type == "relative":
             path = None
         elif path_type == "abs-missing":
             os.remove(path)
         else:
             eq_(path_type, "abs-exists")
         doc = app.document_with_path(path)
         if path_type == "abs-exists":
             content = "content changed"
             with open(path, "w") as fh:
                 fh.write(content)
             assert doc.is_externally_modified()
         else:
             content = ""
         eq_(doc.text, content) # triggers doc._load()
         assert not doc.is_externally_modified()
Example #7
0
 def test(title):
     with test_app() as app, make_file(title or "not used") as path:
         doc = app.document_with_path(path if title else None)
         eq_(doc.displayName(), title or "untitled")