def test_find_symbols_in_normal_ast(self): f = "return x + 5" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set('x')) f = "def lalala(x, y, z): return x + y" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set({'lalala', 'x', 'y', 'z'}))
def test_translate_assignment_statement(self): mathparse = ctxmathparse.ASTMathParse('t') mathparse.parse_string('x = 99 * b') self.assertEqual(mathparse.translate_statements(), { '_t:stmt0': '(99 * [_t:b])', '_t:x': '[_t:stmt0]' })
def test_translate_two_statements(self): mathparse = ctxmathparse.ASTMathParse('t') mathparse.parse_string('x + 5\ny * 9') self.assertEqual(mathparse.translate_statements(), { '_t:stmt0': '([_t:x] + 5)', '_t:stmt1': '([_t:y] * 9)' })
def test_translate_one_statement(self): mathparse = ctxmathparse.ASTMathParse('s') mathparse.parse_string("x + 5") self.assertEqual(mathparse.symbols, set({'x'})) self.assertEqual(mathparse.translate_statements(), { "_s:stmt0": "([_s:x] + 5)", })
def test_separate_out_statements(self): mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string("x + 5") self.assertEqual(len(mathparse.statements), 1) mathparse.parse_string( "x + 5\ny + 150 * 99\ninvoke_a_thingy(99 * y, x/y)") self.assertEqual(len(mathparse.statements), 3)
def test_find_modified_symbols(self): f = "return x + 5" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set('x')) self.assertEqual(mathparse.target_symbols, set()) f = "x = 5\na = x" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set({'a', 'x'})) self.assertEqual(mathparse.target_symbols, set({'a', 'x'})) f = "x = 5 + b\nc = b * x" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set({'x', 'b', 'c'})) self.assertEqual(mathparse.target_symbols, set({'x', 'c'}))
def test_translate_another_assignment_statement(self): mathparse = ctxmathparse.ASTMathParse('t') mathparse.parse_string('x = 99 * b\ny = x * 38 + 15') self.assertEqual( mathparse.translate_statements(), { '_t:stmt0': '(99 * [_t:b])', '_t:x': '[_t:stmt0]', '_t:stmt1': '(([_t:x] * 38) + 15)', '_t:y': '[_t:stmt1]' })
def test_rearrange_statements(self): mathparse = ctxmathparse.ASTMathParse('t') mathparse.parse_string('x = 99 * b\ny = 150 + x') mathparse.statements[1] = mathparse.visit_substitute( mathparse.statements[0], # substitute this mathparse.statements[1] # into this ) self.assertEqual(mathparse.translate_statements(), { '_t:x': '(99 * [_t:b])', '_t:y': '(150 + (99 * [_t:b]))' })
def test_bind_symbols_in_context(self): ctx0 = ctxmathparse.ASTMathParse('ctx0') ctx0.symbols.add('a') ctx1 = ctx0.create_child_context('ctx1') ctx1.symbols.add('b') self.assertEqual(ctx0.qualified_context_name, 'ctx0') self.assertEqual(ctx1.qualified_context_name, 'ctx0:ctx1') self.assertEqual(ctx0.resolve_symbol('a'), '_ctx0:a') self.assertEqual(ctx1.resolve_symbol('a'), '_ctx0:a') self.assertEqual(ctx1.resolve_symbol('b'), '_ctx0:ctx1:b') with self.assertRaises(KeyError): ctx0.resolve_symbol('b')
def test_sort_free_and_bound(self): mathparse = ctxmathparse.ASTMathParse('t') mytree = ast.parse('x = 99 * b') self.assertEqual(mathparse.find_free_variables(mytree), {'b'}) self.assertEqual(mathparse.find_lhs_name(mytree), 'x')
def test_define_statement_symbols(self): mathparse = ctxmathparse.ASTMathParse('s') mathparse.parse_string('x + 5') self.assertEqual(mathparse.symbol_table, {'x': '_s:x'})
def test_imbue_symbols_with_context(self): f = "x, y, z" mathparse = ctxmathparse.ASTMathParse() mathparse.parse_string(f) self.assertEqual(mathparse.symbols, set({'x', 'y', 'z'})) self.assertEqual(mathparse.export_symbols(), set({'_x', '_y', '_z'}))