erlydtl_dateformat_tests.erl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. -module(erlydtl_dateformat_tests).
  2. -include_lib("eunit/include/eunit.hrl").
  3. all_dateformats_test_() ->
  4. [{Title, [test_fun(Param, Test) || Test <- Tests]}
  5. || {Title, Param, Tests} <- test_defs()].
  6. test_fun(Param, {Input, Expect}) ->
  7. ?_assertMatch(Expect, erlydtl_dateformat:format(Param, Input)).
  8. test_defs() ->
  9. [{"date 1",
  10. {1979, 7, 8}, % just a date
  11. [{"a", "a.m."}, {"A", "AM"}, {"c", "1979-07-08T00:00:00"},
  12. {"d", "08"}, {"D", "Sun"}, {"f", "12"}, {"F", "July"},
  13. {"g", "12"}, {"G", "0"},
  14. {"h", "12"}, {"H", "00"}, {"i", "00"},
  15. {"j", "8"}, {"l", "Sunday"}, {"L", "False"},
  16. {"m", "07"}, {"M", "Jul"}, {"b", "jul"},
  17. {"n", "7"}, {"N", "July"}, {"P", "midnight"},
  18. {"s", "00"}, {"S", "th"}, {"t", "31"},
  19. {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
  20. {"jS F Y H:i", "8th July 1979 00:00"},
  21. {"jS \\o\\f F", "8th of July"},
  22. %% We expect these to come back verbatim
  23. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%%", "%%"}
  24. %% TODO : timzeone related tests.
  25. %%{"r", "Sun, 8 Jul 1979 00:00:00 +0000"},
  26. %%{"O", "0000"},
  27. %%{"T", "CET"},
  28. %%{"U", "300531600"},
  29. %%{"Z", "3600"}
  30. ]
  31. },
  32. {"datetime 1",
  33. {{1979, 7, 8}, {22, 7, 12}}, %% date/time tuple
  34. [{"a", "p.m."}, {"A", "PM"}, {"c", "1979-07-08T22:07:12"},
  35. {"d", "08"}, {"D", "Sun"}, {"f", "10:07"}, {"F", "July"},
  36. {"g", "10"}, {"G", "22"},
  37. {"h", "10"}, {"H", "22"}, {"i", "07"},
  38. {"j", "8"}, {"l", "Sunday"}, {"L", "False"},
  39. {"m", "07"}, {"M", "Jul"}, {"b", "jul"},
  40. {"n", "7"}, {"N", "July"}, {"P", "10:07 p.m."},
  41. {"s", "12"}, {"S", "th"}, {"t", "31"},
  42. {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
  43. {"jS F Y H:i", "8th July 1979 22:07"},
  44. {"jS \\o\\f F", "8th of July"},
  45. %% We expect these to come back verbatim
  46. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%%", "%%"}
  47. %% TODO : timzeone related tests.
  48. %% {"r", "Sun, 8 Jul 1979 22:07:12 +0000"},
  49. %% {"O", "0000"},
  50. %% {"T", "CET"},
  51. %% {"U", "300531600"},
  52. %% {"Z", "3600"}
  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. {"datetime 3",
  79. {{2004, 2, 29}, {12, 0, 59}}, %% date/time tuple
  80. [{"a", "p.m."}, {"A", "PM"}, {"c", "2004-02-29T12:00:59"},
  81. {"d", "29"}, {"D", "Sun"}, {"f", "12"}, {"F", "February"},
  82. {"g", "12"}, {"G", "12"},
  83. {"h", "12"}, {"H", "12"}, {"i", "00"},
  84. {"j", "29"}, {"l", "Sunday"}, {"L", "True"},
  85. {"m", "02"}, {"M", "Feb"}, {"b", "feb"},
  86. {"n", "2"}, {"N", "Feb."}, {"P", "noon"},
  87. {"s", "59"}, {"S", "th"}, {"t", "29"},
  88. {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
  89. {"jS F Y H:i", "29th February 2004 12:00"},
  90. {"jS \\o\\f F", "29th of February"},
  91. %% We expect these to come back verbatim
  92. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%%", "%%"}
  93. %% TODO : timzeone related tests.
  94. %% {"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
  95. %% {"O", "0000"},
  96. %% {"T", "CET"},
  97. %% {"U", "300531600"},
  98. %% {"Z", "3600"}
  99. ]
  100. },
  101. {"datetime 4",
  102. {{2004, 2, 29}, {12, 0, 09.256687}}, %% date/time tuple
  103. [{"a", "p.m."}, {"A", "PM"}, {"c", "2004-02-29T12:00:09"},
  104. {"d", "29"}, {"D", "Sun"}, {"f", "12"}, {"F", "February"},
  105. {"g", "12"}, {"G", "12"},
  106. {"h", "12"}, {"H", "12"}, {"i", "00"},
  107. {"j", "29"}, {"l", "Sunday"}, {"L", "True"},
  108. {"m", "02"}, {"M", "Feb"}, {"b", "feb"},
  109. {"n", "2"}, {"N", "Feb."}, {"P", "noon"},
  110. {"s", "09"}, {"S", "th"}, {"t", "29"},
  111. {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
  112. {"jS F Y H:i", "29th February 2004 12:00"},
  113. {"jS \\o\\f F", "29th of February"},
  114. %% We expect these to come back verbatim
  115. {"x", "x"}, {"C", "C"}, {";", ";"}, {"%%", "%%"}
  116. %% TODO : timzeone related tests.
  117. %% {"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
  118. %% {"O", "0000"},
  119. %% {"T", "CET"},
  120. %% {"U", "300531600"},
  121. %% {"Z", "3600"}
  122. ]
  123. },
  124. %% Weeknum tests. Largely based on examples from :
  125. %% http://en.wikipedia.org/wiki/ISO_week_date
  126. { "weeknum 1.1", {2005, 1, 1}, [{"W", "53"}] },
  127. { "weeknum 1.2", {2005, 1, 2}, [{"W", "53"}] },
  128. { "weeknum 1.3", {2005, 12, 31}, [{"W", "52"}] },
  129. { "weeknum 1.4", {2007, 1, 1}, [{"W", "1"}] },
  130. { "weeknum 1.5", {2007, 12, 30}, [{"W", "52"}] },
  131. { "weeknum 1.6", {2007, 12, 31}, [{"W", "1"}] },
  132. { "weeknum 1.6", {2008, 1, 1}, [{"W", "1"}] },
  133. { "weeknum 1.7", {2008, 12, 29}, [{"W", "1"}] },
  134. { "weeknum 1.8", {2008, 12, 31}, [{"W", "1"}] },
  135. { "weeknum 1.9", {2009, 1, 1}, [{"W", "1"}] },
  136. { "weeknum 1.10", {2009, 12, 31}, [{"W", "53"}] },
  137. { "weeknum 1.11", {2010, 1, 3}, [{"W", "53"}] },
  138. %% Examples where the ISO year is three days into
  139. %% the next Gregorian year
  140. { "weeknum 2.1", {2009, 12, 31}, [{"W", "53"}] },
  141. { "weeknum 2.2", {2010, 1, 1}, [{"W", "53"}] },
  142. { "weeknum 2.3", {2010, 1, 2}, [{"W", "53"}] },
  143. { "weeknum 2.4", {2010, 1, 3}, [{"W", "53"}] },
  144. { "weeknum 2.5", {2010, 1, 5}, [{"W", "1"}] },
  145. %% Example where the ISO year is three days into
  146. %% the previous Gregorian year
  147. { "weeknum 3.1", {2008, 12, 28}, [{"W", "52"}] },
  148. { "weeknum 3.2", {2008, 12, 29}, [{"W", "1"}] },
  149. { "weeknum 3.3", {2008, 12, 30}, [{"W", "1"}] },
  150. { "weeknum 3.4", {2008, 12, 31}, [{"W", "1"}] },
  151. { "weeknum 3.5", {2009, 1, 1}, [{"W", "1"}] },
  152. %% freeform tests
  153. { "weeknum 4.1", {2008, 2, 28}, [{"W", "9"}] },
  154. { "weeknum 4.2", {1975, 7, 24}, [{"W","30"}] },
  155. %% Yearweek tests. Largely based on examples from :
  156. %% http://en.wikipedia.org/wiki/ISO_week_date
  157. { "weeknum_year 1.1", {2005, 1, 1}, [{"o", "2004"}] },
  158. { "weeknum_year 1.2", {2005, 1, 2}, [{"o", "2004"}] },
  159. { "weeknum_year 1.3", {2005, 12, 31}, [{"o", "2005"}] },
  160. { "weeknum_year 1.4", {2007, 1, 1}, [{"o", "2007"}] },
  161. { "weeknum_year 1.5", {2007, 12, 30}, [{"o", "2007"}] },
  162. { "weeknum_year 1.6", {2007, 12, 31}, [{"o", "2008"}] },
  163. { "weeknum_year 1.6", {2008, 1, 1}, [{"o", "2008"}] },
  164. { "weeknum_year 1.7", {2008, 12, 29}, [{"o", "2009"}] },
  165. { "weeknum_year 1.8", {2008, 12, 31}, [{"o", "2009"}] },
  166. { "weeknum_year 1.9", {2009, 1, 1}, [{"o", "2009"}] },
  167. { "weeknum_year 1.10", {2009, 12, 31}, [{"o", "2009"}] },
  168. { "weeknum_year 1.11", {2010, 1, 3}, [{"o", "2009"}] },
  169. %% Examples where the ISO year is three days into
  170. %% the next Gregorian year
  171. { "weeknum_year 2.1", {2009, 12, 31}, [{"o", "2009"}] },
  172. { "weeknum_year 2.2", {2010, 1, 1}, [{"o", "2009"}] },
  173. { "weeknum_year 2.3", {2010, 1, 2}, [{"o", "2009"}] },
  174. { "weeknum_year 2.4", {2010, 1, 3}, [{"o", "2009"}] },
  175. { "weeknum_year 2.5", {2010, 1, 5}, [{"o", "2010"}] },
  176. %% Example where the ISO year is three days into
  177. %% the previous Gregorian year
  178. { "weeknum_year 3.1", {2008, 12, 28}, [{"o", "2008"}] },
  179. { "weeknum_year 3.2", {2008, 12, 29}, [{"o", "2009"}] },
  180. { "weeknum_year 3.3", {2008, 12, 30}, [{"o", "2009"}] },
  181. { "weeknum_year 3.4", {2008, 12, 31}, [{"o", "2009"}] },
  182. { "weeknum_year 3.5", {2009, 1, 1}, [{"o", "2009"}] },
  183. %% freeform tests
  184. { "weeknum_year 4.1", {2008, 2, 28}, [{"o", "2008"}] },
  185. { "weeknum_year 4.2", {1975, 7, 24}, [{"o", "1975"}] },
  186. %% Ordinal suffix tests.
  187. { "Ordinal suffix 1", {1984,1,1}, [{"S", "st"}] },
  188. { "Ordinal suffix 2", {1984,2,2}, [{"S", "nd"}] },
  189. { "Ordinal suffix 3", {1984,3,3}, [{"S", "rd"}] },
  190. { "Ordinal suffix 4", {1984,4,4}, [{"S", "th"}] },
  191. { "Ordinal suffix 5", {1984,6,5}, [{"S", "th"}] },
  192. { "Ordinal suffix 7", {1984,2,9}, [{"S", "th"}] },
  193. { "Ordinal suffix 8", {1984,9,9}, [{"S", "th"}] },
  194. { "Ordinal suffix 9", {1984,11,10}, [{"S", "th"}] },
  195. { "Ordinal suffix 10", {1984,12,11}, [{"S", "th"}] },
  196. { "Ordinal suffix 11", {1984,8,12}, [{"S", "th"}] },
  197. { "Ordinal suffix 12", {1984,1,19}, [{"S", "th"}] },
  198. { "Ordinal suffix 13", {1984,2,20}, [{"S", "th"}] },
  199. { "Ordinal suffix 14", {1984,2,21}, [{"S", "st"}] },
  200. { "Ordinal suffix 15", {1984,7,22}, [{"S", "nd"}] },
  201. { "Ordinal suffix 16", {1984,6,23}, [{"S", "rd"}] },
  202. { "Ordinal suffix 17", {1984,5,24}, [{"S", "th"}] },
  203. { "Ordinal suffix 18", {1984,1,29}, [{"S", "th"}] },
  204. { "Ordinal suffix 19", {1984,3,30}, [{"S", "th"}] },
  205. { "Ordinal suffix 20", {1984,1,31}, [{"S", "st"}] },
  206. { "Ordinal suffix 21", {1984,1,310}, [{"S", "th"}] },
  207. { "Ordinal suffix 22", {1984,1,121}, [{"S", "st"}] }
  208. ].