|
| 1 | +import pytest |
| 2 | +from selfie_lib import SnapshotValueReader, ParseException |
| 3 | + |
| 4 | + |
| 5 | +class TestSnapshotValueReader: |
| 6 | + def test_no_escaping_needed(self): |
| 7 | + reader = SnapshotValueReader.of( |
| 8 | + """╔═ 00_empty ═╗ |
| 9 | +╔═ 01_singleLineString ═╗ |
| 10 | +this is one line |
| 11 | +╔═ 01a_singleLineLeadingSpace ═╗ |
| 12 | + the leading space is significant |
| 13 | +╔═ 01b_singleLineTrailingSpace ═╗ |
| 14 | +the trailing space is significant |
| 15 | +╔═ 02_multiLineStringTrimmed ═╗ |
| 16 | +Line 1 |
| 17 | +Line 2 |
| 18 | +╔═ 03_multiLineStringTrailingNewline ═╗ |
| 19 | +Line 1 |
| 20 | +Line 2 |
| 21 | +
|
| 22 | +╔═ 04_multiLineStringLeadingNewline ═╗ |
| 23 | +
|
| 24 | +Line 1 |
| 25 | +Line 2 |
| 26 | +╔═ 05_notSureHowPythonMultilineWorks ═╗ |
| 27 | +""" |
| 28 | + ) |
| 29 | + assert reader.peek_key() == "00_empty" |
| 30 | + assert reader.peek_key() == "00_empty" |
| 31 | + assert reader.next_value().value_string() == "" |
| 32 | + assert reader.peek_key() == "01_singleLineString" |
| 33 | + assert reader.peek_key() == "01_singleLineString" |
| 34 | + assert reader.next_value().value_string() == "this is one line" |
| 35 | + assert reader.peek_key() == "01a_singleLineLeadingSpace" |
| 36 | + assert reader.next_value().value_string() == " the leading space is significant" |
| 37 | + assert reader.peek_key() == "01b_singleLineTrailingSpace" |
| 38 | + assert ( |
| 39 | + reader.next_value().value_string() == "the trailing space is significant " |
| 40 | + ) |
| 41 | + assert reader.peek_key() == "02_multiLineStringTrimmed" |
| 42 | + assert reader.next_value().value_string() == "Line 1\nLine 2" |
| 43 | + assert reader.peek_key() == "03_multiLineStringTrailingNewline" |
| 44 | + assert reader.next_value().value_string() == "Line 1\nLine 2\n" |
| 45 | + assert reader.peek_key() == "04_multiLineStringLeadingNewline" |
| 46 | + assert reader.next_value().value_string() == "\nLine 1\nLine 2" |
| 47 | + assert reader.peek_key() == "05_notSureHowPythonMultilineWorks" |
| 48 | + assert reader.next_value().value_string() == "" |
| 49 | + |
| 50 | + def test_invalid_names(self): |
| 51 | + with pytest.raises(ParseException) as exc_info: |
| 52 | + SnapshotValueReader.of("╔═name ═╗").peek_key() |
| 53 | + assert "Expected to start with '╔═ '" in str(exc_info.value) |
| 54 | + |
| 55 | + with pytest.raises(ParseException) as exc_info: |
| 56 | + SnapshotValueReader.of("╔═ name═╗").peek_key() |
| 57 | + assert "Expected to contain ' ═╗'" in str(exc_info.value) |
| 58 | + |
| 59 | + with pytest.raises(ParseException) as exc_info: |
| 60 | + SnapshotValueReader.of("╔═ name ═╗").peek_key() |
| 61 | + assert "Leading spaces are disallowed: ' name'" in str(exc_info.value) |
| 62 | + |
| 63 | + with pytest.raises(ParseException) as exc_info: |
| 64 | + SnapshotValueReader.of("╔═ name ═╗").peek_key() |
| 65 | + assert "Trailing spaces are disallowed: 'name '" in str(exc_info.value) |
| 66 | + |
| 67 | + assert SnapshotValueReader.of("╔═ name ═╗ comment okay").peek_key() == "name" |
| 68 | + assert SnapshotValueReader.of("╔═ name ═╗okay here too").peek_key() == "name" |
| 69 | + assert ( |
| 70 | + SnapshotValueReader.of( |
| 71 | + "╔═ name ═╗ okay ╔═ ═╗ (it's the first ' ═╗' that counts)" |
| 72 | + ).peek_key() |
| 73 | + == "name" |
| 74 | + ) |
| 75 | + |
| 76 | + def test_escape_characters_in_name(self): |
| 77 | + reader = SnapshotValueReader.of( |
| 78 | + """╔═ test with \\(square brackets\\) in name ═╗ |
| 79 | +╔═ test with \\\\backslash\\\\ in name ═╗ |
| 80 | +╔═ test with\\nnewline\\nin name ═╗ |
| 81 | +╔═ test with \\ttab\\t in name ═╗ |
| 82 | +╔═ test with \\┌\\─ ascii art \\─\\┐ in name ═╗""" |
| 83 | + ) |
| 84 | + assert reader.peek_key() == "test with [square brackets] in name" |
| 85 | + assert reader.next_value().value_string() == "" |
| 86 | + assert reader.peek_key() == "test with \\backslash\\ in name" |
| 87 | + assert reader.next_value().value_string() == "" |
| 88 | + assert reader.peek_key() == "test with\nnewline\nin name" |
| 89 | + assert reader.next_value().value_string() == "" |
| 90 | + assert reader.peek_key() == "test with \ttab\t in name" |
| 91 | + assert reader.next_value().value_string() == "" |
| 92 | + assert reader.peek_key() == "test with ╔═ ascii art ═╗ in name" |
| 93 | + assert reader.next_value().value_string() == "" |
| 94 | + |
| 95 | + def assert_key_value_with_skip(self, test_content, key, expected_value): |
| 96 | + reader = SnapshotValueReader.of(test_content) |
| 97 | + while reader.peek_key() != key: |
| 98 | + reader.skip_value() |
| 99 | + assert reader.peek_key() == key |
| 100 | + assert reader.next_value().value_string() == expected_value |
| 101 | + while reader.peek_key() is not None: |
| 102 | + reader.skip_value() |
| 103 | + |
| 104 | + def test_skip_values(self): |
| 105 | + test_content = """╔═ 00_empty ═╗ |
| 106 | +╔═ 01_singleLineString ═╗ |
| 107 | +this is one line |
| 108 | +╔═ 02_multiLineStringTrimmed ═╗ |
| 109 | +Line 1 |
| 110 | +Line 2 |
| 111 | +╔═ 05_notSureHowKotlinMultilineWorks ═╗""" |
| 112 | + self.assert_key_value_with_skip(test_content, "00_empty", "") |
| 113 | + self.assert_key_value_with_skip( |
| 114 | + test_content, "01_singleLineString", "this is one line" |
| 115 | + ) |
| 116 | + self.assert_key_value_with_skip( |
| 117 | + test_content, "02_multiLineStringTrimmed", "Line 1\nLine 2" |
| 118 | + ) |
| 119 | + |
| 120 | + def test_binary(self): |
| 121 | + reader = SnapshotValueReader.of( |
| 122 | + """╔═ Apple ═╗ base64 length 3 bytes |
| 123 | +c2Fk""" |
| 124 | + ) |
| 125 | + assert reader.peek_key() == "Apple" |
| 126 | + assert reader.next_value().value_binary() == b"sad" |
0 commit comments