OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import os | |
7 import sys | |
8 import unittest | |
9 | |
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( | |
11 os.path.abspath(__file__))))) | |
12 | |
13 from recipe_engine import loader, recipe_api, config | |
14 | |
15 class TestProperties(unittest.TestCase): | |
16 def _makeProp(self, *args, **kwargs): | |
17 return recipe_api.Property(*args, **kwargs) | |
18 | |
19 def testDefault(self): | |
20 """Tests the default option of properties.""" | |
21 for default in (1, object(), "test", None): | |
22 prop = self._makeProp(default=default) | |
23 self.assertEqual(default, prop.interpret(recipe_api.Property.sentinel)) | |
24 | |
25 def testRequired(self): | |
26 """Tests that a required property errors when not provided.""" | |
27 prop = self._makeProp() | |
28 with self.assertRaises(ValueError): | |
29 prop.interpret(recipe_api.Property.sentinel) | |
30 | |
31 def testTypeSingle(self): | |
32 """Tests a simple typed property.""" | |
33 prop = self._makeProp(kind=bool) | |
34 with self.assertRaises(TypeError): | |
35 prop.interpret(1) | |
36 | |
37 self.assertEqual(True, prop.interpret(True)) | |
38 | |
39 def testTypeFancy(self): | |
40 """Tests a config style type property.""" | |
41 prop = self._makeProp(kind=config.List(int)) | |
42 for value in (1, "hi", [3, "test"]): | |
43 with self.assertRaises(TypeError): | |
44 prop.interpret(value) | |
45 | |
46 self.assertEqual([2, 3], prop.interpret([2, 3])) | |
47 | |
48 class TestInvoke(unittest.TestCase): | |
49 def invoke(self, callable, all_properties, prop_defs, **kwargs): | |
50 return loader.invoke_with_properties( | |
51 callable, all_properties, prop_defs, **kwargs) | |
52 | |
53 def testInvokeFuncSimple(self): | |
54 """Simple test of invoke.""" | |
55 def func(): | |
56 pass | |
57 | |
58 self.assertEqual(self.invoke(func, {}, {}), None) | |
59 | |
60 def testInvokeFuncComplex(self): | |
61 """Tests invoke with two different properties.""" | |
62 def func(a, b): | |
63 return a | |
64 | |
65 prop_defs = { | |
66 'a': recipe_api.Property(), | |
67 'b': recipe_api.Property(), | |
68 } | |
69 | |
70 props = { | |
71 'a': 1, | |
72 'b': 2, | |
73 } | |
74 self.assertEqual(1, self.invoke(func, props, prop_defs)) | |
75 | |
76 def testInvokeClass(self): | |
77 """Tests invoking a class.""" | |
78 class test(object): | |
79 def __init__(self, a, b): | |
80 self.answer = a | |
81 | |
82 prop_defs = { | |
83 'a': recipe_api.Property(), | |
84 'b': recipe_api.Property(), | |
85 } | |
86 | |
87 props = { | |
88 'a': 1, | |
89 'b': 2, | |
90 } | |
91 self.assertEqual(1, self.invoke(test, props, prop_defs).answer) | |
92 | |
93 def testMissingProperty(self): | |
94 """Tests that invoke raises an error when missing a property.""" | |
95 def func(a): | |
96 return a | |
97 | |
98 with self.assertRaises(recipe_api.UndefinedPropertyException): | |
99 self.invoke(func, {}, {}) | |
100 | |
101 if __name__ == '__main__': | |
102 unittest.main() | |
OLD | NEW |