Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 4e73dd7

Browse files
committedMar 14, 2024
changed testing to reflect kotlin code and added a way to access the private variables
1 parent 3ba9c3f commit 4e73dd7

File tree

2 files changed

+95
-174
lines changed

2 files changed

+95
-174
lines changed
 

‎python/selfie-lib/selfie_lib/SourceFile.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, filename: str, content: str) -> None:
1212
self.__content_slice: Slice = Slice(content.replace("\r\n", "\n"))
1313
self.__language: Language = Language.from_filename(filename)
1414
self.__escape_leading_whitespace = EscapeLeadingWhitespace.appropriate_for(
15-
self.content_slice.__str__()
15+
self.__content_slice.__str__()
1616
)
1717

1818
@property
@@ -38,6 +38,12 @@ def __init__(
3838
self.__language = language
3939
self.__escape_leading_whitespace = escape_leading_whitespace
4040

41+
def _get_function_call_plus_arg(self):
42+
return self.__function_call_plus_arg
43+
44+
def _get_arg(self):
45+
return self.__arg
46+
4147
def set_literal_and_get_newline_delta(self, literal_value: LiteralValue) -> int:
4248
encoded = literal_value.format.encode(
4349
literal_value.actual, self.__language, self.__escape_leading_whitespace
@@ -57,7 +63,7 @@ def set_literal_and_get_newline_delta(self, literal_value: LiteralValue) -> int:
5763
)
5864
existing_newlines = self.__function_call_plus_arg.count("\n")
5965
new_newlines = encoded.count("\n")
60-
self.content_slice = self.__function_call_plus_arg.replaceSelfWith(
66+
self.__content_slice = self.__function_call_plus_arg.replaceSelfWith(
6167
f"{self.__dot_fun_open_paren}{encoded})"
6268
)
6369
return new_newlines - existing_newlines
@@ -66,7 +72,7 @@ def parse_literal(self, literal_format: LiteralFormat) -> Any:
6672
return literal_format.parse(self.__arg.__str__(), self.__language)
6773

6874
def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:
69-
line_content = self.content_slice.unixLine(line_one_indexed)
75+
line_content = self.__content_slice.unixLine(line_one_indexed)
7076
idx = line_content.indexOf(to_find)
7177
if idx == -1:
7278
raise AssertionError(
@@ -80,12 +86,12 @@ def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:
8086
def replace_on_line(self, line_one_indexed: int, find: str, replace: str) -> None:
8187
assert "\n" not in find
8288
assert "\n" not in replace
83-
line_content = self.content_slice.unixLine(line_one_indexed).__str__()
89+
line_content = self.__content_slice.unixLine(line_one_indexed).__str__()
8490
new_content = line_content.replace(find, replace)
85-
self.content_slice = Slice(self.content_slice.replaceSelfWith(new_content))
91+
self.__content_slice = Slice(self.__content_slice.replaceSelfWith(new_content))
8692

8793
def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
88-
line_content = self.content_slice.unixLine(line_one_indexed)
94+
line_content = self.__content_slice.unixLine(line_one_indexed)
8995
dot_fun_open_paren = None
9096

9197
for to_be_like in TO_BE_LIKES:
@@ -102,24 +108,24 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
102108
dot_function_call = dot_function_call_in_place + line_content.startIndex
103109
arg_start = dot_function_call + len(dot_fun_open_paren)
104110

105-
if self.content_slice.__len__() == arg_start:
111+
if self.__content_slice.__len__() == arg_start:
106112
raise AssertionError(
107113
f"Appears to be an unclosed function call `{dot_fun_open_paren}` "
108114
f"on line {line_one_indexed}"
109115
)
110-
while self.content_slice[arg_start].isspace():
116+
while self.__content_slice[arg_start].isspace():
111117
arg_start += 1
112-
if self.content_slice.__len__() == arg_start:
118+
if self.__content_slice.__len__() == arg_start:
113119
raise AssertionError(
114120
f"Appears to be an unclosed function call `{dot_fun_open_paren}` "
115121
f"on line {line_one_indexed}"
116122
)
117123

118124
end_arg = -1
119125
end_paren = 0
120-
if self.content_slice[arg_start] == '"':
121-
if self.content_slice[arg_start].startswith(self.TRIPLE_QUOTE):
122-
end_arg = self.content_slice.indexOf(
126+
if self.__content_slice[arg_start] == '"':
127+
if self.__content_slice[arg_start].startswith(self.TRIPLE_QUOTE):
128+
end_arg = self.__content_slice.indexOf(
123129
self.TRIPLE_QUOTE, arg_start + len(self.TRIPLE_QUOTE)
124130
)
125131
if end_arg == -1:
@@ -133,11 +139,11 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
133139
else:
134140
end_arg = arg_start + 1
135141
while (
136-
self.content_slice[end_arg] != '"'
137-
or self.content_slice[end_arg - 1] == "\\"
142+
self.__content_slice[end_arg] != '"'
143+
or self.__content_slice[end_arg - 1] == "\\"
138144
):
139145
end_arg += 1
140-
if end_arg == self.content_slice.__len__():
146+
if end_arg == self.__content_slice.__len__():
141147
raise AssertionError(
142148
f'Appears to be an unclosed string literal `"` '
143149
f"on line {line_one_indexed}"
@@ -146,34 +152,34 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
146152
end_paren = end_arg
147153
else:
148154
end_arg = arg_start
149-
while not self.content_slice[end_arg].isspace():
150-
if self.content_slice[end_arg] == ")":
155+
while not self.__content_slice[end_arg].isspace():
156+
if self.__content_slice[end_arg] == ")":
151157
break
152158
end_arg += 1
153-
if end_arg == self.content_slice.__len__():
159+
if end_arg == self.__content_slice.__len__():
154160
raise AssertionError(
155161
f"Appears to be an unclosed numeric literal "
156162
f"on line {line_one_indexed}"
157163
)
158164
end_paren = end_arg
159-
while self.content_slice[end_paren] != ")":
160-
if not self.content_slice[end_paren].isspace():
165+
while self.__content_slice[end_paren] != ")":
166+
if not self.__content_slice[end_paren].isspace():
161167
raise AssertionError(
162168
f"Non-primitive literal in `{dot_fun_open_paren}` starting at "
163169
f"line {line_one_indexed}: error for character "
164-
f"`{self.content_slice[end_paren]}` on line "
165-
f"{self.content_slice.baseLineAtOffset(end_paren)}"
170+
f"`{self.__content_slice[end_paren]}` on line "
171+
f"{self.__content_slice.baseLineAtOffset(end_paren)}"
166172
)
167173
end_paren += 1
168-
if end_paren == self.content_slice.__len__():
174+
if end_paren == self.__content_slice.__len__():
169175
raise AssertionError(
170176
f"Appears to be an unclosed function call `{dot_fun_open_paren}` "
171177
f"starting at line {line_one_indexed}"
172178
)
173179
return self.ToBeLiteral(
174180
dot_fun_open_paren.replace("_TODO", ""),
175-
self.content_slice.subSequence(dot_function_call, end_paren + 1),
176-
self.content_slice.subSequence(arg_start, end_arg),
181+
self.__content_slice.subSequence(dot_function_call, end_paren + 1),
182+
self.__content_slice.subSequence(arg_start, end_arg),
177183
self.__language,
178184
self.__escape_leading_whitespace,
179185
)
Lines changed: 64 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,90 @@
11
from selfie_lib import SourceFile
22

33

4-
def test_todo():
5-
source_file = SourceFile("UnderTest.py", ".toBe_TODO()")
6-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe_TODO()"
7-
assert str(source_file.parse_to_be_like(1).arg) == ""
8-
9-
source_file = SourceFile("UnderTest.py", " .toBe_TODO() ")
10-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe_TODO()"
11-
assert str(source_file.parse_to_be_like(1).arg) == ""
12-
13-
source_file = SourceFile("UnderTest.py", " .toBe_TODO( ) ")
14-
assert (
15-
str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe_TODO( )"
16-
)
17-
assert str(source_file.parse_to_be_like(1).arg) == ""
18-
19-
source_file = SourceFile("UnderTest.py", " .toBe_TODO( \n ) ")
20-
assert (
21-
str(source_file.parse_to_be_like(1).function_call_plus_arg)
22-
== ".toBe_TODO( \n )"
23-
)
24-
assert str(source_file.parse_to_be_like(1).arg) == ""
25-
26-
27-
def test_numeric():
28-
source_file = SourceFile("UnderTest.py", ".toBe(7)")
29-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(7)"
30-
assert str(source_file.parse_to_be_like(1).arg) == "7"
31-
32-
source_file = SourceFile("UnderTest.py", " .toBe(7)")
33-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(7)"
34-
assert str(source_file.parse_to_be_like(1).arg) == "7"
35-
36-
source_file = SourceFile("UnderTest.py", ".toBe(7) ")
37-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(7)"
38-
assert str(source_file.parse_to_be_like(1).arg) == "7"
4+
def python_test(source_raw, function_call_plus_arg_raw, arg_raw=""):
5+
source = source_raw.replace("'", '"')
6+
function_call_plus_arg = function_call_plus_arg_raw.replace("'", '"')
7+
arg = arg_raw.replace("'", '"')
8+
parsed = SourceFile("UnderTest.py", source)
9+
to_be_literal = parsed.parse_to_be_like(1)
10+
assert to_be_literal._get_function_call_plus_arg() == function_call_plus_arg
11+
assert to_be_literal._get_arg() == arg
3912

40-
source_file = SourceFile("UnderTest.py", " .toBe(7) ")
41-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(7)"
42-
assert str(source_file.parse_to_be_like(1).arg) == "7"
4313

44-
source_file = SourceFile("UnderTest.py", " .toBe( 7 ) ")
45-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe( 7 )"
46-
assert str(source_file.parse_to_be_like(1).arg) == "7"
47-
48-
source_file = SourceFile("UnderTest.py", " .toBe(\n7) ")
49-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(\n7)"
50-
assert str(source_file.parse_to_be_like(1).arg) == "7"
14+
def python_test_error(source_raw, error_msg):
15+
try:
16+
python_test(source_raw, "unusedArg")
17+
except AssertionError as e:
18+
assert str(e) == error_msg
5119

52-
source_file = SourceFile("UnderTest.py", " .toBe(7\n) ")
53-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe(7\n)"
54-
assert str(source_file.parse_to_be_like(1).arg) == "7"
5520

21+
def todo():
22+
python_test(".toBe_TODO()", ".toBe_TODO()", "")
23+
python_test(" .toBe_TODO() ", ".toBe_TODO()", "")
24+
python_test(" .toBe_TODO( ) ", ".toBe_TODO( )", "")
25+
python_test(" .toBe_TODO( \n ) ", ".toBe_TODO( \n )", "")
5626

57-
def test_single_line_string():
58-
source_file = SourceFile("UnderTest.py", ".toBe('7')")
59-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe('7')"
60-
assert str(source_file.parse_to_be_like(1).arg) == "'7'"
6127

62-
source_file = SourceFile("UnderTest.py", ".toBe('')")
63-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe('')"
64-
assert str(source_file.parse_to_be_like(1).arg) == "''"
28+
def numeric():
29+
python_test(".toBe(7)", ".toBe(7)", "7")
30+
python_test(" .toBe(7)", ".toBe(7)", "7")
31+
python_test(".toBe(7) ", ".toBe(7)", "7")
32+
python_test(" .toBe(7) ", ".toBe(7)", "7")
33+
python_test(" .toBe( 7 ) ", ".toBe( 7 )", "7")
34+
python_test(" .toBe(\n7) ", ".toBe(\n7)", "7")
35+
python_test(" .toBe(7\n) ", ".toBe(7\n)", "7")
6536

66-
source_file = SourceFile("UnderTest.py", ".toBe( '' )")
67-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe( '' )"
68-
assert str(source_file.parse_to_be_like(1).arg) == "''"
6937

70-
source_file = SourceFile("UnderTest.py", ".toBe( \n '' \n )")
71-
assert (
72-
str(source_file.parse_to_be_like(1).function_call_plus_arg)
73-
== ".toBe( \n '' \n )"
74-
)
75-
assert str(source_file.parse_to_be_like(1).arg) == "''"
38+
def single_line_string():
39+
python_test(".toBe('7')", "'7'")
40+
python_test(".toBe('')", "''")
41+
python_test(".toBe( '' )", "''")
42+
python_test(".toBe( \n '' \n )", "''")
43+
python_test(".toBe( \n '78' \n )", "'78'")
44+
python_test(".toBe('\\'')", "'\\''")
7645

77-
source_file = SourceFile("UnderTest.py", ".toBe( \n '78' \n )")
78-
assert (
79-
str(source_file.parse_to_be_like(1).function_call_plus_arg)
80-
== ".toBe( \n '78' \n )"
81-
)
82-
assert str(source_file.parse_to_be_like(1).arg) == "'78'"
8346

84-
source_file = SourceFile("UnderTest.py", ".toBe('\\'')")
85-
assert str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe('\\'')"
86-
assert str(source_file.parse_to_be_like(1).arg) == "'\\''"
47+
def multi_line_string():
48+
python_test(".toBe('''7''')", "'''7'''")
49+
python_test(".toBe(''' 7 ''')", "''' 7 '''")
50+
python_test(".toBe('''\n7\n''')", "'''\n7\n'''")
51+
python_test(".toBe(''' ' '' ' ''')", "''' ' '' ' '''")
8752

8853

89-
def test_multi_line_string():
90-
source_file = SourceFile("UnderTest.py", ".toBe('''7''')")
91-
assert (
92-
str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe('''7''')"
54+
def error_unclosed():
55+
python_test_error(
56+
".toBe(", "Appears to be an unclosed function call `.toBe()` on line 1"
9357
)
94-
assert str(source_file.parse_to_be_like(1).arg) == "'''7'''"
95-
96-
source_file = SourceFile("UnderTest.py", ".toBe('''7''')")
97-
assert (
98-
str(source_file.parse_to_be_like(1).function_call_plus_arg) == ".toBe('''7''')"
58+
python_test_error(
59+
".toBe( \n ", "Appears to be an unclosed function call `.toBe()` on line 1"
9960
)
100-
assert str(source_file.parse_to_be_like(1).arg) == "'''7'''"
101-
102-
# source_file = SourceFile("UnderTest.py", ".toBe('''\n7\n''')")
103-
# assert (
104-
# str(source_file.parse_to_be_like(1).function_call_plus_arg)
105-
# == ".toBe('''\n7\n''')"
106-
# )
107-
# assert str(source_file.parse_to_be_like(1).arg) == "'''\n7\n'''"
108-
109-
# source_file = SourceFile("UnderTest.py", ".toBe(''' ' '' ' ''')")
110-
# assert (
111-
# str(source_file.parse_to_be_like(1).function_call_plus_arg)
112-
# == ".toBe(''' ' '' ' ''')"
113-
# )
114-
# assert str(source_file.parse_to_be_like(1).arg) == "''' ' '' ' '''"
115-
116-
117-
def test_error_unclosed():
118-
source_file = SourceFile("UnderTest.py", ".toBe(")
119-
assert_raises_error(
120-
source_file, "Appears to be an unclosed function call `.toBe(` on line 1"
61+
python_test_error(
62+
".toBe_TODO(",
63+
"Appears to be an unclosed function call `.toBe_TODO()` on line 1",
12164
)
122-
123-
source_file = SourceFile("UnderTest.py", ".toBe( \n ")
124-
assert_raises_error(
125-
source_file, "Appears to be an unclosed function call `.toBe(` on line 1"
65+
python_test_error(
66+
".toBe_TODO( \n ",
67+
"Appears to be an unclosed function call `.toBe_TODO()` on line 1",
12668
)
127-
128-
source_file = SourceFile("UnderTest.py", ".toBe_TODO(")
129-
assert_raises_error(
130-
source_file, "Appears to be an unclosed function call `.toBe_TODO(` on line 1"
69+
python_test_error(
70+
".toBe_TODO(')", 'Appears to be an unclosed string literal `"` on line 1'
13171
)
132-
133-
source_file = SourceFile("UnderTest.py", ".toBe_TODO( \n ")
134-
assert_raises_error(
135-
source_file, "Appears to be an unclosed function call `.toBe_TODO(` on line 1"
72+
python_test_error(
73+
".toBe_TODO(''')",
74+
'Appears to be an unclosed multiline string literal `"""` on line 1',
13675
)
13776

138-
# source_file = SourceFile("UnderTest.py", ".toBe_TODO(')")
139-
# assert_raises_error(
140-
# source_file, 'Appears to be an unclosed string literal `"` on line 1'
141-
# )
14277

143-
# source_file = SourceFile("UnderTest.py", ".toBe_TODO(''')")
144-
# assert_raises_error(
145-
# source_file,
146-
# 'Appears to be an unclosed multiline string literal `"""` on line 1',
147-
# )
148-
149-
150-
def test_error_non_primitive():
151-
source_file = SourceFile("UnderTest.py", ".toBe(1 + 1)")
152-
assert_raises_error(
153-
source_file,
154-
"Non-primitive literal in `.toBe(` starting at line 1: error for character `+` on line 1",
78+
def error_non_primitive():
79+
python_test_error(
80+
".toBe(1 + 1)",
81+
"Non-primitive literal in `.toBe()` starting at line 1: error for character `+` on line 1",
15582
)
156-
157-
source_file = SourceFile("UnderTest.py", ".toBe('1' + '1')")
158-
assert_raises_error(
159-
source_file,
160-
"Non-primitive literal in `.toBe(` starting at line 1: error for character `+` on line 1",
83+
python_test_error(
84+
".toBe('1' + '1')",
85+
"Non-primitive literal in `.toBe()` starting at line 1: error for character `+` on line 1",
16186
)
162-
163-
source_file = SourceFile("UnderTest.py", ".toBe('''1''' + '''1''')")
164-
assert_raises_error(
165-
source_file,
166-
"Non-primitive literal in `.toBe(` starting at line 1: error for character `+` on line 1",
87+
python_test_error(
88+
".toBe('''1''' + '''1''')",
89+
"Non-primitive literal in `.toBe()` starting at line 1: error for character `+` on line 1",
16790
)
168-
169-
170-
def assert_raises_error(source_file, error_msg):
171-
try:
172-
source_file.parse_to_be_like(1)
173-
assert False, "Expected an AssertionError, but none was raised."
174-
except AssertionError as e:
175-
assert str(e) == error_msg

0 commit comments

Comments
 (0)