|
1 # *-* coding: utf-8 *-* |
|
2 # |
|
3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 # All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of "Eclipse Public License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Nokia Corporation - initial contribution. |
|
12 # |
|
13 # Contributors: |
|
14 # |
|
15 # Description: |
|
16 # |
|
17 |
|
18 import unittest |
|
19 import os, shutil |
|
20 import sys |
|
21 import re |
|
22 |
|
23 from legacyruleplugin import ruleml, relations |
|
24 from cone.public import api, exceptions |
|
25 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
26 |
|
27 class MockObject(object): |
|
28 pass |
|
29 |
|
30 class MockFeature(object): |
|
31 def __init__(self, ref, feature_values): |
|
32 self.ref = ref |
|
33 self.feature_values = feature_values |
|
34 |
|
35 def get_value(self): |
|
36 return self.feature_values[self.ref] |
|
37 |
|
38 def set_value(self, value): |
|
39 self.feature_values[self.ref] = value |
|
40 |
|
41 class MockConfigurationContext(object): |
|
42 def __init__(self, feature_values): |
|
43 self.configuration = MockObject() |
|
44 default_view = MockObject() |
|
45 default_view.get_feature = lambda ref: MockFeature(ref, feature_values) |
|
46 self.configuration.get_default_view = lambda: default_view |
|
47 self.ref_eval_callback = None |
|
48 |
|
49 class MockExpression(object): |
|
50 def __init__(self, expression): |
|
51 self.expression = repr(expression) |
|
52 |
|
53 class TestEvalExpression(unittest.TestCase): |
|
54 def test_extract_refs(self): |
|
55 ee = relations.EvalExpression( |
|
56 None, MockExpression("'%05d 0x%08X %d' % (${Feature1.Setting1}.get_value(), ${Feature1.Setting2}.get_value(), ${Feature2.Setting1}.get_value())")) |
|
57 self.assertEquals(sorted(ee.extract_refs()), sorted(["Feature1.Setting1", "Feature1.Setting2", "Feature2.Setting1"])) |
|
58 |
|
59 ee = relations.EvalExpression(None, MockExpression("'%05d 0x%08X %d' % (1, 2, 3)")) |
|
60 self.assertEquals(ee.extract_refs(), []) |
|
61 |
|
62 ee = relations.EvalExpression(None, MockExpression(u"${ударения.ελληνικά}")) |
|
63 self.assertEquals(ee.extract_refs(), [u'ударения.ελληνικά']) |
|
64 |
|
65 def test_execute(self): |
|
66 feature_values = { |
|
67 "Feature1.Setting1": 16, |
|
68 "Feature1.Setting2": 32, |
|
69 "Feature2.Setting1": 64, |
|
70 u'ударения.ελληνικά': 100, |
|
71 } |
|
72 context = MockConfigurationContext(feature_values) |
|
73 |
|
74 ee = relations.EvalExpression(None, |
|
75 MockExpression("'%05d 0x%08X %d' % (@{Feature1.Setting1}.get_value(), @{Feature1.Setting2}.get_value(), @{Feature2.Setting1}.get_value())")) |
|
76 self.assertEquals(ee.eval(context), "00016 0x00000020 64") |
|
77 |
|
78 ee = relations.EvalExpression(None, MockExpression("'%05d 0x%08X %d' % (1, 2, 3)")) |
|
79 self.assertEquals(ee.eval(context), "00001 0x00000002 3") |
|
80 |
|
81 ee = relations.EvalExpression(None, MockExpression(u"'%d' % @{ударения.ελληνικά}.get_value()")) |
|
82 self.assertEquals(ee.eval(context), "100") |
|
83 |
|
84 class TestReplaceEvalBlocks(unittest.TestCase): |
|
85 def test_replace_eval_blocks(self): |
|
86 replace = ruleml.RuleImplReader2._replace_eval_blocks |
|
87 |
|
88 orig = """some.setting configures x = y""" |
|
89 self.assertEquals(replace(orig), orig) |
|
90 |
|
91 orig = """some.setting configures x = {% do_something(@{Fea.Set}) %}""" |
|
92 self.assertEquals(replace(orig), """some.setting configures x = __eval__ 'do_something(@{Fea.Set})'""") |
|
93 |
|
94 orig = """{% 'test' %}""" |
|
95 self.assertEquals(replace(orig), '''__eval__ "'test'"''') |
|
96 orig = """{%'test'%}""" |
|
97 self.assertEquals(replace(orig), '''__eval__ "'test'"''') |
|
98 |
|
99 orig = """{% len(@{Fea.Set}.get_value()) %} == 3 configures x = {% do_something('test') %}""" |
|
100 self.assertEquals(replace(orig), '''__eval__ 'len(@{Fea.Set}.get_value())' == 3 configures x = __eval__ "do_something('test')"''') |
|
101 |
|
102 orig = u"True configures X.Y = {% len(@{ударения.ελληνικά}.get_value()) %}" |
|
103 self.assertEquals(replace(orig), u"True configures X.Y = __eval__ %r" % u"len(@{ударения.ελληνικά}.get_value())") |
|
104 |
|
105 if __name__ == "__main__": |
|
106 unittest.main() |