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