diff options
| author | Gary Wilson Jr <gary.wilson@gmail.com> | 2007-09-03 23:14:51 +0000 |
|---|---|---|
| committer | Gary Wilson Jr <gary.wilson@gmail.com> | 2007-09-03 23:14:51 +0000 |
| commit | bce7de96477086f98f8cde8aef41a15e6e17b8af (patch) | |
| tree | 5b4de18bbc5c3ddd997524103e50fe5698b2128c /django/test/testcases.py | |
| parent | 853f9c7db9f506de20228eb74d9d34abeba10dbf (diff) | |
Removed some duplication in the Django `TestCase` methods by introducing a `to_list` function for putting a value into a list if it's not already one.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6041 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/testcases.py')
| -rw-r--r-- | django/test/testcases.py | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 400d66cfab..fd08d45ee3 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -10,6 +10,18 @@ from django.test.client import Client normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) +def to_list(value): + """ + Puts value into a list if it's not already one. + Returns an empty list if value is None. + """ + if value is None: + value = [] + elif not isinstance(value, list): + value = [value] + return value + + class OutputChecker(doctest.OutputChecker): def check_output(self, want, got, optionflags): ok = doctest.OutputChecker.check_output(self, want, got, optionflags) @@ -106,18 +118,14 @@ class TestCase(unittest.TestCase): def assertFormError(self, response, form, field, errors): "Assert that a form used to render the response has a specific field error" - if not response.context: - self.fail('Response did not use any contexts to render the response') - - # If there is a single context, put it into a list to simplify processing - if not isinstance(response.context, list): - contexts = [response.context] - else: - contexts = response.context + # Put context(s) into a list to simplify processing. + contexts = to_list(response.context) + if not contexts: + self.fail('Response did not use any contexts to render the' + ' response') - # If a single error string is provided, make it a list to simplify processing - if not isinstance(errors, list): - errors = [errors] + # Put error(s) into a list to simplify processing. + errors = to_list(errors) # Search all contexts for the error. found_form = False @@ -144,24 +152,17 @@ class TestCase(unittest.TestCase): def assertTemplateUsed(self, response, template_name): "Assert that the template with the provided name was used in rendering the response" - if isinstance(response.template, list): - template_names = [t.name for t in response.template] - self.failUnless(template_name in template_names, - u"Template '%s' was not one of the templates used to render the response. Templates used: %s" % - (template_name, u', '.join(template_names))) - elif response.template: - self.assertEqual(template_name, response.template.name, - u"Template '%s' was not used to render the response. Actual template was '%s'" % - (template_name, response.template.name)) - else: + template_names = [t.name for t in to_list(response.template)] + if not template_names: self.fail('No templates used to render the response') + self.failUnless(template_name in template_names, + (u"Template '%s' was not a template used to render the response." + " Actual template(s) used: %s") % (template_name, + u', '.join(template_names))) def assertTemplateNotUsed(self, response, template_name): "Assert that the template with the provided name was NOT used in rendering the response" - if isinstance(response.template, list): - self.failIf(template_name in [t.name for t in response.template], - u"Template '%s' was used unexpectedly in rendering the response" % template_name) - elif response.template: - self.assertNotEqual(template_name, response.template.name, - u"Template '%s' was used unexpectedly in rendering the response" % template_name) - + template_names = [t.name for t in to_list(response.template)] + self.failIf(template_name in template_names, + (u"Template '%s' was used unexpectedly in rendering the" + " response") % template_name) |
