Debian Codegraph @

test/com/google/javascript/jscomp/ReplaceMessagesForChromeTest.java

raw symbols SCIP
1 /*
2 * Copyright 2012 The Closure Compiler Authors.
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 package com.google.javascript.jscomp;
18
19 import static com.google.javascript.jscomp.JsMessage.Style.RELAX;
20
21 import com.google.javascript.jscomp.JsMessage.Style;
22
23 /**
24 * Test which checks that replacer works correctly.
25 *
26 */
27 public class ReplaceMessagesForChromeTest extends CompilerTestCase {
28
29 private Style style = RELAX;
30
31 @Override
32 protected CompilerPass getProcessor(Compiler compiler) {
33 return new ReplaceMessagesForChrome(compiler,
34 new GoogleJsMessageIdGenerator(null), false, style);
35 }
36
37 @Override
38 protected int getNumRepetitions() {
39 // No longer valid on the second run.
40 return 1;
41 }
42
43 @Override
44 protected void setUp() {
45 style = RELAX;
46 }
47
48 public void testReplaceSimpleMessage() {
49 test("/** @desc A simple message. */\n" +
50 "var MSG_A = goog.getMsg('Hello world');",
51 "var MSG_A=chrome.i18n.getMessage('8660696502365331902');");
52
53 test("/** @desc A message attached to an object. */\n" +
54 "foo.bar.MSG_B = goog.getMsg('Goodbye world');",
55 "foo.bar.MSG_B=chrome.i18n.getMessage('2356086230621084760');");
56 }
57
58 public void testReplaceSinglePlaceholder() {
59 test("/** @desc A message with one placeholder. */\n" +
60 "var MSG_C = goog.getMsg('Hello, {$name}', {name: 'Tyler'});",
61 "var MSG_C=chrome.i18n.getMessage('4985325380591528435', ['Tyler']);");
62 }
63
64 public void testReplaceTwoPlaceholders() {
65 test("/** @desc A message with two placeholders. */\n" +
66 "var MSG_D = goog.getMsg('{$greeting}, {$name}', " +
67 "{greeting: 'Hi', name: 'Tyler'});",
68 "var MSG_D=chrome.i18n.getMessage('3605047247574980322', " +
69 "['Hi', 'Tyler']);");
70
71 test("/** @desc A message with two placeholders, but their order is\n" +
72 " * reversed in the object literal. (Shouldn't make a difference.)\n" +
73 " */\n" +
74 "var MSG_E = goog.getMsg('{$greeting}, {$name}!', " +
75 "{name: 'Tyler', greeting: 'Hi'});",
76 "var MSG_E=chrome.i18n.getMessage('691522386483664339', " +
77 "['Hi', 'Tyler']);");
78 }
79
80 public void testReplacePlaceholderMissingValue() {
81 test("/** @desc A message with two placeholders, but one is missing. */\n" +
82 "var MSG_F = goog.getMsg('{$greeting}, {$name}!', {name: 'Tyler'});",
83 null, JsMessageVisitor.MESSAGE_TREE_MALFORMED);
84 }
85
86 public void testReplaceTwoPlaceholdersNonAlphaOrder() {
87 test("/** @desc A message with two placeholders not in order .*/\n" +
88 "var MSG_G = goog.getMsg('{$name}: {$greeting}', " +
89 "{greeting: 'Salutations', name: 'Tyler'});",
90 "var MSG_G=chrome.i18n.getMessage('7437383242562773138', " +
91 "['Salutations', 'Tyler']);");
92 }
93
94 public void testReplaceExternalMessage() {
95 test("/** @desc A message that was extracted with SoyMsgExtractor. */\n" +
96 "var MSG_EXTERNAL_1357902468 = goog.getMsg('Hello world');",
97 "var MSG_EXTERNAL_1357902468 = chrome.i18n.getMessage('1357902468');");
98 }
99 }