Skip to content

Instantly share code, notes, and snippets.

@mcclane
Created November 26, 2018 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcclane/2553245dd65c9d59e2d62095dca0dec4 to your computer and use it in GitHub Desktop.
Save mcclane/2553245dd65c9d59e2d62095dca0dec4 to your computer and use it in GitHub Desktop.
import unittest
import filecmp
from concordance import *
class TestList(unittest.TestCase):
def test_01(self):
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("file1.txt")
conc.write_concordance("file1_con.txt")
self.assertTrue(filecmp.cmp("file1_con.txt", "file1_sol.txt"))
def test_02(self):
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("file2.txt")
conc.write_concordance("file2_con.txt")
self.assertTrue(filecmp.cmp("file2_con.txt", "file2_sol.txt"))
def test_03(self):
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("declaration.txt")
conc.write_concordance("declaration_con.txt")
self.assertTrue(filecmp.cmp("declaration_con.txt", "declaration_sol.txt"))
def test_weird_punctuation_file(self):
"""Tests a file with all the punctuation"""
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("punctuation_test_file.txt")
conc.write_concordance("punctuation_test_file_con.txt")
self.assertTrue(filecmp.cmp("punctuation_test_file_con.txt", "punctuation_test_file_sol.txt"))
def test_hyphen_file(self):
"""Tests a file with hyphens"""
words = ["hello", "world", "hyphenated", "sentence"]
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("hyphen_file.txt")
conc.write_concordance("hyphen_file_con.txt")
self.assertTrue(filecmp.cmp("hyphen_file_con.txt", "hyphen_file_sol.txt"))
def test_empty_file(self):
"""Test against an empty file, expecting an empty file in return"""
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("empty_file.txt")
conc.write_concordance("empty_file_con.txt")
self.assertTrue(filecmp.cmp("empty_file_con.txt", "empty_file_sol.txt"))
def test_stop_file_does_not_exist(self):
"""Assert that FileNotFoundError is raised if the stop words file does not exist"""
conc = Concordance()
with self.assertRaises(FileNotFoundError):
conc.load_stop_table("abcdefghijklmnopqrstuv an impossible file name that does not exist!")
def test_word_file_does_not_exist(self):
"""Assert that FileNotFoundError is raised if the input file does not exist"""
conc = Concordance()
with self.assertRaises(FileNotFoundError):
conc.load_concordance_table("abcdefghijklmnopqrstuv an impossible file name that does not exist!")
def test_duplicate_line_numbers(self):
"""Test a file with the same word multiple times on one line to make sure there are no duplicate line numbers"""
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("duplicate_line_numbers_file.txt")
conc.write_concordance("duplicate_line_numbers_con.txt")
self.assertTrue(filecmp.cmp("duplicate_line_numbers_con.txt", "duplicate_line_numbers_sol.txt"))
def test_stop_words_file(self):
"""Make sure that counting the concordance of the stop words file is an empty file"""
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("stop_words.txt")
conc.write_concordance("stop_words_con.txt")
self.assertTrue(filecmp.cmp("stop_words_con.txt", "empty_file.txt"))
def test_one_word_file(self):
"""Test a file with one word over multiple lines"""
conc = Concordance()
conc.load_stop_table("stop_words.txt")
conc.load_concordance_table("one_word_file.txt")
conc.write_concordance("one_word_file_con.txt")
self.assertTrue(filecmp.cmp("one_word_file_con.txt", "one_word_file_sol.txt"))
# def test_war_and_peace(self):
# conc = Concordance()
# conc.load_stop_table("stop_words.txt")
# conc.load_concordance_table("war_and_peace.txt")
# conc.write_concordance("war_and_peace_con.txt")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment