Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_bencode.py
- This file was added.
# Copyright (C) 2019 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
"""The examples are from the specification: | |||||
https://wiki.theory.org/index.php/BitTorrentSpecification#Bencoding""" | |||||
import pytest | |||||
from swh.core.bencode import encode | |||||
douardda: why not using pytest's test functions here? What's the value of using a TestCase? | |||||
Done Inline ActionsI use assertRaises. vlorentz: I use `assertRaises`. | |||||
def test_bencode_bytes(): | |||||
assert b'4:spam' == encode(b'spam') | |||||
assert b'0:' == encode(b'') | |||||
def test_encode_int(): | |||||
assert b'i3e' == encode(3) | |||||
assert b'i-3e' == encode(-3) | |||||
assert b'i0e' == encode(0) | |||||
def test_encode_list(): | |||||
assert b'l4:spam4:eggse' == encode([b'spam', b'eggs']) | |||||
assert b'le' == encode([]) | |||||
def test_encode_dict(): | |||||
assert \ | |||||
b'd3:cow3:moo4:spam4:eggse' == \ | |||||
encode({b'cow': b'moo', b'spam': b'eggs'}) | |||||
assert \ | |||||
b'd4:spaml1:a1:bee' == \ | |||||
encode({b'spam': [b'a', b'b']}) | |||||
assert \ | |||||
(b'd9:publisher3:bob17:publisher-webpage15:www.example.com' | |||||
b'18:publisher.location4:homee') == \ | |||||
encode({b'publisher': b'bob', | |||||
b'publisher-webpage': b'www.example.com', | |||||
b'publisher.location': b'home'}) | |||||
assert b'de' == encode({}) | |||||
def test_invalid_root_type(): | |||||
with pytest.raises(TypeError): | |||||
encode('string') | |||||
def test_invalid_dict_key_type(): | |||||
with pytest.raises(TypeError): | |||||
encode({'string': b'bytes'}) |
why not using pytest's test functions here? What's the value of using a TestCase?