|
19 | 19 | messages = st.text() |
20 | 20 |
|
21 | 21 |
|
| 22 | +def ansi_csi_escapes(): |
| 23 | + parameter_bytes = st.lists(st.characters(min_codepoint=0x30, max_codepoint=0x3F)) |
| 24 | + intermediate_bytes = st.lists(st.characters(min_codepoint=0x20, max_codepoint=0x2F)) |
| 25 | + final_bytes = st.characters(min_codepoint=0x40, max_codepoint=0x7E) |
| 26 | + |
| 27 | + return st.builds( |
| 28 | + lambda *args: "".join(["\x1b[", *args]), |
| 29 | + parameter_bytes.map("".join), |
| 30 | + intermediate_bytes.map("".join), |
| 31 | + final_bytes, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def ansi_c1_escapes(): |
| 36 | + byte_ = st.characters( |
| 37 | + codec="ascii", min_codepoint=0x40, max_codepoint=0x5F, exclude_characters=["["] |
| 38 | + ) |
| 39 | + return st.builds(lambda b: f"\x1b{b}", byte_) |
| 40 | + |
| 41 | + |
| 42 | +def ansi_fe_escapes(): |
| 43 | + return ansi_csi_escapes() | ansi_c1_escapes() |
| 44 | + |
| 45 | + |
22 | 46 | def preformatted_reports(): |
23 | 47 | return st.tuples(filepaths, names, variants | st.none(), messages).map( |
24 | 48 | lambda x: parse_logs.PreformattedReport(*x) |
@@ -47,3 +71,23 @@ def test_truncate(reports, max_chars): |
47 | 71 | formatted = parse_logs.truncate(reports, max_chars=max_chars, py_version=py_version) |
48 | 72 |
|
49 | 73 | assert formatted is None or len(formatted) <= max_chars |
| 74 | + |
| 75 | + |
| 76 | +@given(st.lists(ansi_fe_escapes()).map("".join)) |
| 77 | +def test_strip_ansi_multiple(escapes): |
| 78 | + assert parse_logs.strip_ansi(escapes) == "" |
| 79 | + |
| 80 | + |
| 81 | +@given(ansi_fe_escapes()) |
| 82 | +def test_strip_ansi(escape): |
| 83 | + message = f"some {escape}text" |
| 84 | + |
| 85 | + assert parse_logs.strip_ansi(message) == "some text" |
| 86 | + |
| 87 | + |
| 88 | +@given(ansi_fe_escapes()) |
| 89 | +def test_preformatted_report_ansi(escape): |
| 90 | + actual = parse_logs.PreformattedReport( |
| 91 | + filepath="a", name="b", variant=None, message=f"{escape}text" |
| 92 | + ) |
| 93 | + assert actual.message == "text" |
0 commit comments