From 0eb9e9731e8f998abe8364fab25f6da01d57e93a Mon Sep 17 00:00:00 2001 From: Johann Bauer Date: Sun, 13 Nov 2016 21:46:19 +0100 Subject: [PATCH] Add unit tests for the methods in util --- tests/test_util.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/test_util.py diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..0e92deb --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,48 @@ +from lib import util + + +def test_cachedproperty(): + class Target: + def __init__(self): + self.call_count = 0 + + @util.cachedproperty + def prop(self): + self.call_count += 1 + return self.call_count + + t = Target() + assert t.prop == t.prop == 1 + + +def test_deep_getsizeof(): + int_t = util.deep_getsizeof(1) + assert util.deep_getsizeof([1, 1]) > 2 * int_t + assert util.deep_getsizeof({1: 1}) > 2 * int_t + assert util.deep_getsizeof({1: {1: 1}}) > 3 * int_t + + +class Base: + pass + + +class A(Base): + pass + + +class B(Base): + pass + + +def test_subclasses(): + assert util.subclasses(Base) == [A, B] + + +def test_chunks(): + assert list(util.chunks([1, 2, 3, 4, 5], 2)) == [[1, 2], [3, 4], [5]] + + +def test_increment_byte_string(): + assert util.increment_byte_string(b'1') == b'2' + assert util.increment_byte_string(b'\x01\x01') == b'\x01\x02' + assert util.increment_byte_string(b'\xff\xff') == b'\x01\x00\x00'