Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2004 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.common.base.Joiner;
22 import com.google.common.collect.Lists;
23
24 import junit.framework.TestCase;
25
26 import java.io.IOException;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32 * Unit test for {@link JsMessageExtractor}.
33 *
34 */
35 public class JsMessageExtractorTest extends TestCase {
36
37 private Collection<JsMessage> extractMessages(String... js) {
38 try {
39 String sourceCode = Joiner.on("\n").join(js);
40 return new JsMessageExtractor(null, RELAX)
41 .extractMessages(SourceFile.fromCode("testcode", sourceCode));
42 } catch (IOException e) {
43 fail(e.getMessage());
44 return null;
45 }
46 }
47
48 private JsMessage extractMessage(String... js) {
49 Collection<JsMessage> messages = extractMessages(js);
50 assertEquals(1, messages.size());
51 return messages.iterator().next();
52 }
53
54 public void testSyntaxError1() {
55 try {
56 extractMessage("if (true) {}}");
57 fail("Expected exception");
58 } catch (RuntimeException e) {
59 assertTrue(e.getMessage().contains("JSCompiler errors\n"));
60 assertTrue(e.getMessage().contains(
61 "testcode:1: ERROR - Parse error. syntax error\n"));
62 assertTrue(e.getMessage().contains("if (true) {}}\n"));
63 }
64 }
65
66 public void testSyntaxError2() {
67 try {
68 extractMessage("", "if (true) {}}");
69 fail("Expected exception");
70 } catch (RuntimeException e) {
71 assertTrue(
72 e.getMessage(),
73 e.getMessage().contains("JSCompiler errors\n"));
74 assertTrue(
75 e.getMessage(),
76 e.getMessage().contains(
77 "testcode:2: ERROR - Parse error. syntax error\n"));
78 assertTrue(
79 e.getMessage(),
80 e.getMessage().contains("if (true) {}}\n"));
81 }
82 }
83
84 public void testExtractNewStyleMessage1() {
85 // A simple message with no description.
86 assertEquals(
87 new JsMessage.Builder("MSG_SILLY")
88 .appendStringPart("silly test message")
89 .build(),
90 extractMessage("var MSG_SILLY = goog.getMsg('silly test message');"));
91 }
92
93 public void testExtractNewStyleMessage2() {
94 // A message with placeholders and meta data.
95 assertEquals(
96 new JsMessage.Builder("MSG_WELCOME")
97 .appendStringPart("Hi ")
98 .appendPlaceholderReference("userName")
99 .appendStringPart("! Welcome to ")
100 .appendPlaceholderReference("product")
101 .appendStringPart(".")
102 .setDesc("The welcome message.")
103 .setIsHidden(true)
104 .build(),
105 extractMessage(
106 "/**",
107 " * @desc The welcome",
108 " * message.",
109 " *",
110 " * @hidden",
111 " */",
112 "var MSG_WELCOME = goog.getMsg(",
113 " 'Hi {$userName}! Welcome to {$product}.',",
114 " {userName: someUserName, product: getProductName()});"));
115 }
116
117 public void testExtractOldStyleMessage1() {
118 // Description before the message.
119 assertEquals(
120 new JsMessage.Builder("MSG_SILLY")
121 .appendStringPart("silly test message")
122 .setDesc("Description.")
123 .build(),
124 extractMessage(
125 "var MSG_SILLY_HELP = 'Description.';",
126 "var MSG_SILLY = 'silly test message';"));
127 }
128
129 public void testExtractOldStyleMessage2() {
130 // Description after the message, broken into parts.
131 assertEquals(
132 new JsMessage.Builder("MSG_SILLY")
133 .appendStringPart("silly test message")
134 .setDesc("Description.")
135 .build(),
136 extractMessage(
137 "var MSG_SILLY = 'silly test message';",
138 "var MSG_SILLY_HELP = 'Descrip' + 'tion.';"));
139 }
140
141 public void testExtractOldStyleMessage3() {
142 // Function-style message with two placeholders and no description.
143 assertEquals(
144 new JsMessage.Builder("MSG_SILLY")
145 .appendPlaceholderReference("one")
146 .appendStringPart(", ")
147 .appendPlaceholderReference("two")
148 .appendStringPart(", buckle my shoe")
149 .build(),
150 extractMessage(
151 "var MSG_SILLY = function(one, two) {",
152 " return one + ', ' + two + ', buckle my shoe';",
153 "};"));
154 }
155
156 public void testExtractMixedMessages() {
157 // Several mixed-style messages in succession, one containing newlines.
158 Iterator<JsMessage> msgs = extractMessages(
159 "var MSG_MONEY = function(amount) {",
160 " return 'You owe $' + amount +",
161 " ' to the credit card company.';",
162 "};",
163 "var MSG_TIME = goog.getMsg('You need to finish your work in ' +",
164 " '{$duration} hours.', {'duration': d});",
165 "var MSG_NAG = 'Clean your room.\\n\\nWash your clothes.';",
166 "var MSG_NAG_HELP = 'Just some ' +",
167 " 'nags.';").iterator();
168
169 assertEquals(
170 new JsMessage.Builder("MSG_MONEY")
171 .appendStringPart("You owe $")
172 .appendPlaceholderReference("amount")
173 .appendStringPart(" to the credit card company.")
174 .build(),
175 msgs.next());
176 assertEquals(
177 new JsMessage.Builder("MSG_TIME")
178 .appendStringPart("You need to finish your work in ")
179 .appendPlaceholderReference("duration")
180 .appendStringPart(" hours.")
181 .build(),
182 msgs.next());
183 assertEquals(
184 new JsMessage.Builder("MSG_NAG")
185 .appendStringPart("Clean your room.\n\nWash your clothes.")
186 .setDesc("Just some nags.")
187 .build(),
188 msgs.next());
189 }
190
191 public void testDuplicateUnnamedVariables() {
192 // Make sure that duplicate unnamed variables don't get swallowed when using
193 // a Google-specific ID generator.
194 Collection<JsMessage> msgs = extractMessages(
195 "function a() {",
196 " var MSG_UNNAMED_2 = goog.getMsg('foo');",
197 "}",
198 "function b() {",
199 " var MSG_UNNAMED_2 = goog.getMsg('bar');",
200 "}");
201
202 assertEquals(2, msgs.size());
203 final Iterator<JsMessage> iter = msgs.iterator();
204 assertEquals("foo", iter.next().toString());
205 assertEquals("bar", iter.next().toString());
206 }
207
208 public void testMeaningAnnotation() {
209 List<JsMessage> msgs = Lists.newArrayList(
210 extractMessages(
211 "var MSG_UNNAMED_1 = goog.getMsg('foo');",
212 "var MSG_UNNAMED_2 = goog.getMsg('foo');"));
213 assertEquals(2, msgs.size());
214 assertTrue(msgs.get(0).getId().equals(msgs.get(1).getId()));
215 assertEquals(msgs.get(0), msgs.get(1));
216
217 msgs = Lists.newArrayList(
218 extractMessages(
219 "var MSG_UNNAMED_1 = goog.getMsg('foo');",
220 "/** @meaning bar */ var MSG_UNNAMED_2 = goog.getMsg('foo');"));
221 assertEquals(2, msgs.size());
222 assertFalse(msgs.get(0).getId().equals(msgs.get(1).getId()));
223 }
224
225 private void assertEquals(JsMessage expected, JsMessage actual) {
226 assertEquals(expected.getId(), actual.getId());
227 assertEquals(expected.getKey(), actual.getKey());
228 assertEquals(expected.parts(), actual.parts());
229 assertEquals(expected.placeholders(), actual.placeholders());
230 assertEquals(expected.getDesc(), actual.getDesc());
231 assertEquals(expected.isHidden(), actual.isHidden());
232 assertEquals(expected.getMeaning(), actual.getMeaning());
233 }
234 }