40e13965af72

tests: factor out the exception checking
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 19 Jun 2010 02:22:48 -0400
parents e29d33ec47b1
children 3de7cde04f0b
branches/tags (none)
files review/tests/test_check.py review/tests/test_comment.py review/tests/test_delete.py review/tests/test_init.py review/tests/test_signoff.py review/tests/util.py

Changes

--- a/review/tests/test_check.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/test_check.py	Sat Jun 19 02:22:48 2010 -0400
@@ -1,22 +1,7 @@
 from nose import *
 from util import *
-from mercurial import util as hgutil
 from .. import messages
 
-BAD_ERROR = 'The correct error message was not printed.'
-def _check_e(e, m):
-    error = str(e)
-    assert m in e
-
-def _should_fail_with(m, **kwargs):
-    try:
-        output = review(**kwargs)
-    except hgutil.Abort, e:
-        _check_e(e, m)
-    else:
-        assert False, BAD_ERROR
-
-
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_check_empty():
     review(check=True)
@@ -61,7 +46,7 @@
     assert messages.CHECK_SUCCESS in output
 
     review(signoff=True, no=True, message='.')
-    _should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True)
+    should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True)
 
     review(signoff=True, yes=True, message='.', force=True)
     output = review(check=True, verbose=True, no_nos=True)
@@ -69,7 +54,7 @@
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_check_yeses():
-    _should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='1')
+    should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='1')
 
     output = review(check=True, verbose=True, yeses='0')
     assert messages.CHECK_SUCCESS in output
@@ -78,11 +63,11 @@
     output = review(check=True, verbose=True, yeses='1')
     assert messages.CHECK_SUCCESS in output
 
-    _should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='2')
+    should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='2')
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_check_seen():
-    _should_fail_with(messages.CHECK_UNSEEN, check=True, verbose=True, seen=True)
+    should_fail_with(messages.CHECK_UNSEEN, check=True, verbose=True, seen=True)
 
     review(signoff=True, yes=True, message='.')
     output = review(check=True, verbose=True, seen=True)
@@ -92,7 +77,7 @@
     output = review(check=True, verbose=True, seen=True)
     assert messages.CHECK_SUCCESS in output
 
-    _should_fail_with(messages.CHECK_UNSEEN, rev='0', check=True, verbose=True, seen=True)
+    should_fail_with(messages.CHECK_UNSEEN, rev='0', check=True, verbose=True, seen=True)
 
     review(rev='0', comment=True, message='.')
     output = review(check=True, verbose=True, seen=True)
@@ -101,9 +86,9 @@
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_check_priority_no_nos():
     review(signoff=True, no=True, message='.')
-    _should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, yeses='0')
-    _should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, seen=True)
-    _should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, seen=True, yeses='0')
+    should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, yeses='0')
+    should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, seen=True)
+    should_fail_with(messages.CHECK_HAS_NOS, check=True, verbose=True, no_nos=True, seen=True, yeses='0')
 
     review(signoff=True, yes=True, message='.', force=True)
     output = review(check=True, verbose=True, no_nos=True, seen=True, yeses='0')
@@ -111,14 +96,14 @@
 
     review(rev='0', signoff=True, no=True, message='.')
     review(rev='0', comment=True, message='.')
-    _should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True)
-    _should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True, seen=True)
-    _should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True, seen=True, yeses='0')
+    should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True)
+    should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True, seen=True)
+    should_fail_with(messages.CHECK_HAS_NOS, rev='0', check=True, verbose=True, no_nos=True, seen=True, yeses='0')
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_check_priority_yeses():
     review(comment=True, message='.')
-    _should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='1', seen=True)
+    should_fail_with(messages.CHECK_TOO_FEW_YESES, check=True, verbose=True, yeses='1', seen=True)
 
     review(signoff=True, yes=True, message='.')
     output = review(check=True, verbose=True, yeses='1', seen=True)
--- a/review/tests/test_comment.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/test_comment.py	Sat Jun 19 02:22:48 2010 -0400
@@ -110,34 +110,19 @@
     output = review(rev='1')
     assert output.count(messages.REVIEW_LOG_COMMENT_LINE % 'Test comment.') == 2
 
-    try:
-        review(comment=True, rev='1', message='Test bad comment.', lines='1',
-            files=['file_one', 'always_changing'])
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.COMMENT_LINES_REQUIRE_FILE in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.COMMENT_LINES_REQUIRE_FILE,
+                     comment=True, rev='1', message='Test bad comment.', lines='1',
+                     files=['file_one', 'always_changing'])
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_add_comments_to_bad_file():
-    try:
-        review(comment=True, message='Test comment one.', files=['bad'])
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.COMMENT_FILE_DOES_NOT_EXIST % ('bad', '2') in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.COMMENT_FILE_DOES_NOT_EXIST % ('bad', '2'),
+                     comment=True, message='Test comment one.', files=['bad'])
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_add_comments_to_file_line():
-    try:
-        review(comment=True, rev='1', message='Test bad comment.', lines='1')
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.COMMENT_LINES_REQUIRE_FILE in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.COMMENT_LINES_REQUIRE_FILE,
+                     comment=True, rev='1', message='Test bad comment.', lines='1')
 
     review(comment=True, rev='1', message='Test comment one.',
         files=['file_one'], lines='1')
--- a/review/tests/test_delete.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/test_delete.py	Sat Jun 19 02:22:48 2010 -0400
@@ -17,31 +17,12 @@
 def test_delete_invalid():
     # TODO: rename files.
 
-    try:
-        review(delete=True)
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.DELETE_REQUIRES_IDS in error
-    else:
-        assert False, 'The correct error message was not printed.'
-
-    try:
-        review(delete=True, files=['a'])
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.DELETE_UNKNOWN_ID % 'a' in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.DELETE_REQUIRES_IDS, delete=True)
+    should_fail_with(messages.DELETE_UNKNOWN_ID % 'a', delete=True, files=['a'])
 
     review(comment=True, message='test')
 
-    try:
-        review(delete=True, files=['z'])
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.DELETE_UNKNOWN_ID % 'z' in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.DELETE_UNKNOWN_ID % 'z', delete=True, files=['z'])
 
     # Use the pidgeonhole princicple to create ambiguous identifiers.
     for i in range(17):
@@ -53,14 +34,7 @@
         id_map[i[0]] = id_map.get(i[0], 0) + 1
     i = str(filter(lambda k: id_map[k] > 1, id_map.keys())[0])
 
-    try:
-        review(delete=True, files=[i])
-    except hgutil.Abort, e:
-        error = str(e)
-        print error
-        assert messages.DELETE_AMBIGUOUS_ID % i in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.DELETE_AMBIGUOUS_ID % i, delete=True, files=[i])
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_delete_comment():
--- a/review/tests/test_init.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/test_init.py	Sat Jun 19 02:22:48 2010 -0400
@@ -25,25 +25,12 @@
 
 @with_setup(setup_sandbox, teardown_sandbox)
 def test_init_without_remote_path():
-    try:
-        review(init=True)
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.INIT_REQUIRES_REMOTE_PATH in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.INIT_REQUIRES_REMOTE_PATH, init=True)
 
 @with_setup(setup_sandbox, teardown_sandbox)
 def test_init_twice():
     review(init=True, remote_path='/sandbox-review')
-
-    try:
-        review(init=True, remote_path='/sandbox-review')
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.INIT_EXISTS_UNCOMMITTED in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.INIT_EXISTS_UNCOMMITTED, init=True, remote_path='/sandbox-review')
 
 @with_setup(setup_reviewed_sandbox, teardown_sandbox)
 def test_init_clone():
--- a/review/tests/test_signoff.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/test_signoff.py	Sat Jun 19 02:22:48 2010 -0400
@@ -67,13 +67,7 @@
 def test_multiple_signoffs():
     review(signoff=True, message='Test signoff one.')
 
-    try:
-        review(signoff=True, message='Test signoff two.')
-    except hgutil.Abort, e:
-        error = str(e)
-        assert messages.SIGNOFF_EXISTS in error
-    else:
-        assert False, 'The correct error message was not printed.'
+    should_fail_with(messages.SIGNOFF_EXISTS, signoff=True, message='Test signoff two.')
 
     review(signoff=True, message='Test signoff two.', force=True)
 
--- a/review/tests/util.py	Sat Jun 19 02:03:37 2010 -0400
+++ b/review/tests/util.py	Sat Jun 19 02:22:48 2010 -0400
@@ -5,6 +5,7 @@
 import os, shutil
 import sample_data
 from mercurial import cmdutil, commands, hg, ui
+from mercurial import util as hgutil
 from .. import api, cli
 
 _ui = ui.ui()
@@ -111,3 +112,17 @@
 def get_ui():
     return _ui
 
+
+BAD_ERROR = 'The correct error message was not printed.'
+def _check_e(e, m):
+    error = str(e)
+    assert m in e
+
+def should_fail_with(m, **kwargs):
+    try:
+        output = review(**kwargs)
+    except hgutil.Abort, e:
+        _check_e(e, m)
+    else:
+        assert False, BAD_ERROR
+