def test_start_and_end_delimiter_context_factory_incomplete(): start_delimiter = 'start_delimiter' end_delimiter = 'end_delimiter' file = get_start_and_end_delimiter(CONTEXT1_LINES, CONTEXT2_LINES, start_delimiter=start_delimiter, end_delimiter=end_delimiter) # Remove last delimiter file.lines.pop() factory = StartAndEndDelimiterContextFactory( file, start_delimiter_matcher=ContainsTextMatcher(text=start_delimiter), end_delimiter_matcher=ContainsTextMatcher(text=end_delimiter), exclude_start_delimiter=True, exclude_end_delimiter=True, ) contexts = list(factory) assert len(contexts) == 2 expected_contexts_lines = [ CONTEXT1_LINES, CONTEXT2_LINES, ] for expected_lines, context in zip(expected_contexts_lines, contexts): assert context.lines == expected_lines
def test_start_and_end_delimiter_context_factory_defaults_different_delimiters_exclude_start_and_end( ): start_delimiter = '====' end_delimiter = '...' file = get_start_and_end_delimiter(CONTEXT1_LINES, CONTEXT2_LINES, start_delimiter=start_delimiter, end_delimiter=end_delimiter) factory = StartAndEndDelimiterContextFactory( file, start_delimiter_matcher=ContainsTextMatcher(text=start_delimiter), end_delimiter_matcher=ContainsTextMatcher(text=end_delimiter), exclude_start_delimiter=True, exclude_end_delimiter=True, ) contexts = list(factory) assert len(contexts) == 2 expected_contexts_lines = [ CONTEXT1_LINES, CONTEXT2_LINES, ] for expected_lines, context in zip(expected_contexts_lines, contexts): assert context.lines == expected_lines
def test_start_and_end_delimiter_context_factory_different_delimiters_separated( ): start_delimiter = '====' end_delimiter = '...' file = get_start_and_end_delimiter(CONTEXT1_LINES, CONTEXT2_LINES, start_delimiter=start_delimiter, end_delimiter=end_delimiter, add_stuff_inbetween=True) factory = StartAndEndDelimiterContextFactory( file, start_delimiter_matcher=ContainsTextMatcher(text=start_delimiter), end_delimiter_matcher=ContainsTextMatcher(text=end_delimiter), ) contexts = list(factory) assert len(contexts) == 2 expected_contexts_lines = [ [start_delimiter] + CONTEXT1_LINES + [end_delimiter], [start_delimiter] + CONTEXT2_LINES + [end_delimiter], ] for expected_lines, context in zip(expected_contexts_lines, contexts): assert context.lines == expected_lines
def test_start_and_end_delimiter_context_factory_end_delimiter_contains_start_delimiter_not_ignore_end_delimiter( ): start_delimiter = 'start_delimiter' end_delimiter = 'end_delimiter start_delimiter' context1 = [ 'This should be there', 'And so should this', ] context2 = [ 'This is the second context', 'And so is this', ] lines = ['This should not be there', start_delimiter ] + context1 + [end_delimiter] + context2 + [end_delimiter] file = get_file_mock(lines) factory = StartAndEndDelimiterContextFactory( file, start_delimiter_matcher=ContainsTextMatcher(text=start_delimiter), end_delimiter_matcher=ContainsTextMatcher(text=end_delimiter), exclude_start_delimiter=True, exclude_end_delimiter=True, ignore_end_delimiter=False, ) contexts = list(factory) # An empty context is created since the last end_delimiter is treated as a start_delimiter assert len(contexts) == 3 expected_contexts_lines = [ context1, context2, [], # Empty lines ] for expected_lines, context in zip(expected_contexts_lines, contexts): assert context.lines == expected_lines
def test_single_delimiter_context_factory_exclude_delimiter(): delimiter = '===' lines = CONTEXT1_LINES + CONTEXT2_LINES file = get_single_delimiter_file_mock(CONTEXT1_LINES, CONTEXT2_LINES) factory = SingleDelimiterContextFactory( file, delimiter_matcher=ContainsTextMatcher(text=delimiter), exclude_delimiter=True) contexts = list(factory) assert len(contexts) == 2 for context in contexts: for line in context.lines: assert line in lines assert line != delimiter
def test_single_delimiter_context_factory_empty_context_exclude_delimiter(): delimiter = '===' lines = CONTEXT1_LINES + CONTEXT2_LINES file = get_single_delimiter_file_mock([delimiter] + CONTEXT1_LINES, CONTEXT2_LINES + [delimiter], delimiter=delimiter) factory = SingleDelimiterContextFactory( file, delimiter_matcher=ContainsTextMatcher(text=delimiter), exclude_delimiter=True) contexts = list(factory) assert len(contexts) == 2 context_lines = contexts[0].lines + contexts[1].lines for line in lines: assert line in context_lines
def text_matcher(): return ContainsTextMatcher(text=TEXT_TO_MATCH)