Beispiel #1
0
def test_concat_function():
    result = function_call('concat', [nodeset_literal([CHILD1]),
                                      string_literal('"3.14"'),
                                      string_literal('"Hi"')]).evaluate_as_string(CONTEXT1)
    assert isinstance(result, datatypes.string)
    expected = datatypes.string('\n    \n    \n    Text1\n  3.14Hi')
    assert result == expected, (result, expected)
Beispiel #2
0
def test_substring_after_function():
    for args, expected in (
        ([string_literal('"3.14Hi"'), string_literal('"3.14"')], datatypes.string('Hi')),
        ([string_literal('"3.14Hi"'), string_literal('""')], datatypes.string())
        ):
        result = function_call('substring-after', args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)
Beispiel #3
0
def test_substring_before_function():
    for args, expected in (
        ([string_literal('"3.14Hi"'), string_literal('"Hi"')], datatypes.string('3.14')),
        ([string_literal('"3.14Hi"'), string_literal('""')], datatypes.string())
        ):
        result = function_call('substring-before', args).evaluate_as_boolean(CONTEXT1)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #4
0
def test_contains_function():
    for args, expected in (
        ([nodeset_literal([CHILD1]), string_literal('"3.14"')], datatypes.FALSE),
        ([nodeset_literal([CHILD1]), nodeset_literal([CHILD1])], datatypes.TRUE),
        ([nodeset_literal([CHILD1]), string_literal('""')], datatypes.TRUE),
        ):
        result = function_call('contains', args).evaluate_as_boolean(CONTEXT1)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #5
0
def test_concat_function():
    result = function_call('concat', [
        nodeset_literal([CHILD1]),
        string_literal('"3.14"'),
        string_literal('"Hi"')
    ]).evaluate_as_string(CONTEXT1)
    assert isinstance(result, datatypes.string)
    expected = datatypes.string('\n    \n    \n    Text1\n  3.14Hi')
    assert result == expected, (result, expected)
Beispiel #6
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)
Beispiel #7
0
def test_substring_before_function():
    for args, expected in (([
            string_literal('"3.14Hi"'),
            string_literal('"Hi"')
    ], datatypes.string('3.14')),
                           ([string_literal('"3.14Hi"'),
                             string_literal('""')], datatypes.string())):
        result = function_call('substring-before',
                               args).evaluate_as_boolean(CONTEXT1)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #8
0
def test_substring_after_function():
    for args, expected in (([
            string_literal('"3.14Hi"'),
            string_literal('"3.14"')
    ], datatypes.string('Hi')),
                           ([string_literal('"3.14Hi"'),
                             string_literal('""')], datatypes.string())):
        result = function_call('substring-after',
                               args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)
Beispiel #9
0
def test_contains_function():
    for args, expected in (
        ([nodeset_literal([CHILD1]),
          string_literal('"3.14"')], datatypes.FALSE),
        ([nodeset_literal([CHILD1]),
          nodeset_literal([CHILD1])], datatypes.TRUE),
        ([nodeset_literal([CHILD1]),
          string_literal('""')], datatypes.TRUE),
    ):
        result = function_call('contains', args).evaluate_as_boolean(CONTEXT1)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #10
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)
Beispiel #11
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)
Beispiel #12
0
def test_string_length_function():
    result = function_call(
        'string-length',
        [string_literal('"3.14Hi"')]).evaluate_as_number(CONTEXT1)
    assert isinstance(result, datatypes.number)
    expected = datatypes.number(6)
    assert result == expected, (result, expected)
Beispiel #13
0
def test_normalize_space_function():
    result = function_call('normalize-space',
                           [string_literal('"Ht    	 There	   Mike"')
                            ]).evaluate_as_string(CONTEXT1)
    assert isinstance(result, datatypes.string)
    expected = datatypes.string(u'Ht There Mike')
    assert result == expected, (result, expected)
Beispiel #14
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)
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
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)
Beispiel #18
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)
Beispiel #19
0
def test_lang_function():
    for arg, expected, context in (
        (string_literal('en'), datatypes.FALSE, CONTEXTLANG1),
        (string_literal('en'), datatypes.TRUE, CONTEXTLANG2),
        (string_literal(''), datatypes.TRUE, CONTEXTLANG1),
        (string_literal(''), datatypes.FALSE, CONTEXTLANG2),
        (string_literal('foo'), datatypes.FALSE, CONTEXTLANG1),
        (string_literal('foo'), datatypes.FALSE, CONTEXTLANG2),
    ):
        result = function_call('lang', [arg]).evaluate_as_boolean(context)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #20
0
def test_lang_function():
    for arg, expected, context in (
        (string_literal('en'), datatypes.FALSE, CONTEXTLANG1),
        (string_literal('en'), datatypes.TRUE, CONTEXTLANG2),
        (string_literal(''), datatypes.TRUE, CONTEXTLANG1),
        (string_literal(''), datatypes.FALSE, CONTEXTLANG2),
        (string_literal('foo'), datatypes.FALSE, CONTEXTLANG1),
        (string_literal('foo'), datatypes.FALSE, CONTEXTLANG2),
        ):
        result = function_call('lang', [arg]).evaluate_as_boolean(context)
        assert isinstance(result, datatypes.boolean)
        assert result == expected, (result, expected)
Beispiel #21
0
def test_string_literal():
    for input, expected in (        
        (u'""', u''),
        (u'"Hi"', u'Hi'),
        (u'"NaN"', u'NaN'),
        (u'"\u2022 = middle dot"', u'\u2022 = middle dot'),
        (u'"0"', u'0'),
        (u'"1"', u'1'),
        (u'"2"', u'2'),
        (u'"3"', u'3'),
        (u'"4"', u'4'),
        (u'"5"', u'5'),
        (u'"31"', u'31'),
        (u'"-1"', u'-1'),
        (u'"-2"', u'-2'),
        (u'"-3"', u'-3'),
        (u'"-4"', u'-4'),
        (u'"-5"', u'-5'),
        (u'"3.1415926535"', u'3.1415926535'),
        ):
        node = string_literal(input)
        result = node.evaluate_as_string(default_context)
        assert isinstance(result, datatypes.string), result
        assert result == expected, (result, expected)
Beispiel #22
0
def test_string_literal():
    for input, expected in (
        (u'""', u''),
        (u'"Hi"', u'Hi'),
        (u'"NaN"', u'NaN'),
        (u'"\u2022 = middle dot"', u'\u2022 = middle dot'),
        (u'"0"', u'0'),
        (u'"1"', u'1'),
        (u'"2"', u'2'),
        (u'"3"', u'3'),
        (u'"4"', u'4'),
        (u'"5"', u'5'),
        (u'"31"', u'31'),
        (u'"-1"', u'-1'),
        (u'"-2"', u'-2'),
        (u'"-3"', u'-3'),
        (u'"-4"', u'-4'),
        (u'"-5"', u'-5'),
        (u'"3.1415926535"', u'3.1415926535'),
    ):
        node = string_literal(input)
        result = node.evaluate_as_string(default_context)
        assert isinstance(result, datatypes.string), result
        assert result == expected, (result, expected)
Beispiel #23
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)
Beispiel #24
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)
Beispiel #25
0
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)
Beispiel #26
0
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)
Beispiel #27
0
def test_translate_function():
    for args, expected in (
            # a,b,c,d,e,f become A,B,C,D,E,F
        ([
            string_literal('"Ht    \t There\t   Mike"'),
            string_literal('abcdef'),
            string_literal('ABCDEF')
        ], datatypes.string('Ht    \t ThErE\t   MikE')),
            # e becomes a
        ([
            string_literal('"hello world"'),
            string_literal('e'),
            string_literal('a')
        ], datatypes.string('hallo world')),

            # e becomes a; extra chars in To string are ignored
        ([
            string_literal('"hello world"'),
            string_literal('e'),
            string_literal('abc')
        ], datatypes.string('hallo world')),

            # e becomes a; l is deleted
        ([
            string_literal('"hello world"'),
            string_literal('el'),
            string_literal('a')
        ], datatypes.string('hao word')),

            # a,b,c,d,e,f become A,B,C,D,E,F;
            # the first occurance of a char in the 2nd arg is the replacement
        ([
            string_literal('"hello world"'),
            string_literal('abcdde'),
            string_literal('ABCDEF')
        ], datatypes.string('hFllo worlD')),

            # if there is a char in the 2nd arg with no char at the same pos
            # in the 3rd arg, that char is removed from the 1st arg
        ([
            string_literal('"hello world"'),
            string_literal('abcdefgh'),
            string_literal('')
        ], datatypes.string('llo worl')),
    ):
        result = function_call('translate', args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)
Beispiel #28
0
def test_not_function():
    result = function_call(
        'not', [string_literal('"3.14Hi"')]).evaluate_as_boolean(CONTEXT1)
    assert isinstance(result, datatypes.boolean)
    assert result == datatypes.FALSE, result
Beispiel #29
0
def test_string_length_function():
    result = function_call('string-length', [string_literal('"3.14Hi"')]).evaluate_as_number(CONTEXT1)
    assert isinstance(result, datatypes.number)
    expected = datatypes.number(6)
    assert result == expected, (result, expected)
Beispiel #30
0
def test_normalize_space_function():
    result = function_call('normalize-space',
                           [string_literal('"Ht    	 There	   Mike"')]).evaluate_as_string(CONTEXT1)
    assert isinstance(result, datatypes.string)
    expected = datatypes.string(u'Ht There Mike')
    assert result == expected, (result, expected)
Beispiel #31
0
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)
Beispiel #32
0
def test_translate_function():
    for args, expected in ( 
        # a,b,c,d,e,f become A,B,C,D,E,F
        ([string_literal('"Ht    \t There\t   Mike"'), string_literal('abcdef'), string_literal('ABCDEF')],
         datatypes.string('Ht    \t ThErE\t   MikE')),
        # e becomes a
        ([string_literal('"hello world"'), string_literal('e'), string_literal('a')],
         datatypes.string('hallo world')),

        # e becomes a; extra chars in To string are ignored
        ([string_literal('"hello world"'), string_literal('e'), string_literal('abc')],
         datatypes.string('hallo world')),

        # e becomes a; l is deleted
        ([string_literal('"hello world"'), string_literal('el'), string_literal('a')],
         datatypes.string('hao word')),

        # a,b,c,d,e,f become A,B,C,D,E,F;
        # the first occurance of a char in the 2nd arg is the replacement
        ([string_literal('"hello world"'), string_literal('abcdde'), string_literal('ABCDEF')],
         datatypes.string('hFllo worlD')),

        # if there is a char in the 2nd arg with no char at the same pos
        # in the 3rd arg, that char is removed from the 1st arg
        ([string_literal('"hello world"'), string_literal('abcdefgh'), string_literal('')],
         datatypes.string('llo worl')),
        ):
        result = function_call('translate', args).evaluate_as_string(CONTEXT1)
        assert isinstance(result, datatypes.string)
        assert result == expected, (result, expected)
Beispiel #33
0
def test_not_function():
    result = function_call('not', [string_literal('"3.14Hi"')]).evaluate_as_boolean(CONTEXT1)
    assert isinstance(result, datatypes.boolean)
    assert result == datatypes.FALSE, result
Beispiel #34
0
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)
Beispiel #35
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)