|
1 | 1 | from selfie_lib import SourceFile
|
2 | 2 |
|
3 | 3 |
|
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 |
39 | 12 |
|
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" |
43 | 13 |
|
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 |
51 | 19 |
|
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" |
55 | 20 |
|
| 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 )", "") |
56 | 26 |
|
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'" |
61 | 27 |
|
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") |
65 | 36 |
|
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) == "''" |
69 | 37 |
|
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('\\'')", "'\\''") |
76 | 45 |
|
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'" |
83 | 46 |
|
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(''' ' '' ' ''')", "''' ' '' ' '''") |
87 | 52 |
|
88 | 53 |
|
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" |
93 | 57 | )
|
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" |
99 | 60 | )
|
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", |
121 | 64 | )
|
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", |
126 | 68 | )
|
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' |
131 | 71 | )
|
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', |
136 | 75 | )
|
137 | 76 |
|
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 |
| - # ) |
142 | 77 |
|
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", |
155 | 82 | )
|
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", |
161 | 86 | )
|
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", |
167 | 90 | )
|
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