示例#1
0
    def test_decode_list(self):
        decoder1 = bencoding.BencodeDecoder(b"l4:spam4:eggse").decode()
        decoder2 = bencoding.BencodeDecoder(b"le").decode()

        self.assertEqual(2, len(decoder1))
        self.assertEqual(b"spam", decoder1[0])
        self.assertEqual(b"eggs", decoder1[1])
        self.assertEqual(0, len(decoder2))
        self.assertEqual([], decoder2)
示例#2
0
    def test_not_bytes_string(self):
        with self.assertRaises(TypeError):
            bencoding.BencodeDecoder("abc")

        with self.assertRaises(TypeError):
            bencoding.BencodeDecoder(123)

        with self.assertRaises(TypeError):
            bencoding.BencodeDecoder([1, 2, 3])
示例#3
0
    def test_decode_dict(self):
        decoder = bencoding.BencodeDecoder(
            b"d9:publisher3:bob17:publisher-webpage15:www.example.come"
        ).decode()

        self.assertIsInstance(decoder, OrderedDict)
        self.assertEqual(decoder[b"publisher"], b"bob")
        self.assertEqual(decoder[b"publisher-webpage"], b"www.example.com")
示例#4
0
    def test_peek_existing(self):
        decoder = bencoding.BencodeDecoder(b"abc")

        self.assertEqual(b"a", decoder._peek())
示例#5
0
    def test_common_decode(self):
        res = bencoding.BencodeDecoder(
            b"d3:catl3:Tom4:Jacke3:agei8ee").decode()

        self.assertEqual(res[b"cat"], [b"Tom", b"Jack"])
        self.assertEqual(res[b"age"], 8)
示例#6
0
    def test__decode_bytes_string(self):
        decoder1 = bencoding.BencodeDecoder(b"4:abcd")
        decoder2 = bencoding.BencodeDecoder(b"0:")

        self.assertEqual(decoder1.decode(), b"abcd")
        self.assertEqual((decoder2.decode()), b"")
示例#7
0
    def test_decode_integer(self):
        decoder = bencoding.BencodeDecoder(b"i123e")

        self.assertEqual(123, decoder.decode())
示例#8
0
    def test_increase_correct(self):
        decoder = bencoding.BencodeDecoder(b"abc")
        decoder._increase_index()

        self.assertEqual(1, decoder._index)
示例#9
0
    def test_peek_none(self):
        decoder = bencoding.BencodeDecoder(b"abc")
        decoder._index += 3

        self.assertEqual(None, decoder._peek())