def test_highlighted_string_extract(input, value, highlights, before='>', after='<'): string = HighlightedString.extract(input, before, after) assert string.value == value for hl in string.highlights: assert hl.start is not None assert hl.stop is not None assert hl.step is None assert [string.value[hl] for hl in string.highlights] == highlights
def test_highlighted_string_apply_fuzz(words, before, after): slices = [] current_index = 0 for word, is_highlight in words: next_index = current_index + len(word) if is_highlight: slices.append(slice(current_index, next_index)) current_index = next_index string = HighlightedString(''.join(w for w, _ in words), slices) expected = ''.join(f'{before}{w}{after}' if h else w for w, h in words) assert string.apply(before, after) == expected
def test_highlighted_string_slice_validation(highlights): with pytest.raises(ValueError): HighlightedString('abcd', highlights)
def test_highlighted_string_roundtrip(input, before, after): assert HighlightedString.extract(input, before, after).apply(before, after) == input
def test_highlighted_string_extract_errors(input): with pytest.raises(ValueError): HighlightedString.extract(input, '>', '<')
input = ''.join(f'{before}{w}{after}' if h else w for w, h in words) value = ''.join(w for w, _ in words) highlights = [w for w, h in words if h] test_highlighted_string_extract(input, value, highlights, before, after) @pytest.mark.parametrize('input', ['>one', '>one >two<<', '<two', 'one>', 'two<']) def test_highlighted_string_extract_errors(input): with pytest.raises(ValueError): HighlightedString.extract(input, '>', '<') HS_SPLIT_APPLY_DATA = [ # string, split, apply, apply_upper (HighlightedString(), [''], '', ''), (HighlightedString('abcd'), ['abcd'], 'abcd', 'ABCD'), (HighlightedString('abcd', [slice(0, 4)]), ['', 'abcd', ''], 'xabcdy', 'xABCDy'), (HighlightedString('abcd', [slice(0, 0)]), ['', '', 'abcd'], 'xyabcd', 'xyABCD'), (HighlightedString('abcd', [slice(4, 4)]), ['abcd', '', ''], 'abcdxy', 'ABCDxy'), (HighlightedString('abcd', [slice(2, 2)]), ['ab', '', 'cd'], 'abxycd', 'ABxyCD'), (HighlightedString('abcd', [slice(1, 3)]), ['a', 'bc', 'd'], 'axbcyd', 'AxBCyD'), ( HighlightedString('abcd', [slice(0, 0), slice(0, 0)]), ['', '', '', '', 'abcd'], 'xyxyabcd',