|
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 |
|
21 import __init__ |
|
22 from cone.public import * |
|
23 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
24 |
|
25 class TestContext(object): |
|
26 """ DefaultContext implements ConE specific context for handling rules |
|
27 """ |
|
28 def __init__(self, data): |
|
29 self.data = data |
|
30 |
|
31 def eval(self, ast, expression, value): |
|
32 pass |
|
33 |
|
34 def get_keys(self, refs): |
|
35 return ASTInterpreter.extract_refs(refs) |
|
36 |
|
37 def get_children_for_reference(self, reference): |
|
38 # implement ConE specific children expansion |
|
39 pass |
|
40 |
|
41 def handle_terminal(self, expression): |
|
42 try: |
|
43 m = re.match("\${(.*)}", expression) |
|
44 if m: |
|
45 return self.data[m.group(1)] |
|
46 elif expression in ['true','1','True']: |
|
47 return True |
|
48 elif expression in ['false','0','False']: |
|
49 return False |
|
50 else: |
|
51 return eval(expression) |
|
52 except: |
|
53 return expression |
|
54 |
|
55 class TestPluginCondition(unittest.TestCase): |
|
56 |
|
57 def test_create_plugin_condition(self): |
|
58 condition = rules.SimpleCondition("foo.bar", "True") |
|
59 |
|
60 def test_create_plugin_and_eval_booleans(self): |
|
61 context = TestContext(None) |
|
62 condition = rules.SimpleCondition("1", "True") |
|
63 self.assertTrue(condition.eval(context)) |
|
64 condition = rules.SimpleCondition("0", True) |
|
65 self.assertFalse(condition.eval(context)) |
|
66 |
|
67 def test_create_plugin_and_eval_integers(self): |
|
68 context = TestContext(None) |
|
69 condition = rules.SimpleCondition("1", "2") |
|
70 self.assertFalse(condition.eval(context)) |
|
71 condition = rules.SimpleCondition("112", "2") |
|
72 self.assertFalse(condition.eval(context)) |
|
73 condition = rules.SimpleCondition("2", "2") |
|
74 self.assertTrue(condition.eval(context)) |
|
75 condition = rules.SimpleCondition(2, 2) |
|
76 self.assertTrue(condition.eval(context)) |
|
77 |
|
78 def test_create_plugin_and_eval_string(self): |
|
79 context = TestContext(None) |
|
80 condition = plugin.rules.SimpleCondition("test", "foo") |
|
81 self.assertFalse(condition.eval(context)) |
|
82 condition = rules.SimpleCondition("test", "") |
|
83 self.assertFalse(condition.eval(context)) |
|
84 condition = rules.SimpleCondition("test foo", "test foo") |
|
85 self.assertTrue(condition.eval(context)) |
|
86 |
|
87 def test_create_plugin_and_eval_data_reference(self): |
|
88 context = TestContext({'test' : 1, 'foo' : 2, 'bar' : True}) |
|
89 condition = rules.SimpleCondition("${test}", 1) |
|
90 self.assertTrue(condition.eval(context)) |
|
91 condition = rules.SimpleCondition("${test}", False) |
|
92 self.assertFalse(condition.eval(context)) |
|
93 condition = rules.SimpleCondition("${test}", "${foo}") |
|
94 self.assertFalse(condition.eval(context)) |
|
95 condition = rules.SimpleCondition("${test}", "${bar}") |
|
96 self.assertTrue(condition.eval(context)) |
|
97 |
|
98 def test_create_plugin_and_eval_data_reference_on_generation_context(self): |
|
99 context = plugin.GenerationContext() |
|
100 context.configuration = api.Configuration() |
|
101 context.configuration.add_feature(api.Feature("test")) |
|
102 context.configuration.add_feature(api.Feature("stringsub"),"test") |
|
103 context.configuration.add_feature(api.Feature("intsub"),"test") |
|
104 condition = rules.SimpleCondition("${test}", None) |
|
105 self.assertTrue(condition.eval(context)) |
|
106 condition = rules.SimpleCondition("${test.stringsub}", "None") |
|
107 self.assertTrue(condition.eval(context)) |
|
108 condition = rules.SimpleCondition("${test.intsub}", "None") |
|
109 self.assertTrue(condition.eval(context)) |
|
110 context.configuration.get_default_view().test.value = True |
|
111 context.configuration.get_default_view().test.stringsub.value = "stringval" |
|
112 context.configuration.get_default_view().test.intsub.value = 2 |
|
113 condition = rules.SimpleCondition("${test}", "true") |
|
114 self.assertTrue(condition.eval(context)) |
|
115 condition = rules.SimpleCondition("${test}", "false") |
|
116 self.assertFalse(condition.eval(context)) |
|
117 condition = rules.SimpleCondition("${test.stringsub}", "tes") |
|
118 self.assertFalse(condition.eval(context)) |
|
119 condition = rules.SimpleCondition("${test.stringsub}", "stringval") |
|
120 self.assertTrue(condition.eval(context)) |
|
121 condition = rules.SimpleCondition("${test.intsub}", "1") |
|
122 self.assertFalse(condition.eval(context)) |
|
123 condition = rules.SimpleCondition("${test.intsub}", "2") |
|
124 self.assertTrue(condition.eval(context)) |
|
125 condition = rules.SimpleCondition("${test.intsub}", 2) |
|
126 self.assertTrue(condition.eval(context)) |
|
127 condition = rules.SimpleCondition("${test.intsub}", 1) |
|
128 self.assertFalse(condition.eval(context)) |
|
129 try: |
|
130 condition = rules.SimpleCondition("${boo}", "false") |
|
131 self.fail("access of non existing elements succeds?") |
|
132 except: |
|
133 pass |