def test_countWords_countsCorrectlyForTwoFiles(self): # arrange inputReader = InputReader(FILE) inputReaderMock = Mock() inputReaderMock.return_value = TEXT inputReader.readInput = inputReaderMock inputReader2 = InputReader(FILE2) inputReaderMock2 = Mock() inputReaderMock2.return_value = TEXT2 inputReader2.readInput = inputReaderMock2 sourceLoader = SourceLoader() sourceLoaderMock = Mock() sourceLoaderMock.return_value = [inputReader, inputReader2] sourceLoader.loadSources = sourceLoaderMock wordsToExcludeInputReader = InputReader(EMPTY) wordsToExcludeInputReaderMock = Mock() wordsToExcludeInputReaderMock.return_value = WORDS_TO_EXCLUDE wordsToExcludeInputReader.readInput = wordsToExcludeInputReaderMock inputCleaner = InputCleaner(wordsToExcludeInputReader, InputReader(EMPTY)) wordCounter = WordCounter(inputCleaner, sourceLoader) # act output = wordCounter.countWords() # assert self.assertEqual(output, OUTPUT2)
def test_loadSources_returnsEmptyArrayWhenNothingIsInput(self): # arrange readInputMock = Mock() readInputMock.return_value = EMPTY inputReader = InputReader(EMPTY) inputReader.readInput = readInputMock sourceLoader = SourceFromCommandLineLoader(inputReaderType = InputReader, inputReader = inputReader) # act sources = sourceLoader.loadSources() # assert self.assertEqual(sources, [])
def test_loadSources_returnsArrayOfLengthOneForOneInput(self): # arrange readInputMock = Mock() readInputMock.side_effect = [INPUT, EMPTY] inputReader = InputReader(EMPTY) inputReader.readInput = readInputMock sourceLoader = SourceFromCommandLineLoader(inputReaderType = InputReader, inputReader = inputReader) # act sources = sourceLoader.loadSources() # assert self.assertEqual(len(sources), 1) self.assertEqual(type(sources[0]), InputReader)
def test_countWords_countsCorrectlyForNoFile(self): # assert sourceLoader = SourceLoader() sourceLoaderMock = Mock() sourceLoaderMock.return_value = [] sourceLoader.loadSources = sourceLoaderMock inputCleaner = InputCleaner(InputReader(EMPTY), InputReader(EMPTY)) wordCounter = WordCounter(inputCleaner, sourceLoader) # act output = wordCounter.countWords() # assert self.assertEqual(output, {})
def test_cleanInputCleansInputCorrectly(self): # arrange symbolsReader = InputReader(EMPTY) wordsReader = InputReader(EMPTY) inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) inputCleaner.symbolsToStrip = [',', '.', '!'] inputCleaner.wordsToExclude = ["der", "die"] # act text = inputCleaner.cleanInput(TEXT) # assert self.assertEqual(text.replace(WHITESPACE, EMPTY), CLEANED_TEXT.replace(WHITESPACE, EMPTY))
def test_loadSources_returnsEmptyListForFileNotFoundAssertion(self): # arrange readInputMock = Mock() readInputMock.side_effect = [INPUT, EMPTY] inputReader = InputReader(EMPTY) inputReader.readInput = readInputMock assertionErrorInputReaderMock = Mock() assertionErrorInputReaderMock.side_effect = AssertionError sourceLoader = SourceFromCommandLineLoader(inputReaderType = assertionErrorInputReaderMock, inputReader = inputReader) # act sources = sourceLoader.loadSources() # assert self.assertEqual(sources, [])
def test_countWords_countsCorrectlyForEmptyFile(self): # arrange inputReader = InputReader(FILE) inputReaderMock = Mock() inputReaderMock.return_value = EMPTY inputReader.readInput = inputReaderMock sourceLoader = SourceLoader() sourceLoaderMock = Mock() sourceLoaderMock.side_effect = [[inputReader]] sourceLoader.loadSources = sourceLoaderMock inputCleaner = InputCleaner(InputReader(EMPTY), InputReader(EMPTY)) wordCounter = WordCounter(inputCleaner, sourceLoader) # act output = wordCounter.countWords() # assert expected = {} expected[FILE] = defaultdict(int) self.assertEqual(output, expected)
def loadSymbolsToStrip(self, symbolsToStripInputReader: InputReader): """ Retruns the symbols that are to be stripped (provided InputReader is expected to read only symbols) """ symbolsToStrip: List[str] = [] # try to load symbols that are to be stripped from the input try: # read the symbols symbols = symbolsToStripInputReader.readInput() # add every symbol to the list of symbols to strip if it is not the empty symbol for symbol in symbols: if symbol != constants.EMPTY: symbolsToStrip.append(symbol) finally: return symbolsToStrip
def test_loadWordsToExclude_returnsEmptyWhenNothingToRead(self): # arrange symbolsReader = InputReader(EMPTY) symbolsReaderMock = Mock() symbolsReader.return_value = EMPTY symbolsReader.readInput = symbolsReaderMock wordsReader = InputReader(EMPTY) wordsReaderMock = Mock() wordsReaderMock.return_value = EMPTY wordsReader.readInput = wordsReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) words = inputCleaner.wordsToExclude # assert self.assertEqual(words, [])
def test_loadWordsToExclude_returnsEmptyOnError(self): # arrange symbolsReader = InputReader(EMPTY) symbolsReaderMock = Mock() symbolsReader.return_value = EMPTY symbolsReader.readInput = symbolsReaderMock wordsReader = InputReader(EMPTY) wordsReaderMock = Mock() wordsReaderMock.side_effect = [AssertionError] wordsReader.readInput = wordsReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) words = inputCleaner.wordsToExclude # assert self.assertEqual(words, [])
def test_loadWordsToExclude_returnsWords(self): # arrange symbolsReader = InputReader(EMPTY) symbolsReaderMock = Mock() symbolsReader.return_value = EMPTY symbolsReader.readInput = symbolsReaderMock wordsReader = InputReader(EMPTY) wordsReaderMock = Mock() wordsReaderMock.return_value = WORDS wordsReader.readInput = wordsReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) words = inputCleaner.wordsToExclude # assert self.assertEqual(len(words), 3) self.assertEqual(words, ["der", "die", "das"])
def test_loadSymbolsToStrip_returnsEmptyWhenErrorOccurs(self): # arrange readSymbolsMock = Mock() readSymbolsMock.side_effect = [AssertionError] symbolsReader = InputReader(EMPTY) symbolsReader.readInput = readSymbolsMock wordReaderMock = Mock() wordReaderMock.return_value = EMPTY wordsReader = InputReader(EMPTY) wordsReader.readInput = wordReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) symbols = inputCleaner.symbolsToStrip # assert self.assertEqual(symbols, [])
def test_loadSymbolsToStrip_returnsEmptyWhenNothingToRead(self): # arrange readSymbolsMock = Mock() readSymbolsMock.return_value = EMPTY symbolsReader = InputReader(EMPTY) symbolsReader.readInput = readSymbolsMock wordReaderMock = Mock() wordReaderMock.return_value = EMPTY wordsReader = InputReader(EMPTY) wordsReader.readInput = wordReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) symbols = inputCleaner.symbolsToStrip # assert self.assertEqual(symbols, [])
def loadWordsToExclude(self, wordsToExcludeInputReader: InputReader): """ Returns the words that are to be excluded (provided InputReader is expected to read comma separated values - csv) """ wordsToExclude: List[str] = [] # try to load words that are to be excluded try: # read the words words = wordsToExcludeInputReader.readInput() # cast to lower case words = words.lower() # remove whitespaces words = words.replace(constants.WHITESPACE, constants.EMPTY) # split the words by comma splittedWords = words.split(constants.COMMA) # remove the empty symbol if constants.EMPTY in splittedWords: splittedWords.remove(constants.EMPTY) # add the words to the words to be excluded wordsToExclude.extend(splittedWords) finally: return wordsToExclude
def test_loadSymbolsToStrip_returnsSymbols(self): # arrange readSymbolsMock = Mock() readSymbolsMock.return_value = SYMBOLS symbolsReader = InputReader(EMPTY) symbolsReader.readInput = readSymbolsMock wordReaderMock = Mock() wordReaderMock.return_value = EMPTY wordsReader = InputReader(EMPTY) wordsReader.readInput = wordReaderMock # act inputCleaner: InputCleaner = InputCleaner(wordsReader, symbolsReader) symbols = inputCleaner.symbolsToStrip # assert self.assertEqual(len(symbols), 5) self.assertEqual(symbols, [".", ",", ";", ":", "!"])
def test(self): reader = InputReader(EMPTY) reader.readInput()
from VV_E1_WordCount.InputReader import InputReader, FileReader, CommandLineReader from VV_E1_WordCount.InputCleaner import InputCleaner from VV_E1_WordCount.SourceLoader import SourceFromCommandLineLoader, SourceLoader from VV_E1_WordCount.OutputWriter import OutputToConsoleWriter from VV_E1_WordCount.WordCounter import WordCounter import VV_E1_WordCount.constants as constants # try to get the location of the file configuring the words to exclude wordsToExcludePath = "" wordsToExcludeInputReader = None if len(sys.argv) == 3: wordsToExcludePath = sys.argv[2] try: wordsToExcludeInputReader = FileReader(wordsToExcludePath) except (AssertionError): wordsToExcludeInputReader = InputReader(constants.EMPTY) # try to get the location of the file configuring the symbols to strip symbolsToStripPath = "" symbolsToStripInputReader = None if len(sys.argv) >= 2: symbolsToStripPath = sys.argv[1] try: symbolsToStripInputReader = FileReader(symbolsToStripPath) except (AssertionError): symbolsToStripInputReader = InputReader(constants.EMPTY) # composition root inputCleaner: InputCleaner = InputCleaner(wordsToExcludeInputReader, symbolsToStripInputReader) sourceLoader: SourceFromCommandLineLoader = SourceFromCommandLineLoader(