|
| 1 | +import pytest |
| 2 | + |
| 3 | +from selfie_lib import PerCharacterEscaper |
| 4 | + |
| 5 | + |
| 6 | +class TestPerCharacterEscaper: |
| 7 | + def test_performance_optimization_self(self): |
| 8 | + escaper = PerCharacterEscaper.self_escape("`123") |
| 9 | + abc = "abc" |
| 10 | + # Correct use of 'is' for checking object identity. |
| 11 | + assert ( |
| 12 | + escaper.escape(abc) is abc |
| 13 | + ), "Escape should return the original object when no change is made" |
| 14 | + assert ( |
| 15 | + escaper.unescape(abc) is abc |
| 16 | + ), "Unescape should return the original object when no change is made" |
| 17 | + |
| 18 | + # Use '==' for checking value equality. |
| 19 | + assert ( |
| 20 | + escaper.escape("1") == "`1" |
| 21 | + ), "Escaping '1' should prepend the escape character" |
| 22 | + assert ( |
| 23 | + escaper.escape("`") == "``" |
| 24 | + ), "Escaping the escape character should duplicate it" |
| 25 | + assert ( |
| 26 | + escaper.escape("abc123`def") == "abc`1`2`3``def" |
| 27 | + ), "Escaping 'abc123`def' did not produce the expected result" |
| 28 | + |
| 29 | + assert escaper.unescape("`1") == "1", "Unescaping '`1' should produce '1'" |
| 30 | + assert escaper.unescape("``") == "`", "Unescaping '``' should produce '`'" |
| 31 | + assert ( |
| 32 | + escaper.unescape("abc`1`2`3``def") == "abc123`def" |
| 33 | + ), "Unescaping 'abc`1`2`3``def' did not produce the expected result" |
| 34 | + |
| 35 | + def test_performance_optimization_specific(self): |
| 36 | + escaper = PerCharacterEscaper.specified_escape("`a1b2c3d") |
| 37 | + abc = "abc" |
| 38 | + # Correct use of 'is' for object identity. |
| 39 | + assert ( |
| 40 | + escaper.escape(abc) is abc |
| 41 | + ), "Escape should return the original object when no change is made" |
| 42 | + assert ( |
| 43 | + escaper.unescape(abc) is abc |
| 44 | + ), "Unescape should return the original object when no change is made" |
| 45 | + |
| 46 | + # Use '==' for value equality. |
| 47 | + assert escaper.escape("1") == "`b", "Escaping '1' should produce '`b'" |
| 48 | + assert escaper.escape("`") == "`a", "Escaping '`' should produce '`a'" |
| 49 | + assert ( |
| 50 | + escaper.escape("abc123`def") == "abc`b`c`d`adef" |
| 51 | + ), "Escaping 'abc123`def' did not produce the expected result" |
| 52 | + |
| 53 | + assert escaper.unescape("`b") == "1", "Unescaping '`b' should produce '1'" |
| 54 | + assert escaper.unescape("`a") == "`", "Unescaping '`a' should produce '`'" |
| 55 | + assert ( |
| 56 | + escaper.unescape("abc`1`2`3``def") == "abc123`def" |
| 57 | + ), "Unescaping 'abc`1`2`3``def' did not produce the expected result" |
| 58 | + |
| 59 | + def test_corner_cases_self(self): |
| 60 | + escaper = PerCharacterEscaper.self_escape("`123") |
| 61 | + with pytest.raises(ValueError) as excinfo: |
| 62 | + escaper.unescape("`") |
| 63 | + assert ( |
| 64 | + str(excinfo.value) |
| 65 | + == "Escape character '`' can't be the last character in a string." |
| 66 | + ), "Unescaping a string ending with a single escape character should raise ValueError" |
| 67 | + assert escaper.unescape("`a") == "a", "Unescaping '`a' should produce 'a'" |
| 68 | + |
| 69 | + def test_corner_cases_specific(self): |
| 70 | + escaper = PerCharacterEscaper.specified_escape("`a1b2c3d") |
| 71 | + with pytest.raises(ValueError) as excinfo: |
| 72 | + escaper.unescape("`") |
| 73 | + assert ( |
| 74 | + str(excinfo.value) |
| 75 | + == "Escape character '`' can't be the last character in a string." |
| 76 | + ), "Unescaping a string ending with a single escape character should raise ValueError" |
| 77 | + assert escaper.unescape("`e") == "e", "Unescaping '`e' should produce 'e'" |
| 78 | + |
| 79 | + def test_roundtrip(self): |
| 80 | + escaper = PerCharacterEscaper.self_escape("`<>") |
| 81 | + |
| 82 | + def roundtrip(str): |
| 83 | + assert ( |
| 84 | + escaper.unescape(escaper.escape(str)) == str |
| 85 | + ), f"Roundtrip of '{str}' did not return the original string" |
| 86 | + |
| 87 | + roundtrip("") |
| 88 | + roundtrip("<local>~`/") |
0 commit comments