erlydtl_dateformat_tests.erl 7.8 KB

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