| | 1 | import unittest |
| | 2 | from django.template import Template, Context, add_to_builtins |
| | 3 | |
| | 4 | add_to_builtins('django.contrib.humanize.templatetags.humanize') |
| | 5 | |
| | 6 | class HumanizeTests(unittest.TestCase): |
| | 7 | |
| | 8 | def humanize_tester(self, test_list, result_list, method): |
| | 9 | # Using max below ensures we go through both lists |
| | 10 | # However, if the lists are not equal length, this raises an exception |
| | 11 | for index in xrange(len(max(test_list,result_list))): |
| | 12 | test_content = test_list[index] |
| | 13 | t = Template('{{ test_content|%s }}' % method) |
| | 14 | rendered = t.render(Context(locals())).strip() |
| | 15 | self.assertEqual(rendered, result_list[index], |
| | 16 | msg="""%s test failed, produced %s, |
| | 17 | should've produced %s""" % (method, rendered, result_list[index])) |
| | 18 | |
| | 19 | def test_ordinal(self): |
| | 20 | test_list = ('1','2','3','4','11','12', |
| | 21 | '13','101','102','103','111', |
| | 22 | 'something else') |
| | 23 | result_list = ('1st', '2nd', '3rd', '4th', '11th', |
| | 24 | '12th', '13th', '101st', '102nd', '103rd', |
| | 25 | '111th', 'something else') |
| | 26 | |
| | 27 | self.humanize_tester(test_list, result_list, 'ordinal') |
| | 28 | |
| | 29 | def test_intcomma(self): |
| | 30 | test_list = ('100','1000','10123','10311','1000000') |
| | 31 | result_list = ('100', '1,000', '10,123', '10,311', '1,000,000') |
| | 32 | |
| | 33 | self.humanize_tester(test_list, result_list, 'intcomma') |
| | 34 | |
| | 35 | def test_intword(self): |
| | 36 | test_list = ('100', '1000000', '1200000', '1290000', |
| | 37 | '1000000000','2000000000','6000000000000') |
| | 38 | result_list = ('100', '1.0 million', '1.2 million', '1.3 million', |
| | 39 | '1.0 billion', '2.0 billion', '6.0 trillion') |
| | 40 | |
| | 41 | self.humanize_tester(test_list, result_list, 'intword') |
| | 42 | |
| | 43 | def test_apnumber(self): |
| | 44 | test_list = [str(x) for x in xrange(1,11)] |
| | 45 | result_list = ('one', 'two', 'three', 'four', 'five', 'six', |
| | 46 | 'seven', 'eight', 'nine', '10') |
| | 47 | |
| | 48 | self.humanize_tester(test_list, result_list, 'apnumber') |
| | 49 | |
| | 50 | if __name__ == '__main__': |
| | 51 | unittest.main() |
| | 52 | |