def test_identity(self): """ By default, values are left as is """ conv = Converter() val = conv.convert(Property, 'foo') self.assertEqual(val, 'foo')
def test_function(self): """ You can use a function for converting. """ conv = Converter() @conv.when(Property) def converter(x): return x + 'hey' val = conv.convert(Property, 'foo') self.assertEqual(val, 'foohey')
def test_class(self): """ Only the given key's converters should be used """ conv = Converter() @conv.when('A') def convA(x): return 'A' @conv.when('B') def convB(x): return 'B' self.assertEqual(conv.convert('A', 'something'), 'A') self.assertEqual(conv.convert('B', 'something'), 'B')
def test_multiple(self): """ You can have multiple functions do conversion """ conv = Converter() @conv.when(Property) def conv1(x): return x + '1' @conv.when(Property) def conv2(x): return x + '2' val = conv.convert(Property, 'foo') self.assertEqual(val, 'foo12')