diff options
Diffstat (limited to 'bitbake/lib/bb/tests/data.py')
-rw-r--r-- | bitbake/lib/bb/tests/data.py | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/bitbake/lib/bb/tests/data.py b/bitbake/lib/bb/tests/data.py new file mode 100644 index 0000000000..f281a353f4 --- /dev/null +++ b/bitbake/lib/bb/tests/data.py | |||
@@ -0,0 +1,254 @@ | |||
1 | # ex:ts=4:sw=4:sts=4:et | ||
2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
3 | # | ||
4 | # BitBake Tests for the Data Store (data.py/data_smart.py) | ||
5 | # | ||
6 | # Copyright (C) 2010 Chris Larson | ||
7 | # Copyright (C) 2012 Richard Purdie | ||
8 | # | ||
9 | # This program is free software; you can redistribute it and/or modify | ||
10 | # it under the terms of the GNU General Public License version 2 as | ||
11 | # published by the Free Software Foundation. | ||
12 | # | ||
13 | # This program is distributed in the hope that it will be useful, | ||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | # GNU General Public License for more details. | ||
17 | # | ||
18 | # You should have received a copy of the GNU General Public License along | ||
19 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
21 | # | ||
22 | |||
23 | import unittest | ||
24 | import bb | ||
25 | import bb.data | ||
26 | |||
27 | class DataExpansions(unittest.TestCase): | ||
28 | def setUp(self): | ||
29 | self.d = bb.data.init() | ||
30 | self.d["foo"] = "value_of_foo" | ||
31 | self.d["bar"] = "value_of_bar" | ||
32 | self.d["value_of_foo"] = "value_of_'value_of_foo'" | ||
33 | |||
34 | def test_one_var(self): | ||
35 | val = self.d.expand("${foo}") | ||
36 | self.assertEqual(str(val), "value_of_foo") | ||
37 | |||
38 | def test_indirect_one_var(self): | ||
39 | val = self.d.expand("${${foo}}") | ||
40 | self.assertEqual(str(val), "value_of_'value_of_foo'") | ||
41 | |||
42 | def test_indirect_and_another(self): | ||
43 | val = self.d.expand("${${foo}} ${bar}") | ||
44 | self.assertEqual(str(val), "value_of_'value_of_foo' value_of_bar") | ||
45 | |||
46 | def test_python_snippet(self): | ||
47 | val = self.d.expand("${@5*12}") | ||
48 | self.assertEqual(str(val), "60") | ||
49 | |||
50 | def test_expand_in_python_snippet(self): | ||
51 | val = self.d.expand("${@'boo ' + '${foo}'}") | ||
52 | self.assertEqual(str(val), "boo value_of_foo") | ||
53 | |||
54 | def test_python_snippet_getvar(self): | ||
55 | val = self.d.expand("${@d.getVar('foo', True) + ' ${bar}'}") | ||
56 | self.assertEqual(str(val), "value_of_foo value_of_bar") | ||
57 | |||
58 | def test_python_snippet_syntax_error(self): | ||
59 | self.d.setVar("FOO", "${@foo = 5}") | ||
60 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
61 | |||
62 | def test_python_snippet_runtime_error(self): | ||
63 | self.d.setVar("FOO", "${@int('test')}") | ||
64 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
65 | |||
66 | def test_python_snippet_error_path(self): | ||
67 | self.d.setVar("FOO", "foo value ${BAR}") | ||
68 | self.d.setVar("BAR", "bar value ${@int('test')}") | ||
69 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
70 | |||
71 | def test_value_containing_value(self): | ||
72 | val = self.d.expand("${@d.getVar('foo', True) + ' ${bar}'}") | ||
73 | self.assertEqual(str(val), "value_of_foo value_of_bar") | ||
74 | |||
75 | def test_reference_undefined_var(self): | ||
76 | val = self.d.expand("${undefinedvar} meh") | ||
77 | self.assertEqual(str(val), "${undefinedvar} meh") | ||
78 | |||
79 | def test_double_reference(self): | ||
80 | self.d.setVar("BAR", "bar value") | ||
81 | self.d.setVar("FOO", "${BAR} foo ${BAR}") | ||
82 | val = self.d.getVar("FOO", True) | ||
83 | self.assertEqual(str(val), "bar value foo bar value") | ||
84 | |||
85 | def test_direct_recursion(self): | ||
86 | self.d.setVar("FOO", "${FOO}") | ||
87 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
88 | |||
89 | def test_indirect_recursion(self): | ||
90 | self.d.setVar("FOO", "${BAR}") | ||
91 | self.d.setVar("BAR", "${BAZ}") | ||
92 | self.d.setVar("BAZ", "${FOO}") | ||
93 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
94 | |||
95 | def test_recursion_exception(self): | ||
96 | self.d.setVar("FOO", "${BAR}") | ||
97 | self.d.setVar("BAR", "${${@'FOO'}}") | ||
98 | self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True) | ||
99 | |||
100 | def test_incomplete_varexp_single_quotes(self): | ||
101 | self.d.setVar("FOO", "sed -i -e 's:IP{:I${:g' $pc") | ||
102 | val = self.d.getVar("FOO", True) | ||
103 | self.assertEqual(str(val), "sed -i -e 's:IP{:I${:g' $pc") | ||
104 | |||
105 | def test_nonstring(self): | ||
106 | self.d.setVar("TEST", 5) | ||
107 | val = self.d.getVar("TEST", True) | ||
108 | self.assertEqual(str(val), "5") | ||
109 | |||
110 | def test_rename(self): | ||
111 | self.d.renameVar("foo", "newfoo") | ||
112 | self.assertEqual(self.d.getVar("newfoo"), "value_of_foo") | ||
113 | self.assertEqual(self.d.getVar("foo"), None) | ||
114 | |||
115 | def test_deletion(self): | ||
116 | self.d.delVar("foo") | ||
117 | self.assertEqual(self.d.getVar("foo"), None) | ||
118 | |||
119 | def test_keys(self): | ||
120 | keys = self.d.keys() | ||
121 | self.assertEqual(keys, ['value_of_foo', 'foo', 'bar']) | ||
122 | |||
123 | class TestNestedExpansions(unittest.TestCase): | ||
124 | def setUp(self): | ||
125 | self.d = bb.data.init() | ||
126 | self.d["foo"] = "foo" | ||
127 | self.d["bar"] = "bar" | ||
128 | self.d["value_of_foobar"] = "187" | ||
129 | |||
130 | def test_refs(self): | ||
131 | val = self.d.expand("${value_of_${foo}${bar}}") | ||
132 | self.assertEqual(str(val), "187") | ||
133 | |||
134 | #def test_python_refs(self): | ||
135 | # val = self.d.expand("${@${@3}**2 + ${@4}**2}") | ||
136 | # self.assertEqual(str(val), "25") | ||
137 | |||
138 | def test_ref_in_python_ref(self): | ||
139 | val = self.d.expand("${@'${foo}' + 'bar'}") | ||
140 | self.assertEqual(str(val), "foobar") | ||
141 | |||
142 | def test_python_ref_in_ref(self): | ||
143 | val = self.d.expand("${${@'f'+'o'+'o'}}") | ||
144 | self.assertEqual(str(val), "foo") | ||
145 | |||
146 | def test_deep_nesting(self): | ||
147 | depth = 100 | ||
148 | val = self.d.expand("${" * depth + "foo" + "}" * depth) | ||
149 | self.assertEqual(str(val), "foo") | ||
150 | |||
151 | #def test_deep_python_nesting(self): | ||
152 | # depth = 50 | ||
153 | # val = self.d.expand("${@" * depth + "1" + "+1}" * depth) | ||
154 | # self.assertEqual(str(val), str(depth + 1)) | ||
155 | |||
156 | def test_mixed(self): | ||
157 | val = self.d.expand("${value_of_${@('${foo}'+'bar')[0:3]}${${@'BAR'.lower()}}}") | ||
158 | self.assertEqual(str(val), "187") | ||
159 | |||
160 | def test_runtime(self): | ||
161 | val = self.d.expand("${${@'value_of' + '_f'+'o'+'o'+'b'+'a'+'r'}}") | ||
162 | self.assertEqual(str(val), "187") | ||
163 | |||
164 | class TestMemoize(unittest.TestCase): | ||
165 | def test_memoized(self): | ||
166 | d = bb.data.init() | ||
167 | d.setVar("FOO", "bar") | ||
168 | self.assertTrue(d.getVar("FOO") is d.getVar("FOO")) | ||
169 | |||
170 | def test_not_memoized(self): | ||
171 | d1 = bb.data.init() | ||
172 | d2 = bb.data.init() | ||
173 | d1.setVar("FOO", "bar") | ||
174 | d2.setVar("FOO", "bar2") | ||
175 | self.assertTrue(d1.getVar("FOO") is not d2.getVar("FOO")) | ||
176 | |||
177 | def test_changed_after_memoized(self): | ||
178 | d = bb.data.init() | ||
179 | d.setVar("foo", "value of foo") | ||
180 | self.assertEqual(str(d.getVar("foo")), "value of foo") | ||
181 | d.setVar("foo", "second value of foo") | ||
182 | self.assertEqual(str(d.getVar("foo")), "second value of foo") | ||
183 | |||
184 | def test_same_value(self): | ||
185 | d = bb.data.init() | ||
186 | d.setVar("foo", "value of") | ||
187 | d.setVar("bar", "value of") | ||
188 | self.assertEqual(d.getVar("foo"), | ||
189 | d.getVar("bar")) | ||
190 | |||
191 | class TestConcat(unittest.TestCase): | ||
192 | def setUp(self): | ||
193 | self.d = bb.data.init() | ||
194 | self.d.setVar("FOO", "foo") | ||
195 | self.d.setVar("VAL", "val") | ||
196 | self.d.setVar("BAR", "bar") | ||
197 | |||
198 | def test_prepend(self): | ||
199 | self.d.setVar("TEST", "${VAL}") | ||
200 | self.d.prependVar("TEST", "${FOO}:") | ||
201 | self.assertEqual(self.d.getVar("TEST", True), "foo:val") | ||
202 | |||
203 | def test_append(self): | ||
204 | self.d.setVar("TEST", "${VAL}") | ||
205 | self.d.appendVar("TEST", ":${BAR}") | ||
206 | self.assertEqual(self.d.getVar("TEST", True), "val:bar") | ||
207 | |||
208 | def test_multiple_append(self): | ||
209 | self.d.setVar("TEST", "${VAL}") | ||
210 | self.d.prependVar("TEST", "${FOO}:") | ||
211 | self.d.appendVar("TEST", ":val2") | ||
212 | self.d.appendVar("TEST", ":${BAR}") | ||
213 | self.assertEqual(self.d.getVar("TEST", True), "foo:val:val2:bar") | ||
214 | |||
215 | class TestOverrides(unittest.TestCase): | ||
216 | def setUp(self): | ||
217 | self.d = bb.data.init() | ||
218 | self.d.setVar("OVERRIDES", "foo:bar:local") | ||
219 | self.d.setVar("TEST", "testvalue") | ||
220 | |||
221 | def test_no_override(self): | ||
222 | bb.data.update_data(self.d) | ||
223 | self.assertEqual(self.d.getVar("TEST", True), "testvalue") | ||
224 | |||
225 | def test_one_override(self): | ||
226 | self.d.setVar("TEST_bar", "testvalue2") | ||
227 | bb.data.update_data(self.d) | ||
228 | self.assertEqual(self.d.getVar("TEST", True), "testvalue2") | ||
229 | |||
230 | def test_multiple_override(self): | ||
231 | self.d.setVar("TEST_bar", "testvalue2") | ||
232 | self.d.setVar("TEST_local", "testvalue3") | ||
233 | self.d.setVar("TEST_foo", "testvalue4") | ||
234 | bb.data.update_data(self.d) | ||
235 | self.assertEqual(self.d.getVar("TEST", True), "testvalue3") | ||
236 | |||
237 | |||
238 | class TestFlags(unittest.TestCase): | ||
239 | def setUp(self): | ||
240 | self.d = bb.data.init() | ||
241 | self.d.setVar("foo", "value of foo") | ||
242 | self.d.setVarFlag("foo", "flag1", "value of flag1") | ||
243 | self.d.setVarFlag("foo", "flag2", "value of flag2") | ||
244 | |||
245 | def test_setflag(self): | ||
246 | self.assertEqual(self.d.getVarFlag("foo", "flag1"), "value of flag1") | ||
247 | self.assertEqual(self.d.getVarFlag("foo", "flag2"), "value of flag2") | ||
248 | |||
249 | def test_delflag(self): | ||
250 | self.d.delVarFlag("foo", "flag2") | ||
251 | self.assertEqual(self.d.getVarFlag("foo", "flag1"), "value of flag1") | ||
252 | self.assertEqual(self.d.getVarFlag("foo", "flag2"), None) | ||
253 | |||
254 | |||