forked from google/error-prone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormatStringTest.java
411 lines (359 loc) · 12.5 KB
/
FormatStringTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2016 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.bugpatterns.formatstring;
import static org.junit.Assume.assumeTrue;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** {@link FormatString}Test */
@RunWith(JUnit4.class)
public class FormatStringTest {
private final CompilationTestHelper compilationHelper =
CompilationTestHelper.newInstance(FormatString.class, getClass());
private void testFormat(String expected, String formatString) {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Locale;",
"import java.io.PrintWriter;",
"import java.io.PrintStream;",
"import java.io.Console;",
"class Test {",
" void f() {",
" // BUG: Diagnostic contains: " + expected,
" " + formatString,
" }",
"}")
.doTest();
}
@Test
public void testDuplicateFormatFlags() throws Exception {
testFormat("duplicate format flags: +", "String.format(\"e = %++10.4f\", Math.E);");
}
@Test
public void testFormatFlagsConversionMismatch() throws Exception {
testFormat(
"format specifier '%b' is not compatible with the given flag(s): #",
"String.format(\"%#b\", Math.E);");
}
@Test
public void testIllegalFormatCodePoint() throws Exception {
testFormat("invalid Unicode code point: 110000", "String.format(\"%c\", 0x110000);");
}
@Test
public void testIllegalFormatConversion() throws Exception {
testFormat(
"illegal format conversion: 'java.lang.String' cannot be formatted using '%f'",
"String.format(\"%f\", \"abcd\");");
}
@Test
public void testIllegalFormatFlags() throws Exception {
testFormat("illegal format flags: -0", "String.format(\"%-010d\", 5);");
}
@Test
public void testIllegalFormatPrecision() throws Exception {
testFormat("illegal format precision: 1", "String.format(\"%.1c\", 'c');");
}
@Test
public void testIllegalFormatWidth() throws Exception {
testFormat("illegal format width: 1", "String.format(\"%1n\");");
}
@Test
public void testMissingFormatArgument() throws Exception {
testFormat("missing argument for format specifier '%<s'", "String.format(\"%<s\", \"test\");");
}
@Test
public void testMissingFormatWidth() throws Exception {
testFormat("missing format width: %-f", "String.format(\"e = %-f\", Math.E);");
}
@Test
public void testUnknownFormatConversion() throws Exception {
testFormat("unknown format conversion: 'r'", "String.format(\"%r\", \"hello\");");
}
@Test
public void testCStyleLongConversion() throws Exception {
testFormat("use %d to format integral types", "String.format(\"%l\", 42);");
}
@Test
public void testCStyleLongConversion2() throws Exception {
testFormat("use %d to format integral types", "String.format(\"%ld\", 42);");
}
@Test
public void testCStyleLongConversion3() throws Exception {
testFormat("use %d to format integral types", "String.format(\"%lld\", 42);");
}
@Test
public void testCStyleLongConversion4() throws Exception {
testFormat("%f, %g or %e to format floating point types", "String.format(\"%lf\", 42);");
}
@Test
public void testCStyleLongConversion5() throws Exception {
testFormat("%f, %g or %e to format floating point types", "String.format(\"%llf\", 42);");
}
@Test
public void testConditionalExpression() throws Exception {
testFormat(
"missing argument for format specifier '%s'", "String.format(true ? \"\" : \"%s\");");
}
@Test
public void testConditionalExpression2() throws Exception {
testFormat(
"missing argument for format specifier '%s'", "String.format(true ? \"%s\" : \"\");");
}
@Test
public void testConditionalExpression3() throws Exception {
testFormat(
"extra format arguments: used 1, provided 2",
"String.format(true ? \"%s\" : \"%s\", 1, 2);");
}
@Test
public void testConditionalExpression4() throws Exception {
testFormat(
"extra format arguments: used 1, provided 2",
"String.format(true ? \"%s\" : \"%s\", 1, 2);");
}
@Test
public void testConditionalExpression5() throws Exception {
testFormat(
"missing argument for format specifier '%s'",
"String.format(true ? \"%s\" : true ? \"%s\" : \"\");");
}
@Test
public void missingArguments() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" // BUG: Diagnostic contains: missing argument for format specifier '%s'",
" String.format(\"%s %s %s\", 42);",
" // BUG: Diagnostic contains: missing argument for format specifier '%s'",
" String.format(\"%s %s %s\", 42, 42);",
" String.format(\"%s %s %s\", 42, 42, 42);",
" }",
"}")
.doTest();
}
@Test
public void extraArguments() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" String.format(\"%s %s\", 1, 2);",
" // BUG: Diagnostic contains: extra format arguments: used 2, provided 3",
" String.format(\"%s %s\", 1, 2, 3);",
" // BUG: Diagnostic contains: extra format arguments: used 2, provided 4",
" String.format(\"%s %s\", 1, 2, 3, 4);",
" // BUG: Diagnostic contains: extra format arguments: used 0, provided 1",
" String.format(\"{0}\", 1);",
" }",
"}")
.doTest();
}
@Test
public void negative() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f(boolean b) {",
" String.format(\"%d\", 42);",
" String.format(\"%d\", 42L);",
" String.format(\"%f\", 42.0f);",
" String.format(\"%f\", 42.0d);",
" String.format(\"%s\", \"hello\");",
" String.format(\"%s\", 42);",
" String.format(\"%s\", (Object) null);",
" String.format(\"%s\", new Object());",
" String.format(\"%c\", 'c');",
" String.format(b ? \"%s\" : \"%d\", 42);",
" }",
"}")
.doTest();
}
@Test
public void testPrintfMethods_StringFormat() throws Exception {
testFormat("", "String.format(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_StringFormatWithLocale() throws Exception {
testFormat("", "String.format(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintWriterFormat() throws Exception {
testFormat("", "new PrintWriter(System.err).format(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintWriterFormatWithLocale() throws Exception {
testFormat("", "new PrintWriter(System.err).format(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintWriterPrintf() throws Exception {
testFormat("", "new PrintWriter(System.err).printf(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintWriterPrintfWithLocale() throws Exception {
testFormat("", "new PrintWriter(System.err).printf(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintStreamFormat() throws Exception {
testFormat("", "new PrintStream(System.err).format(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintStreamFormatWithLocale() throws Exception {
testFormat("", "new PrintStream(System.err).format(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintStreamPrintf() throws Exception {
testFormat("", "new PrintStream(System.err).printf(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_PrintStreamPrintfWithLocale() throws Exception {
testFormat("", "new PrintStream(System.err).printf(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_FormatterFormatWithLocale() throws Exception {
testFormat(
"", "new java.util.Formatter(System.err).format(Locale.ENGLISH, \"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_ConsolePrintf() throws Exception {
testFormat("", "System.console().printf(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_ConsoleFormat() throws Exception {
testFormat("", "System.console().format(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_ConsoleFormat_noErrorsWithEmptyArgs() throws Exception {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" System.console().readLine();",
" }",
"}")
.doTest();
}
@Test
public void testPrintfMethods_ConsoleReadline() throws Exception {
testFormat("", "System.console().readLine(\"%d\", \"hello\");");
}
@Test
public void testPrintfMethods_ConsoleReadPassword() throws Exception {
testFormat("", "System.console().readPassword(\"%d\", \"hello\");");
}
@Test
public void nullArgument() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" String.format(\"%s %s\", null, null);",
" }",
"}")
.doTest();
}
@Test
public void javaUtilTime() {
compilationHelper
.addSourceLines(
"Test.java",
"import java.time.*;",
"class Test {",
" void f() {",
" System.err.printf(\"%tY\",",
" LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));",
" System.err.printf(\"%tQ\", Instant.now());",
" System.err.printf(\"%tZ\", ZonedDateTime.of(LocalDate.of(2018, 12, 27),"
+ " LocalTime.of(17, 0), ZoneId.of(\"Europe/London\")));",
" }",
"}")
.doTest();
}
@Test
public void number() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f(Number n) {",
" System.err.printf(\"%x\", n);",
" }",
"}")
.doTest();
}
@Test
public void invalidIndex() {
assumeTrue(RuntimeVersion.isAtLeast16());
compilationHelper
.addSourceLines(
"T.java",
"class T {",
" public static void main(String[] args) {",
" // BUG: Diagnostic contains: Illegal format argument index",
" System.err.printf(\" %0$2s) %s\", args[0], args[1]);",
" }",
"}")
.doTest();
}
@Test
public void stringFormattedNegativeCase() {
assumeTrue(RuntimeVersion.isAtLeast15());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" \"%s %s\".formatted(\"foo\", \"baz\");",
" }",
"}")
.doTest();
}
@Test
public void stringFormattedPositiveCase() {
assumeTrue(RuntimeVersion.isAtLeast15());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" // BUG: Diagnostic contains: missing argument for format specifier",
" \"%s %s %s\".formatted(\"foo\", \"baz\");",
" }",
"}")
.doTest();
}
@Test
public void nonConstantStringFormattedNegativeCase() {
assumeTrue(RuntimeVersion.isAtLeast15());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f(String f) {",
" f.formatted(\"foo\", \"baz\");",
" }",
"}")
.doTest();
}
}