Skip to content

Commit a130217

Browse files
committed
Fix and revise tests, run more tests with all versions
In order to ensure that our assumptions hold true with all supported versions of JSHint, we should run functional tests against all versions.
1 parent 3c9bbd6 commit a130217

File tree

2 files changed

+180
-139
lines changed

2 files changed

+180
-139
lines changed

com.eclipsesource.jshint.test/src/com/eclipsesource/jshint/JSHint_Compatibility_Test.java

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertTrue;
26+
import static org.junit.Assume.assumeTrue;
2627

2728

2829
@RunWith( value = Parameterized.class )
@@ -36,6 +37,7 @@ public class JSHint_Compatibility_Test {
3637
@Parameters
3738
public static Collection<Object[]> getParameters() {
3839
ArrayList<Object[]> parameters = new ArrayList<Object[]>();
40+
parameters.add( new Object[] { "com/jslint/jslint-2012-02-03.js" } );
3941
parameters.add( new Object[] { "com/jshint/jshint-r03.js" } );
4042
parameters.add( new Object[] { "com/jshint/jshint-r04.js" } );
4143
parameters.add( new Object[] { "com/jshint/jshint-r05.js" } );
@@ -46,7 +48,6 @@ public static Collection<Object[]> getParameters() {
4648
parameters.add( new Object[] { "com/jshint/jshint-r10.js" } );
4749
parameters.add( new Object[] { "com/jshint/jshint-r11.js" } );
4850
parameters.add( new Object[] { "com/jshint/jshint-r12.js" } );
49-
parameters.add( new Object[] { "com/jslint/jslint-2012-02-03.js" } );
5051
return parameters;
5152
}
5253

@@ -63,33 +64,138 @@ public void setUp() throws IOException {
6364
}
6465

6566
@Test
66-
public void check_noProblemsIfValid() {
67+
public void noProblemsForValidCode() {
6768
jsHint.check( "var a = 23;", handler );
6869

6970
assertTrue( problems.isEmpty() );
7071
}
7172

7273
@Test
73-
public void check_problemLineIs_1_Relative() {
74+
public void problemLineIs_1_Relative() {
7475
jsHint.check( "#", handler );
7576

7677
assertEquals( 1, problems.get( 0 ).getLine() );
7778
}
7879

7980
@Test
80-
public void check_problemCharacterIs_0_Relative() {
81+
public void problemCharacterIs_0_Relative() {
8182
jsHint.check( "#", handler );
8283

8384
assertEquals( 0, problems.get( 0 ).getCharacter() );
8485
}
8586

8687
@Test
87-
public void check_problemMessageIsNotEmpty() {
88+
public void cproblemMessageIsNotEmpty() {
8889
jsHint.check( "#", handler );
8990

9091
assertTrue( problems.get( 0 ).getMessage().length() > 0 );
9192
}
9293

94+
@Test
95+
public void undefinedVariable_withoutConfig_succeeds() {
96+
jsHint.check( "foo = {};", handler );
97+
98+
// seems that the undef option is inverted in jslint
99+
String expected = isJsLint() ? "1.0:'foo' was used before it was defined" : "";
100+
assertEquals( expected, getAllProblems() );
101+
}
102+
103+
@Test
104+
public void undefinedVariable_withoutPredefInConfig_fails() {
105+
jsHint.configure( new Configuration().addOption( "undef", true ) );
106+
107+
jsHint.check( "foo = {};", handler );
108+
109+
// seems that the undef option is inverted in jslint
110+
String expected = isJsLint() ? "" : "1.0:'foo' is not defined";
111+
assertEquals( expected, getAllProblems() );
112+
}
113+
114+
@Test
115+
public void undefinedVariable_withPredefInConfig_succeeds() {
116+
jsHint.configure( new Configuration().addOption( "undef", true ).addPredefined( "foo", true ) );
117+
118+
jsHint.check( "foo = {};", handler );
119+
120+
assertEquals( "", getAllProblems() );
121+
}
122+
123+
@Test
124+
public void undefinedVariable_withReadOnlyPredefInConfig_fails() {
125+
// FIXME [rst] See https://github.com/jshint/jshint/issues/665
126+
assumeTrue( !isVersion( "r10" ) && !isVersion( "r11" ) && !isVersion( "r12" ) );
127+
jsHint.configure( new Configuration().addOption( "undef", true ).addPredefined( "foo", false ) );
128+
129+
jsHint.check( "foo = {};", handler );
130+
131+
assertEquals( "1.0:Read only", getAllProblems() );
132+
}
133+
134+
@Test
135+
public void eqnull_withoutConfig() {
136+
jsHint.check( "var x = 23 == null;", handler );
137+
138+
String expected = isJsLint() ? "Expected '===' and instead saw '=='"
139+
: "Use '===' to compare with 'null'";
140+
assertEquals( "1.11:" + expected, getAllProblems() );
141+
}
142+
143+
@Test
144+
public void eqnull_withEmptyConfig() {
145+
jsHint.configure( new Configuration() );
146+
147+
jsHint.check( "var x = 23 == null;", handler );
148+
149+
String expected = isJsLint() ? "Expected '===' and instead saw '=='"
150+
: "Use '===' to compare with 'null'";
151+
assertEquals( "1.11:" + expected, getAllProblems() );
152+
}
153+
154+
@Test
155+
public void eqnull_withEqnullInConfig() {
156+
// JSLint doesn't get this right
157+
assumeTrue( !isJsLint() );
158+
jsHint.configure( new Configuration().addOption( "eqnull", true ) );
159+
160+
jsHint.check( "var f = x == null ? null : x + 1;", handler );
161+
162+
assertEquals( "", getAllProblems() );
163+
}
164+
165+
@Test
166+
public void positionIsCorrect() {
167+
jsHint.check( "var x = 23 == null;", handler );
168+
169+
assertEquals( "1.11", getPositionFromProblem( 0 ) );
170+
}
171+
172+
@Test
173+
public void positionIsCorrectWithLeadingSpace() {
174+
assumeTrue( !isJsLint() );
175+
jsHint.configure( new Configuration().addOption( "white", false ) );
176+
jsHint.check( " var x = 23 == null;", handler );
177+
178+
assertEquals( "1.12", getPositionFromProblem( 0 ) );
179+
}
180+
181+
@Test
182+
public void positionIsCorrectWithLeadingTab() {
183+
assumeTrue( !isJsLint() );
184+
jsHint.configure( new Configuration().addOption( "white", false ) );
185+
jsHint.check( "\tvar x = 23 == null;", handler );
186+
187+
assertEquals( "1.12", getPositionFromProblem( 0 ) );
188+
}
189+
190+
@Test
191+
public void positionIsCorrectWithMultipleTabs() {
192+
assumeTrue( !isJsLint() );
193+
jsHint.configure( new Configuration().addOption( "white", false ) );
194+
jsHint.check( "\tvar x\t= 23 == null;", handler );
195+
196+
assertEquals( "1.12", getPositionFromProblem( 0 ) );
197+
}
198+
93199
private void loadJsHint() throws IOException {
94200
ClassLoader classLoader = getClass().getClassLoader();
95201
InputStream stream = classLoader.getResourceAsStream( jsHintResource );
@@ -100,6 +206,34 @@ private void loadJsHint() throws IOException {
100206
}
101207
}
102208

209+
private boolean isVersion( String version ) {
210+
return jsHintResource.contains( version );
211+
}
212+
213+
private boolean isJsLint() {
214+
return jsHintResource.contains( "jslint" );
215+
}
216+
217+
private String getPositionFromProblem( int n ) {
218+
Problem problem = problems.get( n );
219+
return problem.getLine() + "." + problem.getCharacter();
220+
}
221+
222+
private String getAllProblems() {
223+
StringBuilder builder = new StringBuilder();
224+
for( Problem problem : problems ) {
225+
if( builder.length() > 0 ) {
226+
builder.append( ", " );
227+
}
228+
builder.append( problem.getLine() );
229+
builder.append( '.' );
230+
builder.append( problem.getCharacter() );
231+
builder.append( ':' );
232+
builder.append( problem.getMessage() );
233+
}
234+
return builder.toString();
235+
}
236+
103237
private class TestHandler implements ProblemHandler {
104238

105239
public void handleProblem( Problem problem ) {

0 commit comments

Comments
 (0)