From 9c684f12e1050d8b0dce9cb1e66eb2de0c5f3be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Sat, 16 Jan 2021 19:47:05 +0000 Subject: Improve __repr__ (#23) So we get something like: BlockerResult(matched=false, important=false, redirect=None, exception=None, filter=None, error=None) rather than: BlockerResult(false, false, None, None, None, None) Co-authored-by: Florian Bruhin --- tests/test_repr.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/test_repr.py (limited to 'tests/test_repr.py') diff --git a/tests/test_repr.py b/tests/test_repr.py new file mode 100644 index 0000000..9c1f961 --- /dev/null +++ b/tests/test_repr.py @@ -0,0 +1,37 @@ +import adblock +import re + + +def assert_acceptable_repr(obj): + # Default repr is r"<[A-Za-z]+ object at 0x[0-9a-f]+>" + assert "object at" not in repr(obj) + assert re.match(r"[A-Z][a-zA-Z]+\(.*\)", repr(obj)) or re.match( + r"([A-Z][a-zA-Z]+)?<.*>", repr(obj) + ) + + +def test_has_nondefault_repr(): + for b in (True, False): + fs = adblock.FilterSet(debug=b) + assert_acceptable_repr(fs) + assert repr(b) in repr(fs) + + fs.add_filters(["||example.com^"]) + + e = adblock.Engine(fs) + assert_acceptable_repr(e) + + result = e.check_network_urls( + "https://example.com/picture.png", "https://example.net", "image" + ) + assert_acceptable_repr(result) + assert repr(result) == ( + "BlockerResult(matched={}, important={}, redirect={}, exception={}, filter={}, error={})".format( + repr(result.matched), + repr(result.important), + repr(result.redirect), + repr(result.exception), + repr(result.filter), + repr(result.error), + ) + ) -- cgit v1.2.3