def test_page_count_dialect_as_callable(self): """ Tests that if ``dialect`` argument is callable, it's really called. """ mock_dialect = MagicMock() PageCounter('some text').page_count(mock_dialect) mock_dialect.assert_called()
def test_page_count_str_dialect_must_be_builtin(self): """ Tests that if ``dialect`` argument passed to ``page_count()`` is string, it must be one of builtin dialect names. """ with self.assertRaises(ValueError): PageCounter('some text').page_count('non_existing_builtin_dialect')
def test_dialect_return_1_at_min(self): """ Tests that dialect callable returns 1 page count at minimum. For example one-letter text is still 1 page. """ for dialect_name in dialect_names: with self.subTest(dialect_name=dialect_name): dialect = getattr(dialects, dialect_name) text = "There were froggies by the lake, they were thinking what to make." page_counter = PageCounter(text) self.assertEqual( 1, page_counter.page_count(dialect), 'Dialect "{dialect}" must return at least 1 page count at min' 'even for few words only long text.'.format( dialect=dialect_name))
def in_file(argv): dialect = argv[0] file = argv[1] with open(file, 'r') as f: text = f.read() pages = PageCounter(text).page_count(dialect) print("File '{file}' has {pages} pages using '{dialect}' dialect.".format( file=file, pages=pages, dialect=dialect)) return pages
def in_folder(argv): dialect = argv[0] folder = argv[1] ext = argv[2] total_pages = 0 print( "Counting pages in *.{ext} files in '{folder}' using '{dialect}' dialect:" .format(ext=ext, folder=folder, dialect=dialect)) def walk_error(error): print_err("Encountered '{error}' during traversing folder".format( error=error)) for dirpath, dirname, filenames in os.walk(folder, onerror=walk_error): for filename in filenames: if filename.lower().endswith('.' + ext): file_path = os.path.join(dirpath, filename) relative_file_path = os.path.relpath(file_path, folder) with open(file_path, 'r') as f: text = f.read() pages = PageCounter(text).page_count(dialect) print(" * '{file}' file has {pages} pages".format( file=relative_file_path, pages=pages)) total_pages += pages if total_pages == 0: print("No {ext} files found.") else: print("-> {total_pages} total pages in '{dialect}' dialect.".format( total_pages=total_pages, dialect=dialect)) return total_pages
def test_page_count_dialect_is_not_str_or_callable(self): """ Tests that ``dialect`` argument isn't str or callable, an TypeError is raised. """ with self.assertRaises(TypeError): PageCounter('some text').page_count(3.14) # float passed
def test_strip_html(self): html = '<p class="intro">sometext</p>' actual = PageCounter(html, strip_html=True).text expected = 'sometext' self.assertEqual(expected, actual)
def test_word_count(self): expected = 1333 actual = PageCounter(self.test_text).word_count() self.assertEqual(expected, actual)
def test_chars_without_space(self): expected = 6569 actual = PageCounter(self.test_text).chars_without_spaces() self.assertEqual(expected, actual)
def test_eu_1500_chars_space_savvy_standard_page(self): expected = 5 actual = PageCounter( self.test_text).page_count('eu_1500_chars_standard_page') self.assertEqual(expected, actual)
def test_uk_1000_words_standard_page(self): expected = 1 actual = PageCounter( self.test_text).page_count('uk_1000_words_standard_page') self.assertEqual(expected, actual)
def test_cz_sk_1800_space_savvy_standard_page(self): expected = 4 actual = PageCounter(self.test_text).page_count( 'cz_sk_1800_chars_space_savvy_standard_page') self.assertEqual(expected, actual)