summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2007-11-17 18:21:37 -0200
committerGustavo Niemeyer <gustavo@niemeyer.net>2007-11-17 18:21:37 -0200
commitf208f3845c4ab36caae331fe1f756bd2d7ee07a8 (patch)
treefa5abd06a0ff06b7fd91208a80e278348909b681
parent745fc9a11340f192e2f73c0f1f3101866d26f74f (diff)
downloadmocker-f208f3845c4ab36caae331fe1f756bd2d7ee07a8.tar.gz
Added MockerTestCase.assert[Not]{Starts,Ends}With().
-rw-r--r--NEWS2
-rw-r--r--mocker.py28
-rwxr-xr-xtest.py120
3 files changed, 150 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 8d43cb5..81160de 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@
they accept the same arguments. This is useful to verify if a fake
or stub class have the same API as the real class being simulated.
+- Added MockerTestCase.assert[Not]{Starts,Ends}With().
+
- If the replay() method is called twice, expectations will be fully
reset so that several similar tests may be performed in a row by
just calling replay() again.
diff --git a/mocker.py b/mocker.py
index 7d7160c..2662ec3 100644
--- a/mocker.py
+++ b/mocker.py
@@ -174,6 +174,30 @@ class MockerTestCase(unittest.TestCase):
if first not in second:
raise self.failureException(msg or "%r not in %r" % (first, second))
+ def failUnlessStartsWith(self, first, second, msg=None):
+ """Assert that C{first} starts with C{second}."""
+ if first[:len(second)] != second:
+ raise self.failureException(msg or "%r doesn't start with %r" %
+ (first, second))
+
+ def failIfStartsWith(self, first, second, msg=None):
+ """Assert that C{first} doesn't start with C{second}."""
+ if first[:len(second)] == second:
+ raise self.failureException(msg or "%r starts with %r" %
+ (first, second))
+
+ def failUnlessEndsWith(self, first, second, msg=None):
+ """Assert that C{first} starts with C{second}."""
+ if first[len(first)-len(second):] != second:
+ raise self.failureException(msg or "%r doesn't end with %r" %
+ (first, second))
+
+ def failIfEndsWith(self, first, second, msg=None):
+ """Assert that C{first} doesn't start with C{second}."""
+ if first[len(first)-len(second):] == second:
+ raise self.failureException(msg or "%r ends with %r" %
+ (first, second))
+
def failIfIn(self, first, second, msg=None):
"""Assert that C{first} is not contained in C{second}."""
if first in second:
@@ -229,6 +253,10 @@ class MockerTestCase(unittest.TestCase):
assertIsNot = failIfIs
assertIn = failUnlessIn
assertNotIn = failIfIn
+ assertStartsWith = failUnlessStartsWith
+ assertNotStartsWith = failIfStartsWith
+ assertEndsWith = failUnlessEndsWith
+ assertNotEndsWith = failIfEndsWith
assertApproximates = failUnlessApproximates
assertNotApproximates = failIfApproximates
assertMethodsMatch = failUnlessMethodsMatch
diff --git a/test.py b/test.py
index 62c1230..dd499f7 100755
--- a/test.py
+++ b/test.py
@@ -437,6 +437,114 @@ class MockerTestCaseTest(unittest.TestCase):
except AssertionError:
self.fail("AssertionError shouldn't be raised")
+ def test_fail_unless_starts_with_raises_on_mismatch(self):
+ try:
+ self.test.failUnlessStartsWith("abc", "def")
+ except AssertionError, e:
+ self.assertEquals(str(e), "'abc' doesn't start with 'def'")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_unless_starts_with_uses_msg(self):
+ try:
+ self.test.failUnlessStartsWith("abc", "def", "oops!")
+ except AssertionError, e:
+ self.assertEquals(str(e), "oops!")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_unless_starts_with_succeeds(self):
+ try:
+ self.test.failUnlessStartsWith("abcdef", "abc")
+ except AssertionError:
+ self.fail("AssertionError shouldn't be raised")
+
+ def test_fail_unless_starts_with_works_with_non_strings(self):
+ self.test.failUnlessStartsWith([1, 2, 3], [1, 2])
+ self.assertRaises(AssertionError,
+ self.test.failUnlessStartsWith, [1, 2, 3], [4, 5, 6])
+
+ def test_fail_if_starts_with_raises_on_mismatch(self):
+ try:
+ self.test.failIfStartsWith("abcdef", "abc")
+ except AssertionError, e:
+ self.assertEquals(str(e), "'abcdef' starts with 'abc'")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_if_starts_with_uses_msg(self):
+ try:
+ self.test.failIfStartsWith("abcdef", "abc", "oops!")
+ except AssertionError, e:
+ self.assertEquals(str(e), "oops!")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_if_starts_with_succeeds(self):
+ try:
+ self.test.failIfStartsWith("abc", "def")
+ except AssertionError:
+ self.fail("AssertionError shouldn't be raised")
+
+ def test_fail_if_starts_with_works_with_non_strings(self):
+ self.test.failIfStartsWith([1, 2, 3], [4, 5, 6])
+ self.assertRaises(AssertionError,
+ self.test.failIfStartsWith, [1, 2, 3], [1, 2])
+
+ def test_fail_unless_ends_with_raises_on_mismatch(self):
+ try:
+ self.test.failUnlessEndsWith("abc", "def")
+ except AssertionError, e:
+ self.assertEquals(str(e), "'abc' doesn't end with 'def'")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_unless_ends_with_uses_msg(self):
+ try:
+ self.test.failUnlessEndsWith("abc", "def", "oops!")
+ except AssertionError, e:
+ self.assertEquals(str(e), "oops!")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_unless_ends_with_succeeds(self):
+ try:
+ self.test.failUnlessEndsWith("abcdef", "def")
+ except AssertionError:
+ self.fail("AssertionError shouldn't be raised")
+
+ def test_fail_unless_ends_with_works_with_non_strings(self):
+ self.test.failUnlessEndsWith([1, 2, 3], [2, 3])
+ self.assertRaises(AssertionError,
+ self.test.failUnlessEndsWith, [1, 2, 3], [4, 5, 6])
+
+ def test_fail_if_ends_with_raises_on_mismatch(self):
+ try:
+ self.test.failIfEndsWith("abcdef", "def")
+ except AssertionError, e:
+ self.assertEquals(str(e), "'abcdef' ends with 'def'")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_if_ends_with_uses_msg(self):
+ try:
+ self.test.failIfEndsWith("abcdef", "def", "oops!")
+ except AssertionError, e:
+ self.assertEquals(str(e), "oops!")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_if_ends_with_succeeds(self):
+ try:
+ self.test.failIfEndsWith("abc", "def")
+ except AssertionError:
+ self.fail("AssertionError shouldn't be raised")
+
+ def test_fail_if_ends_with_works_with_non_strings(self):
+ self.test.failIfEndsWith([1, 2, 3], [4, 5, 6])
+ self.assertRaises(AssertionError,
+ self.test.failIfEndsWith, [1, 2, 3], [2, 3])
+
def test_fail_unless_approximates_raises_on_mismatch(self):
try:
self.test.failUnlessApproximates(1, 2, 0.999)
@@ -555,6 +663,18 @@ class MockerTestCaseTest(unittest.TestCase):
self.assertEquals(get_method("assertNotIn"),
get_method("failIfIn"))
+ self.assertEquals(get_method("assertStartsWith"),
+ get_method("failUnlessStartsWith"))
+
+ self.assertEquals(get_method("assertNotStartsWith"),
+ get_method("failIfStartsWith"))
+
+ self.assertEquals(get_method("assertEndsWith"),
+ get_method("failUnlessEndsWith"))
+
+ self.assertEquals(get_method("assertNotEndsWith"),
+ get_method("failIfEndsWith"))
+
self.assertEquals(get_method("assertApproximates"),
get_method("failUnlessApproximates"))