From 054a3c2e91fa07fed7459702f1bb329fe4431240 Mon Sep 17 00:00:00 2001 From: Fred Thomsen Date: Sun, 9 Apr 2017 22:27:07 -0400 Subject: [PATCH 1/2] Use hypothesis to test h2 settings equality --- test/test_settings.py | 150 ++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 102 deletions(-) diff --git a/test/test_settings.py b/test/test_settings.py index 2163bb9b8..d7624a500 100644 --- a/test/test_settings.py +++ b/test/test_settings.py @@ -12,7 +12,9 @@ import h2.settings from hypothesis import given, assume -from hypothesis.strategies import integers +from hypothesis.strategies import ( + integers, booleans, fixed_dictionaries, builds +) class TestSettings(object): @@ -367,125 +369,69 @@ class TestSettingsEquality(object): A class defining tests for the standard implementation of == and != . """ - def an_instance(self): - """ - Return an instance of the class under test. Each call to this method - must return a different object. All objects returned must be equal to - each other. - """ - overrides = { - h2.settings.SettingCodes.HEADER_TABLE_SIZE: 0, - h2.settings.SettingCodes.MAX_FRAME_SIZE: 16384, - h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 4, - h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16, - } - return h2.settings.Settings(client=True, initial_values=overrides) - - def another_instance(self): - """ - Return an instance of the class under test. Each call to this method - must return a different object. The objects must not be equal to the - objects returned by an_instance. They may or may not be equal to - each other (they will not be compared against each other). - """ - overrides = { - h2.settings.SettingCodes.HEADER_TABLE_SIZE: 8080, - h2.settings.SettingCodes.MAX_FRAME_SIZE: 16388, - h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 100, - h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16, - } - return h2.settings.Settings(client=False, initial_values=overrides) - - def test_identical_eq(self): - """ - An object compares equal to itself using the == operator. - """ - o = self.an_instance() - assert (o == o) - - def test_identical_ne(self): - """ - An object doesn't compare not equal to itself using the != operator. - """ - o = self.an_instance() - assert not (o != o) - - def test_same_eq(self): - """ - Two objects that are equal to each other compare equal to each other - using the == operator. - """ - a = self.an_instance() - b = self.an_instance() - assert (a == b) - - def test_same_ne(self): - """ - Two objects that are equal to each other do not compare not equal to - each other using the != operator. - """ - a = self.an_instance() - b = self.an_instance() - assert not (a != b) + SettingsStrategy = builds( + h2.settings.Settings, + client=booleans(), + initial_values=fixed_dictionaries({ + h2.settings.SettingCodes.HEADER_TABLE_SIZE: + integers(0, 2**32 - 1), + h2.settings.SettingCodes.ENABLE_PUSH: integers(0, 1), + h2.settings.SettingCodes.INITIAL_WINDOW_SIZE: + integers(0, 2**31 - 1), + h2.settings.SettingCodes.MAX_FRAME_SIZE: + integers(2**14, 2**24 - 1), + h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: + integers(0, 2**32 - 1), + h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: + integers(0, 2**32 - 1), + }) + ) - def test_different_eq(self): + @given(settings=SettingsStrategy) + def test_equality_reflexive(self, settings): """ - Two objects that are not equal to each other do not compare equal to - each other using the == operator. + An object compares equal to itself using the == operator and the != + operator. """ - a = self.an_instance() - b = self.another_instance() - assert not (a == b) + assert (settings == settings) + assert not (settings != settings) - def test_different_ne(self): + @given(settings=SettingsStrategy, o_settings=SettingsStrategy) + def test_equality_multiple(self, settings, o_settings): """ - Two objects that are not equal to each other compare not equal to each - other using the != operator. + Two objects compare themselves using the == operator and the != + operator. """ - a = self.an_instance() - b = self.another_instance() - assert (a != b) + if settings == o_settings: + assert settings == o_settings + assert not (settings != o_settings) + else: + assert settings != o_settings + assert not (settings == o_settings) - def test_another_type_eq(self): + @given(settings=SettingsStrategy) + def test_another_type_equality(self, settings): """ The object does not compare equal to an object of an unrelated type (which does not implement the comparison) using the == operator. """ - a = self.an_instance() - b = object() - assert not (a == b) - - def test_another_type_ne(self): - """ - The object compares not equal to an object of an unrelated type (which - does not implement the comparison) using the != operator. - """ - a = self.an_instance() - b = object() - assert (a != b) + obj = object() + assert (settings != obj) + assert not (settings == obj) - def test_delegated_eq(self): + @given(settings=SettingsStrategy) + def test_delegated_eq(self, settings): """ - The result of comparison using == is delegated to the right-hand - operand if it is of an unrelated type. + The result of comparison is delegated to the right-hand operand if + it is of an unrelated type. """ class Delegate(object): def __eq__(self, other): return [self] - a = self.an_instance() - b = Delegate() - assert (a == b) == [b] - - def test_delegate_ne(self): - """ - The result of comparison using != is delegated to the right-hand - operand if it is of an unrelated type. - """ - class Delegate(object): def __ne__(self, other): return [self] - a = self.an_instance() - b = Delegate() - assert (a != b) == [b] + delg = Delegate() + assert (settings == delg) == [delg] + assert (settings != delg) == [delg] From 005d6fde9294b97fa63373ff7a36c2c40b1d23bf Mon Sep 17 00:00:00 2001 From: Fred Thomsen Date: Sat, 13 May 2017 16:02:24 -0400 Subject: [PATCH 2/2] Shameless plug --- CONTRIBUTORS.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index bdc4d06c3..5c4849fef 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -111,4 +111,5 @@ In chronological order: - Fred Thomsen (@fredthomsen) - Added logging. - + - Enhance equality testing of ``h2.settings.Settings`` objects with + ``hypothesis``.