[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1150] trunk/t/03-date-handling.t



Revision: 1150
          http://svn.sourceforge.net/ledger-smb/?rev=1150&view=rev
Author:   tetragon
Date:     2007-05-06 23:37:44 -0700 (Sun, 06 May 2007)

Log Message:
-----------
Adding base date handling tests to trunk/

Added Paths:
-----------
    trunk/t/03-date-handling.t

Copied: trunk/t/03-date-handling.t (from rev 1149, branches/1.2/t/03-date-handling.t)
===================================================================
--- trunk/t/03-date-handling.t	                        (rev 0)
+++ trunk/t/03-date-handling.t	2007-05-07 06:37:44 UTC (rev 1150)
@@ -0,0 +1,162 @@
+#!/usr/bin/perl
+#
+# Note: This file assumes good dates, SL behaviour with bad dates is undefined
+#
+
+#LedgerSMB/Form.pm:3153:sub from_to
+#LedgerSMB/Form.pm:1361:sub add_date {
+
+use strict;
+use warnings;
+use Test::More 'no_plan';
+use Math::BigFloat;
+
+use LedgerSMB::Sysconfig;
+use LedgerSMB::Form;
+use LedgerSMB::Locale;
+
+my $form = new Form;
+my $locale_en = LedgerSMB::Locale->get_handle('en');
+my $locale_es = LedgerSMB::Locale->get_handle('es');
+my %myconfig;
+ok(defined $form);
+isa_ok($form, 'Form');
+$form->{dbh} = ${LedgerSMB::Sysconfig::GLOBALDBH};
+
+my @formats = ( ['mm-dd-yy', '-', 2, '02-29-00', '03-01-00'], 
+		['mm/dd/yy', '/', 2, '02/29/00', '03/01/00'],
+		['dd-mm-yy', '-', 2, '29-02-00', '01-03-00'], 
+		['dd/mm/yy', '/', 2, '29/02/00', '01/03/00'],
+		['dd.mm.yy', '.', 2, '29.02.00', '01.03.00'], 
+#		['yyyymmdd', '', 4, '20000229', '20000301'],
+		['yyyy-mm-dd', '-', 4, '2000-02-29', '2000-03-01']);
+
+my @months = ('January', 'February', 'March', 'April', 'May ', 'June', 
+	'July', 'August', 'September', 'October', 'November', 'December');
+
+my $today = `date +\%F`;
+chomp $today;
+my %today_parts;
+$today_parts{'yyyy'} = `date +\%Y`;
+$today_parts{'yy'} = $today_parts{'yyyy'};
+$today_parts{'yy'} =~ s/^..//;
+$today_parts{'mm'} = `date +\%m`;
+$today_parts{'dd'} = `date +\%d`;
+chomp $today_parts{'yyyy'};
+chomp $today_parts{'yy'};
+chomp $today_parts{'mm'};
+chomp $today_parts{'dd'};
+
+# $locale->date checks
+# Note that $locale->date assumes the year range 2000-2099
+# Note that $locale->date does not perform language-specific long forms
+# Note that $locale->date also takes in yyyymmdd
+foreach my $format (0 .. $#formats) {
+	%myconfig = (dateformat => $formats[$format][0]);
+	my $fmt = $formats[$format][0];
+	my $sep = $formats[$format][1];
+	my $yearcount = $formats[$format][2];
+	my $result = $formats[$format][3];
+	$result =~ s/^(.*)(20)?00(.*)$/${1}2000${3}/ if $yearcount == 2;
+	cmp_ok($locale_en->date(\%myconfig), 'eq', 
+		'', "date, $fmt: empty string");
+	cmp_ok($locale_en->date(\%myconfig, $formats[$format][3]), 'eq',
+		$result, "date, $fmt: short");
+	cmp_ok($locale_en->date(\%myconfig, '20000229'), 'eq',
+		$result, "date, $fmt: chopped");
+	for my $mm (1 .. 12) {
+		my $start = $fmt;
+		my $temp = sprintf('%02d', $mm);
+		my $month_en = $locale_en->text($months[$mm - 1]);
+		my $month_es = $locale_es->text($months[$mm - 1]);
+		$start =~ s/dd/29/;
+		$start =~ s/yyyy/2000/;
+		$start =~ s/yy/00/;
+		$start =~ s/mm/$temp/;
+		cmp_ok($locale_es->date(\%myconfig, $start, 1), 'eq',
+			"$month_es 29 2000", "date, $start, $fmt: long, es");
+		cmp_ok($locale_en->date(\%myconfig, $start, 1), 'eq',
+			"$month_en 29 2000", "date, $start, $fmt: long, en");
+	}
+}
+
+# $form->current_date checks
+# Note that $form->current_date always uses the database
+foreach my $format (0 .. $#formats) {
+	%myconfig = (dateformat => $formats[$format][0]);
+	my $fmt = $formats[$format][0];
+	my $sep = $formats[$format][1];
+	my $yearcount = $formats[$format][2];
+	is($form->current_date(\%myconfig), $today, 
+		"current_date, $fmt: $today");
+	is($form->current_date(\%myconfig, $formats[$format][3]), 
+		'2000-02-29', "current_date, $fmt: 2000-02-29");
+	is($form->current_date(\%myconfig, $formats[$format][3], 1), 
+		'2000-03-01', "current_date, $fmt: 2000-03-01");
+}
+
+# $form->datetonum checks
+# Note that $form->datetonum assumes the year range 2000-2099
+foreach my $format (0 .. $#formats) {
+	%myconfig = (dateformat => $formats[$format][0]);
+	my $fmt = $formats[$format][0];
+	my $sep = $formats[$format][1];
+	my $yearcount = $formats[$format][2];
+	cmp_ok($form->datetonum(\%myconfig, $formats[$format][3]), 'eq',
+		'20000229', "datetonum, $fmt");
+}
+
+# $form->split_date checks
+# Note that $form->split_date assumes the year range 2000-2099
+# Note that $form->split_date only outputs two digit years
+foreach my $format (0 .. $#formats) {
+	%myconfig = (dateformat => $formats[$format][0]);
+	my $fmt = $formats[$format][0];
+	my $sep = $formats[$format][1];
+	my $yearcount = $formats[$format][2];
+	my @output = $form->split_date($fmt, $formats[$format][3]);
+	my $rv = $fmt;
+	$rv =~ s/\Q$sep\E//g;
+	$rv =~ s/(yy)?yy/$output[1]/;
+	$rv =~ s/mm/$output[2]/;
+	$rv =~ s/dd/$output[3]/;
+	cmp_ok($output[1], 'eq', '00', "split_date specified, year");
+	cmp_ok($output[2], 'eq', '02', "split_date specified, month");
+	cmp_ok($output[3], 'eq', '29', "split_date specified, day");
+	cmp_ok($output[0], 'eq', $rv, "split_date specified, unit");
+	@output = $form->split_date($fmt);
+	my $rv = $fmt;
+	$rv =~ s/\Q$sep\E//g;
+	$rv =~ s/(yy)?yy/$output[1]/;
+	$rv =~ s/mm/$output[2]/;
+	$rv =~ s/dd/$output[3]/;
+	my $tv = $fmt;
+	$tv =~ s/\Q$sep\E//g;
+	$tv =~ s/(yy)?yy/$today_parts{'yy'}/;
+	$tv =~ s/mm/$today_parts{'mm'}/;
+	$tv =~ s/dd/$today_parts{'dd'}/;
+	cmp_ok($output[1], 'eq', $today_parts{'yy'}, 
+		"split_date unspecified, year");
+	cmp_ok($output[2], 'eq', $today_parts{'mm'}, 
+		"split_date unspecified, month");
+	cmp_ok($output[3], 'eq', $today_parts{'dd'}, 
+		"split_date unspecified, day");
+}
+
+# $form->format_date checks
+# Note that $form->format_date always outputs four digit years
+foreach my $format (0 .. $#formats) {
+	$form->{db_dateformat} = $formats[$format][0];
+	my $fmt = $formats[$format][0];
+	my $sep = $formats[$format][1];
+	my $yearcount = $formats[$format][2];
+	my $results = $fmt;
+	$results =~ s/(yy)?yy/2000/;
+	$results =~ s/mm/02/;
+	$results =~ s/dd/29/;
+	cmp_ok($form->format_date('2000-02-29'), 'eq',
+		$results, "format_date, $fmt, ISO");
+	cmp_ok($form->format_date($formats[$format][3]), 'eq',
+		$formats[$format][3], "format_date, $fmt, non-ISO");
+}
+


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.