Example #1
0
    def test_serialization_custom(self):
        class MyStuff(object):
            def __init__(self, *stuff):
                self.stuff = stuff

        def normalizer(o):
            if isinstance(o, six.integer_types):
                return 'o'
            if isinstance(o, MyStuff):
                return wl.RandomThings(*o.stuff)
            return o

        expr = [1, 2, 'a', {1: "a"}, MyStuff(1, 2, MyStuff(1, 'a'))]
        normalized = [
            "o", "o", "a", {
                "o": "a"
            },
            wl.RandomThings("o", "o", wl.RandomThings("o", "a"))
        ]

        for export_format in available_formats:

            with self.assertRaises(NotImplementedError) as context:
                export(expr, normalizer=identity, target_format=export_format)

            self.assertEqual(
                export(expr,
                       normalizer=normalizer,
                       target_format=export_format),
                export(normalized,
                       normalizer=identity,
                       target_format=export_format),
            )
 def normalizer(o):
     if isinstance(o, six.integer_types):
         return "o"
     if isinstance(o, MyStuff):
         return wl.RandomThings(*o.stuff)
     return o