コード例 #1
0
def test_unary_expr():
    for args, expected in (
        ((number_literal('5'),), -5.0),
        ((number_literal('-2'),), 2.0),
        ((string_literal('5'),), -5.0),
        ((string_literal('-2'),), 2.0),
        ):
        result = unary_expr(*args).evaluate_as_number(default_context)
        _run_test(args, result, expected)
コード例 #2
0
def test_ceiling_function():
    for arg, expected in (
        (string_literal('"3.14"'), datatypes.number(4)),
        (NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (number_literal('0.5'), datatypes.number(1)),
        (number_literal('-0.5'), datatypes.number(-0.0)),
        ):
        result = function_call('ceiling', [arg]).evaluate_as_number(CONTEXT1)
        assert isinstance(result, datatypes.number)
        assert ((result.isnan() and expected.isnan()) or result == expected), (result, expected)
コード例 #3
0
def test_ceiling_function():
    for arg, expected in (
        (string_literal('"3.14"'), datatypes.number(4)),
        (NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (number_literal('0.5'), datatypes.number(1)),
        (number_literal('-0.5'), datatypes.number(-0.0)),
    ):
        result = function_call('ceiling', [arg]).evaluate_as_number(CONTEXT1)
        assert isinstance(result, datatypes.number)
        assert ((result.isnan() and expected.isnan())
                or result == expected), (result, expected)
コード例 #4
0
ファイル: test_basic_expr.py プロジェクト: mredar/amara
def test_number_literal():
    for input, expected in (
        ('0', 0.0),
        ('0.5', 0.5),
        ('-0.5', -0.5),
        ('1', 1.0),
        ('-1', -1.0),
        ('1.5', 1.5),
        ('-1.5', -1.5),
        ('2', 2.0),
        ('-2', -2.0),
        ('2.6', 2.6),
        ('-2.6', -2.6),
        ('3', 3.0),
        ('-3.0', -3.0),
        ('31', 31.0),
        ('4', 4.0),
        ('-4', -4.0),
        ('4.5', 4.5),
        ('-4.5', -4.5),
        ('5', 5.0),
        ('-5', -5.0),
        ('-42', -42.0),
            # Not normal tokens, but need to be handled internally
        (datatypes.NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (datatypes.POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (datatypes.NEGATIVE_INFINITY, datatypes.NEGATIVE_INFINITY),
    ):
        node = number_literal(input)
        result = node.evaluate_as_number(default_context)
        assert isinstance(result, datatypes.number), result
        if hasattr(expected, "isnan") and expected.isnan():
            assert result.isnan(), result
        else:
            assert result == expected, (result, expected)
コード例 #5
0
ファイル: test_basic_expr.py プロジェクト: abed-hawa/amara
def test_number_literal():
    for input, expected in (
        ('0', 0.0),
        ('0.5', 0.5),
        ('-0.5', -0.5),
        ('1', 1.0),
        ('-1', -1.0),
        ('1.5', 1.5),
        ('-1.5', -1.5),
        ('2', 2.0),
        ('-2', -2.0),
        ('2.6', 2.6),
        ('-2.6', -2.6),
        ('3', 3.0),
        ('-3.0', -3.0),
        ('31', 31.0),
        ('4', 4.0),
        ('-4', -4.0),
        ('4.5', 4.5),
        ('-4.5', -4.5),
        ('5', 5.0),
        ('-5', -5.0),
        ('-42', -42.0),
        # Not normal tokens, but need to be handled internally
        (datatypes.NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (datatypes.POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (datatypes.NEGATIVE_INFINITY, datatypes.NEGATIVE_INFINITY),
        ):
        node = number_literal(input)
        result = node.evaluate_as_number(default_context)
        assert isinstance(result, datatypes.number), result
        if hasattr(expected, "isnan") and expected.isnan():
            assert result.isnan(), result
        else:
            assert result == expected, (result, expected)
コード例 #6
0
def test_round_function():
    for arg, expected in (
        (string_literal('"3.14"'), datatypes.number(3)),
        (number_literal('-4.5'), datatypes.number(-4)),
        (NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (NEGATIVE_INFINITY, datatypes.NEGATIVE_INFINITY),
        (string_literal('"12345"'), datatypes.number(12345)),
        ):
        print "Call", arg
        result = function_call('round', [arg]).evaluate_as_number(CONTEXT1)
        assert isinstance(result, datatypes.number)
        assert ((result.isnan() and expected.isnan()) or result == expected), (result, expected)
コード例 #7
0
def test_round_function():
    for arg, expected in (
        (string_literal('"3.14"'), datatypes.number(3)),
        (number_literal('-4.5'), datatypes.number(-4)),
        (NOT_A_NUMBER, datatypes.NOT_A_NUMBER),
        (POSITIVE_INFINITY, datatypes.POSITIVE_INFINITY),
        (NEGATIVE_INFINITY, datatypes.NEGATIVE_INFINITY),
        (string_literal('"12345"'), datatypes.number(12345)),
    ):
        print "Call", arg
        result = function_call('round', [arg]).evaluate_as_number(CONTEXT1)
        assert isinstance(result, datatypes.number)
        assert ((result.isnan() and expected.isnan())
                or result == expected), (result, expected)
コード例 #8
0
def test_additive_expr():
    for args, expected in (
        ((number_literal('5'), '+', number_literal('2')), 7.0),
        ((number_literal('3'), '+', number_literal('-2')), 1.0),
        ((string_literal('5'), '+', string_literal('2')), 7.0),
        ((string_literal('3'), '+', string_literal('-2')), 1.0),
        ((number_literal('5'), '-', number_literal('2')), 3.0),
        ((number_literal('3'), '-', number_literal('-2')), 5.0),
        ((string_literal('5'), '-', string_literal('2')), 3.0),
        ((string_literal('3'), '-', string_literal('-2')), 5.0),
        # IEEE 754 says:
        #   Infinity + Infinity = Infinity
        #   Infinity - Infinity = NaN
        ((POSITIVE_INFINITY, '+', POSITIVE_INFINITY), datatypes.POSITIVE_INFINITY),
        ((POSITIVE_INFINITY, '-', POSITIVE_INFINITY), datatypes.NOT_A_NUMBER),
        ):
        result = additive_expr(*args).evaluate_as_number(default_context)
        _run_test(args, result, expected)
コード例 #9
0
def test_id_function():
    for args, expected in (
        ([number_literal('1')], datatypes.nodeset([CHILD2])),
        ([string_literal('"1 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0"')], datatypes.nodeset()),
        ([string_literal('"0 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0 1 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0 0 1 1"')], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1])], datatypes.nodeset()),
        ([nodeset_literal([EGG1, EGG2])], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1, EGG2, EGG3])], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1, EGG2, EGG3, EGG4])], datatypes.nodeset([CHILD2])),
        ):
        result = function_call('id', args).evaluate_as_nodeset(CONTEXT1)
        assert isinstance(result, datatypes.nodeset)
        result = list(result)
        expected = list(expected)
        assert result == expected, (args, result, expected)
コード例 #10
0
def test_id_function():
    for args, expected in (
        ([number_literal('1')], datatypes.nodeset([CHILD2])),
        ([string_literal('"1 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0"')], datatypes.nodeset()),
        ([string_literal('"0 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0 1 1"')], datatypes.nodeset([CHILD2])),
        ([string_literal('"0 0 1 1"')], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1])], datatypes.nodeset()),
        ([nodeset_literal([EGG1, EGG2])], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1, EGG2, EGG3])], datatypes.nodeset([CHILD2])),
        ([nodeset_literal([EGG1, EGG2, EGG3,
                           EGG4])], datatypes.nodeset([CHILD2])),
    ):
        result = function_call('id', args).evaluate_as_nodeset(CONTEXT1)
        assert isinstance(result, datatypes.nodeset)
        result = list(result)
        expected = list(expected)
        assert result == expected, (args, result, expected)
コード例 #11
0
def test_substring_function():
    for args, expected in (
        ([string_literal('"3.14Hi"'),
          string_literal('"3.14"')], datatypes.string('14Hi')),
        ([
            string_literal('"3.14Hi"'),
            string_literal('"3.14"'),
            number_literal('1')
        ], datatypes.string('1')),
        ([string_literal('"12345"'),
          number_literal('2'),
          number_literal('3')], datatypes.string('234')),
        ([string_literal('"12345"'),
          number_literal('2')], datatypes.string('2345')),
        ([
            string_literal('"12345"'),
            number_literal('1.5'),
            number_literal('2.6')
        ], datatypes.string('234')),
        ([string_literal('"12345"'),
          number_literal('0'),
          number_literal('3')], datatypes.string('12')),
        ([string_literal('"12345"'), NOT_A_NUMBER,
          number_literal('3')], datatypes.string('')),
        ([string_literal('"12345"'),
          number_literal('1'), NOT_A_NUMBER], datatypes.string('')),
        ([string_literal('"12345"'),
          number_literal('-42'),
          POSITIVE_INFINITY], datatypes.string('12345')),
        ([string_literal('"12345"'), NEGATIVE_INFINITY,
          POSITIVE_INFINITY], datatypes.string('')),
    ):
        result = function_call('substring', args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)
コード例 #12
0
ファイル: test_boolean_expr.py プロジェクト: mredar/amara
def test_equality_expr():
    for (left, right), expected in (
            # `=` tests
        ((number_literal('5'), number_literal('5')), datatypes.TRUE),
        ((number_literal('5'), number_literal('-5')), datatypes.FALSE),
        ((number_literal('-5'), number_literal('-5')), datatypes.TRUE),
        ((number_literal('0'), number_literal('0')), datatypes.TRUE),
        ((POSITIVE_INFINITY, POSITIVE_INFINITY), datatypes.TRUE),
        ((POSITIVE_INFINITY, NEGATIVE_INFINITY), datatypes.FALSE),
        ((NEGATIVE_INFINITY, POSITIVE_INFINITY), datatypes.FALSE),
        ((NEGATIVE_INFINITY, NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal('5'), POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal('5'), NOT_A_NUMBER), datatypes.FALSE),
        ((NOT_A_NUMBER, NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal('5'), nodeset_literal([NUM31])), datatypes.FALSE),
        ((string_literal('5'), string_literal('5')), datatypes.TRUE),
        ((string_literal('5'), string_literal('-5')), datatypes.FALSE),
        ((string_literal('-5'), string_literal('-5')), datatypes.TRUE),
        ((string_literal('0'), string_literal('0')), datatypes.TRUE),
        ((string_literal('Hi'), string_literal('Hi')), datatypes.TRUE),
        ((string_literal('5'), string_literal('Hi')), datatypes.FALSE),
        ((string_literal('5'), string_literal('NaN')), datatypes.FALSE),
        ((string_literal('NaN'), string_literal('NaN')), datatypes.TRUE),
        ((string_literal('5'), nodeset_literal([NUM31])), datatypes.FALSE),
        ((EMPTY_NODESET, TRUE), datatypes.FALSE),
        ((EMPTY_NODESET, FALSE), datatypes.TRUE),
        ((EMPTY_NODESET, nodeset_literal([EGG1])), datatypes.FALSE),
        ((nodeset_literal([EGG1]), EMPTY_NODESET), datatypes.FALSE),
        ((nodeset_literal([EGG1]), nodeset_literal([EGG1])), datatypes.TRUE),
        ((string_literal('egg1'), nodeset_literal([EGG1])), datatypes.TRUE),
        ((string_literal('egg2'), nodeset_literal([EGG1])), datatypes.FALSE),
        ((string_literal('egg1'), nodeset_literal([EGG1,
                                                   EGG2])), datatypes.TRUE),
    ):
        node = equality_expr(left, '=', right)
        result = node.evaluate_as_boolean(default_context)
        assert isinstance(result, datatypes.boolean), result
        assert result == expected, (result, expected)
コード例 #13
0
ファイル: test_boolean_expr.py プロジェクト: mredar/amara
def test_relational_expr():
    for (left, op, right), expected in (
        ((number_literal('5'), '<', number_literal('5')), datatypes.FALSE),
        ((number_literal('5'), '<=', number_literal('5')), datatypes.TRUE),
        ((number_literal('5'), '>', number_literal('5')), datatypes.FALSE),
        ((number_literal('5'), '>=', number_literal('5')), datatypes.TRUE),
        ((number_literal('5'), '<', number_literal('-5')), datatypes.FALSE),
        ((number_literal('5'), '<=', number_literal('-5')), datatypes.FALSE),
        ((number_literal('5'), '>', number_literal('-5')), datatypes.TRUE),
        ((number_literal('5'), '>=', number_literal('-5')), datatypes.TRUE),
        ((number_literal('5'), '<', number_literal('0')), datatypes.FALSE),
        ((number_literal('5'), '<=', number_literal('0')), datatypes.FALSE),
        ((number_literal('5'), '>', number_literal('0')), datatypes.TRUE),
        ((number_literal('5'), '>=', number_literal('0')), datatypes.TRUE),
        ((number_literal('5'), '<', POSITIVE_INFINITY), datatypes.TRUE),
        ((number_literal('5'), '<=', POSITIVE_INFINITY), datatypes.TRUE),
        ((number_literal('5'), '>', POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal('5'), '>=', POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal('5'), '<', NEGATIVE_INFINITY), datatypes.FALSE),
        ((number_literal('5'), '<=', NEGATIVE_INFINITY), datatypes.FALSE),
        ((number_literal('5'), '>', NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal('5'), '>=', NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal('5'), '<', NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal('5'), '<=', NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal('5'), '>', NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal('5'), '>=', NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal('5'), '<', nodeset_literal([NUM31])), datatypes.TRUE),
        ((number_literal('5'), '<=', nodeset_literal([NUM31])),
         datatypes.TRUE),
        ((number_literal('5'), '>', nodeset_literal([NUM31])),
         datatypes.FALSE),
        ((number_literal('5'), '>=', nodeset_literal([NUM31])),
         datatypes.FALSE),
        ((number_literal('5'), '<', string_literal('5')), datatypes.FALSE),
        ((number_literal('5'), '<=', string_literal('5')), datatypes.TRUE),
        ((number_literal('5'), '>', string_literal('5')), datatypes.FALSE),
        ((number_literal('5'), '>=', string_literal('5')), datatypes.TRUE),
        ((number_literal('31'), '<', string_literal('5')), datatypes.FALSE),
        ((number_literal('31'), '<=', string_literal('5')), datatypes.FALSE),
        ((number_literal('31'), '>', string_literal('5')), datatypes.TRUE),
        ((number_literal('31'), '>=', string_literal('5')), datatypes.TRUE),
        ((string_literal('5'), '<', string_literal('5')), datatypes.FALSE),
        ((string_literal('5'), '<=', string_literal('5')), datatypes.TRUE),
        ((string_literal('5'), '>', string_literal('5')), datatypes.FALSE),
        ((string_literal('5'), '>=', string_literal('5')), datatypes.TRUE),
        ((string_literal('31'), '<', string_literal('5')), datatypes.FALSE),
        ((string_literal('31'), '<=', string_literal('5')), datatypes.FALSE),
        ((string_literal('31'), '>', string_literal('5')), datatypes.TRUE),
        ((string_literal('31'), '>=', string_literal('5')), datatypes.TRUE),
        ((number_literal('5'), '<', string_literal('-5')), datatypes.FALSE),
        ((number_literal('5'), '<=', string_literal('-5')), datatypes.FALSE),
        ((number_literal('5'), '>', string_literal('-5')), datatypes.TRUE),
        ((number_literal('5'), '>=', string_literal('-5')), datatypes.TRUE),
        ((number_literal('31'), '<', string_literal('-5')), datatypes.FALSE),
        ((number_literal('31'), '<=', string_literal('-5')), datatypes.FALSE),
        ((number_literal('31'), '>', string_literal('-5')), datatypes.TRUE),
        ((number_literal('31'), '>=', string_literal('-5')), datatypes.TRUE),
        ((string_literal('5'), '<', string_literal('-5')), datatypes.FALSE),
        ((string_literal('5'), '<=', string_literal('-5')), datatypes.FALSE),
        ((string_literal('5'), '>', string_literal('-5')), datatypes.TRUE),
        ((string_literal('5'), '>=', string_literal('-5')), datatypes.TRUE),
        ((string_literal('31'), '<', string_literal('-5')), datatypes.FALSE),
        ((string_literal('31'), '<=', string_literal('-5')), datatypes.FALSE),
        ((string_literal('31'), '>', string_literal('-5')), datatypes.TRUE),
        ((string_literal('31'), '>=', string_literal('-5')), datatypes.TRUE),
        ((string_literal('5'), '<', string_literal('Hi')), datatypes.FALSE),
        ((string_literal('5'), '<=', string_literal('Hi')), datatypes.FALSE),
        ((string_literal('5'), '>', string_literal('Hi')), datatypes.FALSE),
        ((string_literal('5'), '>=', string_literal('Hi')), datatypes.FALSE),
        ((string_literal('5'), '<', nodeset_literal([NUM31])), datatypes.TRUE),
        ((string_literal('5'), '<=', nodeset_literal([NUM31])),
         datatypes.TRUE),
        ((string_literal('5'), '>', nodeset_literal([NUM31])),
         datatypes.FALSE),
        ((string_literal('5'), '>=', nodeset_literal([NUM31])),
         datatypes.FALSE),
        ((nodeset_literal([NUM2]), '<', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM2]), '<=', nodeset_literal([NUM2])),
         datatypes.TRUE),
        ((nodeset_literal([NUM2]), '>', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM2]), '>=', nodeset_literal([NUM2])),
         datatypes.TRUE),
        ((nodeset_literal([NUM0]), '<', nodeset_literal([NUM2])),
         datatypes.TRUE),
        ((nodeset_literal([NUM0]), '<=', nodeset_literal([NUM2])),
         datatypes.TRUE),
        ((nodeset_literal([NUM0]), '>', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM0]), '>=', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM4]), '<', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM4]), '<=', nodeset_literal([NUM2])),
         datatypes.FALSE),
        ((nodeset_literal([NUM4]), '>', nodeset_literal([NUM2])),
         datatypes.TRUE),
        ((nodeset_literal([NUM4]), '>=', nodeset_literal([NUM2])),
         datatypes.TRUE),
    ):

        node = relational_expr(left, op, right)
        result = node.evaluate_as_boolean(default_context)
        assert isinstance(result, datatypes.boolean), result
        assert result == expected, (result, expected)
コード例 #14
0
ファイル: test_boolean_expr.py プロジェクト: abed-hawa/amara
def test_relational_expr():
    for (left, op, right), expected in (
        ((number_literal("5"), "<", number_literal("5")), datatypes.FALSE),
        ((number_literal("5"), "<=", number_literal("5")), datatypes.TRUE),
        ((number_literal("5"), ">", number_literal("5")), datatypes.FALSE),
        ((number_literal("5"), ">=", number_literal("5")), datatypes.TRUE),
        ((number_literal("5"), "<", number_literal("-5")), datatypes.FALSE),
        ((number_literal("5"), "<=", number_literal("-5")), datatypes.FALSE),
        ((number_literal("5"), ">", number_literal("-5")), datatypes.TRUE),
        ((number_literal("5"), ">=", number_literal("-5")), datatypes.TRUE),
        ((number_literal("5"), "<", number_literal("0")), datatypes.FALSE),
        ((number_literal("5"), "<=", number_literal("0")), datatypes.FALSE),
        ((number_literal("5"), ">", number_literal("0")), datatypes.TRUE),
        ((number_literal("5"), ">=", number_literal("0")), datatypes.TRUE),
        ((number_literal("5"), "<", POSITIVE_INFINITY), datatypes.TRUE),
        ((number_literal("5"), "<=", POSITIVE_INFINITY), datatypes.TRUE),
        ((number_literal("5"), ">", POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal("5"), ">=", POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal("5"), "<", NEGATIVE_INFINITY), datatypes.FALSE),
        ((number_literal("5"), "<=", NEGATIVE_INFINITY), datatypes.FALSE),
        ((number_literal("5"), ">", NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal("5"), ">=", NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal("5"), "<", NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal("5"), "<=", NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal("5"), ">", NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal("5"), ">=", NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal("5"), "<", nodeset_literal([NUM31])), datatypes.TRUE),
        ((number_literal("5"), "<=", nodeset_literal([NUM31])), datatypes.TRUE),
        ((number_literal("5"), ">", nodeset_literal([NUM31])), datatypes.FALSE),
        ((number_literal("5"), ">=", nodeset_literal([NUM31])), datatypes.FALSE),
        ((number_literal("5"), "<", string_literal("5")), datatypes.FALSE),
        ((number_literal("5"), "<=", string_literal("5")), datatypes.TRUE),
        ((number_literal("5"), ">", string_literal("5")), datatypes.FALSE),
        ((number_literal("5"), ">=", string_literal("5")), datatypes.TRUE),
        ((number_literal("31"), "<", string_literal("5")), datatypes.FALSE),
        ((number_literal("31"), "<=", string_literal("5")), datatypes.FALSE),
        ((number_literal("31"), ">", string_literal("5")), datatypes.TRUE),
        ((number_literal("31"), ">=", string_literal("5")), datatypes.TRUE),
        ((string_literal("5"), "<", string_literal("5")), datatypes.FALSE),
        ((string_literal("5"), "<=", string_literal("5")), datatypes.TRUE),
        ((string_literal("5"), ">", string_literal("5")), datatypes.FALSE),
        ((string_literal("5"), ">=", string_literal("5")), datatypes.TRUE),
        ((string_literal("31"), "<", string_literal("5")), datatypes.FALSE),
        ((string_literal("31"), "<=", string_literal("5")), datatypes.FALSE),
        ((string_literal("31"), ">", string_literal("5")), datatypes.TRUE),
        ((string_literal("31"), ">=", string_literal("5")), datatypes.TRUE),
        ((number_literal("5"), "<", string_literal("-5")), datatypes.FALSE),
        ((number_literal("5"), "<=", string_literal("-5")), datatypes.FALSE),
        ((number_literal("5"), ">", string_literal("-5")), datatypes.TRUE),
        ((number_literal("5"), ">=", string_literal("-5")), datatypes.TRUE),
        ((number_literal("31"), "<", string_literal("-5")), datatypes.FALSE),
        ((number_literal("31"), "<=", string_literal("-5")), datatypes.FALSE),
        ((number_literal("31"), ">", string_literal("-5")), datatypes.TRUE),
        ((number_literal("31"), ">=", string_literal("-5")), datatypes.TRUE),
        ((string_literal("5"), "<", string_literal("-5")), datatypes.FALSE),
        ((string_literal("5"), "<=", string_literal("-5")), datatypes.FALSE),
        ((string_literal("5"), ">", string_literal("-5")), datatypes.TRUE),
        ((string_literal("5"), ">=", string_literal("-5")), datatypes.TRUE),
        ((string_literal("31"), "<", string_literal("-5")), datatypes.FALSE),
        ((string_literal("31"), "<=", string_literal("-5")), datatypes.FALSE),
        ((string_literal("31"), ">", string_literal("-5")), datatypes.TRUE),
        ((string_literal("31"), ">=", string_literal("-5")), datatypes.TRUE),
        ((string_literal("5"), "<", string_literal("Hi")), datatypes.FALSE),
        ((string_literal("5"), "<=", string_literal("Hi")), datatypes.FALSE),
        ((string_literal("5"), ">", string_literal("Hi")), datatypes.FALSE),
        ((string_literal("5"), ">=", string_literal("Hi")), datatypes.FALSE),
        ((string_literal("5"), "<", nodeset_literal([NUM31])), datatypes.TRUE),
        ((string_literal("5"), "<=", nodeset_literal([NUM31])), datatypes.TRUE),
        ((string_literal("5"), ">", nodeset_literal([NUM31])), datatypes.FALSE),
        ((string_literal("5"), ">=", nodeset_literal([NUM31])), datatypes.FALSE),
        ((nodeset_literal([NUM2]), "<", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM2]), "<=", nodeset_literal([NUM2])), datatypes.TRUE),
        ((nodeset_literal([NUM2]), ">", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM2]), ">=", nodeset_literal([NUM2])), datatypes.TRUE),
        ((nodeset_literal([NUM0]), "<", nodeset_literal([NUM2])), datatypes.TRUE),
        ((nodeset_literal([NUM0]), "<=", nodeset_literal([NUM2])), datatypes.TRUE),
        ((nodeset_literal([NUM0]), ">", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM0]), ">=", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM4]), "<", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM4]), "<=", nodeset_literal([NUM2])), datatypes.FALSE),
        ((nodeset_literal([NUM4]), ">", nodeset_literal([NUM2])), datatypes.TRUE),
        ((nodeset_literal([NUM4]), ">=", nodeset_literal([NUM2])), datatypes.TRUE),
    ):

        node = relational_expr(left, op, right)
        result = node.evaluate_as_boolean(default_context)
        assert isinstance(result, datatypes.boolean), result
        assert result == expected, (result, expected)
コード例 #15
0
def test_multiplicative_expr():
    for args, expected in (
        ((number_literal('-5'), '*', number_literal('2')), -10.0),
        ((number_literal('-4'), '*', number_literal('-2')), 8.0),
        ((number_literal('0'), '*', number_literal('2')), 0.0),
        ((string_literal('-5'), '*', string_literal('2')), -10.0),
        ((string_literal('-4'), '*', string_literal('-2')), 8.0),
        ((string_literal('0'), '*', string_literal('2')), 0.0),
        ((string_literal('1'), '*', string_literal('1')), 1.0),
        ((string_literal('3.1415926535'), '*', string_literal('1')), 3.1415926535),
        ((string_literal(''), '*', string_literal('1')), datatypes.NOT_A_NUMBER),
        ((string_literal('hi'), '*', string_literal('1')), datatypes.NOT_A_NUMBER),
        ((string_literal('inf'), '*', string_literal('1')), datatypes.NOT_A_NUMBER),
        ((string_literal(u'\u2022 = middle dot'), '*', string_literal('1')), datatypes.NOT_A_NUMBER),
        ((number_literal('0'), 'div', number_literal('2')), 0.0),
        ((number_literal('-5'), 'div', number_literal('2')), -2.5),
        ((number_literal('-4'), 'div', number_literal('-2')), 2.0),
        ((string_literal('0'), 'div', string_literal('2')), 0.0),
        ((string_literal('1'), 'div', string_literal('1')), 1.0),
        ((string_literal('-5'), 'div', string_literal('2')), -2.5),
        ((string_literal('-4'), 'div', string_literal('-2')), 2.0),
        ((string_literal('0'), 'div', string_literal('0')), datatypes.NOT_A_NUMBER),
        ((string_literal('1'), 'div', string_literal('0')), datatypes.POSITIVE_INFINITY),
        ((string_literal('-1'), 'div', string_literal('0')), datatypes.NEGATIVE_INFINITY),
        ((number_literal('0'), 'mod', number_literal('2')), 0.0),
        ((number_literal('5'), 'mod', number_literal('2')), 1.0),
        ((number_literal('5'), 'mod', number_literal('-2')), -1.0),
        ((number_literal('-5'), 'mod', number_literal('2')), 1.0),
        ((number_literal('-5'), 'mod', number_literal('-2')), -1.0),
        ((string_literal('0'), 'mod', string_literal('2')), 0.0),
        ((string_literal('5'), 'mod', string_literal('2')), 1.0),
        ((string_literal('5'), 'mod', string_literal('-2')), -1.0),
        ((string_literal('-5'), 'mod', string_literal('2')), 1.0),
        ((string_literal('-5'), 'mod', string_literal('-2')), -1.0),
        # IEEE 754 says:
        #   +-Infinity * +-Infinity = +-Infinity
        #   +-Infinity * 0 = NaN
        #   n div +-Infinity = 0
        #   +-nonzero div 0 = +-Infinity
        #   +-Infinity div +-Infinity = NaN
        #   +-0 div +-0 = NaN
        ((POSITIVE_INFINITY, '*', POSITIVE_INFINITY), datatypes.POSITIVE_INFINITY),
        ((NEGATIVE_INFINITY, '*', NEGATIVE_INFINITY), datatypes.POSITIVE_INFINITY),
        ((POSITIVE_INFINITY, '*', NEGATIVE_INFINITY), datatypes.NEGATIVE_INFINITY),
        ((POSITIVE_INFINITY, '*', number_literal('0')), datatypes.NOT_A_NUMBER),
        ((NEGATIVE_INFINITY, '*', number_literal('0')), datatypes.NOT_A_NUMBER),
        ((number_literal('0'), 'div', number_literal('0')), datatypes.NOT_A_NUMBER),
        ((number_literal('1'), 'div', number_literal('0')), datatypes.POSITIVE_INFINITY),
        ((number_literal('-1'), 'div', number_literal('0')), datatypes.NEGATIVE_INFINITY),
        ((number_literal('0'), 'div', POSITIVE_INFINITY), 0.0),
        ((number_literal('1'), 'div', POSITIVE_INFINITY), 0.0),
        ((number_literal('-1'), 'div', POSITIVE_INFINITY), 0.0),
        ((number_literal('0'), 'div', NEGATIVE_INFINITY), 0.0),
        ((number_literal('1'), 'div', NEGATIVE_INFINITY), 0.0),
        ((number_literal('-1'), 'div', NEGATIVE_INFINITY), 0.0),
        ((POSITIVE_INFINITY, 'div', POSITIVE_INFINITY), datatypes.NOT_A_NUMBER),
        ((POSITIVE_INFINITY, 'div', NEGATIVE_INFINITY), datatypes.NOT_A_NUMBER),
        ((NEGATIVE_INFINITY, 'div', NEGATIVE_INFINITY), datatypes.NOT_A_NUMBER),
        ((NEGATIVE_INFINITY, 'div', POSITIVE_INFINITY), datatypes.NOT_A_NUMBER),
        ):
        result = multiplicative_expr(*args).evaluate_as_number(default_context)
        _run_test(args, result, expected)
コード例 #16
0
ファイル: test_boolean_expr.py プロジェクト: abed-hawa/amara
def test_equality_expr():
    for (left, right), expected in (
        # `=` tests
        ((number_literal("5"), number_literal("5")), datatypes.TRUE),
        ((number_literal("5"), number_literal("-5")), datatypes.FALSE),
        ((number_literal("-5"), number_literal("-5")), datatypes.TRUE),
        ((number_literal("0"), number_literal("0")), datatypes.TRUE),
        ((POSITIVE_INFINITY, POSITIVE_INFINITY), datatypes.TRUE),
        ((POSITIVE_INFINITY, NEGATIVE_INFINITY), datatypes.FALSE),
        ((NEGATIVE_INFINITY, POSITIVE_INFINITY), datatypes.FALSE),
        ((NEGATIVE_INFINITY, NEGATIVE_INFINITY), datatypes.TRUE),
        ((number_literal("5"), POSITIVE_INFINITY), datatypes.FALSE),
        ((number_literal("5"), NOT_A_NUMBER), datatypes.FALSE),
        ((NOT_A_NUMBER, NOT_A_NUMBER), datatypes.FALSE),
        ((number_literal("5"), nodeset_literal([NUM31])), datatypes.FALSE),
        ((string_literal("5"), string_literal("5")), datatypes.TRUE),
        ((string_literal("5"), string_literal("-5")), datatypes.FALSE),
        ((string_literal("-5"), string_literal("-5")), datatypes.TRUE),
        ((string_literal("0"), string_literal("0")), datatypes.TRUE),
        ((string_literal("Hi"), string_literal("Hi")), datatypes.TRUE),
        ((string_literal("5"), string_literal("Hi")), datatypes.FALSE),
        ((string_literal("5"), string_literal("NaN")), datatypes.FALSE),
        ((string_literal("NaN"), string_literal("NaN")), datatypes.TRUE),
        ((string_literal("5"), nodeset_literal([NUM31])), datatypes.FALSE),
        ((EMPTY_NODESET, TRUE), datatypes.FALSE),
        ((EMPTY_NODESET, FALSE), datatypes.TRUE),
        ((EMPTY_NODESET, nodeset_literal([EGG1])), datatypes.FALSE),
        ((nodeset_literal([EGG1]), EMPTY_NODESET), datatypes.FALSE),
        ((nodeset_literal([EGG1]), nodeset_literal([EGG1])), datatypes.TRUE),
        ((string_literal("egg1"), nodeset_literal([EGG1])), datatypes.TRUE),
        ((string_literal("egg2"), nodeset_literal([EGG1])), datatypes.FALSE),
        ((string_literal("egg1"), nodeset_literal([EGG1, EGG2])), datatypes.TRUE),
    ):
        node = equality_expr(left, "=", right)
        result = node.evaluate_as_boolean(default_context)
        assert isinstance(result, datatypes.boolean), result
        assert result == expected, (result, expected)
コード例 #17
0
def test_substring_function():
    for args, expected in (
        ([string_literal('"3.14Hi"'), string_literal('"3.14"')], datatypes.string('14Hi')),
        ([string_literal('"3.14Hi"'), string_literal('"3.14"'), number_literal('1')],  datatypes.string('1')),
        ([string_literal('"12345"'), number_literal('2'), number_literal('3')],  datatypes.string('234')),
        ([string_literal('"12345"'), number_literal('2')],  datatypes.string('2345')),
        ([string_literal('"12345"'), number_literal('1.5'), number_literal('2.6')],  datatypes.string('234')),
        ([string_literal('"12345"'), number_literal('0'), number_literal('3')],  datatypes.string('12')),
        ([string_literal('"12345"'), NOT_A_NUMBER, number_literal('3')],  datatypes.string('')),
        ([string_literal('"12345"'), number_literal('1'), NOT_A_NUMBER],  datatypes.string('')),
        ([string_literal('"12345"'), number_literal('-42'), POSITIVE_INFINITY],  datatypes.string('12345')),
        ([string_literal('"12345"'), NEGATIVE_INFINITY, POSITIVE_INFINITY],  datatypes.string('')),
        ):
        result = function_call('substring', args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)