def testSlicing(self): r1 = ropes.Rope(para1) r1 += ropes.Rope(para2) s1 = para1 + para2 start = random.randint(0, len(r1) - 1) end = random.randint(start + 1, len(r1)) self.assertEqual(str(r1[start:end]), s1[start:end])
def testCut_On_commonPrefix(self): editor = TextEditor.SimpleEditor(commonPrefix,commonPrefix) length = len(commonPrefix_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.cut(random_i,random_j) self.assertEqual(editor.paste_text, ropes.Rope(commonPrefix_as_str[random_i:random_j])) self.assertEqual(editor.document, ropes.Rope(commonPrefix_as_str[:random_i]) + ropes.Rope(commonPrefix_as_str[random_j:]))
def testCut_On_manySpaces(self): editor = TextEditor.SimpleEditor(manySpaces,manySpaces) length = len(manySpaces_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.cut(random_i,random_j) self.assertEqual(editor.paste_text, ropes.Rope(manySpaces_as_str[random_i:random_j])) self.assertEqual(editor.document, ropes.Rope(manySpaces_as_str[:random_i]) + ropes.Rope(manySpaces_as_str[random_j:]))
def testCut_On_numbersAndWords(self): editor = TextEditor.SimpleEditor(numbersAndWords,numbersAndWords) length = len(numbersAndWords_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.cut(random_i,random_j) self.assertEqual(editor.paste_text, ropes.Rope(numbersAndWords_as_str[random_i:random_j])) self.assertEqual(editor.document, ropes.Rope(numbersAndWords_as_str[:random_i]) + ropes.Rope(numbersAndWords_as_str[random_j:]))
def testCopy_On_commonText(self): editor = TextEditor.SimpleEditor(commonText1,commonText1) length = len(commonPrefix_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.copy(random_i,random_j) self.assertEqual(editor.paste_text, ropes.Rope(commonText1_as_str[random_i:random_j]))
def populateDocumentWithInputText(self, filename): ''' A function to populate the self.document as a rope containing rope the input file's text. ''' with open(filename, "r") as fi: data_text = fi.read() self.document = ropes.Rope(data_text)
def testCopyPaste_On_manySpaces(self): editor = TextEditor.SimpleEditor(manySpaces,manySpaces) length = len(manySpaces_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.copy(random_i,random_j) random_paste_index = randint(0,length) editor.paste(random_paste_index) self.assertEqual(editor.document, ropes.Rope(manySpaces_as_str[:random_paste_index ]) + ropes.Rope(manySpaces_as_str[random_i:random_j]) + ropes.Rope(manySpaces_as_str[random_paste_index :]) )
def testCopyPaste_On_commonPrefix(self): editor = TextEditor.SimpleEditor(commonPrefix,commonPrefix) length = len(commonPrefix_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.copy(random_i,random_j) random_paste_index = randint(0,length) editor.paste(random_paste_index) self.assertEqual(editor.document, ropes.Rope(commonPrefix_as_str[:random_paste_index ]) + ropes.Rope(commonPrefix_as_str[random_i:random_j]) + ropes.Rope(commonPrefix_as_str[random_paste_index :]) )
def testCopyPaste_On_numbersAndWords(self): editor = TextEditor.SimpleEditor(numbersAndWords,numbersAndWords) length = len(numbersAndWords_as_str) random_i = randint(0,length - 1) random_j = randint(random_i,length) editor.copy(random_i,random_j) random_paste_index = randint(0,length) editor.paste(random_paste_index) self.assertEqual(editor.document, ropes.Rope(numbersAndWords_as_str[:random_paste_index ]) + ropes.Rope(numbersAndWords_as_str[random_i:random_j]) + ropes.Rope(numbersAndWords_as_str[random_paste_index :]) )
def __init__(self, documentPath, dictionaryFilePath): # Create Trie to store dictionary words self.dictionaryTrie = Trie.Trie() # Create rope structure for document self.document = None self.populateDocumentWithInputText(documentPath) # Insert dictionary words in Trie for word in self.yieldWords(dictionaryFilePath): self.dictionaryTrie.insert(word, self.dictionaryTrie.root) # Create an empty rope object to later store pasted text self.paste_text = ropes.Rope("")
def testAppends(self): r1 = ropes.Rope() r2 = ropes.Rope() r2 += ropes.Rope(para4) r2 += ropes.Rope(para5) r1 += ropes.Rope(para1) r1 += ropes.Rope(para2) r1 += ropes.Rope(para3) r1 += r2 self.assertEqual(str(r1), para1 + para2 + para3 + para4 + para5)
def testComparisons(self): r1 = ropes.Rope(para1 + para2) r2 = ropes.Rope(para1) r2 += ropes.Rope(para2) self.assertEqual(r1, r2) r1 = ropes.Rope('hello') * 3 r2 = ropes.Rope('hello') * 2 r2 += ropes.Rope('hello') self.assertEqual(r1, r2)
def testBalance(self): r1 = ropes.Rope() r1 += ropes.Rope(para1) r1 += ropes.Rope(para2) r1 += ropes.Rope(para3) r1 += ropes.Rope(para4) r1 += ropes.Rope(para5) r1.balance() self.assertEqual(str(r1), para1 + para2 + para3 + para4 + para5)
def testRepetition(self): r1 = ropes.Rope('hello') r1 *= 100 self.assertEqual(str(r1), 'hello' * 100)
def testLength(self): r1 = ropes.Rope('hell') r1 += ropes.Rope('o, w') r1 += ropes.Rope('orld') self.assertEqual(len(r1), 12)
def testCopy_On_empty(self): editor = TextEditor.SimpleEditor(empty,empty) editor.copy(0,0) self.assertEqual(editor.paste_text, ropes.Rope(""))
def testEditorCreation_On_numbersAndWords(self): editor = TextEditor.SimpleEditor(numbersAndWords,numbersAndWords) self.assertEqual(editor.paste_text, ropes.Rope("")) for word in numbersAndWords_WordsWithoutPunctuation: self.assertEqual(editor.dictionaryTrie.find(word),True) self.assertEqual(editor.document,ropes.Rope(numbersAndWords_as_str))
def testEditorCreation_On_manySpaces(self): editor = TextEditor.SimpleEditor(manySpaces,manySpaces) self.assertEqual(editor.paste_text, ropes.Rope("")) for word in manySpaces_WordsWithoutPunctuation: self.assertEqual(editor.dictionaryTrie.find(word),True) self.assertEqual(editor.document,ropes.Rope(manySpaces_as_str))
def testEditorCreation_On_Empty(self): editor = TextEditor.SimpleEditor(empty,empty) self.assertEqual(editor.paste_text, ropes.Rope("")) self.assertEqual(len(editor.dictionaryTrie.root.children),0) self.assertEqual(editor.document,ropes.Rope(empty_as_str))
def testEditorCreation_On_CommonPrefix(self): editor = TextEditor.SimpleEditor(commonPrefix,commonPrefix) self.assertEqual(editor.paste_text, ropes.Rope("")) for word in commonPrefix_WordsWithoutPunctuation: self.assertEqual(editor.dictionaryTrie.find(word),True) self.assertEqual(editor.document,ropes.Rope(commonPrefix_as_str))
def testLength(self): r1 = ropes.Rope('hello') r1 *= 100 self.assertEqual(len(r1), 500)
def testHashing(self): r1 = ropes.Rope(para1 + para2 + para3) r2 = ropes.Rope(para1) r2 += ropes.Rope(para2) r2 = r2 + ropes.Rope(para3) self.assertEqual(hash(r1), hash(r2))
def testCut_On_empty(self): editor = TextEditor.SimpleEditor(empty,empty) editor.cut(0,0) self.assertEqual(editor.paste_text, ropes.Rope("")) self.assertEqual(editor.document, ropes.Rope(""))
def testIteration(self): r1 = ropes.Rope(para1 + para2 + para3) r2 = ropes.Rope('') for c in r1: r2 += ropes.Rope(c) self.assertEqual(str(r1), str(r2))
def testCopyPaste_On_empty(self): editor = TextEditor.SimpleEditor(empty,empty) editor.copy(0,0) random_paste_index = randint(0,0) editor.paste(random_paste_index) self.assertEqual(editor.document, ropes.Rope("") )