erlydtl_dateformat_tests.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. -module(erlydtl_dateformat_tests).
  2. -export([run_tests/0]).
  3. run_tests() ->
  4. io:format("Running date format tests...~n"),
  5. Failures = test_group_runner([
  6. {
  7. "date 1",
  8. {1979, 7, 8}, % just a date
  9. [{"a", "a.m."}, {"A", "AM"}, {"c", "1979-07-08T00:00:00"},
  10. {"d", "08"}, {"D", "Sun"}, {"f", "12"}, {"F", "July"},
  11. {"g", "12"}, {"G", "0"},
  12. {"h", "12"}, {"H", "00"}, {"i", "00"},
  13. {"j", "8"}, {"l", "Sunday"}, {"L", "False"},
  14. {"m", "07"}, {"M", "Jul"}, {"b", "jul"},
  15. {"n", "7"}, {"N", "July"}, {"P", "midnight"},
  16. {"s", "00"}, {"S", "th"}, {"t", "31"},
  17. {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
  18. {"jS F Y H:i", "8th July 1979 00:00"},
  19. {"jS \\o\\f F", "8th of July"},
  20. % We expect these to come back verbatim
  21. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
  22. % TODO : timzeone related tests.
  23. %{"r", "Sun, 8 Jul 1979 00:00:00 +0000"},
  24. %{"O", "0000"},
  25. %{"T", "CET"},
  26. %{"U", "300531600"},
  27. %{"Z", "3600"}
  28. ]
  29. },
  30. {
  31. "datetime 1",
  32. {{1979, 7, 8}, {22, 7, 12}}, % date/time tuple
  33. [{"a", "p.m."}, {"A", "PM"}, {"c", "1979-07-08T22:07:12"},
  34. {"d", "08"}, {"D", "Sun"}, {"f", "10:07"}, {"F", "July"},
  35. {"g", "10"}, {"G", "22"},
  36. {"h", "10"}, {"H", "22"}, {"i", "07"},
  37. {"j", "8"}, {"l", "Sunday"}, {"L", "False"},
  38. {"m", "07"}, {"M", "Jul"}, {"b", "jul"},
  39. {"n", "7"}, {"N", "July"}, {"P", "10:07 p.m."},
  40. {"s", "12"}, {"S", "th"}, {"t", "31"},
  41. {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
  42. {"jS F Y H:i", "8th July 1979 22:07"},
  43. {"jS \\o\\f F", "8th of July"},
  44. % We expect these to come back verbatim
  45. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
  46. % TODO : timzeone related tests.
  47. %{"r", "Sun, 8 Jul 1979 22:07:12 +0000"},
  48. %{"O", "0000"},
  49. %{"T", "CET"},
  50. %{"U", "300531600"},
  51. %{"Z", "3600"}
  52. ]
  53. },
  54. {
  55. "datetime 2",
  56. {{2008, 12, 25}, {7, 0, 9}}, % date/time tuple
  57. [{"a", "a.m."}, {"A", "AM"}, {"c", "2008-12-25T07:00:09"},
  58. {"d", "25"}, {"D", "Thu"}, {"f", "7"}, {"F", "December"},
  59. {"g", "7"}, {"G", "7"},
  60. {"h", "07"}, {"H", "07"}, {"i", "00"},
  61. {"j", "25"}, {"l", "Thursday"}, {"L", "True"},
  62. {"m", "12"}, {"M", "Dec"}, {"b", "dec"},
  63. {"n", "12"}, {"N", "Dec."}, {"P", "7 a.m."},
  64. {"s", "09"}, {"S", "th"}, {"t", "31"},
  65. {"w", "4"}, {"W", "52"}, {"y", "08"}, {"Y", "2008"}, {"z", "360"},
  66. {"jS F Y H:i", "25th December 2008 07:00"},
  67. {"jS \\o\\f F", "25th of December"},
  68. % We expect these to come back verbatim
  69. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
  70. % TODO : timzeone related tests.
  71. %{"r", "Thu, 25 Dec 2008 07:00:09 +0000"},
  72. %{"O", "0000"},
  73. %{"T", "CET"},
  74. %{"U", "300531600"},
  75. %{"Z", "3600"}
  76. ]
  77. },
  78. {
  79. "datetime 3",
  80. {{2004, 2, 29}, {12, 0, 59}}, % date/time tuple
  81. [{"a", "p.m."}, {"A", "PM"}, {"c", "2004-02-29T12:00:59"},
  82. {"d", "29"}, {"D", "Sun"}, {"f", "12"}, {"F", "February"},
  83. {"g", "12"}, {"G", "12"},
  84. {"h", "12"}, {"H", "12"}, {"i", "00"},
  85. {"j", "29"}, {"l", "Sunday"}, {"L", "True"},
  86. {"m", "02"}, {"M", "Feb"}, {"b", "feb"},
  87. {"n", "2"}, {"N", "Feb."}, {"P", "noon"},
  88. {"s", "59"}, {"S", "th"}, {"t", "29"},
  89. {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
  90. {"jS F Y H:i", "29th February 2004 12:00"},
  91. {"jS \\o\\f F", "29th of February"},
  92. % We expect these to come back verbatim
  93. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
  94. % TODO : timzeone related tests.
  95. %{"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
  96. %{"O", "0000"},
  97. %{"T", "CET"},
  98. %{"U", "300531600"},
  99. %{"Z", "3600"}
  100. ]
  101. },
  102. {
  103. "datetime 4",
  104. {{2004, 2, 29}, {12, 0, 09.256687}}, % date/time tuple
  105. [{"a", "p.m."}, {"A", "PM"}, {"c", "2004-02-29T12:00:09"},
  106. {"d", "29"}, {"D", "Sun"}, {"f", "12"}, {"F", "February"},
  107. {"g", "12"}, {"G", "12"},
  108. {"h", "12"}, {"H", "12"}, {"i", "00"},
  109. {"j", "29"}, {"l", "Sunday"}, {"L", "True"},
  110. {"m", "02"}, {"M", "Feb"}, {"b", "feb"},
  111. {"n", "2"}, {"N", "Feb."}, {"P", "noon"},
  112. {"s", "09"}, {"S", "th"}, {"t", "29"},
  113. {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
  114. {"jS F Y H:i", "29th February 2004 12:00"},
  115. {"jS \\o\\f F", "29th of February"},
  116. % We expect these to come back verbatim
  117. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
  118. % TODO : timzeone related tests.
  119. %{"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
  120. %{"O", "0000"},
  121. %{"T", "CET"},
  122. %{"U", "300531600"},
  123. %{"Z", "3600"}
  124. ]
  125. },
  126. % Weeknum tests. Largely based on examples from :
  127. % http://en.wikipedia.org/wiki/ISO_week_date
  128. { "weeknum 1.1", {2005, 1, 1}, [{"W", "53"}] },
  129. { "weeknum 1.2", {2005, 1, 2}, [{"W", "53"}] },
  130. { "weeknum 1.3", {2005, 12, 31}, [{"W", "52"}] },
  131. { "weeknum 1.4", {2007, 1, 1}, [{"W", "1"}] },
  132. { "weeknum 1.5", {2007, 12, 30}, [{"W", "52"}] },
  133. { "weeknum 1.6", {2007, 12, 31}, [{"W", "1"}] },
  134. { "weeknum 1.6", {2008, 1, 1}, [{"W", "1"}] },
  135. { "weeknum 1.7", {2008, 12, 29}, [{"W", "1"}] },
  136. { "weeknum 1.8", {2008, 12, 31}, [{"W", "1"}] },
  137. { "weeknum 1.9", {2009, 1, 1}, [{"W", "1"}] },
  138. { "weeknum 1.10", {2009, 12, 31}, [{"W", "53"}] },
  139. { "weeknum 1.11", {2010, 1, 3}, [{"W", "53"}] },
  140. % Examples where the ISO year is three days into
  141. % the next Gregorian year
  142. { "weeknum 2.1", {2009, 12, 31}, [{"W", "53"}] },
  143. { "weeknum 2.2", {2010, 1, 1}, [{"W", "53"}] },
  144. { "weeknum 2.3", {2010, 1, 2}, [{"W", "53"}] },
  145. { "weeknum 2.4", {2010, 1, 3}, [{"W", "53"}] },
  146. { "weeknum 2.5", {2010, 1, 5}, [{"W", "1"}] },
  147. % Example where the ISO year is three days into
  148. % the previous Gregorian year
  149. { "weeknum 3.1", {2008, 12, 28}, [{"W", "52"}] },
  150. { "weeknum 3.2", {2008, 12, 29}, [{"W", "1"}] },
  151. { "weeknum 3.3", {2008, 12, 30}, [{"W", "1"}] },
  152. { "weeknum 3.4", {2008, 12, 31}, [{"W", "1"}] },
  153. { "weeknum 3.5", {2009, 1, 1}, [{"W", "1"}] },
  154. % freeform tests
  155. { "weeknum 4.1", {2008, 2, 28}, [{"W", "9"}] },
  156. { "weeknum 4.2", {1975, 7, 24}, [{"W","30"}] },
  157. % Yearweek tests. Largely based on examples from :
  158. % http://en.wikipedia.org/wiki/ISO_week_date
  159. { "weeknum_year 1.1", {2005, 1, 1}, [{"o", "2004"}] },
  160. { "weeknum_year 1.2", {2005, 1, 2}, [{"o", "2004"}] },
  161. { "weeknum_year 1.3", {2005, 12, 31}, [{"o", "2005"}] },
  162. { "weeknum_year 1.4", {2007, 1, 1}, [{"o", "2007"}] },
  163. { "weeknum_year 1.5", {2007, 12, 30}, [{"o", "2007"}] },
  164. { "weeknum_year 1.6", {2007, 12, 31}, [{"o", "2008"}] },
  165. { "weeknum_year 1.6", {2008, 1, 1}, [{"o", "2008"}] },
  166. { "weeknum_year 1.7", {2008, 12, 29}, [{"o", "2009"}] },
  167. { "weeknum_year 1.8", {2008, 12, 31}, [{"o", "2009"}] },
  168. { "weeknum_year 1.9", {2009, 1, 1}, [{"o", "2009"}] },
  169. { "weeknum_year 1.10", {2009, 12, 31}, [{"o", "2009"}] },
  170. { "weeknum_year 1.11", {2010, 1, 3}, [{"o", "2009"}] },
  171. % Examples where the ISO year is three days into
  172. % the next Gregorian year
  173. { "weeknum_year 2.1", {2009, 12, 31}, [{"o", "2009"}] },
  174. { "weeknum_year 2.2", {2010, 1, 1}, [{"o", "2009"}] },
  175. { "weeknum_year 2.3", {2010, 1, 2}, [{"o", "2009"}] },
  176. { "weeknum_year 2.4", {2010, 1, 3}, [{"o", "2009"}] },
  177. { "weeknum_year 2.5", {2010, 1, 5}, [{"o", "2010"}] },
  178. % Example where the ISO year is three days into
  179. % the previous Gregorian year
  180. { "weeknum_year 3.1", {2008, 12, 28}, [{"o", "2008"}] },
  181. { "weeknum_year 3.2", {2008, 12, 29}, [{"o", "2009"}] },
  182. { "weeknum_year 3.3", {2008, 12, 30}, [{"o", "2009"}] },
  183. { "weeknum_year 3.4", {2008, 12, 31}, [{"o", "2009"}] },
  184. { "weeknum_year 3.5", {2009, 1, 1}, [{"o", "2009"}] },
  185. % freeform tests
  186. { "weeknum_year 4.1", {2008, 2, 28}, [{"o", "2008"}] },
  187. { "weeknum_year 4.2", {1975, 7, 24}, [{"o", "1975"}] },
  188. % Ordinal suffix tests.
  189. { "Ordinal suffix 1", {1984,1,1}, [{"S", "st"}] },
  190. { "Ordinal suffix 2", {1984,2,2}, [{"S", "nd"}] },
  191. { "Ordinal suffix 3", {1984,3,3}, [{"S", "rd"}] },
  192. { "Ordinal suffix 4", {1984,4,4}, [{"S", "th"}] },
  193. { "Ordinal suffix 5", {1984,6,5}, [{"S", "th"}] },
  194. { "Ordinal suffix 7", {1984,2,9}, [{"S", "th"}] },
  195. { "Ordinal suffix 8", {1984,9,9}, [{"S", "th"}] },
  196. { "Ordinal suffix 9", {1984,11,10}, [{"S", "th"}] },
  197. { "Ordinal suffix 10", {1984,12,11}, [{"S", "th"}] },
  198. { "Ordinal suffix 11", {1984,8,12}, [{"S", "th"}] },
  199. { "Ordinal suffix 12", {1984,1,19}, [{"S", "th"}] },
  200. { "Ordinal suffix 13", {1984,2,20}, [{"S", "th"}] },
  201. { "Ordinal suffix 14", {1984,2,21}, [{"S", "st"}] },
  202. { "Ordinal suffix 15", {1984,7,22}, [{"S", "nd"}] },
  203. { "Ordinal suffix 16", {1984,6,23}, [{"S", "rd"}] },
  204. { "Ordinal suffix 17", {1984,5,24}, [{"S", "th"}] },
  205. { "Ordinal suffix 18", {1984,1,29}, [{"S", "th"}] },
  206. { "Ordinal suffix 19", {1984,3,30}, [{"S", "th"}] },
  207. { "Ordinal suffix 20", {1984,1,31}, [{"S", "st"}] },
  208. { "Ordinal suffix 21", {1984,1,310}, [{"S", "th"}] },
  209. { "Ordinal suffix 22", {1984,1,121}, [{"S", "st"}] }
  210. ]),
  211. io:format("Date format failures: ~p~n~n", [Failures]),
  212. ok.
  213. test_group_runner([]) -> 0;
  214. test_group_runner([{Info, DateParam, Tests} | Rest]) ->
  215. io:format(" Test ~p -> ", [Info]),
  216. PassCount = test_runner(DateParam, Tests),
  217. case PassCount =:= length(Tests) of
  218. true ->
  219. io:format("Passed ~p/~p~n", [PassCount, length(Tests)]);
  220. _ ->
  221. io:format("~nFailed ~p/~p~n", [length(Tests) - PassCount, length(Tests)])
  222. end,
  223. test_group_runner(Rest) + length(Tests) - PassCount.
  224. test_runner(DateParam, Tests) ->
  225. test_runner(DateParam, Tests, 1, 0).
  226. test_runner(_DateParam, [], _TestNum, PassCount) ->
  227. PassCount;
  228. test_runner(DateParam, [{Input, Expect} | Rest], TestNum, PassCount) ->
  229. Text = "'" ++ Input ++ "' -> '" ++ Expect ++ "'",
  230. IsPass = is(TestNum, Text, erlydtl_dateformat:format(DateParam, Input), Expect),
  231. test_runner(DateParam, Rest, TestNum + 1, PassCount + IsPass).
  232. is(_TestNum, _Text, Input1, Input2) when Input1 =:= Input2 ->
  233. 1;
  234. is(TestNum, Text, Input1, Input2) ->
  235. io:format("~nnot ok ~p - ~s~n got : ~p~n expected : ~p", [
  236. TestNum, Text, Input1, Input2]),
  237. 0.