def test_ast_expression_getslice_error(self): with self.assertRaises(errors.EvaluationError): ast.GetSliceExpression( context, ast.LiteralExpressionBase.from_value(context, 1.0)) with self.assertRaises(errors.EvaluationError): ast.GetSliceExpression( context, ast.LiteralExpressionBase.from_value(context, None)) with self.assertRaises(errors.EvaluationError): ast.GetSliceExpression( context, ast.LiteralExpressionBase.from_value(context, True))
def test_ast_expression_getslice_safe(self): sym_name = ''.join( random.choice(string.ascii_letters) for _ in range(10)) container = ast.SymbolExpression(context, sym_name) start = ast.FloatExpression(context, 0) stop = ast.FloatExpression(context, -1) get_slice = ast.GetSliceExpression(context, container, start, stop) with self.assertRaises(errors.EvaluationError): get_slice.evaluate({sym_name: None}) get_slice = ast.GetSliceExpression(context, container, start, stop, safe=True) self.assertIsNone(get_slice.evaluate({sym_name: None}))
def test_ast_expression_getslice(self): ary_value = tuple( random.choice(string.ascii_letters) for _ in range(12)) str_value = ''.join(ary_value) cases = ( (ary_value, str_value), (None, 0, 2), (None, -1, -3), ) for container, start, end in itertools.product(*cases): get_slice = ast.GetSliceExpression( context, ast.LiteralExpressionBase.from_value(context, container), start=(None if start is None else ast.LiteralExpressionBase.from_value(context, start)), stop=(None if end is None else ast.LiteralExpressionBase.from_value(context, end))) self.assertEqual(get_slice.evaluate({}), container[start:end])