<s> from __future__ import with_statement <EOL> import datetime <EOL> import new <EOL> from django . template import Template , Context , defaultfilters <EOL> from django . test import TestCase <EOL> from django . utils import translation , tzinfo <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . html import escape <EOL> from django . utils . timezone import utc <EOL> class HumanizeTests ( TestCase ) : <EOL> def humanize_tester ( self , test_list , result_list , method ) : <EOL> for test_content , result in zip ( test_list , result_list ) : <EOL> t = Template ( '<STR_LIT>' % method ) <EOL> rendered = t . render ( Context ( locals ( ) ) ) . strip ( ) <EOL> self . assertEqual ( rendered , escape ( result ) , <EOL> msg = "<STR_LIT>" % ( method , rendered , result ) ) <EOL> def test_ordinal ( self ) : <EOL> test_list = ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' , '<STR_LIT:4>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , None ) <EOL> result_list = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_intcomma ( self ) : <EOL> test_list = ( <NUM_LIT:100> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> None ) <EOL> result_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> None ) <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_l10n_intcomma ( self ) : <EOL> test_list = ( <NUM_LIT:100> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> None ) <EOL> result_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> None ) <EOL> with self . settings ( USE_L10N = True , USE_THOUSAND_SEPARATOR = False ) : <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_intcomma_without_number_grouping ( self ) : <EOL> with translation . override ( '<STR_LIT>' ) : <EOL> with self . settings ( USE_L10N = True ) : <EOL> self . humanize_tester ( [ <NUM_LIT:100> ] , [ '<STR_LIT:100>' ] , '<STR_LIT>' ) <EOL> def test_intword ( self ) : <EOL> test_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , None ) <EOL> result_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , None ) <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_i18n_intcomma ( self ) : <EOL> test_list = ( <NUM_LIT:100> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> result_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> with self . settings ( USE_L10N = True , USE_THOUSAND_SEPARATOR = True ) : <EOL> with translation . override ( '<STR_LIT>' ) : <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_i18n_intword ( self ) : <EOL> test_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> result_list = ( '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . settings ( USE_L10N = True , USE_THOUSAND_SEPARATOR = True ) : <EOL> with translation . override ( '<STR_LIT>' ) : <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_apnumber ( self ) : <EOL> test_list = [ str ( x ) for x in range ( <NUM_LIT:1> , <NUM_LIT:11> ) ] <EOL> test_list . append ( None ) <EOL> result_list = ( u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , <EOL> u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , None ) <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_naturalday ( self ) : <EOL> today = datetime . date . today ( ) <EOL> yesterday = today - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> tomorrow = today + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> someday = today - datetime . timedelta ( days = <NUM_LIT:10> ) <EOL> notdate = u"<STR_LIT>" <EOL> test_list = ( today , yesterday , tomorrow , someday , notdate , None ) <EOL> someday_result = defaultfilters . date ( someday ) <EOL> result_list = ( _ ( u'<STR_LIT>' ) , _ ( u'<STR_LIT>' ) , _ ( u'<STR_LIT>' ) , <EOL> someday_result , u"<STR_LIT>" , None ) <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> def test_naturalday_tz ( self ) : <EOL> from django . contrib . humanize . templatetags . humanize import naturalday <EOL> today = datetime . date . today ( ) <EOL> tz_one = tzinfo . FixedOffset ( datetime . timedelta ( hours = - <NUM_LIT:12> ) ) <EOL> tz_two = tzinfo . FixedOffset ( datetime . timedelta ( hours = <NUM_LIT:12> ) ) <EOL> date_one = datetime . datetime ( today . year , today . month , today . day , tzinfo = tz_one ) <EOL> naturalday_one = naturalday ( date_one ) <EOL> date_two = datetime . datetime ( today . year , today . month , today . day , tzinfo = tz_two ) <EOL> naturalday_two = naturalday ( date_two ) <EOL> self . assertNotEqual ( naturalday_one , naturalday_two ) <EOL> def test_naturaltime ( self ) : <EOL> class naive ( datetime . tzinfo ) : <EOL> def utcoffset ( self , dt ) : <EOL> return None <EOL> now = datetime . datetime ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT:15> ) <EOL> test_list = [ <EOL> now , <EOL> now - datetime . timedelta ( seconds = <NUM_LIT:1> ) , <EOL> now - datetime . timedelta ( seconds = <NUM_LIT:30> ) , <EOL> now - datetime . timedelta ( minutes = <NUM_LIT:1> , seconds = <NUM_LIT:30> ) , <EOL> now - datetime . timedelta ( minutes = <NUM_LIT:2> ) , <EOL> now - datetime . timedelta ( hours = <NUM_LIT:1> , minutes = <NUM_LIT:30> , seconds = <NUM_LIT:30> ) , <EOL> now - datetime . timedelta ( hours = <NUM_LIT> , minutes = <NUM_LIT:50> , seconds = <NUM_LIT:50> ) , <EOL> now - datetime . timedelta ( days = <NUM_LIT:1> ) , <EOL> now - datetime . timedelta ( days = <NUM_LIT> ) , <EOL> now + datetime . timedelta ( seconds = <NUM_LIT:1> ) , <EOL> now + datetime . timedelta ( seconds = <NUM_LIT:30> ) , <EOL> now + datetime . timedelta ( minutes = <NUM_LIT:1> , seconds = <NUM_LIT:30> ) , <EOL> now + datetime . timedelta ( minutes = <NUM_LIT:2> ) , <EOL> now + datetime . timedelta ( hours = <NUM_LIT:1> , minutes = <NUM_LIT:30> , seconds = <NUM_LIT:30> ) , <EOL> now + datetime . timedelta ( hours = <NUM_LIT> , minutes = <NUM_LIT:50> , seconds = <NUM_LIT:50> ) , <EOL> now + datetime . timedelta ( days = <NUM_LIT:1> ) , <EOL> now + datetime . timedelta ( days = <NUM_LIT> ) , <EOL> now . replace ( tzinfo = naive ( ) ) , <EOL> now . replace ( tzinfo = utc ) , <EOL> ] <EOL> result_list = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class MockDateTime ( datetime . datetime ) : <EOL> @ classmethod <EOL> def now ( self , tz = None ) : <EOL> if tz is None or tz . utcoffset ( now ) is None : <EOL> return now <EOL> else : <EOL> return now . replace ( tzinfo = tz ) + tz . utcoffset ( now ) <EOL> from django . contrib . humanize . templatetags import humanize <EOL> from django . utils import timesince <EOL> orig_humanize_datetime = humanize . datetime <EOL> orig_timesince_datetime = timesince . datetime <EOL> humanize . datetime = MockDateTime <EOL> timesince . datetime = new . module ( "<STR_LIT>" ) <EOL> timesince . datetime . datetime = MockDateTime <EOL> try : <EOL> self . humanize_tester ( test_list , result_list , '<STR_LIT>' ) <EOL> finally : <EOL> humanize . datetime = orig_humanize_datetime <EOL> timesince . datetime = orig_timesince_datetime </s>
