0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import unittest
|
|
18 |
import os
|
|
19 |
import re
|
|
20 |
import logging
|
3
|
21 |
|
0
|
22 |
from cone.public import *
|
3
|
23 |
from cone.public.rules import DefaultContext, ASTInterpreter
|
|
24 |
from cone.confml import model
|
0
|
25 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
26 |
|
3
|
27 |
class TestContext(DefaultContext):
|
0
|
28 |
""" DefaultContext implements ConE specific context for handling rules
|
|
29 |
"""
|
|
30 |
def __init__(self, data):
|
|
31 |
self.data = data
|
|
32 |
|
|
33 |
def eval(self, ast, expression, value):
|
|
34 |
pass
|
|
35 |
|
|
36 |
def get_keys(self, refs):
|
|
37 |
return ASTInterpreter.extract_refs(refs)
|
|
38 |
|
|
39 |
def get_children_for_reference(self, reference):
|
|
40 |
# implement ConE specific children expansion
|
|
41 |
pass
|
|
42 |
|
|
43 |
def handle_terminal(self, expression):
|
3
|
44 |
return self.data[expression]
|
0
|
45 |
|
|
46 |
class TestPluginCondition(unittest.TestCase):
|
|
47 |
|
|
48 |
def test_create_plugin_condition(self):
|
3
|
49 |
condition = rules.SimpleCondition("${foo.bar}", "True")
|
|
50 |
self.assertTrue(isinstance(condition.left, rules.ReferenceTerminal))
|
|
51 |
self.assertEquals(str(condition),'(${foo.bar} => None) == True')
|
0
|
52 |
|
|
53 |
def test_create_plugin_and_eval_booleans(self):
|
|
54 |
context = TestContext(None)
|
|
55 |
condition = rules.SimpleCondition("1", "True")
|
|
56 |
self.assertTrue(condition.eval(context))
|
|
57 |
condition = rules.SimpleCondition("0", True)
|
|
58 |
self.assertFalse(condition.eval(context))
|
3
|
59 |
|
|
60 |
def test_create_plugin_and_eval_none(self):
|
|
61 |
context = TestContext(None)
|
|
62 |
condition = rules.SimpleCondition("None", "None")
|
|
63 |
self.assertTrue(condition.eval(context))
|
|
64 |
condition = rules.SimpleCondition("None", None)
|
|
65 |
self.assertTrue(condition.eval(context))
|
|
66 |
condition = rules.SimpleCondition(None, "None")
|
|
67 |
self.assertTrue(condition.eval(context))
|
|
68 |
condition = rules.SimpleCondition(None, None)
|
|
69 |
self.assertTrue(condition.eval(context))
|
|
70 |
|
|
71 |
condition = rules.SimpleCondition("True", None)
|
|
72 |
self.assertFalse(condition.eval(context))
|
|
73 |
condition = rules.SimpleCondition(None, "True")
|
|
74 |
self.assertFalse(condition.eval(context))
|
|
75 |
|
|
76 |
def test_create_plugin_and_eval_none_from_ref(self):
|
|
77 |
context = TestContext({'Foo.Bar': None})
|
|
78 |
condition = rules.SimpleCondition("${Foo.Bar}", None)
|
|
79 |
self.assertTrue(condition.eval(context))
|
|
80 |
|
|
81 |
def test_create_plugin_and_eval_unicode(self):
|
|
82 |
context = TestContext({'Foo.Bar': u"foo\u20ac"})
|
|
83 |
condition = rules.SimpleCondition("${Foo.Bar}", u"foo\u20ac")
|
|
84 |
self.assertTrue(condition.eval(context))
|
|
85 |
|
|
86 |
def test_create_plugin_and_eval_number_against_string(self):
|
|
87 |
context = TestContext({'Foo.Int': 123,
|
|
88 |
'Foo.Real': 123.4})
|
|
89 |
condition = rules.SimpleCondition("${Foo.Int}", "foo")
|
|
90 |
self.assertFalse(condition.eval(context))
|
|
91 |
condition = rules.SimpleCondition("${Foo.Real}", "foo")
|
|
92 |
self.assertFalse(condition.eval(context))
|
|
93 |
|
|
94 |
condition = rules.SimpleCondition("${Foo.Int}", "123")
|
|
95 |
self.assertTrue(condition.eval(context))
|
|
96 |
condition = rules.SimpleCondition("${Foo.Real}", "123.4")
|
|
97 |
self.assertTrue(condition.eval(context))
|
|
98 |
|
|
99 |
def xtest_selection_setting(self):
|
|
100 |
conf = api.Configuration("test.confml", namespace="com.nokia.s60")
|
|
101 |
|
|
102 |
context = TestContext(None)
|
|
103 |
context.configuration = conf
|
|
104 |
fea= api.Feature("foo")
|
|
105 |
fea.add_feature(api.Feature("child1",type='selection'))
|
|
106 |
fea.child1.create_option('one','1')
|
|
107 |
fea.child1.create_option('two','2')
|
|
108 |
|
|
109 |
conf.add_feature(fea)
|
|
110 |
|
|
111 |
fea.child1.add_data(api.Data(value="2"))
|
|
112 |
|
|
113 |
condition = rules.SimpleCondition("${fea.child1}", "2")
|
|
114 |
self.assertTrue(condition.eval(context))
|
|
115 |
|
|
116 |
def test_sequence_setting(self):
|
|
117 |
context = plugin.GenerationContext()
|
|
118 |
context.configuration = api.Configuration()
|
|
119 |
fea = model.ConfmlSequenceSetting("test")
|
|
120 |
fea.add_feature(model.ConfmlIntSetting('child1'))
|
|
121 |
fea.add_feature(model.ConfmlIntSetting('child2'))
|
|
122 |
fea.add_feature(model.ConfmlIntSetting('child3'))
|
|
123 |
context.configuration.add_feature(fea)
|
|
124 |
seq = context.configuration.get_default_view().get_feature('test')
|
|
125 |
seq.add_sequence(['1','2','3'])
|
|
126 |
seq.add_sequence(['4','5','6'])
|
|
127 |
seq.add_sequence(['7','8','9'])
|
|
128 |
|
|
129 |
condition = rules.SimpleCondition("${test}", [[1,2,3], [4,5,6], [7,8,9]])
|
|
130 |
self.assertTrue(condition.eval(context))
|
|
131 |
|
|
132 |
condition = rules.SimpleCondition("${test}", [['11','22','33'], ['44','55','66'], ['77','88','99']])
|
|
133 |
self.assertFalse(condition.eval(context))
|
|
134 |
|
|
135 |
def test_boolean_setting(self):
|
|
136 |
context = plugin.GenerationContext()
|
|
137 |
context.configuration = api.Configuration()
|
|
138 |
context.configuration.add_feature(model.ConfmlBooleanSetting("test"))
|
|
139 |
|
|
140 |
context.configuration.get_default_view().test.value = True
|
|
141 |
|
|
142 |
condition = rules.SimpleCondition("${test}", True)
|
|
143 |
self.assertTrue(condition.eval(context))
|
|
144 |
|
|
145 |
condition = rules.SimpleCondition("${test}", "zoobar")
|
|
146 |
self.assertFalse(condition.eval(context))
|
|
147 |
|
|
148 |
condition = rules.SimpleCondition("${test}", "1")
|
|
149 |
self.assertFalse(condition.eval(context))
|
|
150 |
|
|
151 |
condition = rules.SimpleCondition("${test}", "true")
|
|
152 |
self.assertTrue(condition.eval(context))
|
|
153 |
|
|
154 |
condition = rules.SimpleCondition("${test}", "0")
|
|
155 |
self.assertFalse(condition.eval(context))
|
|
156 |
|
|
157 |
condition = rules.SimpleCondition("${test}", "")
|
|
158 |
self.assertFalse(condition.eval(context))
|
|
159 |
|
|
160 |
condition = rules.SimpleCondition("${test}", None)
|
|
161 |
self.assertFalse(condition.eval(context))
|
|
162 |
|
|
163 |
def test_string_setting(self):
|
|
164 |
context = plugin.GenerationContext()
|
|
165 |
context.configuration = api.Configuration()
|
|
166 |
context.configuration.add_feature(model.ConfmlStringSetting("test"))
|
|
167 |
|
|
168 |
context.configuration.get_default_view().test.value = "foobar"
|
|
169 |
|
|
170 |
condition = rules.SimpleCondition("${test}", "foobar")
|
|
171 |
self.assertTrue(condition.eval(context))
|
|
172 |
|
|
173 |
condition = rules.SimpleCondition("${test}", "zoobar")
|
|
174 |
self.assertFalse(condition.eval(context))
|
|
175 |
|
|
176 |
condition = rules.SimpleCondition("${test}", "1")
|
|
177 |
self.assertFalse(condition.eval(context))
|
|
178 |
|
|
179 |
condition = rules.SimpleCondition("${test}", "true")
|
|
180 |
self.assertFalse(condition.eval(context))
|
|
181 |
|
|
182 |
condition = rules.SimpleCondition("${test}", "false")
|
|
183 |
self.assertFalse(condition.eval(context))
|
|
184 |
|
|
185 |
condition = rules.SimpleCondition("${test}", "0")
|
|
186 |
self.assertFalse(condition.eval(context))
|
|
187 |
|
|
188 |
condition = rules.SimpleCondition("${test}", "")
|
|
189 |
self.assertFalse(condition.eval(context))
|
|
190 |
|
|
191 |
condition = rules.SimpleCondition("${test}", None)
|
|
192 |
self.assertFalse(condition.eval(context))
|
|
193 |
|
|
194 |
def test_int_setting(self):
|
|
195 |
context = plugin.GenerationContext()
|
|
196 |
context.configuration = api.Configuration()
|
|
197 |
context.configuration.add_feature(model.ConfmlIntSetting("test"))
|
|
198 |
|
|
199 |
context.configuration.get_default_view().test.value = "1"
|
|
200 |
|
|
201 |
condition = rules.SimpleCondition("${test}", "1")
|
|
202 |
self.assertTrue(condition.eval(context))
|
|
203 |
|
|
204 |
condition = rules.SimpleCondition("${test}", 2)
|
|
205 |
self.assertFalse(condition.eval(context))
|
|
206 |
|
|
207 |
condition = rules.SimpleCondition("${test}", True)
|
|
208 |
self.assertTrue(condition.eval(context))
|
|
209 |
|
|
210 |
condition = rules.SimpleCondition("${test}", False)
|
|
211 |
self.assertFalse(condition.eval(context))
|
|
212 |
|
|
213 |
condition = rules.SimpleCondition("${test}", 0)
|
|
214 |
self.assertFalse(condition.eval(context))
|
|
215 |
|
|
216 |
condition = rules.SimpleCondition("${test}", "zoobar")
|
|
217 |
try:
|
|
218 |
self.assertFalse(condition.eval(context))
|
|
219 |
self.fail("Exception expected.")
|
|
220 |
except Exception:
|
|
221 |
pass
|
|
222 |
|
|
223 |
condition = rules.SimpleCondition("${test}", "")
|
|
224 |
try:
|
|
225 |
self.assertFalse(condition.eval(context))
|
|
226 |
self.fail("Exception expected.")
|
|
227 |
except Exception:
|
|
228 |
pass
|
|
229 |
|
|
230 |
condition = rules.SimpleCondition("${test}", None)
|
|
231 |
self.assertFalse(condition.eval(context))
|
|
232 |
|
|
233 |
context.configuration.get_default_view().test.value = "-1"
|
|
234 |
|
|
235 |
condition = rules.SimpleCondition("${test}", -1)
|
|
236 |
self.assertTrue(condition.eval(context))
|
|
237 |
|
|
238 |
condition = rules.SimpleCondition("${test}", 0)
|
|
239 |
self.assertFalse(condition.eval(context))
|
|
240 |
|
|
241 |
def test_selection_setting_true(self):
|
|
242 |
context = plugin.GenerationContext()
|
|
243 |
context.configuration = api.Configuration()
|
|
244 |
context.configuration.add_feature(model.ConfmlSelectionSetting("test"))
|
|
245 |
|
|
246 |
context.configuration.get_default_view().test.value = "True"
|
|
247 |
|
|
248 |
condition = rules.SimpleCondition("${test}", "True")
|
|
249 |
self.assertTrue(condition.eval(context))
|
|
250 |
|
|
251 |
condition = rules.SimpleCondition("${test}", True)
|
|
252 |
self.assertTrue(condition.eval(context))
|
|
253 |
|
|
254 |
condition = rules.SimpleCondition("${test}", "1")
|
|
255 |
self.assertFalse(condition.eval(context))
|
|
256 |
|
|
257 |
def test_selection_setting_false(self):
|
|
258 |
context = plugin.GenerationContext()
|
|
259 |
context.configuration = api.Configuration()
|
|
260 |
context.configuration.add_feature(model.ConfmlSelectionSetting("test"))
|
|
261 |
|
|
262 |
context.configuration.get_default_view().test.value = "False"
|
|
263 |
|
|
264 |
condition = rules.SimpleCondition("${test}", False)
|
|
265 |
self.assertTrue(condition.eval(context))
|
|
266 |
|
|
267 |
condition = rules.SimpleCondition("${test}", "False")
|
|
268 |
self.assertTrue(condition.eval(context))
|
|
269 |
|
|
270 |
condition = rules.SimpleCondition("${test}", "0")
|
|
271 |
self.assertFalse(condition.eval(context))
|
|
272 |
|
|
273 |
condition = rules.SimpleCondition("False", "0")
|
|
274 |
self.assertFalse(condition.eval(context))
|
|
275 |
|
|
276 |
def test_selection_setting_int(self):
|
|
277 |
context = plugin.GenerationContext()
|
|
278 |
context.configuration = api.Configuration()
|
|
279 |
context.configuration.add_feature(model.ConfmlSelectionSetting("test"))
|
|
280 |
|
|
281 |
context.configuration.get_default_view().test.value = "2"
|
|
282 |
|
|
283 |
condition = rules.SimpleCondition("${test}", "2")
|
|
284 |
self.assertTrue(condition.eval(context))
|
|
285 |
|
|
286 |
condition = rules.SimpleCondition("${test}", 2)
|
|
287 |
self.assertTrue(condition.eval(context))
|
0
|
288 |
|
|
289 |
def test_create_plugin_and_eval_integers(self):
|
|
290 |
context = TestContext(None)
|
|
291 |
condition = rules.SimpleCondition("1", "2")
|
|
292 |
self.assertFalse(condition.eval(context))
|
|
293 |
condition = rules.SimpleCondition("112", "2")
|
|
294 |
self.assertFalse(condition.eval(context))
|
|
295 |
condition = rules.SimpleCondition("2", "2")
|
|
296 |
self.assertTrue(condition.eval(context))
|
3
|
297 |
condition = rules.SimpleCondition("2", 2)
|
|
298 |
self.assertTrue(condition.eval(context))
|
0
|
299 |
condition = rules.SimpleCondition(2, 2)
|
|
300 |
self.assertTrue(condition.eval(context))
|
3
|
301 |
condition = rules.SimpleCondition("-2", -2)
|
|
302 |
self.assertTrue(condition.eval(context))
|
0
|
303 |
|
|
304 |
def test_create_plugin_and_eval_string(self):
|
|
305 |
context = TestContext(None)
|
|
306 |
condition = plugin.rules.SimpleCondition("test", "foo")
|
|
307 |
self.assertFalse(condition.eval(context))
|
|
308 |
condition = rules.SimpleCondition("test", "")
|
|
309 |
self.assertFalse(condition.eval(context))
|
|
310 |
condition = rules.SimpleCondition("test foo", "test foo")
|
|
311 |
self.assertTrue(condition.eval(context))
|
|
312 |
|
|
313 |
def test_create_plugin_and_eval_data_reference(self):
|
|
314 |
context = TestContext({'test' : 1, 'foo' : 2, 'bar' : True})
|
|
315 |
condition = rules.SimpleCondition("${test}", 1)
|
3
|
316 |
condition.eval(context)
|
0
|
317 |
self.assertTrue(condition.eval(context))
|
|
318 |
condition = rules.SimpleCondition("${test}", False)
|
|
319 |
self.assertFalse(condition.eval(context))
|
|
320 |
condition = rules.SimpleCondition("${test}", "${foo}")
|
|
321 |
self.assertFalse(condition.eval(context))
|
|
322 |
condition = rules.SimpleCondition("${test}", "${bar}")
|
|
323 |
self.assertTrue(condition.eval(context))
|
|
324 |
|
|
325 |
def test_create_plugin_and_eval_data_reference_on_generation_context(self):
|
|
326 |
context = plugin.GenerationContext()
|
|
327 |
context.configuration = api.Configuration()
|
|
328 |
context.configuration.add_feature(api.Feature("test"))
|
|
329 |
context.configuration.add_feature(api.Feature("stringsub"),"test")
|
|
330 |
context.configuration.add_feature(api.Feature("intsub"),"test")
|
|
331 |
condition = rules.SimpleCondition("${test}", None)
|
|
332 |
self.assertTrue(condition.eval(context))
|
|
333 |
condition = rules.SimpleCondition("${test.stringsub}", "None")
|
3
|
334 |
condition.eval(context)
|
0
|
335 |
self.assertTrue(condition.eval(context))
|
|
336 |
condition = rules.SimpleCondition("${test.intsub}", "None")
|
|
337 |
self.assertTrue(condition.eval(context))
|
|
338 |
context.configuration.get_default_view().test.value = True
|
|
339 |
context.configuration.get_default_view().test.stringsub.value = "stringval"
|
|
340 |
context.configuration.get_default_view().test.intsub.value = 2
|
|
341 |
condition = rules.SimpleCondition("${test}", "true")
|
|
342 |
self.assertTrue(condition.eval(context))
|
|
343 |
condition = rules.SimpleCondition("${test}", "false")
|
|
344 |
self.assertFalse(condition.eval(context))
|
|
345 |
condition = rules.SimpleCondition("${test.stringsub}", "tes")
|
|
346 |
self.assertFalse(condition.eval(context))
|
|
347 |
condition = rules.SimpleCondition("${test.stringsub}", "stringval")
|
|
348 |
self.assertTrue(condition.eval(context))
|
|
349 |
condition = rules.SimpleCondition("${test.intsub}", "1")
|
|
350 |
self.assertFalse(condition.eval(context))
|
|
351 |
condition = rules.SimpleCondition("${test.intsub}", "2")
|
|
352 |
self.assertTrue(condition.eval(context))
|
|
353 |
condition = rules.SimpleCondition("${test.intsub}", 2)
|
|
354 |
self.assertTrue(condition.eval(context))
|
|
355 |
condition = rules.SimpleCondition("${test.intsub}", 1)
|
|
356 |
self.assertFalse(condition.eval(context))
|
|
357 |
try:
|
|
358 |
condition = rules.SimpleCondition("${boo}", "false")
|
|
359 |
self.fail("access of non existing elements succeds?")
|
|
360 |
except:
|
|
361 |
pass
|
3
|
362 |
|
|
363 |
if __name__ == '__main__':
|
|
364 |
unittest.main()
|