Pseudolocalizer_test.cpp revision ce5e56e243d262a9b65459c3bd0bb9eaadd40628
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "compile/Pseudolocalizer.h" 18 19#include "test/Test.h" 20#include "util/Util.h" 21 22namespace aapt { 23 24// In this context, 'Axis' represents a particular field in the configuration, 25// such as language or density. 26 27static ::testing::AssertionResult SimpleHelper(const char* input, 28 const char* expected, 29 Pseudolocalizer::Method method) { 30 Pseudolocalizer pseudo(method); 31 std::string result = pseudo.Start() + pseudo.Text(input) + pseudo.End(); 32 if (result != expected) { 33 return ::testing::AssertionFailure() << expected << " != " << result; 34 } 35 return ::testing::AssertionSuccess(); 36} 37 38static ::testing::AssertionResult CompoundHelper( 39 const char* in1, const char* in2, const char* in3, const char* expected, 40 Pseudolocalizer::Method method) { 41 Pseudolocalizer pseudo(method); 42 std::string result = pseudo.Start() + pseudo.Text(in1) + pseudo.Text(in2) + 43 pseudo.Text(in3) + pseudo.End(); 44 if (result != expected) { 45 return ::testing::AssertionFailure() << expected << " != " << result; 46 } 47 return ::testing::AssertionSuccess(); 48} 49 50TEST(PseudolocalizerTest, NoPseudolocalization) { 51 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kNone)); 52 EXPECT_TRUE(SimpleHelper("Hello, world", "Hello, world", 53 Pseudolocalizer::Method::kNone)); 54 55 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "Hello, world", 56 Pseudolocalizer::Method::kNone)); 57} 58 59TEST(PseudolocalizerTest, PlaintextAccent) { 60 EXPECT_TRUE(SimpleHelper("", "[]", Pseudolocalizer::Method::kAccent)); 61 EXPECT_TRUE(SimpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]", 62 Pseudolocalizer::Method::kAccent)); 63 64 EXPECT_TRUE(SimpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]", 65 Pseudolocalizer::Method::kAccent)); 66 67 EXPECT_TRUE(SimpleHelper("Battery %1d%%", "[βåţţéŕý »%1d«%% one two]", 68 Pseudolocalizer::Method::kAccent)); 69 EXPECT_TRUE( 70 SimpleHelper("^1 %", "[^1 % one]", Pseudolocalizer::Method::kAccent)); 71 EXPECT_TRUE( 72 CompoundHelper("", "", "", "[]", Pseudolocalizer::Method::kAccent)); 73 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "[Ĥéļļö, ŵöŕļð one two]", 74 Pseudolocalizer::Method::kAccent)); 75} 76 77TEST(PseudolocalizerTest, PlaintextBidi) { 78 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kBidi)); 79 EXPECT_TRUE(SimpleHelper( 80 "word", "\xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f", 81 Pseudolocalizer::Method::kBidi)); 82 EXPECT_TRUE(SimpleHelper( 83 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ", 84 Pseudolocalizer::Method::kBidi)); 85 EXPECT_TRUE(SimpleHelper( 86 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ", 87 Pseudolocalizer::Method::kBidi)); 88 EXPECT_TRUE( 89 SimpleHelper("hello\n world\n", 90 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n" 91 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n", 92 Pseudolocalizer::Method::kBidi)); 93 EXPECT_TRUE(CompoundHelper( 94 "hello", "\n ", " world\n", 95 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n" 96 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n", 97 Pseudolocalizer::Method::kBidi)); 98} 99 100TEST(PseudolocalizerTest, SimpleICU) { 101 // Single-fragment messages 102 EXPECT_TRUE(SimpleHelper("{placeholder}", "[»{placeholder}«]", 103 Pseudolocalizer::Method::kAccent)); 104 EXPECT_TRUE(SimpleHelper("{USER} is offline", "[»{USER}« îš öƒƒļîñé one two]", 105 Pseudolocalizer::Method::kAccent)); 106 EXPECT_TRUE(SimpleHelper("Copy from {path1} to {path2}", 107 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]", 108 Pseudolocalizer::Method::kAccent)); 109 EXPECT_TRUE(SimpleHelper("Today is {1,date} {1,time}", 110 "[Ţöðåý îš »{1,date}« »{1,time}« one two]", 111 Pseudolocalizer::Method::kAccent)); 112 113 // Multi-fragment messages 114 EXPECT_TRUE(CompoundHelper("{USER}", " ", "is offline", 115 "[»{USER}« îš öƒƒļîñé one two]", 116 Pseudolocalizer::Method::kAccent)); 117 EXPECT_TRUE(CompoundHelper("Copy from ", "{path1}", " to {path2}", 118 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]", 119 Pseudolocalizer::Method::kAccent)); 120} 121 122TEST(PseudolocalizerTest, ICUBidi) { 123 // Single-fragment messages 124 EXPECT_TRUE(SimpleHelper( 125 "{placeholder}", 126 "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f", 127 Pseudolocalizer::Method::kBidi)); 128 EXPECT_TRUE(SimpleHelper( 129 "{COUNT, plural, one {one} other {other}}", 130 "{COUNT, plural, " 131 "one {\xe2\x80\x8f\xE2\x80\xaeone\xE2\x80\xac\xe2\x80\x8f} " 132 "other {\xe2\x80\x8f\xE2\x80\xaeother\xE2\x80\xac\xe2\x80\x8f}}", 133 Pseudolocalizer::Method::kBidi)); 134} 135 136TEST(PseudolocalizerTest, Escaping) { 137 // Single-fragment messages 138 EXPECT_TRUE(SimpleHelper("'{USER'} is offline", 139 "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]", 140 Pseudolocalizer::Method::kAccent)); 141 142 // Multi-fragment messages 143 EXPECT_TRUE(CompoundHelper("'{USER}", " ", "''is offline", 144 "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]", 145 Pseudolocalizer::Method::kAccent)); 146} 147 148TEST(PseudolocalizerTest, PluralsAndSelects) { 149 EXPECT_TRUE(SimpleHelper( 150 "{COUNT, plural, one {Delete a file} other {Delete {COUNT} files}}", 151 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} " 152 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]", 153 Pseudolocalizer::Method::kAccent)); 154 155 EXPECT_TRUE( 156 SimpleHelper("Distance is {COUNT, plural, one {# mile} other {# miles}}", 157 "[Ðîšţåñçé îš {COUNT, plural, one {# ḿîļé one two} " 158 "other {# ḿîļéš one two}}]", 159 Pseudolocalizer::Method::kAccent)); 160 161 EXPECT_TRUE(SimpleHelper( 162 "{1, select, female {{1} added you} " 163 "male {{1} added you} other {{1} added you}}", 164 "[{1, select, female {»{1}« åððéð ýöû one two} " 165 "male {»{1}« åððéð ýöû one two} other {»{1}« åððéð ýöû one two}}]", 166 Pseudolocalizer::Method::kAccent)); 167 168 EXPECT_TRUE( 169 CompoundHelper("{COUNT, plural, one {Delete a file} " 170 "other {Delete ", 171 "{COUNT}", " files}}", 172 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} " 173 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]", 174 Pseudolocalizer::Method::kAccent)); 175} 176 177TEST(PseudolocalizerTest, NestedICU) { 178 EXPECT_TRUE( 179 SimpleHelper("{person, select, " 180 "female {" 181 "{num_circles, plural," 182 "=0{{person} didn't add you to any of her circles.}" 183 "=1{{person} added you to one of her circles.}" 184 "other{{person} added you to her # circles.}}}" 185 "male {" 186 "{num_circles, plural," 187 "=0{{person} didn't add you to any of his circles.}" 188 "=1{{person} added you to one of his circles.}" 189 "other{{person} added you to his # circles.}}}" 190 "other {" 191 "{num_circles, plural," 192 "=0{{person} didn't add you to any of their circles.}" 193 "=1{{person} added you to one of their circles.}" 194 "other{{person} added you to their # circles.}}}}", 195 "[{person, select, " 196 "female {" 197 "{num_circles, plural," 198 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥéŕ çîŕçļéš." 199 " one two three four five}" 200 "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥéŕ çîŕçļéš." 201 " one two three four}" 202 "other{»{person}« åððéð ýöû ţö ĥéŕ # çîŕçļéš." 203 " one two three four}}}" 204 "male {" 205 "{num_circles, plural," 206 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥîš çîŕçļéš." 207 " one two three four five}" 208 "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥîš çîŕçļéš." 209 " one two three four}" 210 "other{»{person}« åððéð ýöû ţö ĥîš # çîŕçļéš." 211 " one two three four}}}" 212 "other {{num_circles, plural," 213 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ţĥéîŕ çîŕçļéš." 214 " one two three four five}" 215 "=1{»{person}« åððéð ýöû ţö öñé öƒ ţĥéîŕ çîŕçļéš." 216 " one two three four}" 217 "other{»{person}« åððéð ýöû ţö ţĥéîŕ # çîŕçļéš." 218 " one two three four}}}}]", 219 Pseudolocalizer::Method::kAccent)); 220} 221 222TEST(PseudolocalizerTest, RedefineMethod) { 223 Pseudolocalizer pseudo(Pseudolocalizer::Method::kAccent); 224 std::string result = pseudo.Text("Hello, "); 225 pseudo.SetMethod(Pseudolocalizer::Method::kNone); 226 result += pseudo.Text("world!"); 227 ASSERT_EQ(StringPiece("Ĥéļļö, world!"), result); 228} 229 230} // namespace aapt 231