TestListFormat.cpp (6589B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 #include "gtest/gtest.h" 5 6 #include "mozilla/intl/ListFormat.h" 7 #include "mozilla/Span.h" 8 #include "TestBuffer.h" 9 10 namespace mozilla::intl { 11 12 // Test ListFormat.format with default options. 13 TEST(IntlListFormat, FormatDefault) 14 { 15 ListFormat::Options options; 16 UniquePtr<ListFormat> lf = 17 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 18 ListFormat::StringList list; 19 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 20 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 21 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 22 TestBuffer<char16_t> buf16; 23 ASSERT_TRUE(lf->Format(list, buf16).isOk()); 24 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob, and Charlie"); 25 26 UniquePtr<ListFormat> lfDe = 27 ListFormat::TryCreate(MakeStringSpan("de"), options).unwrap(); 28 ASSERT_TRUE(lfDe->Format(list, buf16).isOk()); 29 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob und Charlie"); 30 } 31 32 // Test ListFormat.format with Type::Conjunction and other styles. 33 TEST(IntlListFormat, FormatConjunction) 34 { 35 ListFormat::Options options{ListFormat::Type::Conjunction, 36 ListFormat::Style::Narrow}; 37 UniquePtr<ListFormat> lf = 38 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 39 ListFormat::StringList list; 40 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 41 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 42 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 43 TestBuffer<char16_t> buf16; 44 ASSERT_TRUE(lf->Format(list, buf16).isOk()); 45 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob, Charlie"); 46 47 ListFormat::Options optionsSh{ListFormat::Type::Conjunction, 48 ListFormat::Style::Short}; 49 UniquePtr<ListFormat> lfSh = 50 ListFormat::TryCreate(MakeStringSpan("en-US"), optionsSh).unwrap(); 51 ASSERT_TRUE(lfSh->Format(list, buf16).isOk()); 52 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob, & Charlie"); 53 } 54 55 // Test ListFormat.format with Type::Disjunction. 56 TEST(IntlListFormat, FormatDisjunction) 57 { 58 // When Type is Disjunction, the results will be the same regardless of the 59 // style for most locales, so simply test with Style::Long. 60 ListFormat::Options options{ListFormat::Type::Disjunction, 61 ListFormat::Style::Long}; 62 UniquePtr<ListFormat> lf = 63 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 64 ListFormat::StringList list; 65 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 66 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 67 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 68 TestBuffer<char16_t> buf16; 69 ASSERT_TRUE(lf->Format(list, buf16).isOk()); 70 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob, or Charlie"); 71 } 72 73 // Test ListFormat.format with Type::Unit. 74 TEST(IntlListFormat, FormatUnit) 75 { 76 ListFormat::Options options{ListFormat::Type::Unit, ListFormat::Style::Long}; 77 // For locale "en", Style::Long and Style::Short have the same result, so just 78 // test Style::Long here. 79 UniquePtr<ListFormat> lf = 80 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 81 ListFormat::StringList list; 82 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 83 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 84 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 85 TestBuffer<char16_t> buf16; 86 ASSERT_TRUE(lf->Format(list, buf16).isOk()); 87 ASSERT_EQ(buf16.get_string_view(), u"Alice, Bob, Charlie"); 88 89 ListFormat::Options optionsNa{ListFormat::Type::Unit, 90 ListFormat::Style::Narrow}; 91 UniquePtr<ListFormat> lfNa = 92 ListFormat::TryCreate(MakeStringSpan("en-US"), optionsNa).unwrap(); 93 ASSERT_TRUE(lfNa->Format(list, buf16).isOk()); 94 ASSERT_EQ(buf16.get_string_view(), u"Alice Bob Charlie"); 95 } 96 97 // Pass a long list (list.length() > DEFAULT_LIST_LENGTH) and check the result 98 // is still correct. (result.length > INITIAL_CHAR_BUFFER_SIZE) 99 TEST(IntlListFormat, FormatBufferLength) 100 { 101 ListFormat::Options options; 102 UniquePtr<ListFormat> lf = 103 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 104 ListFormat::StringList list; 105 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 106 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 107 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 108 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"David"))); 109 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Eve"))); 110 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Frank"))); 111 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Grace"))); 112 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Heidi"))); 113 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Ivan"))); 114 TestBuffer<char16_t> buf16; 115 ASSERT_TRUE(lf->Format(list, buf16).isOk()); 116 ASSERT_EQ(buf16.get_string_view(), 117 u"Alice, Bob, Charlie, David, Eve, Frank, Grace, Heidi, and Ivan"); 118 } 119 120 TEST(IntlListFormat, FormatToParts) 121 { 122 ListFormat::Options options; 123 UniquePtr<ListFormat> lf = 124 ListFormat::TryCreate(MakeStringSpan("en-US"), options).unwrap(); 125 ListFormat::StringList list; 126 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Alice"))); 127 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Bob"))); 128 MOZ_RELEASE_ASSERT(list.append(MakeStringSpan(u"Charlie"))); 129 130 TestBuffer<char16_t> buf16; 131 mozilla::intl::ListFormat::PartVector parts; 132 ASSERT_TRUE(lf->FormatToParts(list, buf16, parts).isOk()); 133 134 std::u16string_view strView = buf16.get_string_view(); 135 ASSERT_EQ(strView, u"Alice, Bob, and Charlie"); 136 137 // 3 elements, and 2 literals. 138 ASSERT_EQ((parts.length()), (5u)); 139 140 auto getSubStringView = [strView, &parts](size_t index) { 141 size_t pos = index == 0 ? 0 : parts[index - 1].second; 142 size_t count = parts[index].second - pos; 143 return strView.substr(pos, count); 144 }; 145 146 ASSERT_EQ(parts[0].first, ListFormat::PartType::Element); 147 ASSERT_EQ(getSubStringView(0), u"Alice"); 148 149 ASSERT_EQ(parts[1].first, ListFormat::PartType::Literal); 150 ASSERT_EQ(getSubStringView(1), u", "); 151 152 ASSERT_EQ(parts[2].first, ListFormat::PartType::Element); 153 ASSERT_EQ(getSubStringView(2), u"Bob"); 154 155 ASSERT_EQ(parts[3].first, ListFormat::PartType::Literal); 156 ASSERT_EQ(getSubStringView(3), u", and "); 157 158 ASSERT_EQ(parts[4].first, ListFormat::PartType::Element); 159 ASSERT_EQ(getSubStringView(4), u"Charlie"); 160 } 161 162 } // namespace mozilla::intl