| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2013 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 shutil | |
| 8 import tempfile | |
| 9 import unittest | |
| 10 | |
| 11 import test_env # pylint: disable=W0403,W0611 | |
| 12 import sys | |
| 13 from slave import field_composer | |
| 14 | |
| 15 | |
| 16 # Functors are compared by reference. | |
| 17 BEAUTY_LAMBDA = lambda x, y: x + y + 1 | |
| 18 | |
| 19 | |
| 20 class TestFieldComposer(unittest.TestCase): | |
| 21 | |
| 22 def setUp(self): | |
| 23 super(TestFieldComposer, self).setUp() | |
| 24 _functors = { | |
| 25 'beauty': {'combine': BEAUTY_LAMBDA}, | |
| 26 'despair': {'combine': lambda x, y: x * y}} | |
| 27 self.fc = field_composer.FieldComposer( | |
| 28 {'beauty': 7, 'despair': 10}, _functors) | |
| 29 | |
| 30 def test_init_degenerate_registry(self): | |
| 31 """Lack of 'combine' in registry value should raise an error.""" | |
| 32 with self.assertRaises(field_composer.DegenerateRegistryError): | |
| 33 field_composer.FieldComposer( | |
| 34 {'hello': 'hello'}, {'hello': {'darkness': 'my old friend'}}) | |
| 35 | |
| 36 def test_dict_methods_ok(self): | |
| 37 """FieldComposer acts as a dict for some methods.""" | |
| 38 for key in ['beauty', 'despair', 'absence']: | |
| 39 # fc.get returns fc._fields.get | |
| 40 self.assertEqual(self.fc._fields.get(key), self.fc.get(key)) | |
| 41 self.assertEqual(self.fc._fields.get(key, 1), self.fc.get(key, 1)) | |
| 42 | |
| 43 # in fc returns in fc._fields | |
| 44 self.assertEqual(key in self.fc, key in self.fc._fields) | |
| 45 | |
| 46 # fc[key] returns fc._fields[key] | |
| 47 if key in self.fc: | |
| 48 self.assertEqual(self.fc[key], self.fc._fields[key]) | |
| 49 else: | |
| 50 with self.assertRaises(KeyError): | |
| 51 _ = self.fc._fields[key] | |
| 52 | |
| 53 def test_compose_with_dict_ok(self): | |
| 54 new_fields = {'beauty': 9} | |
| 55 new_fc = self.fc.compose(new_fields) | |
| 56 expected = {'beauty': 17, 'despair': 10} | |
| 57 self.assertEqual(expected, new_fc._fields) | |
| 58 | |
| 59 def test_compose_with_unknown_field(self): | |
| 60 """CompositionUndefined must be raised when kwargs don't have combiners.""" | |
| 61 with self.assertRaises(field_composer.CompositionUndefined): | |
| 62 self.fc.compose({'beauty': 9, 'hope': 'none to speak of'}) | |
| 63 | |
| 64 def test_compose_with_compositor_ok(self): | |
| 65 second_fc = field_composer.FieldComposer( | |
| 66 {'beauty': 9}, {'beauty': {'combine': BEAUTY_LAMBDA}}) | |
| 67 new_fc = self.fc.compose(second_fc) | |
| 68 expected = {'beauty': 17, 'despair': 10} | |
| 69 self.assertEqual(expected, new_fc._fields) | |
| 70 | |
| 71 def test_compose_with_sneaky_bad_registry(self): | |
| 72 """RegistryConflict must be raised when functors clash.""" | |
| 73 second_fc = field_composer.FieldComposer( | |
| 74 {}, {'beauty': {'combine': lambda x, y: 0}}) | |
| 75 with self.assertRaises(field_composer.RegistryConflict): | |
| 76 self.fc.compose(second_fc) | |
| 77 | |
| 78 | |
| 79 if __name__ == '__main__': | |
| 80 unittest.main() | |
| OLD | NEW |