def maketest(n, testcases): babbler = Babbler(n) babbler.add_file(testcases) starters = str(sorted(babbler.get_starters())) stoppers = str(sorted(babbler.get_stoppers())) ngrams = str(sorted(babbler.get_all_ngrams())) next_states = {} for state in sorted(babbler.get_all_ngrams()): next_states[state] = babbler.get_successors(state) next_states = str(next_states).replace('], ', '],\n\t') return maketestcode(f'TestMarkovBabbler{n}', n, testcases, starters, stoppers, ngrams, next_states)
class TestMarkovBabbler3(unittest.TestCase): def setUp(self): warnings.simplefilter("ignore") self.babbler = Babbler(3) self.babbler.add_file('tests/test2.txt') def test_starters(self): expected = ['a b c', 'a b c', 'a b c', 'a b c', 'x y z', 'x y z'] actual = self.babbler.get_starters() self.assertEqual(expected, sorted(actual)) def test_stoppers(self): expected = ['b c !', 'b c ?', 'c d .', 'c e .', 'd e .', 'y z .'] actual = self.babbler.get_stoppers() self.assertEqual(expected, sorted(actual)) def test_ngrams(self): expected = [ 'a b c', 'b c !', 'b c ?', 'b c d', 'b c e', 'b c x', 'c d .', 'c d e', 'c e .', 'c x y', 'd e .', 'x y z', 'y z .', 'y z a', 'y z z', 'z a b', 'z z a' ] actual = self.babbler.get_all_ngrams() self.assertEqual(expected, sorted(actual)) def test_next_states(self): expected = { 'a b c': ['d', 'e', 'd', 'x', '!', '?'], 'b c !': ['EOL'], 'b c ?': ['EOL'], 'b c d': ['.', 'e'], 'b c e': ['.'], 'b c x': ['y'], 'c d .': ['EOL'], 'c d e': ['.'], 'c e .': ['EOL'], 'c x y': ['z'], 'd e .': ['EOL'], 'x y z': ['.', 'a', 'z'], 'y z .': ['EOL'], 'y z a': ['b'], 'y z z': ['a'], 'z a b': ['c', 'c'], 'z z a': ['b'] } for state in self.babbler.get_all_ngrams(): actual = sorted(self.babbler.get_successors(state)) correct = sorted(expected[state]) self.assertEqual( actual, correct, f"state '{state}' next should be {correct} but instead was {actual}" )