summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/manifestparser/tests/test_expressionparser.py
blob: 806f2347efb1e23e7c465d87bfc521ee501cb6cf (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python

import unittest

import mozunit
from manifestparser import parse


class ExpressionParserTest(unittest.TestCase):
    """Test the conditional expression parser."""

    def test_basic(self):

        self.assertEqual(parse("1"), 1)
        self.assertEqual(parse("100"), 100)
        self.assertEqual(parse("true"), True)
        self.assertEqual(parse("false"), False)
        self.assertEqual("", parse('""'))
        self.assertEqual(parse('"foo bar"'), "foo bar")
        self.assertEqual(parse("'foo bar'"), "foo bar")
        self.assertEqual(parse("foo", foo=1), 1)
        self.assertEqual(parse("bar", bar=True), True)
        self.assertEqual(parse("abc123", abc123="xyz"), "xyz")

    def test_equality(self):

        self.assertTrue(parse("true == true"))
        self.assertTrue(parse("false == false"))
        self.assertTrue(parse("1 == 1"))
        self.assertTrue(parse("100 == 100"))
        self.assertTrue(parse('"some text" == "some text"'))
        self.assertTrue(parse("true != false"))
        self.assertTrue(parse("1 != 2"))
        self.assertTrue(parse('"text" != "other text"'))
        self.assertTrue(parse("foo == true", foo=True))
        self.assertTrue(parse("foo == 1", foo=1))
        self.assertTrue(parse('foo == "bar"', foo="bar"))
        self.assertTrue(parse("foo == bar", foo=True, bar=True))
        self.assertTrue(parse("true == foo", foo=True))
        self.assertTrue(parse("foo != true", foo=False))
        self.assertTrue(parse("foo != 2", foo=1))
        self.assertTrue(parse('foo != "bar"', foo="abc"))
        self.assertTrue(parse("foo != bar", foo=True, bar=False))
        self.assertTrue(parse("true != foo", foo=False))
        self.assertTrue(parse("!false"))

    def test_conjunctures(self):
        self.assertTrue(parse("true && true"))
        self.assertTrue(parse("true || false"))
        self.assertFalse(parse("false || false"))
        self.assertFalse(parse("true && false"))
        self.assertTrue(parse("true || false && false"))

    def test_parentheses(self):
        self.assertTrue(parse("(true)"))
        self.assertEqual(parse("(10)"), 10)
        self.assertEqual(parse('("foo")'), "foo")
        self.assertEqual(parse("(foo)", foo=1), 1)
        self.assertTrue(parse("(true == true)"), True)
        self.assertTrue(parse("(true != false)"))
        self.assertTrue(parse("(true && true)"))
        self.assertTrue(parse("(true || false)"))
        self.assertTrue(parse("(true && true || false)"))
        self.assertFalse(parse("(true || false) && false"))
        self.assertTrue(parse("(true || false) && true"))
        self.assertTrue(parse("true && (true || false)"))
        self.assertTrue(parse("true && (true || false)"))
        self.assertTrue(parse("(true && false) || (true && (true || false))"))

    def test_comments(self):
        # comments in expressions work accidentally, via an implementation
        # detail - the '#' character doesn't match any of the regular
        # expressions we specify as tokens, and thus are ignored.
        # However, having explicit tests for them means that should the
        # implementation ever change, comments continue to work, even if that
        # means a new implementation must handle them explicitly.
        self.assertTrue(parse("true == true # it does!"))
        self.assertTrue(parse("false == false # it does"))
        self.assertTrue(parse("false != true # it doesnt"))
        self.assertTrue(parse('"string with #" == "string with #" # really, it does'))
        self.assertTrue(
            parse('"string with #" != "string with # but not the same" # no match!')
        )

    def test_not(self):
        """
        Test the ! operator.
        """
        self.assertTrue(parse("!false"))
        self.assertTrue(parse("!(false)"))
        self.assertFalse(parse("!true"))
        self.assertFalse(parse("!(true)"))
        self.assertTrue(parse("!true || true)"))
        self.assertTrue(parse("true || !true)"))
        self.assertFalse(parse("!true && true"))
        self.assertFalse(parse("true && !true"))

    def test_lesser_than(self):
        """
        Test the < operator.
        """
        self.assertTrue(parse("1 < 2"))
        self.assertFalse(parse("3 < 2"))
        self.assertTrue(parse("false || (1 < 2)"))
        self.assertTrue(parse("1 < 2 && true"))
        self.assertTrue(parse("true && 1 < 2"))
        self.assertTrue(parse("!(5 < 1)"))
        self.assertTrue(parse("'abc' < 'def'"))
        self.assertFalse(parse("1 < 1"))
        self.assertFalse(parse("'abc' < 'abc'"))

    def test_greater_than(self):
        """
        Test the > operator.
        """
        self.assertTrue(parse("2 > 1"))
        self.assertFalse(parse("2 > 3"))
        self.assertTrue(parse("false || (2 > 1)"))
        self.assertTrue(parse("2 > 1 && true"))
        self.assertTrue(parse("true && 2 > 1"))
        self.assertTrue(parse("!(1 > 5)"))
        self.assertTrue(parse("'def' > 'abc'"))
        self.assertFalse(parse("1 > 1"))
        self.assertFalse(parse("'abc' > 'abc'"))

    def test_lesser_or_equals_than(self):
        """
        Test the <= operator.
        """
        self.assertTrue(parse("1 <= 2"))
        self.assertFalse(parse("3 <= 2"))
        self.assertTrue(parse("false || (1 <= 2)"))
        self.assertTrue(parse("1 < 2 && true"))
        self.assertTrue(parse("true && 1 <= 2"))
        self.assertTrue(parse("!(5 <= 1)"))
        self.assertTrue(parse("'abc' <= 'def'"))
        self.assertTrue(parse("1 <= 1"))
        self.assertTrue(parse("'abc' <= 'abc'"))

    def test_greater_or_equals_than(self):
        """
        Test the > operator.
        """
        self.assertTrue(parse("2 >= 1"))
        self.assertFalse(parse("2 >= 3"))
        self.assertTrue(parse("false || (2 >= 1)"))
        self.assertTrue(parse("2 >= 1 && true"))
        self.assertTrue(parse("true && 2 >= 1"))
        self.assertTrue(parse("!(1 >= 5)"))
        self.assertTrue(parse("'def' >= 'abc'"))
        self.assertTrue(parse("1 >= 1"))
        self.assertTrue(parse("'abc' >= 'abc'"))


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