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