补充test用例

Signed-off-by: zhangdd_ewan <zhangdongdong50@huawei.com>
This commit is contained in:
zhangdd_ewan 2023-11-29 20:31:34 +08:00
parent 0c854b94a7
commit 407104c33f
6 changed files with 207 additions and 19 deletions

View File

@ -91,6 +91,7 @@ void GetAllValidLocalesTag(std::unordered_set<std::string>& allValidLocalesLangu
for (int i = 0; i < validCount; i++) {
allValidLocalesLanguageTag.insert(validLocales[i].getLanguage());
}
delete[] validLocales;
init = true;
}
} // namespace I18n

View File

@ -22,6 +22,7 @@ ohos_unittest("intl_test") {
"unittest/i18n_test.cpp",
"unittest/intl_test.cpp",
"unittest/locale_config_test.cpp",
"unittest/mock/src/generate_ics_file.cpp",
"unittest/mock/src/i18n_timezone_mock.cpp",
"unittest/mock/src/phone_number_format_mock.cpp",
]
@ -43,6 +44,7 @@ ohos_unittest("intl_test") {
"//third_party/googletest:gtest_main",
"//third_party/icu/icu4c:shared_icui18n",
"//third_party/icu/icu4c:shared_icuuc",
"//third_party/libphonenumber/cpp:phonenumber_standard",
"//third_party/libpng:libpng",
]
}

View File

@ -19,7 +19,9 @@
#include "character.h"
#include "collator.h"
#include "date_time_filter.h"
#include "date_time_format.h"
#include "date_time_rule.h"
#include "holiday_manager.h"
#include "i18n_break_iterator.h"
#include "i18n_calendar.h"
@ -34,11 +36,14 @@
#include "phone_number_format.h"
#include "plural_rules.h"
#include "preferred_language.h"
#include "regex_rule.h"
#include "relative_time_format.h"
#include "system_locale_manager.h"
#include "taboo_utils.h"
#include "taboo.h"
#include "utils.h"
#include "intl_test.h"
#include "generate_ics_file.h"
using namespace OHOS::Global::I18n;
using testing::ext::TestSize;
@ -80,7 +85,7 @@ HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1)
locales.push_back("jessie");
locales.push_back(locale);
map<string, string> options = { { "year", "numeric" },
{ "month", "long" },
{ "month", "narrow" },
{ "day", "numeric" },
{ "hour", "numeric" },
{ "minute", "2-digit" },
@ -96,7 +101,7 @@ HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1)
string out = dateFormat->Format(milliseconds);
EXPECT_EQ(out, expects);
EXPECT_EQ(dateFormat->GetYear(), "numeric");
EXPECT_EQ(dateFormat->GetMonth(), "long");
EXPECT_EQ(dateFormat->GetMonth(), "narrow");
EXPECT_EQ(dateFormat->GetDay(), "numeric");
EXPECT_EQ(dateFormat->GetHour(), "numeric");
EXPECT_EQ(dateFormat->GetMinute(), "2-digit");
@ -432,7 +437,8 @@ HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1)
EXPECT_EQ(out, expects);
options = {
{ "dateStyle", "long" },
{ "hourCycle", "h11" }
{ "hourCycle", "h11" },
{ "hour12", "fakeValue" }
};
DateTimeFormat *dateFormatH11 = new (std::nothrow) DateTimeFormat(locales, options);
options.insert({ "hourCycle", "h12" });
@ -447,9 +453,15 @@ HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1)
delete dateFormatH24;
locales.clear();
DateTimeFormat *dateFormatEmpty = new (std::nothrow) DateTimeFormat(locales, options);
locales.push_back("fake locale");
options = {};
locales.push_back("@@@&&");
options = { { "dayPeriod", "short" } };
DateTimeFormat *dateFormatFake = new (std::nothrow) DateTimeFormat(locales, options);
options.insert({ "dayPeriod", "long" });
DateTimeFormat *dateFormatDayPeriod = new (std::nothrow) DateTimeFormat(locales, options);
delete dateFormatDayPeriod;
options.insert({ "dayPeriod", "narrow" });
DateTimeFormat *dateFormatDayPeriod2 = new (std::nothrow) DateTimeFormat(locales, options);
delete dateFormatDayPeriod2;
options.insert({ "dateStyle", "long" });
DateTimeFormat *dateFormatFake2 = new (std::nothrow) DateTimeFormat(locales, options);
delete dateFormatEmpty;
@ -515,14 +527,14 @@ HWTEST_F(IntlTest, IntlFuncTest0015, TestSize.Level1)
vector<string> locales;
locales.push_back("jessie");
locales.push_back(locale);
map<string, string> options = { { "timeZone", "America/Los_Angeles" }, { "timeZoneName", "long" } };
map<string, string> options = { { "timeZone", "America/Los_Angeles" }, { "timeZoneName", "short" } };
DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
if (!dateFormat) {
EXPECT_TRUE(false);
return;
}
EXPECT_EQ(dateFormat->GetTimeZone(), "America/Los_Angeles");
EXPECT_EQ(dateFormat->GetTimeZoneName(), "long");
EXPECT_EQ(dateFormat->GetTimeZoneName(), "short");
delete dateFormat;
}
@ -688,7 +700,7 @@ HWTEST_F(IntlTest, IntlFuncTest0019, TestSize.Level1)
{
// abnormal test case
vector<string> locales;
locales.push_back("fake locale");
locales.push_back("7776@");
map<string, string> inputOptions = {
{ "localeMatcher", "fake value" },
{ "usage", "fake value"},
@ -737,7 +749,7 @@ HWTEST_F(IntlTest, IntlFuncTest0020, TestSize.Level1)
{
// abnormal test case
vector<string> locales;
locales.push_back("fake locale");
locales.push_back("$$##");
map<string, string> inputOptions = {
{ "ignorePunctuation", "fake value" },
{ "collation", "fake value"},
@ -881,7 +893,7 @@ HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1)
// abnormal test cast
// normal test case
vector<string> locales;
locales.push_back("fake_locale");
locales.push_back("$$$###");
map<string, string> options = {
{ "fake_localeMatcher", "best fit" },
{ "type", "fake_cardinal" },
@ -1099,8 +1111,8 @@ HWTEST_F(IntlTest, IntlFuncTest0028, TestSize.Level1)
HWTEST_F(IntlTest, IntlFuncTest0029, TestSize.Level1)
{
vector<string> locales;
locales.push_back("xxxx");
locales.push_back("zh-Hans");
locales.push_back("####");
locales.push_back("zh-Hans-u-nu-latn");
map<string, string> options = {
{ "localeMatcher", "best fit" },
{ "numeric", "auto" },
@ -1271,7 +1283,7 @@ HWTEST_F(IntlTest, IntlFuncTest0033, TestSize.Level1)
vector<string> locales;
locales.push_back("zh-Hans");
map<string, string> options = {
{ "locale", "fake locale" },
{ "locale", "@@##" },
{ "currency", "fake currency" },
{ "currencySign", "fake sign" },
{ "currencyDisplay", "symbol" },
@ -1319,7 +1331,7 @@ HWTEST_F(IntlTest, IntlFuncTest0034, TestSize.Level1)
vector<string> locales;
locales.push_back("en-US");
map<string, string> options = {
{ "locale", "fake locale" },
{ "locale", "$$$##43" },
{ "currency", "USD" },
{ "currencySign", "symbol" },
{ "currencyDisplay", "symbol" },
@ -1546,7 +1558,6 @@ HWTEST_F(IntlTest, IntlFuncTest0039, TestSize.Level1)
{ "dateStyle", "medium" },
{ "timeStyle", "long" },
{ "hourCycle", "h12" },
{ "timeZone", "Asia/Shanghai" },
{ "numberingSystem", "latn" },
{ "hour12", "true" },
{ "weekday", "long" },
@ -1951,7 +1962,7 @@ HWTEST_F(IntlTest, IntlFuncTest0050, TestSize.Level1)
{
const std::string langugae = "zh-CN";
const std::string langugaeDe = "de-DE";
const std::string fakeLangugae = "fake-language";
const std::string fakeLangugae = "1**1";
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
PreferredLanguage::AddPreferredLanguage(langugae, 0, errorCode);
EXPECT_TRUE(errorCode != I18nErrorCode::SUCCESS);
@ -2041,6 +2052,8 @@ HWTEST_F(IntlTest, IntlFuncTest0053, TestSize.Level1)
std::string number = "+8618622350085";
std::string formated = phoneNumberFormat->format(number);
EXPECT_EQ(formated, "186 2235 0085");
std::string location2 = phoneNumberFormat->getLocationName("fake-number", "zh-CN");
EXPECT_EQ(location2, "");
delete phoneNumberFormat;
}
@ -2056,7 +2069,11 @@ HWTEST_F(IntlTest, IntlFuncTest0054, TestSize.Level1)
EXPECT_EQ(res1, "China");
std::string res2 = tabooUtils->ReplaceLanguageName("zh", "en", "chinese");
EXPECT_EQ(res2, "chinese");
std::string res3 = tabooUtils->ReplaceCountryName("TW", "zh-Hans", "中国台湾");
EXPECT_EQ(res3, "中国台湾");
delete tabooUtils;
Taboo* taboo = new Taboo();
delete taboo;
}
/**
@ -2099,7 +2116,7 @@ HWTEST_F(IntlTest, IntlFuncTest0055, TestSize.Level1)
EXPECT_EQ(resultHashEn, 8);
int32_t resultHashQaagEn = LocaleCompare::Compare("en-Qaag-WS", "en-001");
EXPECT_EQ(resultHashQaagEn, 8);
I18nBreakIterator *breakIterator = new I18nBreakIterator("fake locale");
I18nBreakIterator *breakIterator = new I18nBreakIterator("2--**");
breakIterator->Current();
breakIterator->First();
breakIterator->Last();
@ -2215,7 +2232,7 @@ HWTEST_F(IntlTest, IntlFuncTest0058, TestSize.Level1)
*/
HWTEST_F(IntlTest, IntlFuncTest0059, TestSize.Level1)
{
std::vector<std::string> localeTags = { "fake locale tag"};
std::vector<std::string> localeTags = { "$$@@#"};
std::map<std::string, std::string> configs = {
{"style", "unit"}, {"unit", "fake unit"}
};
@ -2231,7 +2248,8 @@ HWTEST_F(IntlTest, IntlFuncTest0059, TestSize.Level1)
*/
HWTEST_F(IntlTest, IntlFuncTest0060, TestSize.Level1)
{
std::string path = "/system/lib/TR.ics";
IcsFileWriter icsFileWriter;
std::string path = icsFileWriter.GenerateFile();
HolidayManager *holiday_manager = new HolidayManager(path.c_str());
std::map<std::string, std::vector<HolidayInfoItem>> holidayDataMap;
std::vector<HolidayInfoItem> infoList;
@ -2253,6 +2271,7 @@ HWTEST_F(IntlTest, IntlFuncTest0060, TestSize.Level1)
EXPECT_TRUE(flag);
flag = holiday_manager->IsHoliday();
EXPECT_TRUE(!flag);
remove(path);
delete holiday_manager;
}
@ -2280,6 +2299,69 @@ HWTEST_F(IntlTest, IntlFuncTest061, TestSize.Level1)
EXPECT_EQ(numFmt->GetStyle(), style);
delete numFmt;
}
/**
* @tc.name: IntlFuncTest0062
* @tc.desc: Test Intl utils.cpp
* @tc.type: FUNC
*/
HWTEST_F(IntlTest, IntlFuncTest062, TestSize.Level1)
{
string emptyStr = "";
string sep = ";";
vector<string> dest;
std::unordered_set<std::string> allValidLocalesLanguageTag;
Split(emptyStr, sep, dest);
int32_t status = 0;
string numberStr = "12345678901234567890123456789012345678901234567890987654321";
ConvertString2Int(numberStr, status);
icu::Locale locale("$$$$5%%%");
bool isValid = IsValidLocaleTag(locale);
EXPECT_TRUE(!isValid);
GetAllValidLocalesTag(allValidLocalesLanguageTag);
EXPECT_TRUE(allValidLocalesLanguageTag.size() == 0);
std::string localeRule = "zh-Hans";
DateTimeRule* dateTimeRule = new DateTimeRule(localeRule);
DateTimeFilter* dateTimeFilter = new DateTimeFilter(localeRule, dateTimeRule);
delete dateTimeFilter;
delete dateTimeRule;
}
/**
* @tc.name: IntlFuncTest0062
* @tc.desc: Test Intl utils.cpp
* @tc.type: FUNC
*/
HWTEST_F(IntlTest, IntlFuncTest063, TestSize.Level1)
{
using namespace i18n::phonenumbers;
string regexStr = "[a-z]1?";
icu::UnicodeString regex(regexStr.c_str());
std::string isValidType = "PrexxxxSuf";
std::string handleType = "EndWithMobile";
std::string insensitive = "False";
std::string type = "xxx";
RegexRule* regexRule = new RegexRule(regex, isValidType, handleType, insensitive, type);
handleType = "fake";
isValidType = "Code";
RegexRule* regexRule2 = new RegexRule(regex, isValidType, handleType, insensitive, type);
icu::UnicodeString message(type.c_str());
i18n::phonenumbers::PhoneNumber phoneNumber;
PhoneNumberUtil* phoneNumberUtil = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
std::string input = "(010111111111111;ext=0782";
std::string country = "CN";
size_t size = input.length();
phoneNumberUtil->Parse(input, country, &phoneNumber);
PhoneNumberMatch* possibleNumber = new PhoneNumberMatch(size, input, phoneNumber);
PhoneNumberMatch* phoneNumberMatch = regexRule2->IsValid(possibleNumber, message);
EXPECT_TRUE(phoneNumberMatch != nullptr);
delete phoneNumberUtil;
delete phoneNumberMatch;
delete regexRule;
delete regexRule2;
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -80,6 +80,8 @@ int IntlFuncTest0058(void);
int IntlFuncTest0059(void);
int IntlFuncTest0060(void);
int IntlFuncTest0061(void);
int IntlFuncTest0062(void);
int IntlFuncTest0063(void);
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_GLOBAL_I18N_ICS_FILE_WRITER_H
#define OHOS_GLOBAL_I18N_ICS_FILE_WRITER_H
namespace OHOS {
namespace Global {
namespace I18n {
class IcsFileWriter {
public:
IcsFileWriter();
~IcsFileWriter();
std::string GenerateFile();
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
#endif

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <fstream>
#include <vector>
#include "generate_ics_file.h"
namespace OHOS {
namespace Global {
namespace I18n {
IcsFileWriter::IcsFileWriter()
{
}
IcsFileWriter::~IcsFileWriter()
{
}
std::string IcsFileWriter::GenerateFile()
{
using namespace std;
std::string filePath = "/system/lib/TR.ics";
ofstream fstream;
fstream.open(filePath, ios::out);
if (!fstream) {
printf("file is noll\r\n");
return filePath;
}
std::vector<std::string> list = {
"BEGIN:VCALENDAR\r\n",
"METHOD:PUBLISH\r\n",
"BEGIN:VEVENT\r\n",
"UID:1\r\n",
"DTSTART;VALUE=DATE:20220625\r\n",
"DTEND;VALUE=DATE:20220625\r\n",
"SUMMARY:Sacrifice Feast Holiday\r\n",
"RESOURCES;LANGUAGE=TR:Kurban Bayrami Tatili\r\n"
"END:VEVENT\r\n",
"BEGIN:VEVENT\r\n",
"UID:2\r\n",
"DTSTART;VALUE=DATE:20220626\r\n",
"DTEND;VALUE=DATE:20220626\r\n",
"SUMMARY:The Second Day of Sacrifice Feast\r\n",
"RESOURCES;LANGUAGE=TR:Kurban Bayrami 2. Günü\r\n"
"END:VEVENT\r\n",
"END:VCALENDAR\r\n"
};
for (size_t i = 0; i < list.size(); i++) {
fstream.write(list[i].c_str(), sizeof(list[i]));
}
fstream.close();
return filePath;
}
} // namespace I18n
} // namespace Global
} // namespace OHOS