summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/harness/wptrunner/wptmanifest/tests/test_static.py
blob: 5bd270d9f51f4435adebe45b56c7cf7582813b3c (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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 ..backends import static

# 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 TestStatic(unittest.TestCase):
    def parse(self, input_str):
        return self.parser.parse(StringIO(input_str))

    def compile(self, input_text, input_data):
        return static.compile(input_text, input_data)

    def test_get_0(self):
        data = """
key: value

[Heading 1]
  other_key:
    if a == 1: value_1
    if a == 2: value_2
    value_3
"""

        manifest = self.compile(data, {"a": 2})

        self.assertEquals(manifest.get("key"), "value")
        children = list(item for item in manifest.iterchildren())
        self.assertEquals(len(children), 1)
        section = children[0]
        self.assertEquals(section.name, "Heading 1")

        self.assertEquals(section.get("other_key"), "value_2")
        self.assertEquals(section.get("key"), "value")

    def test_get_1(self):
        data = """
key: value

[Heading 1]
  other_key:
    if a == 1: value_1
    if a == 2: value_2
    value_3
"""
        manifest = self.compile(data, {"a": 3})

        children = list(item for item in manifest.iterchildren())
        section = children[0]
        self.assertEquals(section.get("other_key"), "value_3")

    def test_get_3(self):
        data = """key:
  if a == "1": value_1
  if a[0] == "ab"[0]: value_2
"""
        manifest = self.compile(data, {"a": "1"})
        self.assertEquals(manifest.get("key"), "value_1")

        manifest = self.compile(data, {"a": "ac"})
        self.assertEquals(manifest.get("key"), "value_2")

    def test_get_4(self):
        data = """key:
  if not a: value_1
  value_2
"""
        manifest = self.compile(data, {"a": True})
        self.assertEquals(manifest.get("key"), "value_2")

        manifest = self.compile(data, {"a": False})
        self.assertEquals(manifest.get("key"), "value_1")

    def test_api(self):
        data = """key:
  if a == 1.5: value_1
  value_2
key_1: other_value
"""
        manifest = self.compile(data, {"a": 1.5})

        self.assertFalse(manifest.is_empty)
        self.assertEquals(manifest.root, manifest)
        self.assertTrue(manifest.has_key("key_1"))
        self.assertFalse(manifest.has_key("key_2"))

        self.assertEquals(set(manifest.iterkeys()), set(["key", "key_1"]))
        self.assertEquals(set(manifest.itervalues()), set(["value_1", "other_value"]))

    def test_is_empty_1(self):
        data = """
[Section]
  [Subsection]
"""
        manifest = self.compile(data, {})

        self.assertTrue(manifest.is_empty)