PageRenderTime 37ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/tests/lldateutil_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 196 lines | 135 code | 20 blank | 41 comment | 2 complexity | f154e875b5baa6b37ab1edd4733ac263 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lldateutil_test.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "../test/lltut.h"
  27. #include "../lldateutil.h"
  28. #include "lldate.h"
  29. #include "llstring.h" // LLStringUtil::format()
  30. #include "lltrans.h"
  31. #include "llui.h"
  32. #include <map>
  33. // Baked-in return values for getString()
  34. std::map< std::string, std::string > gString;
  35. // Baked-in return values for getCountString()
  36. // map of pairs of input xml_desc and integer count
  37. typedef std::pair< std::string, int > count_string_t;
  38. std::map< count_string_t, std::string > gCountString;
  39. std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args)
  40. {
  41. std::string text = gString[xml_desc];
  42. LLStringUtil::format(text, args);
  43. return text;
  44. }
  45. std::string LLTrans::getCountString(const std::string& language, const std::string& xml_desc, S32 count)
  46. {
  47. count_string_t key(xml_desc, count);
  48. if (gCountString.find(key) == gCountString.end())
  49. {
  50. return std::string("Couldn't find ") + xml_desc;
  51. }
  52. return gCountString[ count_string_t(xml_desc, count) ];
  53. }
  54. std::string LLUI::getLanguage()
  55. {
  56. return "en";
  57. }
  58. namespace tut
  59. {
  60. struct dateutil
  61. {
  62. // Hard-code a "now" date so unit test doesn't change with
  63. // current time. Because server strings are in Pacific time
  64. // roll this forward 8 hours to compensate. This represents
  65. // 2009-12-31T00:00:00Z UTC.
  66. dateutil()
  67. : mNow(std::string("2009-12-31T08:00:00Z"))
  68. {
  69. // copied from strings.xml
  70. gString["YearsMonthsOld"] = "[AGEYEARS] [AGEMONTHS] old";
  71. gString["YearsOld"] = "[AGEYEARS] old";
  72. gString["MonthsOld"] = "[AGEMONTHS] old";
  73. gString["WeeksOld"] = "[AGEWEEKS] old";
  74. gString["DaysOld"] = "[AGEDAYS] old";
  75. gString["TodayOld"] = "Joined today";
  76. gCountString[ count_string_t("AgeYears", 1) ] = "1 year";
  77. gCountString[ count_string_t("AgeYears", 2) ] = "2 years";
  78. gCountString[ count_string_t("AgeMonths", 1) ] = "1 month";
  79. gCountString[ count_string_t("AgeMonths", 2) ] = "2 months";
  80. gCountString[ count_string_t("AgeMonths", 11) ]= "11 months";
  81. gCountString[ count_string_t("AgeWeeks", 1) ] = "1 week";
  82. gCountString[ count_string_t("AgeWeeks", 2) ] = "2 weeks";
  83. gCountString[ count_string_t("AgeWeeks", 3) ] = "3 weeks";
  84. gCountString[ count_string_t("AgeWeeks", 4) ] = "4 weeks";
  85. gCountString[ count_string_t("AgeDays", 1) ] = "1 day";
  86. gCountString[ count_string_t("AgeDays", 2) ] = "2 days";
  87. }
  88. LLDate mNow;
  89. };
  90. typedef test_group<dateutil> dateutil_t;
  91. typedef dateutil_t::object dateutil_object_t;
  92. tut::dateutil_t tut_dateutil("LLDateUtil");
  93. template<> template<>
  94. void dateutil_object_t::test<1>()
  95. {
  96. set_test_name("Years");
  97. ensure_equals("years + months",
  98. LLDateUtil::ageFromDate("10/30/2007", mNow),
  99. "2 years 2 months old" );
  100. ensure_equals("years",
  101. LLDateUtil::ageFromDate("12/31/2007", mNow),
  102. "2 years old" );
  103. ensure_equals("years",
  104. LLDateUtil::ageFromDate("1/1/2008", mNow),
  105. "1 year 11 months old" );
  106. ensure_equals("single year + one month",
  107. LLDateUtil::ageFromDate("11/30/2008", mNow),
  108. "1 year 1 month old" );
  109. ensure_equals("single year + a bit",
  110. LLDateUtil::ageFromDate("12/12/2008", mNow),
  111. "1 year old" );
  112. ensure_equals("single year",
  113. LLDateUtil::ageFromDate("12/31/2008", mNow),
  114. "1 year old" );
  115. }
  116. template<> template<>
  117. void dateutil_object_t::test<2>()
  118. {
  119. set_test_name("Months");
  120. ensure_equals("months",
  121. LLDateUtil::ageFromDate("10/30/2009", mNow),
  122. "2 months old" );
  123. ensure_equals("months 2",
  124. LLDateUtil::ageFromDate("10/31/2009", mNow),
  125. "2 months old" );
  126. ensure_equals("single month",
  127. LLDateUtil::ageFromDate("11/30/2009", mNow),
  128. "1 month old" );
  129. }
  130. template<> template<>
  131. void dateutil_object_t::test<3>()
  132. {
  133. set_test_name("Weeks");
  134. ensure_equals("4 weeks",
  135. LLDateUtil::ageFromDate("12/1/2009", mNow),
  136. "4 weeks old" );
  137. ensure_equals("weeks",
  138. LLDateUtil::ageFromDate("12/17/2009", mNow),
  139. "2 weeks old" );
  140. ensure_equals("single week",
  141. LLDateUtil::ageFromDate("12/24/2009", mNow),
  142. "1 week old" );
  143. }
  144. template<> template<>
  145. void dateutil_object_t::test<4>()
  146. {
  147. set_test_name("Days");
  148. ensure_equals("days",
  149. LLDateUtil::ageFromDate("12/29/2009", mNow),
  150. "2 days old" );
  151. ensure_equals("single day",
  152. LLDateUtil::ageFromDate("12/30/2009", mNow),
  153. "1 day old" );
  154. ensure_equals("today",
  155. LLDateUtil::ageFromDate("12/31/2009", mNow),
  156. "Joined today" );
  157. }
  158. template<> template<>
  159. void dateutil_object_t::test<5>()
  160. {
  161. set_test_name("2010 rollover");
  162. LLDate now(std::string("2010-01-04T12:00:00Z"));
  163. ensure_equals("days",
  164. LLDateUtil::ageFromDate("12/13/2009", now),
  165. "3 weeks old" );
  166. }
  167. //template<> template<>
  168. //void dateutil_object_t::test<6>()
  169. //{
  170. // set_test_name("ISO dates");
  171. // LLDate now(std::string("2010-01-04T12:00:00Z"));
  172. // ensure_equals("days",
  173. // LLDateUtil::ageFromDateISO("2009-12-13", now),
  174. // "3 weeks old" );
  175. //}
  176. }