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
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(),
15 def test_command(self
):
16 self
.assertEqual(cp
.command
.parseString('blah()').asList(),
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'},
36 self
.assertTrue(cp
.evaluate_boolean({'ABC': 'DEF', 'DEF': 1},
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
):
43 set(STUFF A B C D E F)
44 foreach(item ${STUFF})
48 parsed
= cp
.cmake
.parseString(s
)
50 cp
.evaluate(variables
, [], parsed
)
51 for k
in ['A', 'B', 'C', 'D', 'E', 'F']:
52 self
.assertEqual(variables
['YES_%s' % k
], '1')
55 set(STUFF "A;B;C;D;E;F")
56 foreach(item ${STUFF})
60 parsed
= cp
.cmake
.parseString(s
)
62 cp
.evaluate(variables
, [], parsed
)
63 for k
in ['A', 'B', 'C', 'D', 'E', 'F']:
64 self
.assertEqual(variables
[k
], '1')
68 foreach(item A B C ${STUFF})
72 parsed
= cp
.cmake
.parseString(s
)
74 cp
.evaluate(variables
, [], parsed
)
75 for k
in ['A', 'B', 'C', 'D', 'E', 'F']:
76 self
.assertEqual(variables
[k
], '1')
79 s
= 'list(APPEND TEST 1 1 2 3 5 8 13)'
80 parsed
= cp
.cmake
.parseString(s
)
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)
89 list(APPEND TEST 1 2 3 5 8 13)
91 parsed
= cp
.cmake
.parseString(s
)
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
,
104 self
.assertEqual(cp
.identifier
.parseString('asd-lf')[0], 'asd')
105 self
.assertRaises(ParseException
, cp
.quoted_argument
.parseString
,
111 self
.assertRaises(ParseException
, cp
.quoted_argument
.parseString
,
113 self
.assertRaises(ParseException
, cp
.quoted_argument
.parseString
,
115 self
.assertRaises(ParseException
, cp
.unquoted_argument
.parseString
,
118 def test_parse_if(self
):
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
):
153 parsed
= cp
.cmake
.parseString(s
)
156 cp
.evaluate(variables
, cache_variables
, parsed
)
157 self
.assertEqual(variables
['TEST'], '2')
158 self
.assertEqual(len(cache_variables
), 0)
161 s
= """set(TEST 2)"""
162 parsed
= cp
.cmake
.parseString(s
)
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
)
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
)
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__':