From 048d1d2cd3f5544ab1d5dc89dd31143119023798 Mon Sep 17 00:00:00 2001 From: Gabriel Pettier Date: Mon, 3 Sep 2018 01:36:51 +0200 Subject: [PATCH] experiment with using hypothesis to find bugs --- setup.py | 2 +- tests/test_parser.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 93135b3..936f80f 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ packages=find_packages(exclude=['contrib', 'docs', 'tests']), install_requires=[], extras_require={ - 'dev': ['pytest', 'wheel', 'pytest-cov', 'pycodestyle'], + 'dev': ['pytest', 'wheel', 'pytest-cov', 'pycodestyle', 'hypothesis'], 'travis': ['coveralls'], }, package_data={}, diff --git a/tests/test_parser.py b/tests/test_parser.py index 107adf1..f01e57c 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -5,6 +5,10 @@ format_message, format_bundle, timetag_to_time, time_to_timetag ) from pytest import approx, raises +from hypothesis import given, example, assume, settings, Verbosity +from hypothesis.strategies import \ + text, binary, floats, integers, lists, tuples, choices, one_of + from time import time import struct @@ -239,3 +243,29 @@ def test_format_encoding(): format_message('/test', [s], encoding='utf8'), encoding='ascii', encoding_errors='replace' )[2][0] == u'������������' + + +@given( + lists( + one_of( + binary(), + floats( + allow_nan=False, + min_value=-2147483647, + max_value=2147483647 + ), + integers( + min_value=-2147483648, + max_value=2147483647 + ) + ), + ) +) +def test_decode_encoded(args): + for x in args: + assume(not (isinstance(x, bytes) and b'\x00' in x)) + + assert read_message(format_message(b'/test', values=args))[2] == [ + approx(x) if isinstance(x, float) else x + for x in args + ]