Bug 1792034 [wpt PR 36019] - Make location.search always expect UTF-8, a=testonly
[gecko.git] / media / libaom / test_cmakeparser.py
blobaf68c3313446cbb450a6c6163c9ce7f4e9ddfcd2
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 from pyparsing import ParseException
5 import unittest
7 import cmakeparser as cp
9 class TestCMakeParser(unittest.TestCase):
10 def test_arguments(self):
11 self.assertEqual(cp.arguments.parseString('1').asList(), ['1'])
12 self.assertEqual(cp.arguments.parseString('(1 2)').asList(),
13 ['(', '1', '2', ')'])
15 def test_command(self):
16 self.assertEqual(cp.command.parseString('blah()').asList(),
17 [['blah', '(', ')']])
18 self.assertEqual(cp.command.parseString('blah(1)').asList(),
19 [['blah', '(', '1', ')']])
20 self.assertEqual(cp.command.parseString('blah(1 (2 3))').asList(),
21 [['blah', '(', '1', '(', '2', '3', ')', ')']])
23 def test_evaluate_boolean(self):
24 self.assertTrue(cp.evaluate_boolean({}, ['TRUE']))
25 self.assertFalse(cp.evaluate_boolean({}, ['NOT', 'TRUE']))
26 self.assertFalse(cp.evaluate_boolean({}, ['TRUE', 'AND', 'FALSE']))
27 self.assertTrue(cp.evaluate_boolean({}, ['ABC', 'MATCHES', '^AB']))
28 self.assertTrue(cp.evaluate_boolean({}, ['TRUE', 'OR', 'FALSE']))
29 self.assertTrue(cp.evaluate_boolean({}, ['ABC', 'STREQUAL', 'ABC']))
30 self.assertTrue(cp.evaluate_boolean({'ABC': '1'}, ['ABC']))
31 self.assertFalse(cp.evaluate_boolean({'ABC': '0'}, ['${ABC}']))
32 self.assertFalse(cp.evaluate_boolean({}, ['ABC']))
33 self.assertFalse(cp.evaluate_boolean({}, ['${ABC}']))
34 self.assertTrue(cp.evaluate_boolean({'YES_ABC': 1, 'VAL': 'ABC'},
35 ['YES_${VAL}']))
36 self.assertTrue(cp.evaluate_boolean({'ABC': 'DEF', 'DEF': 1},
37 ['${ABC}']))
38 self.assertTrue(cp.evaluate_boolean({}, ['FALSE', 'OR', '(', 'FALSE', 'OR', 'TRUE', ')']))
39 self.assertFalse(cp.evaluate_boolean({}, ['FALSE', 'OR', '(', 'FALSE', 'AND', 'TRUE', ')']))
41 def test_foreach(self):
42 s = """
43 set(STUFF A B C D E F)
44 foreach(item ${STUFF})
45 set(YES_${item} 1)
46 endforeach ()
47 """
48 parsed = cp.cmake.parseString(s)
49 variables = {}
50 cp.evaluate(variables, [], parsed)
51 for k in ['A', 'B', 'C', 'D', 'E', 'F']:
52 self.assertEqual(variables['YES_%s' % k], '1')
54 s = """
55 set(STUFF "A;B;C;D;E;F")
56 foreach(item ${STUFF})
57 set(${item} 1)
58 endforeach ()
59 """
60 parsed = cp.cmake.parseString(s)
61 variables = {}
62 cp.evaluate(variables, [], parsed)
63 for k in ['A', 'B', 'C', 'D', 'E', 'F']:
64 self.assertEqual(variables[k], '1')
66 s = """
67 set(STUFF D E F)
68 foreach(item A B C ${STUFF})
69 set(${item} 1)
70 endforeach ()
71 """
72 parsed = cp.cmake.parseString(s)
73 variables = {}
74 cp.evaluate(variables, [], parsed)
75 for k in ['A', 'B', 'C', 'D', 'E', 'F']:
76 self.assertEqual(variables[k], '1')
78 def test_list(self):
79 s = 'list(APPEND TEST 1 1 2 3 5 8 13)'
80 parsed = cp.cmake.parseString(s)
81 variables = {}
82 cache_variables = []
83 cp.evaluate(variables, cache_variables, parsed)
84 self.assertEqual(variables['TEST'], '1 1 2 3 5 8 13')
85 self.assertEqual(len(cache_variables), 0)
87 s = """
88 set(TEST 1)
89 list(APPEND TEST 1 2 3 5 8 13)
90 """
91 parsed = cp.cmake.parseString(s)
92 variables = {}
93 cache_variables = []
94 cp.evaluate(variables, cache_variables, parsed)
95 self.assertEqual(variables['TEST'], '1 1 2 3 5 8 13')
96 self.assertEqual(len(cache_variables), 0)
98 def test_malformed_input(self):
99 self.assertEqual(len(cp.cmake.parseString('func ((A)')), 0)
100 self.assertEqual(len(cp.cmake.parseString('cmd"arg"(arg2)')), 0)
101 self.assertRaises(ParseException, cp.command.parseString, 'func ((A)')
102 self.assertRaises(ParseException, cp.identifier.parseString,
103 '-asdlf')
104 self.assertEqual(cp.identifier.parseString('asd-lf')[0], 'asd')
105 self.assertRaises(ParseException, cp.quoted_argument.parseString,
106 'blah"')
107 s = """
108 " blah blah
109 blah
111 self.assertRaises(ParseException, cp.quoted_argument.parseString,
113 self.assertRaises(ParseException, cp.quoted_argument.parseString,
114 'asdlf')
115 self.assertRaises(ParseException, cp.unquoted_argument.parseString,
116 '#asdflkj')
118 def test_parse_if(self):
119 s = """
120 if (A)
122 elseif (C)
124 elseif (E)
126 else ()
128 endif ()
131 parsed = cp.cmake.parseString(s)
132 self.assertEqual(len(parsed), 10)
133 end, conditions = cp.parse_if(parsed, 0)
134 self.assertEqual(parsed[end][0], 'endif')
135 self.assertEqual(len(conditions), 4)
136 self.assertEqual(conditions[0][0], ['A'])
137 self.assertEqual(conditions[0][1][0], parsed[1])
138 self.assertEqual(conditions[1][0], ['C'])
139 self.assertEqual(conditions[1][1][0], parsed[3])
140 self.assertEqual(conditions[2][0], ['E'])
141 self.assertEqual(conditions[2][1][0], parsed[5])
142 self.assertEqual(conditions[3][0], ['TRUE'])
143 self.assertEqual(conditions[3][1][0], parsed[7])
145 def test_return(self):
146 s = """
147 set(TEST 2)
148 if (true)
149 return()
150 endif ()
151 set(TEST 3)
153 parsed = cp.cmake.parseString(s)
154 variables = {}
155 cache_variables = []
156 cp.evaluate(variables, cache_variables, parsed)
157 self.assertEqual(variables['TEST'], '2')
158 self.assertEqual(len(cache_variables), 0)
160 def test_set(self):
161 s = """set(TEST 2)"""
162 parsed = cp.cmake.parseString(s)
163 variables = {}
164 cache_variables = []
165 cp.evaluate(variables, cache_variables, parsed)
166 self.assertEqual(variables['TEST'], '2')
167 self.assertEqual(len(cache_variables), 0)
169 s = """set(TEST 3 CACHE "documentation")"""
170 parsed = cp.cmake.parseString(s)
171 variables = {}
172 cache_variables = []
173 cp.evaluate(variables, cache_variables, parsed)
174 self.assertEqual(variables['TEST'], '3')
175 self.assertTrue('TEST' in cache_variables)
177 s = """set(TEST A B C D)"""
178 parsed = cp.cmake.parseString(s)
179 variables = {}
180 cp.evaluate(variables, [], parsed)
181 self.assertEqual(variables['TEST'], 'A B C D')
183 s = """set(TEST ${TEST} E F G H)"""
184 parsed = cp.cmake.parseString(s)
185 cp.evaluate(variables, [], parsed)
186 self.assertEqual(variables['TEST'], 'A B C D E F G H')
189 if __name__ == '__main__':
190 unittest.main()