| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import static com.google.javascript.jscomp.JsMessage.Style.RELAX; |
| 20 |
import static com.google.javascript.jscomp.JsMessageVisitor.MESSAGE_TREE_MALFORMED; |
| 21 |
|
| 22 |
import com.google.common.collect.Maps; |
| 23 |
import com.google.javascript.jscomp.JsMessage.Style; |
| 24 |
|
| 25 |
import java.util.Map; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ReplaceMessagesTest extends CompilerTestCase { |
| 32 |
|
| 33 |
private Map<String, JsMessage> messages; |
| 34 |
private Style style = RELAX; |
| 35 |
private boolean strictReplacement; |
| 36 |
|
| 37 |
@Override |
| 38 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 39 |
return new ReplaceMessages(compiler, |
| 40 |
new SimpleMessageBundle(), false, style, strictReplacement); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected int getNumRepetitions() { |
| 45 |
|
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
protected void setUp() { |
| 51 |
messages = Maps.newHashMap(); |
| 52 |
strictReplacement = false; |
| 53 |
style = RELAX; |
| 54 |
} |
| 55 |
|
| 56 |
public void testReplaceSimpleMessage() { |
| 57 |
registerMessage(new JsMessage.Builder("MSG_A") |
| 58 |
.appendStringPart("Hi\nthere") |
| 59 |
.build()); |
| 60 |
|
| 61 |
test("/** @desc d */\n" + |
| 62 |
"var MSG_A = goog.getMsg('asdf');", |
| 63 |
"var MSG_A=\"Hi\\nthere\""); |
| 64 |
} |
| 65 |
|
| 66 |
public void testNameReplacement() { |
| 67 |
registerMessage(new JsMessage.Builder("MSG_B") |
| 68 |
.appendStringPart("One ") |
| 69 |
.appendPlaceholderReference("measly") |
| 70 |
.appendStringPart(" ph") |
| 71 |
.build()); |
| 72 |
|
| 73 |
test("/** @desc d */\n" + |
| 74 |
"var MSG_B=goog.getMsg('asdf {$measly}', {measly: x});", |
| 75 |
"var MSG_B=\"One \"+ (x +\" ph\" )"); |
| 76 |
} |
| 77 |
|
| 78 |
public void testGetPropReplacement() { |
| 79 |
registerMessage(new JsMessage.Builder("MSG_C") |
| 80 |
.appendPlaceholderReference("amount") |
| 81 |
.build()); |
| 82 |
|
| 83 |
test("/** @desc d */\n" + |
| 84 |
"var MSG_C = goog.getMsg('${$amount}', {amount: a.b.amount});", |
| 85 |
"var MSG_C=a.b.amount"); |
| 86 |
} |
| 87 |
|
| 88 |
public void testFunctionCallReplacement() { |
| 89 |
registerMessage(new JsMessage.Builder("MSG_D") |
| 90 |
.appendPlaceholderReference("amount") |
| 91 |
.build()); |
| 92 |
|
| 93 |
test("/** @desc d */\n" + |
| 94 |
"var MSG_D = goog.getMsg('${$amount}', {amount: getAmt()});", |
| 95 |
"var MSG_D=getAmt()"); |
| 96 |
} |
| 97 |
|
| 98 |
public void testMethodCallReplacement() { |
| 99 |
registerMessage(new JsMessage.Builder("MSG_E") |
| 100 |
.appendPlaceholderReference("amount") |
| 101 |
.build()); |
| 102 |
|
| 103 |
test("/** @desc d */\n" + |
| 104 |
"var MSG_E = goog.getMsg('${$amount}', {amount: obj.getAmt()});", |
| 105 |
"var MSG_E=obj.getAmt()"); |
| 106 |
} |
| 107 |
|
| 108 |
public void testHookReplacement() { |
| 109 |
registerMessage(new JsMessage.Builder("MSG_F") |
| 110 |
.appendStringPart("#") |
| 111 |
.appendPlaceholderReference("amount") |
| 112 |
.appendStringPart(".") |
| 113 |
.build()); |
| 114 |
|
| 115 |
test("/** @desc d */\n" + |
| 116 |
"var MSG_F = goog.getMsg('${$amount}', {amount: (a ? b : c)});", |
| 117 |
"var MSG_F=\"#\"+((a?b:c)+\".\")"); |
| 118 |
} |
| 119 |
|
| 120 |
public void testAddReplacement() { |
| 121 |
registerMessage(new JsMessage.Builder("MSG_G") |
| 122 |
.appendPlaceholderReference("amount") |
| 123 |
.build()); |
| 124 |
|
| 125 |
test("/** @desc d */\n" + |
| 126 |
"var MSG_G = goog.getMsg('${$amount}', {amount: x + ''});", |
| 127 |
"var MSG_G=x+\"\""); |
| 128 |
} |
| 129 |
|
| 130 |
public void testPlaceholderValueReferencedTwice() { |
| 131 |
registerMessage(new JsMessage.Builder("MSG_H") |
| 132 |
.appendPlaceholderReference("dick") |
| 133 |
.appendStringPart(", ") |
| 134 |
.appendPlaceholderReference("dick") |
| 135 |
.appendStringPart(" and ") |
| 136 |
.appendPlaceholderReference("jane") |
| 137 |
.build()); |
| 138 |
|
| 139 |
test("/** @desc d */\n" + |
| 140 |
"var MSG_H = goog.getMsg('{$dick}{$jane}', {jane: x, dick: y});", |
| 141 |
"var MSG_H=y+(\", \"+(y+(\" and \"+x)))"); |
| 142 |
} |
| 143 |
|
| 144 |
public void testPlaceholderNameInLowerCamelCase() { |
| 145 |
registerMessage(new JsMessage.Builder("MSG_I") |
| 146 |
.appendStringPart("Sum: $") |
| 147 |
.appendPlaceholderReference("amtEarned") |
| 148 |
.build()); |
| 149 |
|
| 150 |
test("/** @desc d */\n" + |
| 151 |
"var MSG_I = goog.getMsg('${$amtEarned}', {amtEarned: x});", |
| 152 |
"var MSG_I=\"Sum: $\"+x"); |
| 153 |
} |
| 154 |
|
| 155 |
public void testQualifiedMessageName() { |
| 156 |
registerMessage(new JsMessage.Builder("MSG_J") |
| 157 |
.appendStringPart("One ") |
| 158 |
.appendPlaceholderReference("measly") |
| 159 |
.appendStringPart(" ph") |
| 160 |
.build()); |
| 161 |
|
| 162 |
test("/** @desc d */\n" + |
| 163 |
"a.b.c.MSG_J = goog.getMsg('asdf {$measly}', {measly: x});", |
| 164 |
"a.b.c.MSG_J=\"One \"+(x+\" ph\")"); |
| 165 |
} |
| 166 |
|
| 167 |
public void testSimpleMessageReplacementMissing() { |
| 168 |
style = Style.LEGACY; |
| 169 |
test("/** @desc d */\n" + |
| 170 |
"var MSG_E = 'd*6a0@z>t';", |
| 171 |
"var MSG_E = 'd*6a0@z>t'"); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public void testSimpleMessageReplacementMissingWithNewStyle() { |
| 176 |
test("/** @desc d */\n" + |
| 177 |
"var MSG_E = goog.getMsg('missing');", |
| 178 |
"var MSG_E = 'missing'"); |
| 179 |
} |
| 180 |
|
| 181 |
public void testStrictModeAndMessageReplacementAbsentInBundle() { |
| 182 |
strictReplacement = true; |
| 183 |
test("var MSG_E = 'Hello';", "var MSG_E = 'Hello';", |
| 184 |
ReplaceMessages.BUNDLE_DOES_NOT_HAVE_THE_MESSAGE); |
| 185 |
} |
| 186 |
|
| 187 |
public void testStrictModeAndMessageReplacementAbsentInNonEmptyBundle() { |
| 188 |
registerMessage(new JsMessage.Builder("MSG_J") |
| 189 |
.appendStringPart("One ") |
| 190 |
.appendPlaceholderReference("measly") |
| 191 |
.appendStringPart(" ph") |
| 192 |
.build()); |
| 193 |
|
| 194 |
strictReplacement = true; |
| 195 |
test("var MSG_E = 'Hello';", "var MSG_E = 'Hello';", |
| 196 |
ReplaceMessages.BUNDLE_DOES_NOT_HAVE_THE_MESSAGE); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
public void testFunctionReplacementMissing() { |
| 201 |
style = Style.LEGACY; |
| 202 |
test("var MSG_F = function() {return 'asdf'};", |
| 203 |
"var MSG_F = function() {return\"asdf\"}"); |
| 204 |
} |
| 205 |
|
| 206 |
public void testFunctionWithParamReplacementMissing() { |
| 207 |
style = Style.LEGACY; |
| 208 |
test( |
| 209 |
"var MSG_G = function(measly) {return 'asdf' + measly};", |
| 210 |
"var MSG_G=function(measly){return\"asdf\"+measly}"); |
| 211 |
} |
| 212 |
|
| 213 |
public void testPlaceholderNameInLowerUnderscoreCase() { |
| 214 |
test( |
| 215 |
"var MSG_J = goog.getMsg('${$amt_earned}', {amt_earned: x});", |
| 216 |
"var MSG_J = goog.getMsg('${$amt_earned}', {amt_earned: x});", |
| 217 |
MESSAGE_TREE_MALFORMED); |
| 218 |
} |
| 219 |
|
| 220 |
public void testBadPlaceholderReferenceInReplacement() { |
| 221 |
registerMessage(new JsMessage.Builder("MSG_K") |
| 222 |
.appendPlaceholderReference("amount") |
| 223 |
.build()); |
| 224 |
|
| 225 |
test( |
| 226 |
"var MSG_K = goog.getMsg('Hi {$jane}', {jane: x});", |
| 227 |
"var MSG_K = goog.getMsg('Hi {$jane}', {jane: x});", |
| 228 |
MESSAGE_TREE_MALFORMED); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
public void testLegacyStyleNoPlaceholdersVarSyntax() { |
| 233 |
registerMessage(new JsMessage.Builder("MSG_A") |
| 234 |
.appendStringPart("Hi\nthere") |
| 235 |
.build()); |
| 236 |
style = Style.LEGACY; |
| 237 |
test("var MSG_A = 'd*6a0@z>t';", |
| 238 |
"var MSG_A=\"Hi\\nthere\""); |
| 239 |
} |
| 240 |
|
| 241 |
public void testLegacyStyleNoPlaceholdersFunctionSyntax() { |
| 242 |
registerMessage(new JsMessage.Builder("MSG_B") |
| 243 |
.appendStringPart("Hi\nthere") |
| 244 |
.build()); |
| 245 |
style = Style.LEGACY; |
| 246 |
test("var MSG_B = function() {return 'asdf'};", |
| 247 |
"var MSG_B=function(){return\"Hi\\nthere\"}"); |
| 248 |
} |
| 249 |
|
| 250 |
public void testLegacyStyleOnePlaceholder() { |
| 251 |
registerMessage(new JsMessage.Builder("MSG_C") |
| 252 |
.appendStringPart("One ") |
| 253 |
.appendPlaceholderReference("measly") |
| 254 |
.appendStringPart(" ph") |
| 255 |
.build()); |
| 256 |
style = Style.LEGACY; |
| 257 |
test( |
| 258 |
"var MSG_C = function(measly) {return 'asdf' + measly};", |
| 259 |
"var MSG_C=function(measly){return\"One \"+(measly+\" ph\")}"); |
| 260 |
} |
| 261 |
|
| 262 |
public void testLegacyStyleTwoPlaceholders() { |
| 263 |
registerMessage(new JsMessage.Builder("MSG_D") |
| 264 |
.appendPlaceholderReference("dick") |
| 265 |
.appendStringPart(" and ") |
| 266 |
.appendPlaceholderReference("jane") |
| 267 |
.build()); |
| 268 |
style = Style.LEGACY; |
| 269 |
test( |
| 270 |
"var MSG_D = function(jane, dick) {return jane + dick};", |
| 271 |
"var MSG_D=function(jane,dick){return dick+(\" and \"+jane)}"); |
| 272 |
} |
| 273 |
|
| 274 |
public void testLegacyStylePlaceholderNameInLowerCamelCase() { |
| 275 |
registerMessage(new JsMessage.Builder("MSG_E") |
| 276 |
.appendStringPart("Sum: $") |
| 277 |
.appendPlaceholderReference("amtEarned") |
| 278 |
.build()); |
| 279 |
style = Style.LEGACY; |
| 280 |
test( |
| 281 |
"var MSG_E = function(amtEarned) {return amtEarned + 'x'};", |
| 282 |
"var MSG_E=function(amtEarned){return\"Sum: $\"+amtEarned}"); |
| 283 |
} |
| 284 |
|
| 285 |
public void testLegacyStylePlaceholderNameInLowerUnderscoreCase() { |
| 286 |
registerMessage(new JsMessage.Builder("MSG_F") |
| 287 |
.appendStringPart("Sum: $") |
| 288 |
.appendPlaceholderReference("amt_earned") |
| 289 |
.build()); |
| 290 |
|
| 291 |
|
| 292 |
style = Style.LEGACY; |
| 293 |
test( |
| 294 |
"var MSG_F = function(amt_earned) {return amt_earned + 'x'};", |
| 295 |
"var MSG_F=function(amt_earned){return\"Sum: $\"+amt_earned}"); |
| 296 |
} |
| 297 |
|
| 298 |
public void testLegacyStyleBadPlaceholderReferenceInReplacemen() { |
| 299 |
registerMessage(new JsMessage.Builder("MSG_B") |
| 300 |
.appendStringPart("Ola, ") |
| 301 |
.appendPlaceholderReference("chimp") |
| 302 |
.build()); |
| 303 |
|
| 304 |
test("var MSG_B = function(chump) {return chump + 'x'};", |
| 305 |
"var MSG_B = function(chump) {return chump + 'x'};", |
| 306 |
JsMessageVisitor.MESSAGE_TREE_MALFORMED); |
| 307 |
} |
| 308 |
|
| 309 |
public void testTranslatedPlaceHolderMissMatch() { |
| 310 |
registerMessage(new JsMessage.Builder("MSG_A") |
| 311 |
.appendPlaceholderReference("a") |
| 312 |
.appendStringPart("!") |
| 313 |
.build()); |
| 314 |
|
| 315 |
test("var MSG_A = goog.getMsg('{$a}');", |
| 316 |
"var MSG_A = goog.getMsg('{$a}');", |
| 317 |
MESSAGE_TREE_MALFORMED); |
| 318 |
} |
| 319 |
|
| 320 |
public void testBadFallbackSyntax1() { |
| 321 |
test("/** @desc d */\n" + |
| 322 |
"var MSG_A = goog.getMsg('asdf');" + |
| 323 |
"var x = goog.getMsgWithFallback(MSG_A);", null, |
| 324 |
JsMessageVisitor.BAD_FALLBACK_SYNTAX); |
| 325 |
} |
| 326 |
|
| 327 |
public void testBadFallbackSyntax2() { |
| 328 |
test("var x = goog.getMsgWithFallback('abc', 'bcd');", null, |
| 329 |
JsMessageVisitor.BAD_FALLBACK_SYNTAX); |
| 330 |
} |
| 331 |
|
| 332 |
public void testBadFallbackSyntax3() { |
| 333 |
test("/** @desc d */\n" + |
| 334 |
"var MSG_A = goog.getMsg('asdf');" + |
| 335 |
"var x = goog.getMsgWithFallback(MSG_A, y);", null, |
| 336 |
JsMessageVisitor.FALLBACK_ARG_ERROR); |
| 337 |
} |
| 338 |
|
| 339 |
public void testBadFallbackSyntax4() { |
| 340 |
test("/** @desc d */\n" + |
| 341 |
"var MSG_A = goog.getMsg('asdf');" + |
| 342 |
"var x = goog.getMsgWithFallback(y, MSG_A);", null, |
| 343 |
JsMessageVisitor.FALLBACK_ARG_ERROR); |
| 344 |
} |
| 345 |
|
| 346 |
public void testUseFallback() { |
| 347 |
registerMessage(new JsMessage.Builder("MSG_B") |
| 348 |
.appendStringPart("translated") |
| 349 |
.build()); |
| 350 |
test("/** @desc d */\n" + |
| 351 |
"var MSG_A = goog.getMsg('msg A');" + |
| 352 |
"/** @desc d */\n" + |
| 353 |
"var MSG_B = goog.getMsg('msg B');" + |
| 354 |
"var x = goog.getMsgWithFallback(MSG_A, MSG_B);", |
| 355 |
"var MSG_A = 'msg A';" + |
| 356 |
"var MSG_B = 'translated';" + |
| 357 |
"var x = MSG_B;"); |
| 358 |
} |
| 359 |
|
| 360 |
public void testFallbackEmptyBundle() { |
| 361 |
test("/** @desc d */\n" + |
| 362 |
"var MSG_A = goog.getMsg('msg A');" + |
| 363 |
"/** @desc d */\n" + |
| 364 |
"var MSG_B = goog.getMsg('msg B');" + |
| 365 |
"var x = goog.getMsgWithFallback(MSG_A, MSG_B);", |
| 366 |
"var MSG_A = 'msg A';" + |
| 367 |
"var MSG_B = 'msg B';" + |
| 368 |
"var x = MSG_A;"); |
| 369 |
} |
| 370 |
|
| 371 |
public void testNoUseFallback() { |
| 372 |
registerMessage(new JsMessage.Builder("MSG_A") |
| 373 |
.appendStringPart("translated") |
| 374 |
.build()); |
| 375 |
test("/** @desc d */\n" + |
| 376 |
"var MSG_A = goog.getMsg('msg A');" + |
| 377 |
"/** @desc d */\n" + |
| 378 |
"var MSG_B = goog.getMsg('msg B');" + |
| 379 |
"var x = goog.getMsgWithFallback(MSG_A, MSG_B);", |
| 380 |
"var MSG_A = 'translated';" + |
| 381 |
"var MSG_B = 'msg B';" + |
| 382 |
"var x = MSG_A;"); |
| 383 |
} |
| 384 |
|
| 385 |
public void testNoUseFallback2() { |
| 386 |
registerMessage(new JsMessage.Builder("MSG_C") |
| 387 |
.appendStringPart("translated") |
| 388 |
.build()); |
| 389 |
test("/** @desc d */\n" + |
| 390 |
"var MSG_A = goog.getMsg('msg A');" + |
| 391 |
"/** @desc d */\n" + |
| 392 |
"var MSG_B = goog.getMsg('msg B');" + |
| 393 |
"var x = goog.getMsgWithFallback(MSG_A, MSG_B);", |
| 394 |
"var MSG_A = 'msg A';" + |
| 395 |
"var MSG_B = 'msg B';" + |
| 396 |
"var x = MSG_A;"); |
| 397 |
} |
| 398 |
|
| 399 |
private void registerMessage(JsMessage message) { |
| 400 |
messages.put(message.getKey(), message); |
| 401 |
} |
| 402 |
|
| 403 |
private class SimpleMessageBundle implements MessageBundle { |
| 404 |
|
| 405 |
@Override |
| 406 |
public JsMessage getMessage(String id) { |
| 407 |
return messages.get(id); |
| 408 |
} |
| 409 |
|
| 410 |
@Override |
| 411 |
public Iterable<JsMessage> getAllMessages() { |
| 412 |
return messages.values(); |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public JsMessage.IdGenerator idGenerator() { |
| 417 |
return null; |
| 418 |
} |
| 419 |
} |
| 420 |
} |