Skip to content

Commit

Permalink
Renaming be_almost_equal to almost_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvrbanac committed May 20, 2016
1 parent 802db97 commit 4887ccb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/writing_tests/index.rst
Expand Up @@ -161,9 +161,9 @@ If you were expecting a status_code object was equal to 200 you would write:
Available Comparisons
^^^^^^^^^^^^^^^^^^^^^^^
* equal(expected_object)
* almost_equal(expected_number, places)
* be_greater_than(expected_object)
* be_less_than(expected_object)
* be_almost_equal(expected_number, places)
* be_none()
* be_true()
* be_false()
Expand Down
13 changes: 9 additions & 4 deletions specter/expect.py
Expand Up @@ -69,10 +69,15 @@ def equal(self, expected):
self._compare(action_name=_('equal'), expected=expected,
condition=self.target == expected)

def be_almost_equal(self, expected, places=None):
places = places or 7
self._compare(action_name=_('be almost equal within {} places'), expected=expected,
condition=round(abs(self.target - expected), places) == 0)
def almost_equal(self, expected, places=7):
if not isinstance(places, int):
raise TypeError('Places must be an integer')

self._compare(
action_name=_('almost equal'),
expected=expected,
condition=round(abs(self.target - expected), places) == 0
)

def be_greater_than(self, expected):
self._compare(action_name=_('be greater than'), expected=expected,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_expect.py
Expand Up @@ -45,19 +45,19 @@ def test_expect_not_to_equal(self):
def test_expect_almost_equal(self):
target = 0.010000001
expect = self._create_assert(target)
expect.to.be_almost_equal(0.01)
expect.to.almost_equal(0.01)
self.assertTrue(expect.success)

def test_expect_almost_equal_with_places(self):
target = 0.011
expect = self._create_assert(target)
expect.to.be_almost_equal(0.01, places=2)
expect.to.almost_equal(0.01, places=2)
self.assertTrue(expect.success)

def test_expect_not_to_almost_equal(self):
target = 0.01
expect = self._create_assert(target)
expect.not_to.be_almost_equal(0.02)
expect.not_to.almost_equal(0.02)
self.assertTrue(expect.success)

def test_expect_greater_than(self):
Expand Down

0 comments on commit 4887ccb

Please sign in to comment.