From 03620da7ff3e99c7b506cf701ad4c28e631da811 Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Wed, 4 Jul 2012 19:04:32 +0100 Subject: Add a matcher, because I can't bear to write that code again. --- lib/fixtures/tests/_fixtures/test_tempdir.py | 5 +++-- lib/fixtures/tests/_fixtures/test_temphomedir.py | 5 +++-- lib/fixtures/tests/helpers.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py index 1e42257..5fd5a46 100644 --- a/lib/fixtures/tests/_fixtures/test_tempdir.py +++ b/lib/fixtures/tests/_fixtures/test_tempdir.py @@ -23,13 +23,14 @@ from fixtures import ( NestedTempfile, TempDir, ) +from fixtures.tests.helpers import NotHasattr + class TestTempDir(testtools.TestCase): def test_basic(self): fixture = TempDir() - sentinel = object() - self.assertEqual(sentinel, getattr(fixture, 'path', sentinel)) + self.assertThat(fixture, NotHasattr('path')) fixture.setUp() try: path = fixture.path diff --git a/lib/fixtures/tests/_fixtures/test_temphomedir.py b/lib/fixtures/tests/_fixtures/test_temphomedir.py index 339ce2c..5e0db9a 100644 --- a/lib/fixtures/tests/_fixtures/test_temphomedir.py +++ b/lib/fixtures/tests/_fixtures/test_temphomedir.py @@ -22,13 +22,14 @@ from fixtures import ( TempDir, TempHomeDir, ) +from fixtures.tests.helpers import NotHasattr + class TestTempDir(testtools.TestCase): def test_basic(self): fixture = TempHomeDir() - sentinel = object() - self.assertEqual(sentinel, getattr(fixture, 'path', sentinel)) + self.assertThat(fixture, NotHasattr('path')) fixture.setUp() try: path = fixture.path diff --git a/lib/fixtures/tests/helpers.py b/lib/fixtures/tests/helpers.py index ae0d8d3..680d396 100644 --- a/lib/fixtures/tests/helpers.py +++ b/lib/fixtures/tests/helpers.py @@ -15,6 +15,9 @@ import fixtures +from testtools.matchers import Mismatch + + class LoggingFixture(fixtures.Fixture): def __init__(self, suffix='', calls=None): @@ -31,3 +34,21 @@ class LoggingFixture(fixtures.Fixture): def reset(self): self.calls.append('reset' + self.suffix) + + +class NotHasattr(object): + """For asserting that an object does not have a particular attribute.""" + + def __init__(self, attr_name): + self._attr_name = attr_name + + def __str__(self): + return 'NotHasattr(%s)' % (self._attr_name,) + + def match(self, obj): + sentinel = object() + value = getattr(obj, self._attr_name, sentinel) + if value is not sentinel: + return Mismatch( + "%s is an attribute of %r: %r" % ( + self._attr_name, obj, value)) -- cgit v1.2.1