Django

Code

Changeset 6041

Show
Ignore:
Timestamp:
09/03/07 18:14:51 (1 year ago)
Author:
gwilson
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r6039 r6041  
    1010 
    1111normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) 
     12 
     13def to_list(value): 
     14    """ 
     15    Puts value into a list if it's not already one. 
     16    Returns an empty list if value is None. 
     17    """ 
     18    if value is None: 
     19        value = [] 
     20    elif not isinstance(value, list): 
     21        value = [value] 
     22    return value 
     23 
    1224 
    1325class OutputChecker(doctest.OutputChecker): 
     
    107119    def assertFormError(self, response, form, field, errors): 
    108120        "Assert that a form used to render the response has a specific field error" 
    109         if not response.context: 
    110             self.fail('Response did not use any contexts to render the response') 
     121        # Put context(s) into a list to simplify processing. 
     122        contexts = to_list(response.context)  
     123        if not contexts: 
     124            self.fail('Response did not use any contexts to render the' 
     125                      ' response') 
    111126 
    112         # If there is a single context, put it into a list to simplify processing 
    113         if not isinstance(response.context, list): 
    114             contexts = [response.context] 
    115         else: 
    116             contexts = response.context 
    117  
    118         # If a single error string is provided, make it a list to simplify processing 
    119         if not isinstance(errors, list): 
    120             errors = [errors] 
     127        # Put error(s) into a list to simplify processing. 
     128        errors = to_list(errors) 
    121129         
    122130        # Search all contexts for the error. 
     
    145153    def assertTemplateUsed(self, response, template_name): 
    146154        "Assert that the template with the provided name was used in rendering the response" 
    147         if isinstance(response.template, list): 
    148             template_names = [t.name for t in response.template] 
    149             self.failUnless(template_name in template_names, 
    150                 u"Template '%s' was not one of the templates used to render the response. Templates used: %s" % 
    151                     (template_name, u', '.join(template_names))) 
    152         elif response.template: 
    153             self.assertEqual(template_name, response.template.name, 
    154                 u"Template '%s' was not used to render the response. Actual template was '%s'" % 
    155                     (template_name, response.template.name)) 
    156         else: 
     155        template_names = [t.name for t in to_list(response.template)] 
     156        if not template_names: 
    157157            self.fail('No templates used to render the response') 
     158        self.failUnless(template_name in template_names, 
     159            (u"Template '%s' was not a template used to render the response." 
     160              " Actual template(s) used: %s") % (template_name, 
     161                                                 u', '.join(template_names))) 
    158162 
    159163    def assertTemplateNotUsed(self, response, template_name): 
    160164        "Assert that the template with the provided name was NOT used in rendering the response" 
    161         if isinstance(response.template, list):             
    162             self.failIf(template_name in [t.name for t in response.template], 
    163                 u"Template '%s' was used unexpectedly in rendering the response" % template_name) 
    164         elif response.template: 
    165             self.assertNotEqual(template_name, response.template.name, 
    166                 u"Template '%s' was used unexpectedly in rendering the response" % template_name) 
    167  
     165        template_names = [t.name for t in to_list(response.template)] 
     166        self.failIf(template_name in template_names, 
     167            (u"Template '%s' was used unexpectedly in rendering the" 
     168              " response") % template_name) 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r6039 r6041  
    7676            self.assertTemplateUsed(response, 'Empty POST Template')         
    7777        except AssertionError, e: 
    78             self.assertEquals(str(e), "Template 'Empty POST Template' was not used to render the response. Actual template was 'Empty GET Template'") 
     78            self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template") 
    7979     
    8080    def test_multiple_context(self): 
     
    102102            self.assertTemplateUsed(response, "Valid POST Template")         
    103103        except AssertionError, e: 
    104             self.assertEquals(str(e), "Template 'Valid POST Template' was not one of the templates used to render the response. Templates used: form_view.html, base.html") 
     104            self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html") 
    105105 
    106106class AssertRedirectsTests(TestCase):