From f66c096d75721ccdb3b57326b418b6c6443e8927 Mon Sep 17 00:00:00 2001 From: Dan Minor Date: Tue, 27 Oct 2020 20:39:57 +0000 Subject: [PATCH] Bug 1426907 - Rework DateTimeFormat gtests to not rely on hard-coded connector patterns; r=zbraniecki The connector patterns can change over time, we should instead check for the date time components that we expect to be present. Differential Revision: https://phabricator.services.mozilla.com/D94849 --- .../locale/tests/gtest/TestDateTimeFormat.cpp | 119 ++++++++---------- 1 file changed, 52 insertions(+), 67 deletions(-) diff --git a/intl/locale/tests/gtest/TestDateTimeFormat.cpp b/intl/locale/tests/gtest/TestDateTimeFormat.cpp index e0ce925a87c1..e585aff4adea 100644 --- a/intl/locale/tests/gtest/TestDateTimeFormat.cpp +++ b/intl/locale/tests/gtest/TestDateTimeFormat.cpp @@ -3,28 +3,6 @@ namespace mozilla { -// Normalise time. -static nsAutoCString nt(nsAutoCString aDatetime) { - nsAutoCString datetime = aDatetime; - - // Replace "January 01" with "January 1" (found on Windows). - int32_t ind = datetime.Find("January 01"); - if (ind != kNotFound) datetime.ReplaceLiteral(ind, 10, "January 1"); - - // Strip trailing " GMT" (found on Mac/Linux). - ind = datetime.Find(" GMT"); - if (ind != kNotFound) datetime.Truncate(ind); - - // Strip leading "Thursday, " or "Wednesday, " (found on Windows). - ind = datetime.Find("Thursday, "); - if (ind == 0) datetime.ReplaceLiteral(0, 10, ""); - - ind = datetime.Find("Wednesday, "); - if (ind == 0) datetime.ReplaceLiteral(0, 11, ""); - - return datetime; -} - TEST(DateTimeFormat, FormatPRExplodedTime) { PRTime prTime = 0; @@ -38,23 +16,26 @@ TEST(DateTimeFormat, FormatPRExplodedTime) nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("January 1, 1970, 12:00:00 AM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:00:00 AM") != kNotFound); prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("January 1, 1970, 12:19:00 AM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:19:00 AM") != kNotFound); prExplodedTime = {0, 0, 0, 7, 1, 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("January 1, 1970, 7:00:00 AM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("7:00:00 AM") != kNotFound); prExplodedTime = { 0, 0, 29, 11, 1, @@ -62,22 +43,27 @@ TEST(DateTimeFormat, FormatPRExplodedTime) rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("January 1, 1970, 11:29:00 AM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:29:00 AM") != kNotFound); prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("December 31, 1969, 11:37:00 PM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:37:00 PM") != kNotFound); prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("December 31, 1969, 5:00:00 PM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("5:00:00 PM") != kNotFound); prExplodedTime = { 0, 0, 47, 14, 31, @@ -85,8 +71,10 @@ TEST(DateTimeFormat, FormatPRExplodedTime) rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("December 31, 1969, 2:47:00 PM", - nt(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("2:47:00 PM") != kNotFound); } TEST(DateTimeFormat, DateFormatSelectors) @@ -123,23 +111,6 @@ TEST(DateTimeFormat, DateFormatSelectors) ASSERT_STREQ("Thu", NS_ConvertUTF16toUTF8(formattedTime).get()); } -// Normalise time. -static nsAutoCString ntd(nsAutoCString aDatetime) { - nsAutoCString datetime = aDatetime; - - // Strip trailing " GMT" (found on Mac/Linux). - int32_t ind = datetime.Find(" GMT"); - if (ind != kNotFound) datetime.Truncate(ind); - - // Strip leading "Donnerstag, " or "Mittwoch, " (found on Windows). - ind = datetime.Find("Donnerstag, "); - if (ind == 0) datetime.Replace(0, 12, ""); - ind = datetime.Find("Mittwoch, "); - if (ind == 0) datetime.Replace(0, 10, ""); - - return datetime; -} - TEST(DateTimeFormat, FormatPRExplodedTimeForeign) { PRTime prTime = 0; @@ -153,23 +124,29 @@ TEST(DateTimeFormat, FormatPRExplodedTimeForeign) nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("1. Januar 1970, 00:00:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("00:00:00") != kNotFound); prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("1. Januar 1970, 00:19:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("00:19:00") != kNotFound); prExplodedTime = {0, 0, 0, 7, 1, 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("1. Januar 1970, 07:00:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("07:00:00") != kNotFound); prExplodedTime = { 0, 0, 29, 11, 1, @@ -177,22 +154,28 @@ TEST(DateTimeFormat, FormatPRExplodedTimeForeign) rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("1. Januar 1970, 11:29:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:29:00") != kNotFound); prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("31. Dezember 1969, 23:37:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("23:37:00") != kNotFound); prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("31. Dezember 1969, 17:00:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("17:00:00") != kNotFound); prExplodedTime = { 0, 0, 47, 14, 31, @@ -200,8 +183,10 @@ TEST(DateTimeFormat, FormatPRExplodedTimeForeign) rv = mozilla::DateTimeFormat::FormatPRExplodedTime( kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); ASSERT_TRUE(NS_SUCCEEDED(rv)); - ASSERT_STREQ("31. Dezember 1969, 14:47:00", - ntd(NS_ConvertUTF16toUTF8(formattedTime)).get()); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("14:47:00") != kNotFound); } TEST(DateTimeFormat, DateFormatSelectorsForeign)