blob: 92eb3b58515ff34ab202491a935ba58ae1f8d701 [file] [log] [blame]
Adam Lesinski393b5f02015-12-17 13:03:11 -08001/*
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"
Adam Lesinski393b5f02015-12-17 13:03:11 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "test/Test.h"
20#include "util/Util.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080021
22namespace aapt {
23
24// In this context, 'Axis' represents a particular field in the configuration,
25// such as language or density.
26
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027static ::testing::AssertionResult SimpleHelper(const char* input,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 const char* expected,
Adam Lesinski393b5f02015-12-17 13:03:11 -080029 Pseudolocalizer::Method method) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 Pseudolocalizer pseudo(method);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 std::string result = pseudo.Start() + pseudo.Text(input) + pseudo.End();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 if (result != expected) {
33 return ::testing::AssertionFailure() << expected << " != " << result;
34 }
35 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080036}
37
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038static ::testing::AssertionResult CompoundHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 const char* in1, const char* in2, const char* in3, const char* expected,
40 Pseudolocalizer::Method method) {
41 Pseudolocalizer pseudo(method);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 std::string result = pseudo.Start() + pseudo.Text(in1) + pseudo.Text(in2) +
43 pseudo.Text(in3) + pseudo.End();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 if (result != expected) {
45 return ::testing::AssertionFailure() << expected << " != " << result;
46 }
47 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080048}
49
50TEST(PseudolocalizerTest, NoPseudolocalization) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kNone));
52 EXPECT_TRUE(SimpleHelper("Hello, world", "Hello, world",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "Hello, world",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080057}
58
59TEST(PseudolocalizerTest, PlaintextAccent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 EXPECT_TRUE(SimpleHelper("", "[]", Pseudolocalizer::Method::kAccent));
61 EXPECT_TRUE(SimpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 EXPECT_TRUE(SimpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 EXPECT_TRUE(SimpleHelper("Battery %1d%%", "[βåţţéŕý »%1d«%% one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 Pseudolocalizer::Method::kAccent));
69 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 SimpleHelper("^1 %", "[^1 % one]", Pseudolocalizer::Method::kAccent));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 CompoundHelper("", "", "", "[]", Pseudolocalizer::Method::kAccent));
73 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "[Ĥéļļö, ŵöŕļð one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080075}
76
77TEST(PseudolocalizerTest, PlaintextBidi) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kBidi));
79 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 "word", "\xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f",
81 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
84 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
87 Pseudolocalizer::Method::kBidi));
88 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 SimpleHelper("hello\n world\n",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 "\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));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 EXPECT_TRUE(CompoundHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 "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));
Adam Lesinski393b5f02015-12-17 13:03:11 -080098}
99
100TEST(PseudolocalizerTest, SimpleICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 EXPECT_TRUE(SimpleHelper("{placeholder}", "[»{placeholder}«]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 EXPECT_TRUE(SimpleHelper("{USER} is offline", "[»{USER}« îš öƒƒļîñé one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 EXPECT_TRUE(SimpleHelper("Copy from {path1} to {path2}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
108 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 EXPECT_TRUE(SimpleHelper("Today is {1,date} {1,time}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 "[Ţöðåý îš »{1,date}« »{1,time}« one two]",
111 Pseudolocalizer::Method::kAccent));
112
113 // Multi-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 EXPECT_TRUE(CompoundHelper("{USER}", " ", "is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 "[»{USER}« îš öƒƒļîñé one two]",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800116 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 EXPECT_TRUE(CompoundHelper("Copy from ", "{path1}", " to {path2}",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800118 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
119 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800120}
121
122TEST(PseudolocalizerTest, ICUBidi) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 "{placeholder}",
126 "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f",
127 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 "{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));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800134}
135
136TEST(PseudolocalizerTest, Escaping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 EXPECT_TRUE(SimpleHelper("'{USER'} is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]",
140 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 // Multi-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 EXPECT_TRUE(CompoundHelper("'{USER}", " ", "''is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]",
145 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800146}
147
148TEST(PseudolocalizerTest, PluralsAndSelects) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 "{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(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 SimpleHelper("Distance is {COUNT, plural, one {# mile} other {# miles}}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 "[Ðîšţåñçé îš {COUNT, plural, one {# ḿîļé one two} "
158 "other {# ḿîļéš one two}}]",
159 Pseudolocalizer::Method::kAccent));
160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 "{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(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 CompoundHelper("{COUNT, plural, one {Delete a file} "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 "other {Delete ",
171 "{COUNT}", " files}}",
172 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
Adam Lesinski393b5f02015-12-17 13:03:11 -0800173 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800175}
176
177TEST(PseudolocalizerTest, NestedICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 SimpleHelper("{person, select, "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 "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));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800220}
221
222TEST(PseudolocalizerTest, RedefineMethod) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 Pseudolocalizer pseudo(Pseudolocalizer::Method::kAccent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 std::string result = pseudo.Text("Hello, ");
225 pseudo.SetMethod(Pseudolocalizer::Method::kNone);
226 result += pseudo.Text("world!");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 ASSERT_EQ(StringPiece("Ĥéļļö, world!"), result);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800228}
229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230} // namespace aapt