3 # Unit tester for Axis--Date object 5 require_once(
'Axis--Date--crh.php');
7 # Adds leading zeros to months and days 10 return (($data<10)?
"0":
"").$data ;
14 # FormatDate -- Generate one of the formats that Axis--Date should be able to parse 15 # $FmtNo -- a format number : 24 # ( 8) September 9th, 1999 49 $LongMonthName = $MonthNames[$Month];
50 $ShortMonthName = substr($LongMonthName,0,3);
52 # Suffix for long-form month numbers 53 $Sfx = ($Day % 10) == 1 ?
"st" :
54 ($Day % 10) == 2 ?
"nd" :
55 ($Day % 10) == 3 ?
"rd" :
"th";
58 $ShortMonthName.
"-".$Year,
59 $ShortMonthName.
" ".$Year,
61 $Year.
"-".$Month.
"-".$Day,
62 $Month.
"-".$Day.
"-".$Year,
63 $Day.
"-".$Month.
"-".$Year,
64 $ShortMonthName.
" ".$Day.
" ".$Year,
65 $LongMonthName.
" ".$Day.$Sfx.
", ".$Year,
66 $Month.
"/".$Day.
"/".substr($Year,2),
67 $Month.
"-".$Day.
"-".substr($Year,2),
68 $Year.lz($Month).lz($Day),
69 lz($Day).
"-".$ShortMonthName.
"-".$Year,
70 lz($Day).
" ".$ShortMonthName.
" ".$Year,
74 return $Values[$FmtNo];
81 # We want to test years both above and below 2000 82 foreach ( array(1999,2001) as $Year )
84 # Test each month, to make sure that none of the long/short name parsing is 85 # messed up for a particular month. 86 for ( $Month = 1; $Month <= 12; $Month++ )
88 # We don't need to test every day of every month 89 # 1-4 will have different suffixes in the long-format (1st, 2nd, etc) 90 # so those should be tested. 91 # We should be testing both single and double-digit dates 92 # 1-4,15 should exercise all the important parts. 93 foreach( array(1,2,3,4,15,21,22,23) as $Day )
95 # Index through each of the formats. 99 # Try parsing a single date: 100 $TestValue =
FormatDate( $Ix, $Year, $Month, $Day );
101 $TestDate =
new Date($TestValue);
102 $Parsed = $TestDate->BeginDate();
104 # Construct the expected result 105 $Expected = $Year.
"-".
lz($Month).
"-".(($Ix<=3)?
"00":
lz($Day));
106 if( $Parsed != $Expected )
108 # We expect to fail on D-M-Y when D<=12, because we can't 109 # Distinguish that from M-D-Y 110 if( $Ix == 6 && $Day <= 12 )
112 print(
"Exfail Parsing '".$TestValue.
"': " 113 .
"Expected '".$Expected.
"' but got '".$Parsed.
"'\n");
118 print(
"FAIL Parsing '".$TestValue.
"': " 119 .
"Expected '".$Expected.
"' but got '".$Parsed.
"'\n");
123 # Now for a date range 124 # Try an end date in each possible format 125 # We'll go from the starting date till 2 years later. 128 $TestValue =
FormatDate( $Ix, $Year, $Month, $Day ) .
" - " .
130 $TestDate =
new Date($TestValue);
131 $Parsed = $TestDate->BeginDate().
" - ".$TestDate->EndDate();
133 $Expected = $Year.
"-".
lz($Month).
"-".(($Ix <=3)?
"00":
lz($Day)).
" - ".
134 ($Year+2).
"-".
lz($Month).
"-".(($Ix2<=3)?
"00":
lz($Day));
135 if( $Parsed != $Expected )
137 if( ($Ix == 6 || $Ix2 == 6 ) && $Day <= 12 )
139 # Again, expected failures on D-M-Y when D<=12 140 print(
"Exfail Parsing '".$TestValue.
"': " 141 .
"Expected '".$Expected.
"' but got '".$Parsed.
"'\n");
146 print(
"FAIL Parsing '".$TestValue.
"': " 147 .
"Expected '".$Expected.
"' but got '".$Parsed.
"'\n");
156 # If any tests failed, exit with an error.
FormatDate($FmtNo, $Year, $Month, $Day)