aboutsummaryrefslogtreecommitdiffstats
path: root/t/utils.t
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2016-09-20 16:19:37 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2016-09-20 16:19:37 +0100
commit1c5c614af7a0265bd296ef67c0d342b27898185c (patch)
tree26e1199c1010e8d88c87e4821c7f82c3494db5d0 /t/utils.t
parent33be8df8f21f6ec5cee9c4675847b8e26f72f96b (diff)
Some tests for the Util modules.
Diffstat (limited to 't/utils.t')
-rw-r--r--t/utils.t36
1 files changed, 36 insertions, 0 deletions
diff --git a/t/utils.t b/t/utils.t
index 29759cddc..ac9eb1a4a 100644
--- a/t/utils.t
+++ b/t/utils.t
@@ -4,6 +4,9 @@ use strict;
use warnings;
use Test::More;
+use mySociety::Locale;
+mySociety::Locale::gettext_domain('FixMyStreet');
+
use Utils;
my @truncate_tests = (
@@ -34,9 +37,14 @@ foreach my $test (@convert_en_to_latlon_tests) {
[ Utils::convert_en_to_latlon_truncated( $e, $n ) ], #
[ $lat, $lon ], #
"convert ($e,$n) to ($lat,$lon)";
+ is_deeply
+ [ Utils::convert_latlon_to_en( $lat, $lon ) ],
+ [ $e, $n ],
+ "convert ($lat,$lon) to ($e,$n)";
}
my @cleanup_tests = (
+ [ '', '', '' ],
[ 'dog shit', 'Dog poo', 'dog poo' ],
[ 'dog shit', 'Dog poo', 'with spaces' ],
[ 'dog shite', 'Dog poo', 'with extra e' ],
@@ -57,4 +65,32 @@ foreach my $test ( @cleanup_tests ) {
is Utils::cleanup_text( "This has new\n\n\nlines in it", { allow_multiline => 1 } ), "This has new\n\nLines in it", 'new lines allowed';
+
+is Utils::prettify_dt(), "[unknown time]";
+my $dt = DateTime->now;
+is Utils::prettify_dt($dt), $dt->strftime("%H:%M today");
+
+# Same week test
+if ($dt->day_of_week == 7) { # Sunday
+ $dt = DateTime->now->add(days => 1);
+} else {
+ $dt = DateTime->now->subtract(days => 1);
+}
+is Utils::prettify_dt($dt), $dt->strftime("%H:%M, %A");
+
+$dt = DateTime->now->subtract(days => 100);
+is Utils::prettify_dt($dt), $dt->strftime("%H:%M, %A %e %B %Y");
+is Utils::prettify_dt($dt, "date"), $dt->strftime("%A %e %B %Y");
+is Utils::prettify_dt($dt, "zurich"), $dt->strftime("%H:%M, %e. %B %Y");
+is Utils::prettify_dt($dt, "short"), $dt->strftime("%H:%M, %e %b %Y");
+is Utils::prettify_dt($dt, 1), $dt->strftime("%H:%M, %e %b %Y");
+$dt = DateTime->now->subtract(days => 400);
+is Utils::prettify_dt($dt), $dt->strftime("%H:%M, %a %e %B %Y");
+
+is Utils::prettify_duration(7*86400+3600+60+1, 'week'), '1 week';
+is Utils::prettify_duration(86400+3600+60+1, 'day'), '1 day';
+is Utils::prettify_duration(86400+3600+60+1, 'hour'), '1 day, 1 hour';
+is Utils::prettify_duration(86400+3600+60+1, 'minute'), '1 day, 1 hour, 1 minute';
+is Utils::prettify_duration(20, 'minute'), 'less than a minute';
+
done_testing();