|
17 | 17 | package com.google.errorprone.bugpatterns; |
18 | 18 |
|
19 | 19 | import com.google.errorprone.CompilationTestHelper; |
| 20 | +import com.google.testing.junit.testparameterinjector.TestParameter; |
| 21 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
20 | 22 | import org.junit.Test; |
21 | 23 | import org.junit.runner.RunWith; |
22 | | -import org.junit.runners.JUnit4; |
23 | 24 |
|
24 | | -@RunWith(JUnit4.class) |
| 25 | +@RunWith(TestParameterInjector.class) |
25 | 26 | public final class MisleadingEscapedSpaceTest { |
26 | 27 | private final CompilationTestHelper testHelper = |
27 | 28 | CompilationTestHelper.newInstance(MisleadingEscapedSpace.class, getClass()); |
28 | 29 |
|
| 30 | + private enum LineSeparator { |
| 31 | + CRLF("\r\n"), |
| 32 | + LF("\n"); |
| 33 | + |
| 34 | + final String separator; |
| 35 | + |
| 36 | + LineSeparator(String separator) { |
| 37 | + this.separator = separator; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @TestParameter private LineSeparator lineSeparator; |
| 42 | + |
| 43 | + private void test(String text) { |
| 44 | + testHelper.addSourceLines("Test.class", text.replace("\n", lineSeparator.separator)).doTest(); |
| 45 | + } |
| 46 | + |
29 | 47 | @Test |
30 | 48 | public void misleadingEscape() { |
31 | | - testHelper |
32 | | - .addSourceLines( |
33 | | - "Test.class", |
34 | | - """ |
35 | | - class Test { |
36 | | - // BUG: Diagnostic contains: |
37 | | - private static final String FOO = " \\s "; |
38 | | - } |
39 | | - """) |
40 | | - .doTest(); |
| 49 | + test( |
| 50 | + """ |
| 51 | + class Test { |
| 52 | + // BUG: Diagnostic contains: |
| 53 | + private static final String FOO = " \\s "; |
| 54 | + } |
| 55 | + """); |
41 | 56 | } |
42 | 57 |
|
43 | 58 | @Test |
44 | 59 | public void literalBackslashS() { |
45 | | - testHelper |
46 | | - .addSourceLines( |
47 | | - "Test.class", |
48 | | - """ |
49 | | - class Test { |
50 | | - private static final String FOO = " \\\\s "; |
51 | | - } |
52 | | - """) |
53 | | - .doTest(); |
| 60 | + test( |
| 61 | + """ |
| 62 | + class Test { |
| 63 | + private static final String FOO = " \\\\s "; |
| 64 | + } |
| 65 | + """); |
54 | 66 | } |
55 | 67 |
|
56 | 68 | @Test |
57 | 69 | public void asSingleCharacter_misleading() { |
58 | | - testHelper |
59 | | - .addSourceLines( |
60 | | - "Test.class", |
61 | | - """ |
62 | | - class Test { |
63 | | - // BUG: Diagnostic contains: |
64 | | - private static final char x = '\\s'; |
65 | | - } |
66 | | - """) |
67 | | - .doTest(); |
| 70 | + test( |
| 71 | + """ |
| 72 | + class Test { |
| 73 | + // BUG: Diagnostic contains: |
| 74 | + private static final char x = '\\s'; |
| 75 | + } |
| 76 | + """); |
68 | 77 | } |
69 | 78 |
|
70 | 79 | @Test |
71 | 80 | public void withinTextBlock_notAtEndOfLine_misleading() { |
72 | | - testHelper |
73 | | - .addSourceLines( |
74 | | - "Test.class", |
75 | | - """ |
76 | | - class Test { |
77 | | - private static final String FOO = |
78 | | - // BUG: Diagnostic contains: |
79 | | - \""" |
80 | | - foo \\s bar |
81 | | - \"""; |
82 | | - private static final String BAZ = |
83 | | - // BUG: Diagnostic contains: |
84 | | - \""" |
85 | | - foo \\s |
86 | | - bar \\s baz |
87 | | - \"""; |
88 | | - } |
89 | | - """) |
90 | | - .doTest(); |
| 81 | + test( |
| 82 | + """ |
| 83 | + class Test { |
| 84 | + private static final String FOO = |
| 85 | + // BUG: Diagnostic contains: |
| 86 | + \""" |
| 87 | + foo \\s bar |
| 88 | + \"""; |
| 89 | + private static final String BAZ = |
| 90 | + // BUG: Diagnostic contains: |
| 91 | + \""" |
| 92 | + foo \\s |
| 93 | + bar \\s baz |
| 94 | + \"""; |
| 95 | + } |
| 96 | + """); |
91 | 97 | } |
92 | 98 |
|
93 | 99 | @Test |
94 | 100 | public void atEndOfLine_notMisleading() { |
95 | | - testHelper |
96 | | - .addSourceLines( |
97 | | - "Test.class", |
98 | | - """ |
99 | | - class Test { |
100 | | - private static final String FOO = |
101 | | - \""" |
102 | | - foo \\s |
103 | | - bar \\s |
104 | | - \"""; |
105 | | - } |
106 | | - """) |
107 | | - .doTest(); |
| 101 | + test( |
| 102 | + """ |
| 103 | + class Test { |
| 104 | + private static final String FOO = |
| 105 | + \""" |
| 106 | + foo \\s |
| 107 | + bar \\s |
| 108 | + \"""; |
| 109 | + } |
| 110 | + """); |
108 | 111 | } |
109 | 112 |
|
110 | 113 | @Test |
111 | 114 | public void multipleAtEndOfLine_notMisleading() { |
112 | | - testHelper |
113 | | - .addSourceLines( |
114 | | - "Test.class", |
115 | | - """ |
116 | | - class Test { |
117 | | - private static final String FOO = |
118 | | - \""" |
119 | | - foo \\s\\s\\s\\s |
120 | | - \"""; |
121 | | - } |
122 | | - """) |
123 | | - .doTest(); |
| 115 | + test( |
| 116 | + """ |
| 117 | + class Test { |
| 118 | + private static final String FOO = |
| 119 | + \""" |
| 120 | + foo \\s\\s\\s\\s |
| 121 | + \"""; |
| 122 | + } |
| 123 | + """); |
124 | 124 | } |
125 | 125 |
|
126 | 126 | @Test |
127 | 127 | public void withinCommentInBrokenUpString_noFinding() { |
128 | | - testHelper |
129 | | - .addSourceLines( |
130 | | - "Test.class", |
131 | | - """ |
132 | | - class Test { |
133 | | - private static final String FOO = "foo" + /* \\s */ " bar"; |
134 | | - } |
135 | | - """) |
136 | | - .doTest(); |
| 128 | + test( |
| 129 | + """ |
| 130 | + class Test { |
| 131 | + private static final String FOO = "foo" + /* \\s */ " bar"; |
| 132 | + } |
| 133 | + """); |
137 | 134 | } |
138 | 135 |
|
139 | 136 | @Test |
140 | 137 | public void atEndOfString_noFinding() { |
141 | | - testHelper |
142 | | - .addSourceLines( |
143 | | - "Test.class", |
144 | | - """ |
145 | | - class Test { |
146 | | - private static final String FOO = |
147 | | - \""" |
148 | | - foo |
149 | | - bar\\s\"""; |
150 | | - } |
151 | | - """) |
152 | | - .doTest(); |
| 138 | + test( |
| 139 | + """ |
| 140 | + class Test { |
| 141 | + private static final String FOO = |
| 142 | + \""" |
| 143 | + foo |
| 144 | + bar\\s\"""; |
| 145 | + } |
| 146 | + """); |
153 | 147 | } |
154 | 148 |
|
155 | 149 | @Test |
156 | 150 | public void escapedSpaceAtEndOfString() { |
157 | | - testHelper |
158 | | - .addSourceLines( |
159 | | - "Test.class", |
160 | | - """ |
161 | | - class Test { |
162 | | - // BUG: Diagnostic contains: |
163 | | - private static final String FOO = "foo\\s"; |
164 | | - } |
165 | | - """) |
166 | | - .doTest(); |
| 151 | + test( |
| 152 | + """ |
| 153 | + class Test { |
| 154 | + // BUG: Diagnostic contains: |
| 155 | + private static final String FOO = "foo\\s"; |
| 156 | + } |
| 157 | + """); |
167 | 158 | } |
168 | 159 | } |
0 commit comments