diff --git a/ago.py b/ago.py index 6dc218c..d7e88c4 100644 --- a/ago.py +++ b/ago.py @@ -3,6 +3,7 @@ from datetime import timedelta def delta2dict( delta ): """return dictionary of delta""" + delta = abs( delta ) return { 'year' : delta.days / 365 , 'day' : delta.days % 365 , @@ -15,9 +16,7 @@ def delta2dict( delta ): def delta2human(delta, precision=2, past_format='{0} ago', future_format='in {0}'): """return human readable delta string""" the_format = past_format - if delta < timedelta(0): # if true delta is in future - the_format = future_format - delta = abs( delta ) + if delta < timedelta(0): the_format = future_format d = delta2dict( delta ) hlist = [] count = 0 diff --git a/test_ago.py b/test_ago.py index c087ee7..7309c3c 100644 --- a/test_ago.py +++ b/test_ago.py @@ -21,6 +21,40 @@ def test_delta2human_is_string(): def test_delta2dict_is_dict(): assert 'dict' in str(type(delta2dict( PAST_DELTA ))) +def test_ago_in_past_human(): + assert 'ago' in human( PAST ) + +def test_in_in_future_human(): + assert 'in' in human( FUTURE ) + +def test_no_coma_in_one_precision(): + assert ',' not in human( PAST, precision = 1 ) + assert ',' not in human( FUTURE, precision = 1 ) + +def test_coma_in_three_precision(): + assert ',' in human( PAST, precision = 3 ) + assert ',' in human( FUTURE, precision = 3 ) + +def test_one_day_singular(): + assert 's' not in delta2human( timedelta(1) ) + +def test_two_day_plural(): + assert 's' in delta2human( timedelta(2) ) + +def test_past_format(): + output = human( PAST, + format_past = 'titanic sunk {0} ago', + format_future = 'titanic will sink in {0} from now' + ) + assert 'titanic sunk' in output + +def test_future_format(): + output = human( FUTURE, + format_past = 'titanic sunk {0} ago', + format_future = 'titanic will sink in {0} from now' + ) + assert 'titanic will sink in' in output + def test_valid_dict(): past_dict = delta2dict( PAST_DELTA ) assert past_dict['year'] == 1 @@ -35,11 +69,11 @@ def test_output(): print '\nTesting past:\n' print delta2dict( PAST_DELTA ) print 'Commented ' + delta2human( PAST_DELTA, 3 ) - print 'Commented ' + human( PAST ) + print human( PAST, format_past = "Commented {0} ago" ) print '\nTesting future:\n' print delta2dict( FUTURE_DELTA ) print 'Shutdown ' + delta2human( FUTURE_DELTA, 3 ) - print 'Shutdown ' + human( FUTURE ) + print human( FUTURE, format_future = 'Shutdown in {0} from now' ) print ''