-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_string_utils.py
198 lines (185 loc) · 4.77 KB
/
test_string_utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
import pytest
from py_gql._string_utils import (
highlight_location,
levenshtein,
parse_block_string,
wrapped_lines,
)
def test_highlight_location_1():
assert (
highlight_location(
"""{
query {
Node (search: "foo") {
id
name
}
}
}
""",
40,
)
== """(3:27):
1:{
2: query {
3: Node (search: "foo") {
^
4: id
5: name
"""
)
def test_highlight_location_2():
assert (
highlight_location(
"""{
me {
email
id
date_created
roles {
id
}
}
}
}
""",
80,
)
== """(11:1):
09: }
10: }
11:}
^
12:
"""
)
@pytest.mark.parametrize(
"value,expected",
[
pytest.param(
"\n".join(
[
"",
" Hello,",
" World!",
"",
" Yours,",
" GraphQL.",
]
),
"\n".join(["Hello,", " World!", "", "Yours,", " GraphQL."]),
id="Removes uniform indentation from a string",
),
pytest.param(
"\n".join(
[
" ",
"",
" Hello,",
" World!",
"",
" Yours,",
" GraphQL.",
"",
"",
]
),
"\n".join(["Hello,", " World!", "", "Yours,", " GraphQL."]),
id="Removes empty leading and trailing lines",
),
pytest.param(
"\n".join(
[
" Hello,",
" World!",
"",
" Yours,",
" GraphQL.",
]
),
"\n".join([" Hello,", " World!", "", "Yours,", " GraphQL."]),
id="Retains indentation from first line",
),
pytest.param(
"\n".join(
[
" ",
" Hello, ",
" World! ",
" ",
" Yours, ",
" GraphQL. ",
" ",
]
),
"\n".join(
[
"Hello, ",
" World! ",
" ",
"Yours, ",
" GraphQL. ",
]
),
id="Does not alter trailing spaces",
),
],
)
def test_parse_block_string(value, expected):
assert parse_block_string(value) == expected
def test_wrapped_lines():
source_lines = [
"This line is shorter and should not be wrapped.",
"This line is long and should be wrapped at around this position.",
"This line is longer and should be wrapped twice. This line is longer and "
"should be wrapped twice.",
"It should also wrap around underscores like this_token",
"and it should also wrap around dashes like this-kind-of-token.",
]
assert list(wrapped_lines(source_lines, 50)) == [
"This line is shorter and should not be wrapped.",
"This line is long and should be wrapped at around ",
"this position.",
"This line is longer and should be wrapped twice. ",
"This line is longer and should be wrapped twice.",
"It should also wrap around underscores like this_",
"token",
"and it should also wrap around dashes like this-",
"kind-of-token.",
]
@pytest.mark.parametrize(
"a,b,expected",
[
("", "", 0),
("a", "", 1),
("", "a", 1),
("abc", "", 3),
("", "abc", 3),
("", "", 0),
("a", "a", 0),
("abc", "abc", 0),
("", "a", 1),
("a", "ab", 1),
("b", "ab", 1),
("ac", "abc", 1),
("abcdefg", "xabxcdxxefxgx", 6),
("a", "", 1),
("ab", "a", 1),
("ab", "b", 1),
("abc", "ac", 1),
("xabxcdxxefxgx", "abcdefg", 6),
("a", "b", 1),
("ab", "ac", 1),
("ac", "bc", 1),
("abc", "axc", 1),
("xabxcdxxefxgx", "1ab2cd34ef5g6", 6),
("example", "samples", 3),
("sturgeon", "urgently", 6),
("levenshtein", "frankenstein", 6),
("distance", "difference", 5),
("java was neat", "scala is great", 7),
],
)
def test_levenshtein(a, b, expected):
assert levenshtein(a, b) == expected