示例#1
0
 def test_ast_expression_left_operator_right_arithmeticcomparison_boolean(
         self):
     for left, right in itertools.product([True, False], repeat=2):
         left_expr = ast.BooleanExpression(context, left)
         right_expr = ast.BooleanExpression(context, right)
         self.assertExpressionTests('ge', left_expr, right_expr,
                                    left >= right)
         self.assertExpressionTests('gt', left_expr, right_expr,
                                    left > right)
         self.assertExpressionTests('le', left_expr, right_expr,
                                    left <= right)
         self.assertExpressionTests('lt', left_expr, right_expr,
                                    left < right)
示例#2
0
 def test_ast_expression_left_operator_right_arithmetic_type_errors(self):
     for operation in ('add', 'sub', 'fdiv', 'tdiv', 'mod', 'mul', 'pow'):
         with self.assertRaises(errors.EvaluationError):
             self.assertExpressionTests(
                 operation, ast.FloatExpression(context, 2.0),
                 ast.StringExpression(context, '4.0'))
         with self.assertRaises(errors.EvaluationError):
             self.assertExpressionTests(
                 operation, ast.StringExpression(context, '2.0'),
                 ast.FloatExpression(context, 4.0))
         with self.assertRaises(errors.EvaluationError):
             self.assertExpressionTests(
                 operation, ast.FloatExpression(context, 2.0),
                 ast.BooleanExpression(context, True))
         with self.assertRaises(errors.EvaluationError):
             self.assertExpressionTests(
                 operation, ast.BooleanExpression(context, True),
                 ast.FloatExpression(context, 4.0))
示例#3
0
import unittest

import rule_engine.ast as ast
import rule_engine.engine as engine

__all__ = ('LiteralExpressionTests', )

context = engine.Context()
context.builtins = engine.Builtins.from_defaults(
    {'test': {
        'one': 1.0,
        'two': 2.0
    }})
# literal expressions which should evaluate to false
falseish = (ast.ArrayExpression(context, tuple()),
            ast.BooleanExpression(context,
                                  False), ast.FloatExpression(context, 0.0),
            ast.NullExpression(context), ast.StringExpression(context, ''))
# literal expressions which should evaluate to true
trueish = (ast.ArrayExpression(context, tuple(
    (ast.NullExpression(context), ))),
           ast.ArrayExpression(context,
                               tuple((ast.FloatExpression(context, 1.0), ))),
           ast.BooleanExpression(context, True),
           ast.DatetimeExpression(context, datetime.datetime.now()),
           ast.FloatExpression(context, float('-inf')),
           ast.FloatExpression(context,
                               -1.0), ast.FloatExpression(context, 1.0),
           ast.FloatExpression(context, float('inf')),
           ast.StringExpression(context, 'non-empty'))

示例#4
0
import datetime
import unittest

import rule_engine.ast as ast
import rule_engine.engine as engine

__all__ = ('LiteralExpressionTests',)

context = engine.Context()
context.builtins = engine.Builtins.from_defaults(
	{'test': {'one': 1.0, 'two': 2.0}}
)
# literal expressions which should evaluate to false
falseish = (
	ast.ArrayExpression(context, tuple()),
	ast.BooleanExpression(context, False),
	ast.FloatExpression(context, 0.0),
	ast.NullExpression(context),
	ast.StringExpression(context, '')
)
# literal expressions which should evaluate to true
trueish = (
	ast.ArrayExpression(context, tuple((ast.NullExpression(context),))),
	ast.BooleanExpression(context, True),
	ast.DatetimeExpression(context, datetime.datetime.now()),
	ast.FloatExpression(context, float('-inf')),
	ast.FloatExpression(context, -1.0),
	ast.FloatExpression(context, 1.0),
	ast.FloatExpression(context, float('inf')),
	ast.StringExpression(context, 'non-empty')
)