Skip to content

Instantly share code, notes, and snippets.

@markrwilliams
Created June 6, 2017 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrwilliams/1cbe9114ea6c9120ceb2b1bb91434cc3 to your computer and use it in GitHub Desktop.
Save markrwilliams/1cbe9114ea6c9120ceb2b1bb91434cc3 to your computer and use it in GitHub Desktop.
import unittest
from hypothesis import given, strategies as st
class ComparisonTests(unittest.TestCase):
def assertNonesCauseTypeError(self, first, second):
try:
first > second
except TypeError:
comparison_raised = True
else:
comparison_raised = False
try:
for i in range(min(len(first), len(second))):
if first[i] > second[i] or first[i] < second[i]:
break
except TypeError:
iteration_raised = True
else:
iteration_raised = False
self.assertEqual(comparison_raised, iteration_raised,
"comparison_raised={!r} "
"iteration_raised={!r} "
"{!r} {!r}".format(comparison_raised,
iteration_raised,
first, second))
@given(
first=st.lists(st.just(None) | st.integers()).map(tuple),
second=st.lists(st.just(None) | st.integers()).map(tuple),
)
def test_tuple_comparison(self, first, second):
self.assertNonesCauseTypeError(first, second)
@given(
first=st.lists(st.just(None) | st.integers()),
second=st.lists(st.just(None) | st.integers()),
)
def test_list_comparison(self, first, second):
self.assertNonesCauseTypeError(first, second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment