Esempio n. 1
0
 def test_invalid_values(self):
     invalid_values = (
         object(),    # Objects.
         object,      # Classes.
     )
     for value in invalid_values:
         with self.assertRaises(TypeError):
             hipack.dumps({ "value": value })
Esempio n. 2
0
 def test_invalid_key_values(self):
     invalid_keys = (
         "col:on",    # Colons are not allowed.
         "sp ace",    # Ditto for spaces.
     )
     for key in invalid_keys:
         with self.assertRaises(ValueError):
             hipack.dumps({ key: True })
Esempio n. 3
0
 def test_dumps(self):
     for value, expected_noindent, expected \
             in self.get_dumps_test_values():
         result = hipack.dumps(value)
         expected = six.b(dedent(expected))
         self.assertEqual(expected, result)
         self.assertTrue(isinstance(result, bytes))
         expected_noindent = six.b(dedent(expected_noindent))
         result = hipack.dumps(value, False)
         self.assertEqual(expected_noindent, result)
         self.assertTrue(isinstance(result, bytes))
Esempio n. 4
0
 def test_invalid_key_types(self):
     invalid_keys = (
         42, 3.14,    # Numeric.
         object(),    # Objects.
         object,      # Classes.
         True, False, # Booleans.
         [], (), {},  # Collections.
     )
     for key in invalid_keys:
         with self.assertRaises(TypeError):
             hipack.dumps({ key: True })
Esempio n. 5
0
 def test_dump_non_dict(self):
     invalid_values = (
         "string",
         42, 3.14,    # Numbers.
         object(),    # Objects.
         object,      # Classes.
         True, False, # Booleans.
         [], (),      # Non-dict collections.
     )
     for value in invalid_values:
         with self.assertRaises(TypeError):
             hipack.dumps(value)
Esempio n. 6
0
    def test_serialize_object_nested(self):
        class Person(object):
            def __init__(self, name, surname):
                self.name = name
                self.surname = surname
            def as_dict(self):
                return { "name": self.name, "surname": self.surname }
        class Hero(object):
            def __init__(self, nick, alterego):
                self.nick = nick
                self.alterego = alterego
            def as_dict(self):
                return { "nick": self.nick, "alter-ego": self.alterego }

        def obj_value(obj):
            if isinstance(obj, (Person, Hero)):
                return obj.as_dict(), (obj.__class__.__name__,)
            return obj, None

        h = Hero("Spider-Man", Person("Peter", "Parker"))
        expected = dedent("""\
            item::Hero {
              alter-ego::Person {
                name: "Peter"
                surname: "Parker"
              }
              nick: "Spider-Man"
            }
            """)
        text = hipack.dumps({"item": h}, value=obj_value)
        self.assertEqual(six.u(expected), _U(text))
Esempio n. 7
0
    def to_hipack(self, indent=False):
        """
        Serializes an object to HiPack.

        The `indent` argument is passed to `hipack.dumps()`.
        """
        from hipack import dumps
        return dumps(self, indent=indent, value=self.__to_hipack_serializable)
Esempio n. 8
0
    def test_serialize_object(self):
        class Person(object):
            def __init__(self, name, surname):
                self.name = name
                self.surname = surname
            def as_dict(self):
                return { "name": self.name, "surname": self.surname }

        def obj_value(obj):
            if isinstance(obj, Person):
                return obj.as_dict(), ("person",)
            return obj, None

        text = hipack.dumps({"spiderman": Person("Peter", "Parker")},
                value=obj_value)
        self.assertEqual(six.u(dedent("""\
                spiderman::person {
                  name: "Peter"
                  surname: "Parker"
                }
                """)), _U(text))
Esempio n. 9
0
 def test_serialize_duplicate_annotation(self):
     with self.assertRaises(ValueError):
         hipack.dumps({"x":0}, value=lambda x: (x, ("a", "a")))
Esempio n. 10
0
 def test_invalid_annots(self, annot):
     with self.assertRaises(ValueError):
         text = hipack.dumps({"x": 0}, value=lambda x: (x, (annot,)))
Esempio n. 11
0
 def test_dump_annot(self, data):
     annotations, expected = data
     text = hipack.dumps({"x": 0}, value=lambda x: (x, annotations))
     self.assertEqual(six.u(expected + "\n"), _U(text))