Example #1
0
    def test_map(self):
        m = Map()
        self.assertTrue(isinstance(m, dict))

        m = Map(foo="bar")
        self.assertEqual(m["foo"], "bar")

        m = Map([("x", "y")], foo="bar")
        self.assertEqual(m["foo"], "bar")
        self.assertEqual(m["x"], "y")
Example #2
0
    def test_encode_map(self):
        self.assertEqual(
            b"\x83t\x00\x00\x00\x01t\x00\x00\x00\x00t\x00\x00\x00\x00",
            encode(Map({Map(): Map()})))

        self.assertEqual(
            b'\x83t\x00\x00\x00\x01k\x00\x06hello1k\x00\x06world1',
            encode(Map({b"hello1": b"world1"})))

        self.assertEqual(
            b"\x83t\x00\x00\x00\x01k\x00\x06hello1k\x00\x03\x01\x02\x03",
            encode(Map({b"hello1": List([1, 2, 3])})))
Example #3
0
    def test_encode_map(self):
        self.assertEqual(
            "\x83t\x00\x00\x00\x01t\x00\x00\x00\x00t\x00\x00\x00\x00",
            encode(Map({Map(): Map()}))
        )

        self.assertEqual(
            "\x83t\x00\x00\x00\x01m\x00\x00\x00\x06"
            "hello1m\x00\x00\x00\x06world1",
            encode(Map(hello1="world1"))
        )

        self.assertEqual(
            "\x83t\x00\x00\x00\x01m\x00\x00\x00\x06"
            "hello1k\x00\x03\x01\x02\x03",
            encode(Map(hello1=List([1, 2, 3])))
        )
Example #4
0
    def test_decode_map(self):
        self.assertEqual(
            (Map({"hello": "world"}), b""),
            decode(
                b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05hellom\x00\x00\x00\x05world"
            ))

        self.assertEqual(
            (Map({"hello": "world"}), b"rest"),
            decode(
                b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05hellom\x00\x00\x00\x05worldrest"
            ))

        self.assertEqual(
            (Map({"hello": List()}), b""),
            decode(b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05helloj"))

        self.assertRaises(IncompleteData, decode, b"\x83t\x00\x00\x00")
Example #5
0
 def test_hash_list(self):
     input = Map({List([1, 2, 3]): [4, 5, 6]})
     self.assertEquals(
         input[List([1, 2, 3])],
         List([4, 5, 6])
     )
     self.assertEquals(
         (input, b""),
         decode(encode(input))
     )
Example #6
0
    def test_hash_improper_list(self):
        input = Map({
            ImproperList([1, 2, 3], 1000): b"xxx"
        })

        self.assertEquals(
            (input, ""),
            decode(encode(input))
        )

        self.assertEquals(
            input[ImproperList([1, 2, 3], 1000)], b"xxx")
Example #7
0
    def test_empty_map(self):
        input = Map()
        self.assertEquals(
            (input, b""),
            decode(encode(input))
        )

        input = {}
        self.assertEquals(
            (input, b""),
            decode(encode(input))
        )
Example #8
0
    def test_decode_map(self):
        self.assertEqual(
            (Map(hello="world"), ""),
            decode(
                "\x83t\x00\x00\x00\x01m\x00\x00\x00\x05"
                "hellom\x00\x00\x00\x05world")
        )

        self.assertEqual(
            (Map(hello="world"), "rest"),
            decode(
                "\x83t\x00\x00\x00\x01m\x00\x00\x00\x05"
                "hellom\x00\x00\x00\x05worldrest")
        )

        self.assertEqual(
            (Map(hello=List()), ""),
            decode("\x83t\x00\x00\x00\x01m\x00\x00\x00\x05helloj")
        )

        self.assertRaises(IncompleteData, decode, "\x83t\x00\x00\x00")
Example #9
0
 def test_construct(self):
     m = Map(x=[1, 2, 3])
     self.assertTrue(isinstance(m[b"x"], List))
Example #10
0
 def test_immutable(self):
     m = Map({"bx": 100})
     self.assertRaises(MutationError, m.pop)
Example #11
0
 def test_simple_map(self):
     input = Map({b"hello": b"world"})
     self.assertEquals(
         (input, b""),
         decode(encode(input))
     )
Example #12
0
 def test_encode_empty_map(self):
     self.assertEqual(b"\x83t\x00\x00\x00\x00", encode(Map()))
Example #13
0
 def test_decode_empty_map(self):
     self.assertEqual((Map(), b""), decode(b"\x83t\x00\x00\x00\x00"))