Skip to content

Commit

Permalink
Delete actually_equal
Browse files Browse the repository at this point in the history
I love deleting code so much.
  • Loading branch information
DRMacIver committed Mar 18, 2015
1 parent 550570a commit b1c715c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 492 deletions.
18 changes: 6 additions & 12 deletions src/hypothesis/descriptortests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from hypothesis.database import ExampleDatabase
from hypothesis.settings import Settings
from hypothesis.internal.compat import text_type, integer_types
from hypothesis.internal.fixers import nice_string, actually_equal
from hypothesis.internal.fixers import nice_string
from hypothesis.database.backend import SQLiteBackend
from hypothesis.internal.verifier import Verifier
from hypothesis.searchstrategy.strategies import BuildContext, \
Expand Down Expand Up @@ -95,13 +95,6 @@ def __repr__(self):
def test_does_not_error(self, value):
pass

@descriptor_test
def test_two_reifications_are_equal(self, template):
assert actually_equal(
strat.reify(template),
strat.reify(template),
)

def test_can_give_example(self):
strat.example()

Expand All @@ -126,7 +119,7 @@ def test_can_round_trip_through_the_database(self, template):
storage.save(template)
values = list(storage.fetch())
assert len(values) == 1
assert actually_equal(template, values[0])
assert repr(template) == repr(values[0])

@descriptor_test
def test_template_is_hashable(self, template):
Expand All @@ -148,9 +141,10 @@ def test_can_perform_all_basic_operations(self, random):
lambda x: True
))[-1]
strat.reify(minimal_template)
assert actually_equal(
minimal_template,
strat.from_basic(strat.to_basic(minimal_template))
assert (
strat.to_basic(minimal_template) ==
strat.to_basic(
strat.from_basic(strat.to_basic(minimal_template)))
)
list(strat.decompose(minimal_template))

Expand Down
120 changes: 4 additions & 116 deletions src/hypothesis/internal/fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,122 +29,6 @@
from hypothesis.utils.extmethod import ExtMethod


class Equality(ExtMethod):

def __call__(self, x, y, fuzzy=False):
if x is y:
return True
if type(x) != type(y):
return False
return super(Equality, self).__call__(x, y, fuzzy)


equal = Equality()


primitives = [
int, float, bool, type, text_type, binary_type
] + list(integer_types)


@equal.extend(object)
def generic_equal(x, y, fuzzy):
try:
if len(x) != len(y):
return False
except (TypeError, AttributeError):
pass
try:
iter(x)
iter(y)
except TypeError:
return x == y
return actually_equal(
tuple(x), tuple(y), fuzzy
)


@equal.extend(int)
@equal.extend(bool)
@equal.extend(type)
@equal.extend(text_type)
@equal.extend(binary_type)
def primitive_equal(x, y, fuzzy):
return x == y


@equal.extend(float)
def float_equal(x, y, fuzzy=False):
if math.isnan(x) and math.isnan(y):
return True
if x == y:
return True
return fuzzy and (repr(x) == repr(y))


@equal.extend(complex)
def complex_equal(x, y, fuzzy=False):
return (
float_equal(x.real, y.real, fuzzy) and
float_equal(x.imag, y.imag, fuzzy)
)


@equal.extend(tuple)
@equal.extend(list)
def sequence_equal(x, y, fuzzy=False):
if len(x) != len(y):
return False
for u, v in zip(x, y):
if not actually_equal(u, v, fuzzy):
return False
return True


@equal.extend(set)
@equal.extend(frozenset)
def set_equal(x, y, fuzzy=False):
if len(x) != len(y):
return False
for u in x:
if not actually_in(u, y):
return False
return True


@equal.extend(dict)
def dict_equal(x, y, fuzzy=False):
if len(x) != len(y):
return False
for k, v in x.items():
if k not in y:
return False
if not actually_equal(x[k], y[k], fuzzy):
return False
return True


def actually_equal(x, y, fuzzy=False):
return equal(x, y, fuzzy)


def actually_in(x, ys, fuzzy=False):
return any(actually_equal(x, y, fuzzy) for y in ys)


def real_index(value, y, fuzzy=False):
i = 0
while i < len(value):
if actually_equal(value[i], y, fuzzy):
return i
i += 1
raise ValueError('%r is not in list %r' % (y, value))


def is_nasty_float(x):
return math.isnan(x) or math.isinf(x)


class IdKey(object):

def __init__(self, value):
Expand Down Expand Up @@ -249,6 +133,10 @@ def type_string(value, seen):
return value.__name__


def is_nasty_float(x):
return math.isnan(x) or math.isinf(x)


@nice_string.extend(float)
def float_string(value, seen):
if is_nasty_float(value):
Expand Down
4 changes: 3 additions & 1 deletion src/hypothesis/searchstrategy/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ def to_basic(self, value):
check_type(frozenset, value)
if not self.descriptor:
return []
return list(map(self.element_strategy.to_basic, value))
result = list(map(self.element_strategy.to_basic, value))
result.sort()
return result

def from_basic(self, value):
if not self.descriptor:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from hypothesis.database import ExampleDatabase
from tests.common.descriptors import DescriptorWithValue
from hypothesis.internal.compat import text_type, integer_types
from hypothesis.internal.fixers import actually_equal
from hypothesis.database.backend import Backend, SQLiteBackend
from hypothesis.database.formats import Format, JSONFormat
from hypothesis.internal.verifier import Verifier
Expand All @@ -42,7 +41,7 @@ def run_round_trip(descriptor, value, format=None, backend=None):
storage = db.storage_for(descriptor)
storage.save(value)
saved = list(storage.fetch())
assert actually_equal(saved, [value])
assert repr(saved) == repr([value])


class InMemoryBackend(Backend):
Expand Down

1 comment on commit b1c715c

@rboulton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good red:green ratio.

Please sign in to comment.