6 class IsIntegerTests(unittest.TestCase):
7 def test_returns_true_for_integers(self):
9 self.assertTrue(stutter.is_integer(i))
11 def test_returns_false_for_booleans(self):
12 self.assertFalse(stutter.is_integer(True))
13 self.assertFalse(stutter.is_integer(False))
15 def test_returns_false_for_other_types(self):
16 for o in [object(), '', 0.1, [], (), {}, set()]:
17 self.assertFalse(stutter.is_integer(o))
19 class UndelimitStringTests(unittest.TestCase):
20 def test_returns_empty_strings(self):
22 actual = stutter.undelimit_string('""')
24 self.assertEqual(expected, actual)
26 def test_returns_strings_without_escapes(self):
27 expected = 'Hello, world'
28 actual = stutter.undelimit_string('"Hello, world"')
30 self.assertEqual(expected, actual)
32 def test_returns_strings_with_newlines(self):
33 expected = 'Hello, world\nGoodbye, cruel world'
34 actual = stutter.undelimit_string('"Hello, world\\nGoodbye, cruel world"')
36 self.assertEqual(expected, actual)
38 def test_returns_strings_with_escaped_delimiters(self):
39 expected = '"Hello, world"'
40 actual = stutter.undelimit_string('"\\"Hello, world\\""')
42 self.assertEqual(expected, actual)
44 def test_returns_strings_with_escaped_escape_characters(self):
46 actual = stutter.undelimit_string('"\\\\no"')
48 self.assertEqual(expected, actual)
50 class IndentTests(unittest.TestCase):
51 def test_indents_single_line(self):
52 expected = ' Hello, world'
53 actual = stutter.indent('Hello, world')
54 self.assertEqual(expected, actual)
56 def test_indents_multiple_lines(self):
57 expected = ' Hello, world\n Goodbye, cruel world'
58 actual = stutter.indent('Hello, world\nGoodbye, cruel world')
59 self.assertEqual(expected, actual)
61 def test_leaves_empty_lines_empty(self):
62 expected = ' Hello, world\n\n Goodbye, cruel world'
63 actual = stutter.indent('Hello, world\n \nGoodbye, cruel world')
64 self.assertEqual(expected, actual)
66 def test_indents_already_indented_lines(self):
67 expected = ' Hello, world\n\n Goodbye, cruel world'
68 actual = stutter.indent(' Hello, world\n\n Goodbye, cruel world')
69 self.assertEqual(expected, actual)
71 class ParseAllTests(unittest.TestCase):
72 def test_parses_integers(self):
74 actual = stutter.parse_all('0')
76 self.assertEqual(expected, actual)
78 def test_parses_identifiers(self):
79 expected = [stutter.Symbol('print')]
80 actual = stutter.parse_all('print')
82 self.assertEqual(expected, actual)
84 def test_parses_identifiers_with_dashes(self):
85 expected = [stutter.Symbol('hello-world')]
86 actual = stutter.parse_all('hello-world')
88 self.assertEqual(expected, actual)
90 def test_parses_strings(self):
91 expected = ['Hello, world']
92 actual = stutter.parse_all('"Hello, world"')
94 self.assertEqual(expected, actual)
96 def test_parses_strings_with_escaped_delimiters(self):
97 expected = ['"Hello, world"']
98 actual = stutter.parse_all('"\\"Hello, world\\""')
100 self.assertEqual(expected, actual)
102 def test_parses_empty_s_expressions(self):
104 actual = stutter.parse_all('()')
106 self.assertEqual(expected, actual)
108 def test_parses_s_expressions(self):
109 expected = [(0, 1, 2)]
110 actual = stutter.parse_all('(0 1 2)')
112 self.assertEqual(expected, actual)
114 def test_parses_nested_s_expressions(self):
115 expected = [(0, (1, (2,)))]
116 actual = stutter.parse_all('(0 (1 (2)))')
118 self.assertEqual(expected, actual)
120 def test_parses_multiple_expressions(self):
122 actual = stutter.parse_all('0 ()')
124 self.assertEqual(expected, actual)
126 def test_raises_exception_for_unclosed_parenthese(self):
127 self.assertRaises(Exception, stutter.parse_all, '(')
129 def test_raises_exception_for_unopened_parenthese(self):
130 self.assertRaises(Exception, stutter.parse_all, ')')
132 class QuoteToCTests(unittest.TestCase):
133 def test_quotes_integer_literals(self):
135 expected = stutter.CFunctionCallExpression(
136 'makeObjectPointerFromInteger',
137 [stutter.CIntegerLiteralExpression(i)],
140 actual = stutter.quote_to_c(i)
142 self.assertEqual(expected, actual)
144 def test_quotes_string_literals(self):
146 expected = stutter.CFunctionCallExpression(
147 'makeObjectPointerFromString',
148 [stutter.CStringLiteralExpression(s)],
151 actual = stutter.quote_to_c(s)
153 self.assertEqual(expected, actual)
155 def test_quotes_symbols(self):
157 expected = stutter.CFunctionCallExpression(
159 [stutter.CStringLiteralExpression(s)],
162 actual = stutter.quote_to_c(stutter.Symbol(s))
164 self.assertEqual(expected, actual)
166 class EvaluateApplicationArgumentsToCTests(unittest.TestCase):
167 def test_evaluates_empty_to_null(self):
168 expected = stutter.CVariableExpression('NULL')
169 actual = stutter.evaluate_application_arguments_to_c(())
171 self.assertEqual(expected, actual)
173 def test_evaluates_one_argument_to_cons(self):
176 sentinel = stutter.CStringLiteralExpression('1bd9707e76f8f807f3bad3e39049fea4a36d8ef2f8e2ed471ec755f7adb291d5')
178 def mock(argument_to_quote):
179 if argument_to_quote == argument:
182 expected = stutter.CFunctionCallExpression(
184 (sentinel, stutter.CVariableExpression('NULL')),
187 actual = stutter.evaluate_application_arguments_to_c(
192 self.assertEqual(expected, actual)
194 class EvaluateApplicationToCTests(unittest.TestCase):
195 def test_evaluates_function_calls_with_no_arguments(self):
198 sentinel = stutter.CVariableExpression('NULL')
201 assert arguments == ()
204 result = stutter.evaluate_application_to_c(
205 (stutter.Symbol(name),),
206 evaluate_application_arguments_to_c = mock,
209 self.assertEqual(result.name, name)
210 self.assertEqual(len(result.arguments), 2)
211 self.assertTrue(isinstance(
213 stutter.CReferenceExpression,
215 self.assertIs(result.arguments[1], sentinel)
217 def test_evaluates_function_calls_with_arguments(self):
221 sentinel = stutter.CFunctionCallExpression(
224 stutter.CFunctionCallExpression(
225 'makeObjectPointerFromInteger',
226 [stutter.CIntegerLiteralExpression(argument)],
232 assert arguments == (argument,)
235 result = stutter.evaluate_application_to_c(
236 (stutter.Symbol(name), argument),
237 evaluate_application_arguments_to_c = mock,
240 self.assertEqual(result.name, name)
241 self.assertEqual(len(result.arguments), 2)
242 self.assertTrue(isinstance(
244 stutter.CReferenceExpression,
246 self.assertIs(result.arguments[1], sentinel)
248 class EvaluateToCTests(unittest.TestCase):
249 def test_evaluates_integers(self):
251 result = stutter.evaluate_to_c(i)
252 self.assertIsInstance(result, stutter.CIntegerLiteralExpression)
253 self.assertEqual(result.integer, i)
255 def test_evaluates_string_literals(self):
257 result = stutter.evaluate_to_c(s)
259 self.assertIsInstance(result, stutter.CStringLiteralExpression)
260 self.assertEqual(result.string, s)
262 def test_calls_evaluate_application_when_given_an_application(self):
264 application = (stutter.Symbol('print'), 'Hello, world')
267 if argument == application:
270 result = stutter.evaluate_to_c(
272 evaluate_application_to_c = mock,
275 self.assertIs(result, sentinel)
277 class EvaluateAllToCTests(unittest.TestCase):
278 def test_returns_function_body(self):
279 result = stutter.evaluate_all_to_c([0])
281 self.assertIsInstance(result, stutter.CFunctionBody)
283 def test_main_contains_expression_statements_followed_by_return_statement(self):
284 result = stutter.evaluate_all_to_c([0,0,0])
286 self.assertIsInstance(result.statements[0],stutter.CDefinitionStatement)
287 self.assertIsInstance(result.statements[1],stutter.CExpressionStatement)
288 self.assertIsInstance(result.statements[2],stutter.CExpressionStatement)
289 self.assertIsInstance(result.statements[3],stutter.CReturnStatement)
291 class GeneratePointerTypeTests(unittest.TestCase):
292 def test_basic(self):
294 actual = stutter.generate_pointer_type(stutter.CPointerType(stutter.CType('int')))
295 self.assertEqual(expected, actual)
297 class GenerateTypeTests(unittest.TestCase):
298 def test_basic(self):
300 actual = stutter.generate_type(stutter.CType('int'))
301 self.assertEqual(expected, actual)
303 def test_generates_pointer_types(self):
305 actual = stutter.generate_type(
306 stutter.CPointerType(stutter.CType('int')),
307 generate_pointer_type = lambda x : expected)
309 self.assertIs(expected, actual)
311 class GenerateArgumentDeclarationTests(unittest.TestCase):
312 def test_basic(self):
313 expected = 'int argc'
314 actual = stutter.generate_argument_declaration(
315 stutter.CArgumentDeclaration(stutter.CType('int'), 'argc'))
316 self.assertEqual(expected, actual)
318 class GenerateArgumentDeclarationListTests(unittest.TestCase):
319 def test_basic(self):
320 argument_declarations = [
321 stutter.CArgumentDeclaration(stutter.CType('int'),'argc'),
322 stutter.CArgumentDeclaration(stutter.CPointerType(stutter.CPointerType(stutter.CType('char'))), 'argv'),
324 expected = 'int argc, char** argv'
325 actual = stutter.generate_argument_declaration_list(argument_declarations)
326 self.assertEqual(expected, actual)
328 class GenerateIntegerLiteralExpressionTests(unittest.TestCase):
329 def test_basic(self):
331 actual = stutter.generate_integer_literal_expression(
332 stutter.CIntegerLiteralExpression(0),
334 self.assertEqual(expected, actual)
336 class GenerateStringLiteralExpressionTests(unittest.TestCase):
337 def test_basic(self):
338 expected = '"Hello, world"'
339 actual = stutter.generate_string_literal_expression(
340 stutter.CStringLiteralExpression('Hello, world'),
342 self.assertEqual(expected, actual)
344 def test_escapes(self):
345 expected = r'"\\\n\"\t"'
346 actual = stutter.generate_string_literal_expression(
347 stutter.CStringLiteralExpression('\\\n"\t'),
349 self.assertEqual(expected, actual)
351 class GenerateVariableExpressionTests(unittest.TestCase):
352 def test_generates(self):
354 actual = stutter.generate_variable_expression(
355 stutter.CVariableExpression(expected),
358 self.assertEqual(expected, actual)
360 class GenerateReferenceExpressionTests(unittest.TestCase):
361 def test_generates(self):
363 actual = stutter.generate_reference_expression(
364 stutter.CReferenceExpression(stutter.CVariableExpression('name')),
367 self.assertEqual(expected, actual)
369 class GenerateFunctionCallExpressionTests(unittest.TestCase):
370 def test_no_arguments(self):
372 actual = stutter.generate_function_call_expression(
373 stutter.CFunctionCallExpression('name', []),
375 self.assertEqual(expected, actual)
377 def test_one_argument(self):
379 actual = stutter.generate_function_call_expression(
380 stutter.CFunctionCallExpression(
383 stutter.CIntegerLiteralExpression(0),
387 self.assertEqual(expected, actual)
389 def test_many_arguments(self):
390 expected = 'name(0, 1)'
391 actual = stutter.generate_function_call_expression(
392 stutter.CFunctionCallExpression(
395 stutter.CIntegerLiteralExpression(0),
396 stutter.CIntegerLiteralExpression(1),
400 self.assertEqual(expected, actual)
402 class GenerateExpressionTests(unittest.TestCase):
403 def test_generates_integer_literal_expressions(self):
405 actual = stutter.generate_expression(
406 stutter.CIntegerLiteralExpression(0),
407 generate_integer_literal_expression = lambda x : expected)
409 self.assertIs(expected, actual)
411 def test_generates_string_literal_expressions(self):
413 actual = stutter.generate_expression(
414 stutter.CStringLiteralExpression('Hello, world'),
415 generate_string_literal_expression = lambda x : expected)
417 self.assertIs(expected, actual)
419 def test_generates_variable_expression(self):
421 actual = stutter.generate_expression(
422 stutter.CVariableExpression('name'),
423 generate_variable_expression = lambda x : expected)
425 self.assertIs(expected, actual)
427 def test_generates_variable_expression(self):
429 actual = stutter.generate_expression(
430 stutter.CReferenceExpression(stutter.CVariableExpression('name')),
431 generate_reference_expression = lambda x : expected)
433 self.assertIs(expected, actual)
435 def test_generates_function_call_expression(self):
437 actual = stutter.generate_expression(
438 stutter.CFunctionCallExpression('name',[]),
439 generate_function_call_expression = lambda x : expected)
441 self.assertIs(expected, actual)
443 class GenerateStatement(unittest.TestCase):
444 def test_generates_expression_statement(self):
445 return_statement = stutter.CExpressionStatement('0')
448 actual = stutter.generate_statement(
450 generate_expression_statement = lambda _ : expected)
452 self.assertIs(expected, actual)
454 def test_generates_return_statement(self):
455 return_statement = stutter.CReturnStatement(stutter.CIntegerLiteralExpression(0))
458 actual = stutter.generate_statement(
460 generate_return_statement = lambda _ : expected)
462 self.assertIs(expected, actual)
464 def test_generates_definition_statement(self):
465 definition_statement = stutter.CDefinitionStatement(
466 stutter.CType('int'),
468 stutter.CIntegerLiteralExpression(0),
472 actual = stutter.generate_statement(
473 definition_statement,
474 generate_definition_statement = lambda _ : expected)
476 self.assertIs(expected, actual)
478 class GenerateExpressionStatementTests(unittest.TestCase):
479 def test_generates_return_statement(self):
480 expression_statement = stutter.CExpressionStatement(stutter.CIntegerLiteralExpression(0))
483 actual = stutter.generate_expression_statement(expression_statement)
485 self.assertEqual(expected, actual)
487 class GenerateReturnStatementTests(unittest.TestCase):
488 def test_generates_return_statement(self):
489 return_statement = stutter.CReturnStatement(stutter.CIntegerLiteralExpression(0))
491 expected = 'return 0;'
492 actual = stutter.generate_return_statement(return_statement)
494 self.assertEqual(expected, actual)
496 class GenerateDefinitionStatementTests(unittest.TestCase):
497 def test_generates_definition_statement(self):
498 definition_statement = stutter.CDefinitionStatement(
499 stutter.CType('int'),
501 stutter.CIntegerLiteralExpression(0),
504 expected = 'int number = 0;'
505 actual = stutter.generate_definition_statement(definition_statement)
507 self.assertEqual(expected, actual)
509 class GenerateFunctionDeclarationTests(unittest.TestCase):
510 def test_basic(self):
511 return_type = stutter.CType('int')
512 argument_declarations = [
513 stutter.CArgumentDeclaration(stutter.CType('int'),'argc'),
514 stutter.CArgumentDeclaration(stutter.CPointerType(stutter.CPointerType(stutter.CType('char'))), 'argv'),
517 function_declaration = stutter.CFunctionDeclaration(
520 argument_declarations,
521 stutter.CFunctionBody(
522 [stutter.CReturnStatement(stutter.CIntegerLiteralExpression(0))],
526 expected = 'int main(int argc, char** argv)\n{\n return 0;\n}'
527 actual = stutter.generate_function_declaration(function_declaration)
528 self.assertEqual(expected, actual)