Exemplo n.º 1
0
    def test_countWords_countsCorrectlyForOneFile(self):
        # arrange
        inputReader = InputReader(FILE)
        inputReaderMock = Mock()
        inputReaderMock.return_value = TEXT
        inputReader.readInput = inputReaderMock

        sourceLoader = SourceLoader()
        sourceLoaderMock = Mock()
        sourceLoaderMock.return_value = [inputReader]
        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, OUTPUT)
Exemplo n.º 2
0
    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, [])
Exemplo n.º 3
0
    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, [])
Exemplo n.º 4
0
    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, [])
Exemplo n.º 5
0
    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, [])
Exemplo n.º 6
0
    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"])
Exemplo n.º 7
0
    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, [".", ",", ";", ":", "!"])
Exemplo n.º 8
0
    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, [])
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    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, [])
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
    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
Exemplo n.º 14
0
 def test(self):
     reader = InputReader(EMPTY)
     reader.readInput()