def check_formatter_output(self, expected_strings: Iterable[str], delimiter: str = ' '): sample_input = delimiter.join(expected_strings) regexp = PrefixTree(expected_strings).to_regexp(self._Formatter) output_strings = sorted(set(re.compile(regexp).findall(sample_input))) self.assertEqual( sorted(set(expected_strings)), output_strings, msg="Regexp: {}\nInput: '{}'".format(regexp, sample_input) )
class PrefixTreeTest(TestCase): def setUp(self): self._tree = PrefixTree() def test_correctly_encodes_strings_with_special_characters(self): for input_string, expected_regexp in SPECIAL_CHARACTER_STRINGS.items(): with self.subTest(input_string=input_string): tree = PrefixTree() tree.add(input_string) self.assertEqual(expected_regexp, tree._root_node.to_regexp()) @given(NON_EMPTY_TEXT_ITERABLES) def test_accepts_individual_strings(self, expected_strings): for string in expected_strings: self._tree.add(string) assert_strings_can_be_matched(self, self._tree.to_regexp(PythonFormatter), expected_strings) @given(NON_EMPTY_TEXT_ITERABLES) def test_accepts_iterable_of_strings(self, expected_strings): self._tree.extend(expected_strings) assert_strings_can_be_matched(self, self._tree.to_regexp(PythonFormatter), expected_strings) @given(NON_EMPTY_TEXT_ITERABLES) def test_can_be_instantiated_with_initial_iterable_of_strings( self, expected_strings): self._tree = PrefixTree(expected_strings) assert_strings_can_be_matched(self, self._tree.to_regexp(PythonFormatter), expected_strings) def test_it_ignores_empty_strings(self): tree = PrefixTree(['']) self.assertEqual(PythonFormatter._EMPTY_STRING_MATCH, tree.to_regexp(PythonFormatter))
def check_formatter_output_matches_empty_string(self): regexp = PrefixTree([]).to_regexp(self._Formatter) output_word = re.compile(regexp).findall('') self.assertListEqual([''], output_word)
def test_it_ignores_empty_strings(self): tree = PrefixTree(['']) self.assertEqual(PythonFormatter._EMPTY_STRING_MATCH, tree.to_regexp(PythonFormatter))
def test_can_be_instantiated_with_initial_iterable_of_strings( self, expected_strings): self._tree = PrefixTree(expected_strings) assert_strings_can_be_matched(self, self._tree.to_regexp(PythonFormatter), expected_strings)
def test_correctly_encodes_strings_with_special_characters(self): for input_string, expected_regexp in SPECIAL_CHARACTER_STRINGS.items(): with self.subTest(input_string=input_string): tree = PrefixTree() tree.add(input_string) self.assertEqual(expected_regexp, tree._root_node.to_regexp())
def setUp(self): self._tree = PrefixTree()
def iterable_to_regexp( iterable: Iterable[str], formatter: Type[BaseFormatter] = PythonFormatter) -> str: return PrefixTree(iterable).to_regexp(formatter)
def stream_to_regexp(stream: TextIO, formatter: Type[BaseFormatter] = PythonFormatter) -> str: lines_generator = (line for line in stream.read().split(os.linesep) if line) prefix_tree = PrefixTree(lines_generator) return prefix_tree.to_regexp(formatter)