Implement getattr operation

This commit is contained in:
2024-11-13 13:05:25 -08:00
parent 2278a16ca0
commit fb81eb84a7
6 changed files with 80 additions and 12 deletions

View File

@@ -114,6 +114,22 @@ class TestRefs(unittest.TestCase):
cfg = interp.Interp(io.StringIO('x = ["123", "456", "789"][1][2]'), "TestRefs.test_multi_index").run()
self.assertIn('x', cfg)
self.assertEqual(cfg['x'], '6')
def test_index_legacy(self):
i = interp.Interp(io.StringIO('x = y.1'), "TestRefs.test_index_legacy")
i['y'] = [123, 456, 789]
cfg = i.run()
self.assertIn('x', cfg)
self.assertEqual(cfg['x'], 456)
def test_getattr(self):
class Y:
z = 123
i = interp.Interp(io.StringIO('x = Y.z'), "TestRefs.test_getattr")
i['Y'] = Y()
cfg = i.run()
self.assertIn('x', cfg)
self.assertEqual(cfg['x'], 123)
def test_func(self):
def y(a, b):

View File

@@ -205,6 +205,29 @@ class TestExpressions(unittest.TestCase):
pos = ast.Position(name='TestExpressions.test_index', line=1, col=3),
value = 0,
))
def test_index_legacy(self):
val = parser.Parser(io.StringIO('x.0'), 'TestExpressions.test_index_legacy')._parse_expr()
self.assertIsInstance(val, ast.Index)
assert type(val) is ast.Index
self.assertEqual(val.value, ast.VariableRef(
pos = ast.Position(name='TestExpressions.test_index_legacy', line=1, col=1),
name = 'x',
))
self.assertEqual(val.index, ast.Integer(
pos = ast.Position(name='TestExpressions.test_index_legacy', line=1, col=3),
value = 0,
))
def test_getattr(self):
val = parser.Parser(io.StringIO('x.y'), 'TestExpressions.test_getattr')._parse_expr()
self.assertIsInstance(val, ast.GetAttr)
assert type(val) is ast.GetAttr
self.assertEqual(val.value, ast.VariableRef(
pos = ast.Position(name='TestExpressions.test_getattr', line=1, col=1),
name = 'x',
))
self.assertEqual(val.attr, 'y')
def test_unary(self):
val = parser.Parser(io.StringIO('!true'), 'TestExpressions.test_unary')._parse_value()