зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1719696 - Add a nicer assertion for checking the buffer contents; r=platform-i18n-reviewers,dminor
Differential Revision: https://phabricator.services.mozilla.com/D123820
This commit is contained in:
Родитель
7eeb5c0b59
Коммит
3971654d9f
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <string_view>
|
||||
#include "mozilla/DebugOnly.h"
|
||||
#include "mozilla/Utf8.h"
|
||||
#include "mozilla/Vector.h"
|
||||
|
||||
namespace mozilla::intl {
|
||||
|
@ -69,6 +70,77 @@ class TestBuffer {
|
|||
*/
|
||||
void clear() { mBuffer.clear(); }
|
||||
|
||||
/**
|
||||
* A utility function to convert UTF-16 strings to UTF-8 strings so that they
|
||||
* can be logged to stderr.
|
||||
*/
|
||||
static std::string toUtf8(mozilla::Span<const char16_t> input) {
|
||||
size_t buff_len = input.Length() * 3;
|
||||
std::string result(buff_len, ' ');
|
||||
result.reserve(buff_len);
|
||||
size_t result_len =
|
||||
ConvertUtf16toUtf8(input, mozilla::Span(result.data(), buff_len));
|
||||
result.resize(result_len);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* String buffers, especially UTF-16, do not assert nicely, and are difficult
|
||||
* to debug. This function is verbose in that it prints the buffer contents
|
||||
* and expected contents to stderr when they do not match.
|
||||
*
|
||||
* Usage:
|
||||
* ASSERT_TRUE(buffer.assertStringView(u"9/23/2002, 8:07:30 PM"));
|
||||
*
|
||||
* Here is what gtests output:
|
||||
*
|
||||
* Expected equality of these values:
|
||||
* buffer.get_string_view()
|
||||
* Which is: { '0' (48, 0x30), '9' (57, 0x39), '/' (47, 0x2F), ... }
|
||||
* "9/23/2002, 8:07:30 PM"
|
||||
* Which is: 0x11600afb9
|
||||
*
|
||||
* Here is what this method outputs:
|
||||
*
|
||||
* The buffer did not match:
|
||||
* Buffer:
|
||||
* u"9/23/2002, 8:07:30 PM"
|
||||
* Expected:
|
||||
* u"09/23/2002, 08:07:30 PM"
|
||||
*/
|
||||
bool verboseMatches(const CharType* aExpected) {
|
||||
std::basic_string_view<CharType> actualSV(data(), length());
|
||||
std::basic_string_view<CharType> expectedSV(aExpected);
|
||||
|
||||
if (actualSV.compare(expectedSV) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static_assert(std::is_same_v<CharType, char> ||
|
||||
std::is_same_v<CharType, char16_t>);
|
||||
|
||||
std::string actual;
|
||||
std::string expected;
|
||||
const char* startQuote;
|
||||
|
||||
if constexpr (std::is_same_v<CharType, char>) {
|
||||
actual = std::string(actualSV);
|
||||
expected = std::string(expectedSV);
|
||||
startQuote = "\"";
|
||||
}
|
||||
if constexpr (std::is_same_v<CharType, char16_t>) {
|
||||
actual = toUtf8(actualSV);
|
||||
expected = toUtf8(expectedSV);
|
||||
startQuote = "u\"";
|
||||
}
|
||||
|
||||
fprintf(stderr, "The buffer did not match:\n");
|
||||
fprintf(stderr, " Buffer:\n %s%s\"\n", startQuote, actual.c_str());
|
||||
fprintf(stderr, " Expected:\n %s%s\"\n", startQuote, expected.c_str());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector<C, inlineCapacity> mBuffer{};
|
||||
};
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ TEST(IntlDateTimeFormat, Style_enUS_utf8)
|
|||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), "Sep 23, 2002, 8:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches("Sep 23, 2002, 8:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Style_enUS_utf16)
|
||||
|
@ -42,7 +42,7 @@ TEST(IntlDateTimeFormat, Style_enUS_utf16)
|
|||
TestBuffer<char16_t> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), u"Sep 23, 2002, 8:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches(u"Sep 23, 2002, 8:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Style_ar_utf8)
|
||||
|
@ -51,7 +51,7 @@ TEST(IntlDateTimeFormat, Style_ar_utf8)
|
|||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), "٨:٠٧:٣٠ م");
|
||||
ASSERT_TRUE(buffer.verboseMatches("٨:٠٧:٣٠ م"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Style_ar_utf16)
|
||||
|
@ -60,7 +60,7 @@ TEST(IntlDateTimeFormat, Style_ar_utf16)
|
|||
TestBuffer<char16_t> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), u"٨:٠٧:٣٠ م");
|
||||
ASSERT_TRUE(buffer.verboseMatches(u"٨:٠٧:٣٠ م"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Style_enUS_fallback_to_default_styles)
|
||||
|
@ -69,7 +69,7 @@ TEST(IntlDateTimeFormat, Style_enUS_fallback_to_default_styles)
|
|||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), "Sep 23, 2002, 8:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches("Sep 23, 2002, 8:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Skeleton_enUS_utf8_in)
|
||||
|
@ -81,7 +81,7 @@ TEST(IntlDateTimeFormat, Skeleton_enUS_utf8_in)
|
|||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), "9/23/2002, 8:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches("9/23/2002, 8:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Skeleton_enUS_utf16_in)
|
||||
|
@ -93,7 +93,7 @@ TEST(IntlDateTimeFormat, Skeleton_enUS_utf16_in)
|
|||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
|
||||
ASSERT_EQ(buffer.get_string_view(), "9/23/2002, 8:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches("9/23/2002, 8:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimeFormat, Time_zone_IANA_identifier)
|
||||
|
@ -105,7 +105,7 @@ TEST(IntlDateTimeFormat, Time_zone_IANA_identifier)
|
|||
.unwrap();
|
||||
TestBuffer<char> buffer;
|
||||
dtFormat->TryFormat(DATE, buffer).unwrap();
|
||||
ASSERT_EQ(buffer.get_string_view(), "Sep 23, 2002, 12:07:30 PM");
|
||||
ASSERT_TRUE(buffer.verboseMatches("Sep 23, 2002, 12:07:30 PM"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimePatternGenerator, GetBestPattern)
|
||||
|
@ -114,7 +114,7 @@ TEST(IntlDateTimePatternGenerator, GetBestPattern)
|
|||
TestBuffer<char16_t> buffer;
|
||||
|
||||
gen->GetBestPattern(MakeStringSpan(u"yMd"), buffer).unwrap();
|
||||
ASSERT_EQ(buffer.get_string_view(), u"M/d/y");
|
||||
ASSERT_TRUE(buffer.verboseMatches(u"M/d/y"));
|
||||
}
|
||||
|
||||
TEST(IntlDateTimePatternGenerator, GetSkeleton)
|
||||
|
@ -124,7 +124,7 @@ TEST(IntlDateTimePatternGenerator, GetSkeleton)
|
|||
|
||||
DateTimePatternGenerator::GetSkeleton(MakeStringSpan(u"M/d/y"), buffer)
|
||||
.unwrap();
|
||||
ASSERT_EQ(buffer.get_string_view(), u"yMd");
|
||||
ASSERT_TRUE(buffer.verboseMatches(u"yMd"));
|
||||
}
|
||||
|
||||
} // namespace mozilla::intl
|
||||
|
|
Загрузка…
Ссылка в новой задаче