def test_list_of_blank_string(self): """ Test the list of blank strings. """ self.assertEqual(count_lines([' ']), 0, 'only one blank tring') self.assertEqual(count_lines([' ', ' ']), 0, 'multiple_blank strings')
def test_list_of_empty_string(self): """ Test the list of empty strings. """ self.assertEqual(count_lines(['']), 0, 'only one empty string') self.assertEqual(count_lines(['', '']), 0, 'multiple empty strings')
def test_list_of_non_empty_non_blank_string(self): """ Test the list of non-blank, non-empty strings. """ self.assertEqual(count_lines(['dada ']), 1, 'list of one non-blank, non-empty string') self.assertEqual(count_lines(['dada ', 'daa']), 2, 'list of multiple non-blank, non-empty lines')
def test_list_of_strings_with_character_of_new_line(self): """ Test the list of strings with characters. """ self.assertEqual(count_lines(['faff\n', '\n', ' \n']), 1, 'list of strings with character of new line in the end') self.assertEqual(count_lines(['fa\nf', ' \n ']), 1, 'list of strings with character of new line in the middle') self.assertEqual(count_lines(['da\n\n', '\n \n ', '\n \n']), 1, 'list of strings with characters of new line in the end or in' + \ 'the middle')
def test_regular_input(self): """ Test a regular input in which all lines are non-blank, non-empty strings and meet the precondition. """ normal_lst = ["Hello there!\n", "Hi, how are you?\n", "I'm fine thanks!"] returned = count_lines(normal_lst) self.assertEqual(returned, 3, "value should be 3, but returned " + str(returned))
def check_poem(poem_lines, pattern, word_to_phonemes, form_name): """ (list of str, poetry pattern, pronunciation dictionary, str) -> NoneType Check whether the poem in poem_lines has the right number of lines to match for form given in pattern, and print a message if it doesn't. If it does, then check whether the lines in the poem have the right number of syllables and report the lines that don't; also check whether the lines of the poem have the correct rhyming scheme and report the lines that should rhyme but don't. """ if poetry_functions.count_lines(poem_lines) != len(pattern[0]): print("\n== The poem doesn't have the right number of lines. == \n") else: problem_lines = poetry_functions.check_syllables( poem_lines, pattern, word_to_phonemes) if len(problem_lines) == 0: print( '\nThe poem has the right number of syllables on each line.\n') else: print('\n== The poem is not a {}. These lines don\'t have the ' 'right number of syllables: == '.format(form_name)) print('\n'.join(problem_lines) + '\n') problem_rhymes = poetry_functions.check_rhyme_scheme( poem_lines, pattern, word_to_phonemes) if len(problem_rhymes) == 0: print('The poem follows the rhyme scheme.\n') else: print('\n== The poem is not a {}. These lines should rhyme' " but don't: ==".format(form_name)) for lines in problem_rhymes: print('\n'.join(lines) + '\n')
def test_last_no_newline_char(self): ''' Test if the last line does not end with a newline character. ''' last_no_newline_char_lst = ['test\n', 'another test\n', 'test'] returned = count_lines(last_no_newline_char_lst) self.assertEqual(returned, 3, 'value should be 3, but returned '\ + str(returned))
def test_blank_strings(self): ''' Test for blank lines in the list of string. ''' blank_str_lst = [' \n', 'testing\n', ' '] returned = count_lines(blank_str_lst) self.assertEqual(returned, 1, 'value should be 1 but returned ' \ + str(returned))
def test_punctuation(self): """ Test if each line contains punctuation regardless of grammatical correctness. """ punct_lst = ["testing!\n", ";;{]hello:)\n", "[]\\?last value!!!"] returned = count_lines(punct_lst) self.assertEqual(returned, 3, "value should be 3, but returned " + str(returned))
def test_empty_lst(self): ''' Test for no lines being passed in. ''' empty_lst = [] returned = count_lines(empty_lst) self.assertEqual(returned, 0, 'value should be 0, but returned '\ + str(returned))
def test_only_newline_char(self): """ Test if all lines contain only newline characters, and thus, also if the last line ends with a newline character. """ only_newline_lst = ["\n", "\n", "\n", "\n"] returned = count_lines(only_newline_lst) self.assertEqual(returned, 0, "value should be 0, but returned " + str(returned))
def test_empty_strings(self): """ Test for the last line being an empty string (this can only occur at the last line because of the precondition). """ empty_str_lst = ["testing\n", ""] returned = count_lines(empty_str_lst) self.assertEqual(returned, 1, "value should be 1 but returned " + str(returned))
def check_poem(poem_lines, pattern, word_to_phonemes, form_name): """ (list of str, poetry pattern, pronunciation dictionary, str) -> NoneType Check whether the poem in poem_lines has the right number of lines to match for form given in pattern, and print a message if it doesn't. If it does, then check whether the lines in the poem have the right number of syllables and report the lines that don't; also check whether the lines of the poem have the correct rhyming scheme and report the lines that should rhyme but don't. """ if poetry_functions.count_lines(poem_lines) != len(pattern[0]): print("\n== The poem doesn't have the right number of lines. == \n") else: problem_lines = poetry_functions.check_syllables( poem_lines, pattern, word_to_phonemes) if len(problem_lines) == 0: print('\nThe poem has the right number of syllables on each line.\n') else: print('\n== The poem is not a {}. These lines don\'t have the ' 'right number of syllables: == '.format(form_name)) print('\n'.join(problem_lines) + '\n') problem_rhymes = poetry_functions.check_rhyme_scheme( poem_lines, pattern, word_to_phonemes) if len(problem_rhymes) == 0: print('The poem follows the rhyme scheme.\n') else: print('\n== The poem is not a {}. These lines should rhyme' " but don't: ==".format(form_name)) for lines in problem_rhymes: print('\n'.join(lines) + '\n')
def test_count_lines_1(self): ''' Test count_lines with only new line characters only and no strings in list.''' lst = ['\n', ' \n'] actual = poetry_functions.count_lines(lst) expected = 0 self.assertEqual(actual, expected)
def test_empty_strings(self): ''' Test for the last line being an empty string (this can only occur at the last line because of the precondition). ''' empty_str_lst = ['testing\n', ''] returned = count_lines(empty_str_lst) self.assertEqual(returned, 1, 'value should be 1 but returned '\ + str(returned))
def test_only_newline_char(self): ''' Test if all lines contain only newline characters, and thus, also if the last line ends with a newline character. ''' only_newline_lst = ['\n', '\n', '\n', '\n'] returned = count_lines(only_newline_lst) self.assertEqual(returned, 0, 'value should be 0, but returned '\ + str(returned))
def test_punctuation(self): ''' Test if each line contains punctuation regardless of grammatical correctness. ''' punct_lst = ['testing!\n', ';;{]hello:)\n', '[]\\?last value!!!'] returned = count_lines(punct_lst) self.assertEqual(returned, 3, 'value should be 3, but returned '\ + str(returned))
def test_empty_list(self): """ Test the empty list. """ self.assertEqual(count_lines([]),0, 'empty list')
def test_regular_input(self): ''' Test a regular input in which all lines are non-blank, non-empty strings and meet the precondition. ''' normal_lst = ['Hello there!\n' ,'Hi, how are you?\n', \ "I'm fine thanks!"] returned = count_lines(normal_lst) self.assertEqual(returned, 3, 'value should be 3, but returned '\ + str(returned))
def test_list_of_mutiple_types_of_lines(self): """ Test the list of strings with empty string,blank string and non-empty, non-blank string. """ self.assertEqual(count_lines(['', 'dad', ' ', ' dsadf']), 2, 'list of srings with empty string,blank string and non-empty,' + \ 'non-blank string')
def test_count_lines_3(self): ''' Test count_lines with five strings in list combined with strings only containing new line characters.''' lst = [ 'The first line leads off,\n', '\n', ' \n', 'With a gap before the next.\n', 'Then the poem doesn\'t ends.\n', 'The poem continues with the next line,\n', 'Then ends here.\n' ] actual = poetry_functions.count_lines(lst) expected = 5 self.assertEqual(actual, expected)
def test_case_1(self): actual=count_lines(['The first line leads off,\n', '\n', ' \n', 'With a gap before the next.\n', 'Then the poem ends.\n']) expected= 3
our_print = print def disable_print(*args): raise Exception("You must not call built-in function print!") def disable_input(*args): raise Exception("You must not call built-in function input!") builtins.print = disable_print builtins.input = disable_input import poetry_functions # Type check poetry_functions.count_lines result = poetry_functions.count_lines(['First line;\n', '\n', 'last line\n']) assert isinstance(result, int), \ '''poetry_functions.count_lines should return an int, but returned {0} .'''.format(type(result)) # Type check poetry_functions.get_poem_lines result = poetry_functions.get_poem_lines('One,\ntwo,\nthree.\n') assert isinstance(result, list), \ '''poetry_functions.get_poem_lines should return a list, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, str), \ '''poetry_functions.get_poem_lines should return a list of str, but returned a list of {0}.''' \ .format(type(item))
def test_04_one_string_list(self): ''' Tests only one string in a list ''' L = ['text\n'] expected = 1 self.assertEqual(count_lines(L), expected, "count_lines(['text\n'])")
def test_case_2(self): actual=count_lines([ '\n']) expected=0 self.assertEqual(actual, expected)
def test_last_no_newline_char(self): """ Test if the last line does not end with a newline character. """ last_no_newline_char_lst = ["test\n", "another test\n", "test"] returned = count_lines(last_no_newline_char_lst) self.assertEqual(returned, 3, "value should be 3, but returned " + str(returned))
def test_blank_strings(self): """ Test for blank lines in the list of string. """ blank_str_lst = [" \n", "testing\n", " "] returned = count_lines(blank_str_lst) self.assertEqual(returned, 1, "value should be 1 but returned " + str(returned))
def test_empty_lst(self): """ Test for no lines being passed in. """ empty_lst = [] returned = count_lines(empty_lst) self.assertEqual(returned, 0, "value should be 0, but returned " + str(returned))
def test_03_multiple_words_with_empty_and_newline_strings(self): ''' Tests mixed strings with blank strings and newline character ''' L = ['text\n', '', 'more text\n', '\n'] expected = 2 self.assertEqual(count_lines(L), expected, "count_lines(['text\n', '', 'more text\n', '\n'])")
def test_count_lines_2(self): ''' Test count_lines with three strings in list.''' lst = ['First line,\n', 'Second line,\n', 'Last line\n'] actual = poetry_functions.count_lines(lst) expected = 3 self.assertEqual(actual, expected)
def disable_print(*args): raise Exception("You must not call built-in function print!") def disable_input(*args): raise Exception("You must not call built-in function input!") builtins.print = disable_print builtins.input = disable_input import poetry_functions # Type check poetry_functions.count_lines result = poetry_functions.count_lines(['First line;\n', '\n', 'last line\n']) assert isinstance(result, int), \ '''poetry_functions.count_lines should return an int, but returned {0} .'''.format(type(result)) # Type check poetry_functions.get_poem_lines result = poetry_functions.get_poem_lines('One,\ntwo,\nthree.\n') assert isinstance(result, list), \ '''poetry_functions.get_poem_lines should return a list, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, str), \ '''poetry_functions.get_poem_lines should return a list of str, but returned a list of {0}.''' \ .format(type(item))
def test_05_multiple_string_list(self): ''' Tests multiple strings in a list ''' L = ['text\n', 'coding\n', 'is\n', 'awesome\n'] expected = 4 self.assertEqual(count_lines(L), expected, "count_lines(['text\n', 'coding\n', 'is\n', 'awesome\n'])")
def test_02_empty_list(self): '''Tests an empty list ''' L = [] expected = 0 self.assertEqual(count_lines(L), expected, "count_lines([])")
def test_01_all_blank_empty_string(self): ''' Tests a list with only blanks and empty strings ''' L = [' \n', '\n', ' \n\n'] expected = 0 self.assertEqual(count_lines(L), expected, "count_lines([' ', '\n', ''])")