summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/harness/wptrunner/wptmanifest/tests/test_parser.py
blob: 6e8e6e6bea934b319bd523d054f275a5374c2c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import unittest

from cStringIO import StringIO

from .. import parser

# There aren't many tests here because it turns out to be way more convenient to
# use test_serializer for the majority of cases


class TestExpression(unittest.TestCase):
    def setUp(self):
        self.parser = parser.Parser()

    def parse(self, input_str):
        return self.parser.parse(StringIO(input_str))

    def compare(self, input_text, expected):
        actual = self.parse(input_text)
        self.match(expected, actual)

    def match(self, expected_node, actual_node):
        self.assertEquals(expected_node[0], actual_node.__class__.__name__)
        self.assertEquals(expected_node[1], actual_node.data)
        self.assertEquals(len(expected_node[2]), len(actual_node.children))
        for expected_child, actual_child in zip(expected_node[2], actual_node.children):
            self.match(expected_child, actual_child)

    def test_expr_0(self):
        self.compare(
            """
key:
  if x == 1 : value""",
            ["DataNode", None,
             [["KeyValueNode", "key",
               [["ConditionalNode", None,
                 [["BinaryExpressionNode", None,
                   [["BinaryOperatorNode", "==", []],
                    ["VariableNode", "x", []],
                       ["NumberNode", "1", []]
                    ]],
                     ["ValueNode", "value", []],
                  ]]]]]]
        )

    def test_expr_1(self):
        self.compare(
            """
key:
  if not x and y : value""",
            ["DataNode", None,
             [["KeyValueNode", "key",
               [["ConditionalNode", None,
                 [["BinaryExpressionNode", None,
                   [["BinaryOperatorNode", "and", []],
                    ["UnaryExpressionNode", None,
                       [["UnaryOperatorNode", "not", []],
                        ["VariableNode", "x", []]
                        ]],
                       ["VariableNode", "y", []]
                    ]],
                     ["ValueNode", "value", []],
                  ]]]]]]
        )

    def test_atom_0(self):
        with self.assertRaises(parser.ParseError):
            self.parse("key: @Unknown")

    def test_atom_1(self):
        with self.assertRaises(parser.ParseError):
            self.parse("key: @true")

if __name__ == "__main__":
    unittest.main()