PageRenderTime 287ms CodeModel.GetById 141ms app.highlight 17ms RepoModel.GetById 124ms 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
  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
 26#include "linden_common.h"
 27
 28#include "../test/lltut.h"
 29
 30#include "../lldateutil.h"
 31
 32#include "lldate.h"
 33#include "llstring.h"	// LLStringUtil::format()
 34#include "lltrans.h"
 35#include "llui.h"
 36
 37#include <map>
 38
 39
 40// Baked-in return values for getString()
 41std::map< std::string, std::string > gString;
 42
 43// Baked-in return values for getCountString()
 44// map of pairs of input xml_desc and integer count
 45typedef std::pair< std::string, int > count_string_t;
 46std::map< count_string_t, std::string > gCountString;
 47
 48std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args)
 49{
 50	std::string text = gString[xml_desc];
 51	LLStringUtil::format(text, args);
 52	return text;
 53}
 54
 55std::string LLTrans::getCountString(const std::string& language, const std::string& xml_desc, S32 count)
 56{
 57	count_string_t key(xml_desc, count);
 58	if (gCountString.find(key) == gCountString.end())
 59	{
 60		return std::string("Couldn't find ") + xml_desc;
 61	}
 62	return gCountString[ count_string_t(xml_desc, count) ];
 63}
 64
 65std::string LLUI::getLanguage()
 66{
 67	return "en";
 68}
 69
 70namespace tut
 71{
 72    struct dateutil
 73    {
 74		// Hard-code a "now" date so unit test doesn't change with
 75		// current time.  Because server strings are in Pacific time
 76		// roll this forward 8 hours to compensate.  This represents
 77		// 2009-12-31T00:00:00Z UTC.
 78		dateutil()
 79			:	mNow(std::string("2009-12-31T08:00:00Z"))
 80		{
 81			// copied from strings.xml
 82			gString["YearsMonthsOld"] = "[AGEYEARS] [AGEMONTHS] old";
 83			gString["YearsOld"] = "[AGEYEARS] old";
 84			gString["MonthsOld"] = "[AGEMONTHS] old";
 85			gString["WeeksOld"] = "[AGEWEEKS] old";
 86			gString["DaysOld"] = "[AGEDAYS] old";
 87			gString["TodayOld"] = "Joined today";
 88
 89			gCountString[ count_string_t("AgeYears", 1) ]  = "1 year";
 90			gCountString[ count_string_t("AgeYears", 2) ]  = "2 years";
 91			gCountString[ count_string_t("AgeMonths", 1) ] = "1 month";
 92			gCountString[ count_string_t("AgeMonths", 2) ] = "2 months";
 93			gCountString[ count_string_t("AgeMonths", 11) ]= "11 months";
 94			gCountString[ count_string_t("AgeWeeks", 1) ]  = "1 week";
 95			gCountString[ count_string_t("AgeWeeks", 2) ]  = "2 weeks";
 96			gCountString[ count_string_t("AgeWeeks", 3) ]  = "3 weeks";
 97			gCountString[ count_string_t("AgeWeeks", 4) ]  = "4 weeks";
 98			gCountString[ count_string_t("AgeDays", 1) ]   = "1 day";
 99			gCountString[ count_string_t("AgeDays", 2) ]   = "2 days";
100		}
101		LLDate mNow;
102    };
103    
104	typedef test_group<dateutil> dateutil_t;
105	typedef dateutil_t::object dateutil_object_t;
106	tut::dateutil_t tut_dateutil("LLDateUtil");
107
108	template<> template<>
109	void dateutil_object_t::test<1>()
110	{
111		set_test_name("Years");
112		ensure_equals("years + months",
113			LLDateUtil::ageFromDate("10/30/2007", mNow),
114			"2 years 2 months old" );
115		ensure_equals("years",
116			LLDateUtil::ageFromDate("12/31/2007", mNow),
117			"2 years old" );
118		ensure_equals("years",
119			LLDateUtil::ageFromDate("1/1/2008", mNow),
120			"1 year 11 months old" );
121		ensure_equals("single year + one month",
122			LLDateUtil::ageFromDate("11/30/2008", mNow),
123			"1 year 1 month old" );
124		ensure_equals("single year + a bit",
125			LLDateUtil::ageFromDate("12/12/2008", mNow),
126			"1 year old" );
127		ensure_equals("single year",
128			LLDateUtil::ageFromDate("12/31/2008", mNow),
129			"1 year old" );
130    }
131
132	template<> template<>
133	void dateutil_object_t::test<2>()
134	{
135		set_test_name("Months");
136		ensure_equals("months",
137			LLDateUtil::ageFromDate("10/30/2009", mNow),
138			"2 months old" );
139		ensure_equals("months 2",
140			LLDateUtil::ageFromDate("10/31/2009", mNow),
141			"2 months old" );
142		ensure_equals("single month",
143			LLDateUtil::ageFromDate("11/30/2009", mNow),
144			"1 month old" );
145	}
146
147	template<> template<>
148	void dateutil_object_t::test<3>()
149	{
150		set_test_name("Weeks");
151		ensure_equals("4 weeks",
152			LLDateUtil::ageFromDate("12/1/2009", mNow),
153			"4 weeks old" );
154		ensure_equals("weeks",
155			LLDateUtil::ageFromDate("12/17/2009", mNow),
156			"2 weeks old" );
157		ensure_equals("single week",
158			LLDateUtil::ageFromDate("12/24/2009", mNow),
159			"1 week old" );
160	}
161
162	template<> template<>
163	void dateutil_object_t::test<4>()
164	{
165		set_test_name("Days");
166		ensure_equals("days",
167			LLDateUtil::ageFromDate("12/29/2009", mNow),
168			"2 days old" );
169		ensure_equals("single day",
170			LLDateUtil::ageFromDate("12/30/2009", mNow),
171			"1 day old" );
172		ensure_equals("today",
173			LLDateUtil::ageFromDate("12/31/2009", mNow),
174			"Joined today" );
175	}
176
177	template<> template<>
178	void dateutil_object_t::test<5>()
179	{
180		set_test_name("2010 rollover");
181		LLDate now(std::string("2010-01-04T12:00:00Z"));
182		ensure_equals("days",
183			LLDateUtil::ageFromDate("12/13/2009", now),
184			"3 weeks old" );
185	}
186
187	//template<> template<>
188	//void dateutil_object_t::test<6>()
189	//{
190	//	set_test_name("ISO dates");
191	//	LLDate now(std::string("2010-01-04T12:00:00Z"));
192	//	ensure_equals("days",
193	//		LLDateUtil::ageFromDateISO("2009-12-13", now),
194	//		"3 weeks old" );
195	//}
196}