From f208f3845c4ab36caae331fe1f756bd2d7ee07a8 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Sat, 17 Nov 2007 18:21:37 -0200 Subject: Added MockerTestCase.assert[Not]{Starts,Ends}With(). --- NEWS | 2 ++ mocker.py | 28 +++++++++++++++ test.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) 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")) -- cgit v1.2.1