def test_ast_expression_getitem_reduces(self): container = ast.MappingExpression(context, (('one', 1), ('two', 2))) member = ast.FloatExpression(context, 0) with self.assertRaises(errors.LookupError): get_item = ast.GetItemExpression(context, container, member) get_item = ast.GetItemExpression(context, container, member, safe=True) self.assertIsInstance(get_item.reduce(), ast.NullExpression)
def test_ast_expression_getitem_error(self): container = ast.StringExpression(context, 'Rule Engine') member = ast.FloatExpression(context, 100.0) with self.assertRaises(errors.LookupError): ast.GetItemExpression(context, container, member).evaluate(None) member = ast.FloatExpression(context, 1.1) with self.assertRaises(errors.EvaluationError): ast.GetItemExpression(context, container, member).evaluate(None)
def test_ast_expression_getitem_safe(self): sym_name = ''.join( random.choice(string.ascii_letters) for _ in range(10)) container = ast.SymbolExpression(context, sym_name) member = ast.FloatExpression(context, 0) get_item = ast.GetItemExpression(context, container, member) with self.assertRaises(errors.EvaluationError): get_item.evaluate({sym_name: None}) get_item = ast.GetItemExpression(context, container, member, safe=True) self.assertIsNone(get_item.evaluate({sym_name: None}))
def test_ast_expression_getitem_error(self): for container in self.containers.values(): member = ast.FloatExpression(context, 100.0) with self.assertRaises(errors.LookupError): ast.GetItemExpression(context, container, member).evaluate(None) member = ast.FloatExpression(context, 1.1) with self.assertRaises(errors.EvaluationError): ast.GetItemExpression(context, container, member).evaluate(None) member = ast.NullExpression(context) with self.assertRaises(errors.EvaluationError): ast.GetItemExpression(context, container, member)
def test_ast_expression_getitem(self): container = self.containers[types.DataType.ARRAY] item_0 = ast.FloatExpression(context, 0.0) get_item = ast.GetItemExpression(context, container, item_0) self.assertEqual(get_item.evaluate(None), 'one') self.assertIsInstance(get_item.reduce(), ast.StringExpression) item_n1 = ast.FloatExpression(context, -1.0) get_item = ast.GetItemExpression(context, container, item_n1) self.assertEqual(get_item.evaluate(None), 'two') self.assertIsInstance(get_item.reduce(), ast.StringExpression) container = self.containers[types.DataType.STRING] get_item = ast.GetItemExpression(context, container, item_0) self.assertEqual(get_item.evaluate(None), 'R') self.assertIsInstance(get_item.reduce(), ast.StringExpression) get_item = ast.GetItemExpression(context, container, item_n1) self.assertEqual(get_item.evaluate(None), '!') self.assertIsInstance(get_item.reduce(), ast.StringExpression)
def test_ast_expression_getitem(self): container = ast.LiteralExpressionBase.from_value(context, range(5)) item_1 = ast.FloatExpression(context, 1.0) get_item = ast.GetItemExpression(context, container, item_1) self.assertEqual(get_item.evaluate(None), 1) self.assertIsInstance(get_item.reduce(), ast.FloatExpression) item_n1 = ast.FloatExpression(context, -1.0) get_item = ast.GetItemExpression(context, container, item_n1) self.assertEqual(get_item.evaluate(None), 4) self.assertIsInstance(get_item.reduce(), ast.FloatExpression) container = ast.StringExpression(context, 'Rule Engine') get_item = ast.GetItemExpression(context, container, item_1) self.assertEqual(get_item.evaluate(None), 'u') self.assertIsInstance(get_item.reduce(), ast.StringExpression) get_item = ast.GetItemExpression(context, container, item_n1) self.assertEqual(get_item.evaluate(None), 'e') self.assertIsInstance(get_item.reduce(), ast.StringExpression)
def test_ast_expression_getitem_mapping(self): container = self.containers[types.DataType.MAPPING] item = ast.StringExpression(context, 'foo') get_item = ast.GetItemExpression(context, container, item) self.assertEqual(get_item.evaluate(None), 'bar') self.assertIsInstance(get_item.reduce(), ast.StringExpression)