def testLineLength(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) self.assertEqual(doc.lineLength(1), 6) self.assertEqual(doc.lineLength(2), 13) doc = TextDocument(fixtures.get("empty_file.txt")) self.assertEqual(doc.lineLength(1), 1)
def testInitFromEmptyFile(self): doc = TextDocument(fixtures.get("empty_file.txt")) self.assertTrue(doc.isEmpty()) self.assertEqual(doc.filename(), fixtures.get("empty_file.txt")) self.assertFalse(doc.isModified()) self.assertEqual(doc.numLines(), 1) self.assertEqual(doc.documentText(), '\n')
def testInitFromNonEmptyFile(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) self.assertFalse(doc.isEmpty()) self.assertEqual(doc.filename(), fixtures.get("basic_nonempty_file.txt")) self.assertFalse(doc.isModified()) self.assertEqual(doc.numLines(), 2) self.assertEqual(doc.documentText(), 'hello\nhow are you?\n')
def testLineLength(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.lineLength(1), 6) self.assertEqual(doc.lineLength(2), 13) doc = TextDocument() with open(fixtures.get("empty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.lineLength(1), 1)
def testLineLength(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) self.assertEqual(doc.lineLength(1), 6) self.assertEqual(doc.lineLength(2), 13) doc = TextDocument() with open(fixtures.get("empty_file.txt"), "r") as f: doc.read(f) self.assertEqual(doc.lineLength(1), 1)
def testDeleteChars(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) self.assertRaises(ValueError, lambda: doc.deleteChars((1, 3), -1)) d = doc.deleteChars((1, 3), 1) self.assertEqual(doc.lineText(1), "helo\n") self.assertEqual(d[0], "l") d = doc.deleteChars((1, 2), 100) self.assertEqual(doc.lineText(1), "h\n") self.assertEqual(d[0], "elo") d = doc.deleteChars((1, 2), 100) self.assertEqual(doc.lineText(1), "h\n") self.assertEqual(d[0], "") d = doc.deleteChars((1, 1), 100) self.assertEqual(doc.lineText(1), "\n") self.assertEqual(d[0], "h") d = doc.deleteChars((1, 1), 100) self.assertEqual(doc.lineText(1), "\n") self.assertEqual(d[0], "")
def testBreakLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) doc.breakLine((1, 3)) self.assertEqual(doc.lineText(1), "he\n") self.assertEqual(doc.lineText(2), "llo\n")
def testUpdateDocumentMeta(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.createDocumentMetaInfo("Hello") doc.documentMetaInfo("Hello").setData(5) self.assertEqual(doc.documentMetaInfo("Hello").data(), 5)
def testInitFromFile(self): filename = fixtures.get("example_configuration.rc") config = Configuration.initFromFile(filename) self.assertEqual(config['colors.status_bar.fg'], 'red')
def testGetBufferCursorPos(self): f = fixtures.get("editor_state") EditorState.editorStatePath = lambda x : f state = EditorState.instance() self.assertEqual(state.cursorPosForPath('foobar'), (1,2)) self.assertEqual(state.cursorPosForPath('notpresent'), None)
def testDeleteLine2(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2) self.assertFalse(doc.isModified()) self.assertRaises(IndexError, lambda : doc.deleteLine(5)) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2) self.assertFalse(doc.isModified()) doc.deleteLine(2) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.numLines(), 1) self.assertFalse(doc.isEmpty()) self.assertTrue(doc.isModified()) doc.deleteLine(1) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(doc.numLines(), 1) self.assertTrue(doc.isEmpty()) self.assertTrue(doc.isModified())
def testUpdateLineMeta(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) doc.updateLineMeta(1, {"hello": 5}) self.assertEqual(type(doc.lineMeta(1)), dict) self.assertEqual(doc.lineMeta(1)["hello"], 5) self.assertRaises(IndexError, lambda : doc.updateLineMeta(20, {}))
def testJoinWithNextLine2(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) doc.joinWithNextLine(2) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2) self.assertFalse(doc.isModified())
def testUpdateDocumentMeta(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) doc.createDocumentMetaInfo("Hello") doc.documentMetaInfo("Hello").setData(5) self.assertEqual(doc.documentMetaInfo("Hello").data(), 5)
def testDeleteDocumentMeta(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) doc.updateDocumentMeta({"Hello": 5}) self.assertEqual(doc.documentMeta()["Hello"], 5) doc.deleteDocumentMeta("Hello") self.assertNotIn("Hello", doc.documentMeta())
def testUpdateCharMeta(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) doc.updateCharMeta( (1, 3), { "foo" : ["a", "a"], "bar" : ['b', None, 'b'], } )
def testDeleteChars(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) self.assertRaises(ValueError, lambda : doc.deleteChars((1,3), -1)) d = doc.deleteChars( (1,3), 1) self.assertEqual(doc.lineText(1), 'helo\n') self.assertEqual(d[0], 'l') d = doc.deleteChars( (1,2), 100) self.assertEqual(doc.lineText(1), 'h\n') self.assertEqual(d[0], 'elo') d = doc.deleteChars( (1,2), 100) self.assertEqual(doc.lineText(1), 'h\n') self.assertEqual(d[0], '') d = doc.deleteChars( (1,1), 100) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(d[0], 'h') d = doc.deleteChars( (1,1), 100) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(d[0], '')
def testBreakLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.breakLine((1, 3)) self.assertEqual(doc.lineText(1), "he\n") self.assertEqual(doc.lineText(2), "llo\n")
def testLastModified(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) last_modified = doc.lastModified() self.assertEqual(doc.lastModified(), last_modified) time.sleep(0.1) doc.insertLine(1,"") self.assertNotEqual(doc.lastModified(), last_modified)
def testGetBufferCursorPos(self): f = fixtures.get("editor_state") paths.stateFile = lambda : f state = EditorState.instance() self.assertEqual(state.cursorPosForPath('foobar'), (1,2)) self.assertEqual(state.cursorPosForPath('notpresent'), None)
def testDeleteChars(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertRaises(ValueError, lambda: doc.deleteChars((1, 3), -1)) d = doc.deleteChars((1, 3), 1) self.assertEqual(doc.lineText(1), 'helo\n') self.assertEqual(d[0], 'l') d = doc.deleteChars((1, 2), 100) self.assertEqual(doc.lineText(1), 'h\n') self.assertEqual(d[0], 'elo') d = doc.deleteChars((1, 2), 100) self.assertEqual(doc.lineText(1), 'h\n') self.assertEqual(d[0], '') d = doc.deleteChars((1, 1), 100) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(d[0], 'h') d = doc.deleteChars((1, 1), 100) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(d[0], '')
def testCharMeta(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) doc.updateCharMeta((1, 1), {"Hello": [1]}) self.assertEqual(len(doc.charMeta((1, 1))["Hello"]), len(doc.lineText(1))) self.assertEqual(doc.charMeta((1, 1))["Hello"], [1, None, None, None, None, None])
def testInitFromEmptyFile(self): doc = TextDocument() with open(fixtures.get("empty_file.txt"), "r") as f: doc.read(f) self.assertTrue(doc.isEmpty()) self.assertEqual(doc.numLines(), 1) self.assertEqual(doc.documentText(), '\n')
def testJoinWithNextLine2(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.joinWithNextLine(2) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2)
def testInitFromNonEmptyFile(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) self.assertFalse(doc.isEmpty()) self.assertEqual(doc.numLines(), 2) self.assertEqual(doc.documentText(), "hello\nhow are you?\n")
def testUpdateCharMeta(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.updateCharMeta((1, 3), { "foo": ["a", "a"], "bar": ['b', None, 'b'], })
def testInitFromNonEmptyFile(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertFalse(doc.isEmpty()) self.assertEqual(doc.numLines(), 2) self.assertEqual(doc.documentText(), 'hello\nhow are you?\n')
def testInitFromEmptyFile(self): doc = TextDocument() with open(fixtures.get("empty_file.txt"), "r") as f: doc.read(f) self.assertTrue(doc.isEmpty()) self.assertEqual(doc.numLines(), 1) self.assertEqual(doc.documentText(), "\n")
def testInitFromFile(self): filename = fixtures.get("example_configuration.rc") Configuration.setFilename(filename) config = Configuration.instance() self.assertNotEqual(config['colors.status_bar.fg'], None)
def testJoinWithNextLine2(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) doc.joinWithNextLine(2) self.assertEqual(doc.lineText(1), "hello\n") self.assertEqual(doc.lineText(2), "how are you?\n") self.assertEqual(doc.numLines(), 2)
def testDeleteToEndOfWordCommand(self): self.buffer = models.Buffer() with open(fixtures.get("basic_python.py"), 'r') as f: self.buffer.document.read(f) self.buffer.cursor.toPos((1, 6)) command = commands.DeleteToEndOfWordCommand(self.buffer) result = command.execute() self.assertEqual(self.buffer.cursor.pos, (1, 6)) self.assertEqual(self.buffer.document.lineText(1), "#!pyt\n")
def testDeleteToEndOfWordCommand(self): self.buffer = models.Buffer() with open(fixtures.get("basic_python.py"), "r") as f: self.buffer.document.read(f) self.buffer.cursor.toPos((1, 6)) command = commands.DeleteToEndOfWordCommand(self.buffer) result = command.execute() self.assertEqual(self.buffer.cursor.pos, (1, 6)) self.assertEqual(self.buffer.document.lineText(1), "#!pyt\n")
def testDeleteLineMeta(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) doc.updateLineMeta(1, {"hello": 5}) self.assertEqual(doc.lineMeta(1)["hello"], 5) doc.deleteLineMeta(1, ["hello"]) self.assertNotIn("hello", doc.lineMeta(1)) self.assertRaises(IndexError, lambda : doc.deleteLineMeta(20, "hello"))
def testLine(self): doc = TextDocument(fixtures.get("basic_nonempty_file.txt")) print(doc.documentText()) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertRaises(IndexError, lambda : doc.lineText(-1)) self.assertRaises(IndexError, lambda : doc.lineText(0)) self.assertRaises(IndexError, lambda : doc.lineText(5))
def testCharMeta(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.updateCharMeta((1, 1), {"Hello": [1]}) self.assertEqual(len(doc.charMeta((1, 1))["Hello"]), len(doc.lineText(1))) self.assertEqual( doc.charMeta((1, 1))["Hello"], [1, None, None, None, None, None])
def testReplaceChars(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) doc.replaceChars( (1,3), 1, "hello") self.assertEqual(doc.lineText(1), 'hehellolo\n') doc.replaceChars( (1,1), 1, "c") self.assertEqual(doc.lineText(1), 'cehellolo\n')
def testParenthesisPreserved(self): """Check if parentheses are preserved (#124)""" self.buffer = models.Buffer() with open(fixtures.get("issue_124"), "r") as f: self.buffer.document.read(f) self.buffer.cursor.toPos((1, 6)) command = commands.DeleteToEndOfWordCommand(self.buffer) result = command.execute() self.assertEqual(self.buffer.cursor.pos, (1, 6)) self.assertEqual(self.buffer.document.lineText(1), "def f(bar, baz):\n")
def testBug165(self): """Check if the linting data is correctly reset every time we perform a lint operation""" document = self.buffer_list.current.document document.open(fixtures.get("basic_python.py")) document.deleteChars((3,9), 1) self.editor_controller._doLint() self.assertTrue(isinstance(document.lineMetaInfo("LinterResult").data()[2], LinterResult)) document.insertChars((3,9), ')') self.editor_controller._doLint() self.assertEqual(document.lineMetaInfo("LinterResult").data()[2], None)
def testReplaceChars(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.replaceChars((1, 3), 1, "hello") self.assertEqual(doc.lineText(1), 'hehellolo\n') doc.replaceChars((1, 1), 1, "c") self.assertEqual(doc.lineText(1), 'cehellolo\n')
def testLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), "r") as f: doc.read(f) self.assertEqual(doc.lineText(1), "hello\n") self.assertEqual(doc.lineText(2), "how are you?\n") self.assertRaises(IndexError, lambda: doc.lineText(-1)) self.assertRaises(IndexError, lambda: doc.lineText(0)) self.assertRaises(IndexError, lambda: doc.lineText(5))
def testParenthesisPreserved(self): """Check if parentheses are preserved (#124)""" self.buffer = models.Buffer() with open(fixtures.get("issue_124"), 'r') as f: self.buffer.document.read(f) self.buffer.cursor.toPos((1, 6)) command = commands.DeleteToEndOfWordCommand(self.buffer) result = command.execute() self.assertEqual(self.buffer.cursor.pos, (1, 6)) self.assertEqual(self.buffer.document.lineText(1), "def f(bar, baz):\n")
def testInsertLine(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) self.assertEqual(doc.numLines(), 2) doc.insertLine(1,"babau") self.assertEqual(doc.numLines(), 3) self.assertRaises(IndexError, lambda : doc.insertLine(6,"say what again")) doc.insertLine(4,"say what again") self.assertEqual(doc.numLines(), 4)
def testLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertRaises(IndexError, lambda: doc.lineText(-1)) self.assertRaises(IndexError, lambda: doc.lineText(0)) self.assertRaises(IndexError, lambda: doc.lineText(5))
def testJoinWithNextLine(self): doc = TextDocument() doc.open(fixtures.get("basic_nonempty_file.txt")) doc.joinWithNextLine(1) self.assertEqual(doc.numLines(), 1) self.assertEqual(doc.lineText(1), 'hellohow are you?\n') self.assertTrue(doc.isModified()) doc.joinWithNextLine(1) self.assertEqual(doc.numLines(), 1) self.assertEqual(doc.lineText(1), 'hellohow are you?\n')
def testInsertFileCommand(self): cursor = self.buffer.cursor document = self.buffer.document cursor.toPos((1,1)) command = commands.InsertFileCommand(self.buffer, fixtures.get("basic_python.py")) result = command.execute() self.assertTrue(result.success) self.assertEqual(document.lineText(1), document.lineText(1)) self.assertEqual(document.numLines(), 8)
def testInsertLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.numLines(), 2) doc.insertLine(1, "babau") self.assertEqual(doc.numLines(), 3) self.assertRaises(IndexError, lambda: doc.insertLine(6, "say what again")) doc.insertLine(4, "say what again") self.assertEqual(doc.numLines(), 4)
def testBug165(self): """Check if the linting data is correctly reset every time we perform a lint operation""" document = self.buffer_list.current.document document.read(open(fixtures.get("basic_python.py"))) document.documentMetaInfo("Filename").setData("basic_python.py") document.deleteChars((3, 9), 1) self.editor_controller._doLint() self.assertTrue( isinstance( document.lineMetaInfo("LinterResult").data()[2], LinterResult)) document.insertChars((3, 9), ')') self.editor_controller._doLint() self.assertEqual(document.lineMetaInfo("LinterResult").data()[2], None)
def testReplaceFromMemento(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.createLineMetaInfo("whatever") initial_text = doc.documentText() meta_info1 = doc.lineMetaInfo("whatever") meta_info1.setData("hello", 1) memento = doc.lineMemento(1) doc.insertChars((1, 1), 'gnakgnak') meta_info1.setData("byebye", 1) doc.replaceFromMemento(1, memento) self.assertEqual(doc.documentText(), initial_text) self.assertEqual(meta_info1.data(1), "hello")
def testUndo(self): cursor = self.buffer.cursor document = self.buffer.document old_lines = [document.lineText(i) for i in range(1,5)] cursor.toPos((1,1)) command = commands.InsertFileCommand(self.buffer, fixtures.get("basic_python.py")) result = command.execute() command.undo() self.assertEqual(document.numLines(), 4) for i in range(1,5): self.assertEqual(document.lineText(i), old_lines[i-1])
def testInsertFragment(self): doc = TextDocument() with open(fixtures.get("numbers.txt"), 'r') as f: doc.read(f) doc.createLineMetaInfo("whatever") doc.createLineMetaInfo("whatever2") fragment = doc.extractFragment(2) self.assertNotEqual(fragment.lineText(1), doc.lineText(15)) doc.insertFragment(15, fragment) self.assertEqual(fragment.lineText(1), doc.lineText(15)) fragment = doc.extractFragment(2, 4) self.assertNotEqual(fragment.lineText(1), doc.lineText(10)) doc.insertFragment(10, fragment) self.assertEqual(fragment.lineText(1), doc.lineText(10)) self.assertEqual(fragment.lineText(2), doc.lineText(11)) self.assertEqual(fragment.lineText(3), doc.lineText(12)) self.assertEqual(fragment.lineText(4), doc.lineText(13))
def testExtractFragment(self): doc = TextDocument() with open(fixtures.get("bigfile.py"), 'r') as f: doc.read(f) doc.createLineMetaInfo("whatever") doc.createLineMetaInfo("whatever2") fragment = doc.extractFragment(2) self.assertEqual(fragment.numLines(), 1) self.assertEqual(fragment.lineText(1), doc.lineText(2)) self.assertTrue(fragment.hasLineMetaInfo("whatever")) self.assertTrue(fragment.hasLineMetaInfo("whatever2")) fragment = doc.extractFragment(2, 3) self.assertEqual(fragment.numLines(), 3) self.assertEqual(fragment.lineText(1), doc.lineText(2)) self.assertEqual(fragment.lineText(2), doc.lineText(3)) self.assertEqual(fragment.lineText(3), doc.lineText(4)) self.assertTrue(fragment.hasLineMetaInfo("whatever")) self.assertTrue(fragment.hasLineMetaInfo("whatever2"))
def testDeleteLine2(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2) self.assertRaises(IndexError, lambda: doc.deleteLine(5)) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.lineText(2), 'how are you?\n') self.assertEqual(doc.numLines(), 2) doc.deleteLine(2) self.assertEqual(doc.lineText(1), 'hello\n') self.assertEqual(doc.numLines(), 1) self.assertFalse(doc.isEmpty()) doc.deleteLine(1) self.assertEqual(doc.lineText(1), '\n') self.assertEqual(doc.numLines(), 1) self.assertTrue(doc.isEmpty())
def createCursor(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.createCursor()
def testHasLine(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertTrue(doc.hasLine(1)) self.assertFalse(doc.hasLine(20))
def testInsertLines(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) doc.insertLines(2, ['foo', 'bar']) self.assertEqual(doc.numLines(), 4)
def testDeleteLines(self): doc = TextDocument() with open(fixtures.get("bigfile.py"), 'r') as f: doc.read(f) doc.deleteLines(1, 3)
def testWordAt(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) self.assertEqual(doc.wordAt((2, 6)), ('are', 5))
def setUp(self): self.buffer = models.Buffer() with open(fixtures.get("basic_python.py"), 'r') as f: self.buffer.document.read(f)
def registerCursor(self): doc = TextDocument() with open(fixtures.get("basic_nonempty_file.txt"), 'r') as f: doc.read(f) cursor = TextDocumentCursor(doc)