def test_replace_word_creates_backup(self): path = "../books/sentence_test.txt" try: with open(path) as f: contents = f.read() solution.replace_word(path, "kaugel", "far") files = os.listdir("../books/") self.assertTrue("sentence_test_old.txt" in files) finally: self.restore_dir(path, contents)
def test_replace_word_counts(self): path = "../books/sentence_test.txt" try: with open(path) as f: contents = f.read() solution.replace_word(path, "kaugel", "far") with open(path) as f: new_contents = f.read() self.assertTrue(new_contents) self.assertEqual(0, new_contents.count("kaugel")) self.assertEqual(2, new_contents.count("far")) finally: self.restore_dir(path, contents)
def test_replace_word_whole(self): path = "../books/sentence_test.txt" try: with open(path) as f: contents = f.read() solution.replace_word(path, "kaugel", "far") with open(path) as f: new_contents = f.read() with open("../books/backups/sentence_test_mod.txt") as f: expected = f.read() self.assertEqual(expected, new_contents) finally: self.restore_dir(path, contents)
def test_replace_word(self): path = "../books/sentence_test.txt" try: with open(path) as f: contents = f.read() solution.replace_word(path, "kaugel", "far") with open(path) as f: new_contents = f.read() self.assertTrue(new_contents) self.assertFalse("kaugel" in new_contents) self.assertTrue("far" in new_contents) finally: self.restore_dir(path, contents)