Skip to content

Commit

Permalink
Create javascript-checks-testkit module
Browse files Browse the repository at this point in the history
  • Loading branch information
vilchik-elena committed Mar 1, 2016
1 parent 6733d9c commit 2f8bbdb
Show file tree
Hide file tree
Showing 147 changed files with 567 additions and 505 deletions.
40 changes: 40 additions & 0 deletions javascript-checks-testkit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>javascript</artifactId>
<groupId>org.sonarsource.javascript</groupId>
<version>2.11-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>javascript-checks-testkit</artifactId>

<name>SonarQube JavaScript :: Check Test Toolkit</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>javascript-frontend</artifactId>
</dependency>

<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks.utils;
package org.sonar.javascript.checks.verifier;

import com.google.common.base.Function;
import com.google.common.base.Splitter;
Expand All @@ -28,10 +28,7 @@
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.fest.assertions.Assertions;
import org.sonar.javascript.visitors.JavaScriptVisitorContext;
import org.sonar.javascript.checks.tests.TestIssue;
import org.sonar.javascript.checks.tests.TestUtils;
import org.sonar.plugins.javascript.api.JavaScriptCheck;
import org.sonar.plugins.javascript.api.tree.Tree;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
Expand All @@ -42,12 +39,53 @@
import org.sonar.plugins.javascript.api.visitors.LineIssue;
import org.sonar.plugins.javascript.api.visitors.PreciseIssue;
import org.sonar.plugins.javascript.api.visitors.SubscriptionVisitorCheck;
import org.sonar.squidbridge.checks.CheckMessagesVerifier;

import static org.fest.assertions.Assertions.assertThat;

public class JavaScriptCheckVerifier extends SubscriptionVisitorCheck {

private final List<TestIssue> expectedIssues = new ArrayList<>();

/**
* Example:
* <pre>
* JavaScriptCheckVerifier.issues(new MyCheck(), myFile))
* .next().atLine(2).withMessage("This is message for line 2.")
* .next().atLine(3).withMessage("This is message for line 3.").withCost(2.)
* .next().atLine(8)
* .noMore();
* </pre>
*/
public static CheckMessagesVerifier issues(JavaScriptCheck check, File file) {
return CheckMessagesVerifier.verify(TreeCheckTest.getIssues(file.getAbsolutePath(), check));
}

/**
* To use this message you should provide a comment on each line of the source file where you expect an issue.
* For example:
* <pre>
* var x = 1; // Noncompliant {{A message for this line.}}
*
* function foo() { // Noncompliant [[effortToFix=2]] [[secondary=+0,+1]] [[sc=5;ec=6;el=+0]]
* }
* </pre>
* How to write these comments:
* <ul>
* <li>Put a comment starting with "Noncompliant" if you expect an issue on this line.</li>
* <li>In double curly braces <code>{{MESSAGE}}</code> provide expected message.</li>
* <li>In double brackets provide expected effort to fix (cost) with <code>effortToFix</code> keyword.</li>
* <li>In double brackets provide precise location description with <code>sc, ec, el</code> keywords respectively for start column, end column and end line.</li>
* <li>In double brackets provide secondary locations with <code>secondary</code> keyword.</li>
* <li>To specify the line you can use relative location by putting <code>+</code> or <code>-</code>.</li>
* <li>All listed elements are optional (except "Noncompliant").</li>
* </ul>
*
* Example of call:
* <pre>
* JavaScriptCheckVerifier.verify(new MyCheck(), myFile));
* </pre>
*/
public static void verify(JavaScriptCheck check, File file) {
JavaScriptCheckVerifier javaScriptCheckVerifier = new JavaScriptCheckVerifier();
JavaScriptVisitorContext context = TestUtils.createContext(file);
Expand Down Expand Up @@ -84,22 +122,22 @@ private static void verifyIssue(TestIssue expected, Issue actual) {
throw new AssertionError("Unexpected issue at line " + line(actual) + ": \"" + message(actual) + "\"");
}
if (expected.message() != null) {
Assertions.assertThat(message(actual)).as("Bad message at line " + expected.line()).isEqualTo(expected.message());
assertThat(message(actual)).as("Bad message at line " + expected.line()).isEqualTo(expected.message());
}
if (expected.effortToFix() != null) {
Assertions.assertThat(actual.cost()).as("Bad effortToFix at line " + expected.line()).isEqualTo(expected.effortToFix());
assertThat(actual.cost()).as("Bad effortToFix at line " + expected.line()).isEqualTo(expected.effortToFix());
}
if (expected.startColumn() != null) {
Assertions.assertThat(((PreciseIssue) actual).primaryLocation().startLineOffset() + 1).as("Bad start column at line " + expected.line()).isEqualTo(expected.startColumn());
assertThat(((PreciseIssue) actual).primaryLocation().startLineOffset() + 1).as("Bad start column at line " + expected.line()).isEqualTo(expected.startColumn());
}
if (expected.endColumn() != null) {
Assertions.assertThat(((PreciseIssue) actual).primaryLocation().endLineOffset() + 1).as("Bad end column at line " + expected.line()).isEqualTo(expected.endColumn());
assertThat(((PreciseIssue) actual).primaryLocation().endLineOffset() + 1).as("Bad end column at line " + expected.line()).isEqualTo(expected.endColumn());
}
if (expected.endLine() != null) {
Assertions.assertThat(((PreciseIssue) actual).primaryLocation().endLine()).as("Bad end line at line " + expected.line()).isEqualTo(expected.endLine());
assertThat(((PreciseIssue) actual).primaryLocation().endLine()).as("Bad end line at line " + expected.line()).isEqualTo(expected.endLine());
}
if (expected.secondaryLines() != null) {
Assertions.assertThat(secondary(actual)).as("Bad secondary locations at line " + expected.line()).isEqualTo(expected.secondaryLines());
assertThat(secondary(actual)).as("Bad secondary locations at line " + expected.line()).isEqualTo(expected.secondaryLines());
}
}

Expand Down Expand Up @@ -137,7 +175,7 @@ public void visitNode(Tree tree) {
}
}

private void addParams(TestIssue issue, String params) {
private static void addParams(TestIssue issue, String params) {
for (String param : Splitter.on(';').split(params)) {
int equalIndex = param.indexOf("=");
if (equalIndex == -1) {
Expand All @@ -159,21 +197,25 @@ private void addParams(TestIssue issue, String params) {
issue.endLine(lineValue(issue.line(), value));

} else if ("secondary".equalsIgnoreCase(name)) {
List<Integer> secondaryLines = new ArrayList<>();
if (!value.equals("")) {
for (String secondary : Splitter.on(',').split(value)) {
secondaryLines.add(lineValue(issue.line(), secondary));
}
}
issue.secondary(secondaryLines);
addSecondaryLines(issue, value);

} else {
throw new IllegalStateException("Invalid param at line 1: " + name);
}
}
}

private int lineValue(int baseLine, String shift) {
private static void addSecondaryLines(TestIssue issue, String value) {
List<Integer> secondaryLines = new ArrayList<>();
if (!"".equals(value)) {
for (String secondary : Splitter.on(',').split(value)) {
secondaryLines.add(lineValue(issue.line(), secondary));
}
}
issue.secondary(secondaryLines);
}

private static int lineValue(int baseLine, String shift) {
if (shift.startsWith("+")) {
return baseLine + Integer.valueOf(shift.substring(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks.tests;
package org.sonar.javascript.checks.verifier;

import com.google.common.primitives.Ints;
import java.util.List;
import javax.annotation.Nullable;

public class TestIssue {
class TestIssue {

private String message;
private final int line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks.tests;
package org.sonar.javascript.checks.verifier;

import com.google.common.base.Charsets;
import com.sonar.sslr.api.typed.ActionParser;
Expand All @@ -31,7 +31,7 @@
import org.sonar.plugins.javascript.api.tree.ScriptTree;
import org.sonar.plugins.javascript.api.tree.Tree;

public class TestUtils {
class TestUtils {

private TestUtils() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,30 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks.tests;
package org.sonar.javascript.checks.verifier;

import com.sonar.sslr.api.RecognitionException;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.sonar.javascript.visitors.JavaScriptVisitorContext;
import org.sonar.javascript.checks.ParsingErrorCheck;
import org.sonar.plugins.javascript.api.JavaScriptCheck;
import org.sonar.plugins.javascript.api.visitors.FileIssue;
import org.sonar.plugins.javascript.api.visitors.Issue;
import org.sonar.plugins.javascript.api.visitors.LineIssue;
import org.sonar.plugins.javascript.api.visitors.PreciseIssue;
import org.sonar.squidbridge.api.CheckMessage;

public class TreeCheckTest {
class TreeCheckTest {

public Collection<CheckMessage> getIssues(String relativePath, JavaScriptCheck check) {
File file = new File(relativePath);
List<Issue> issues = new ArrayList<>();
private TreeCheckTest() {
}

try {
JavaScriptVisitorContext context = TestUtils.createContext(file);
issues = check.scanFile(context);
public static Collection<CheckMessage> getIssues(String relativePath, JavaScriptCheck check) {
File file = new File(relativePath);

} catch (RecognitionException e) {
if (check instanceof ParsingErrorCheck) {
issues.add(new LineIssue(check, e.getLine(), e.getMessage()));
}
}
JavaScriptVisitorContext context = TestUtils.createContext(file);
List<Issue> issues = check.scanFile(context);

return getCheckMessages(issues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,8 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks;

import org.junit.Test;
import org.sonar.javascript.checks.tests.TreeCheckTest;
import org.sonar.squidbridge.checks.CheckMessagesVerifier;

import static org.hamcrest.Matchers.containsString;

public class ParsingErrorCheckTest extends TreeCheckTest {

@Test
public void test() {
CheckMessagesVerifier.verify(getIssues("src/test/resources/checks/parsingError.js", new ParsingErrorCheck()))
.next().atLine(3).withMessageThat(containsString("Parse error"))
.noMore();
}

}
/**
* Provides helper classes for coding rules implementation
*/
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.javascript.checks.verifier;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.javascript.checks.utils;
package org.sonar.javascript.checks.verifier;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
Expand All @@ -33,7 +33,6 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.javascript.checks.tests.TestIssue;
import org.sonar.javascript.tree.impl.JavaScriptTree;
import org.sonar.javascript.tree.impl.lexical.InternalSyntaxToken;
import org.sonar.plugins.javascript.api.JavaScriptCheck;
Expand Down Expand Up @@ -314,8 +313,6 @@ private static InternalSyntaxToken createToken(int line, @Nullable Integer colum
int tokenColumn = column == null ? 0 : column - 1;
return new InternalSyntaxToken(line, tokenColumn, tokenValue, ImmutableList.<SyntaxTrivia>of(), 0, false);
}


}

}
24 changes: 9 additions & 15 deletions javascript-checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,30 @@
<groupId>${project.groupId}</groupId>
<artifactId>javascript-frontend</artifactId>
</dependency>

<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
<groupId>${project.groupId}</groupId>
<artifactId>javascript-checks-testkit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.io.File;
import org.junit.Test;
import org.sonar.javascript.checks.utils.JavaScriptCheckVerifier;
import org.sonar.javascript.checks.verifier.JavaScriptCheckVerifier;

public class AlertUseCheckTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.io.File;
import org.junit.Test;
import org.sonar.javascript.checks.utils.JavaScriptCheckVerifier;
import org.sonar.javascript.checks.verifier.JavaScriptCheckVerifier;

public class AlwaysUseCurlyBracesCheckTest {

Expand Down
Loading

0 comments on commit 2f8bbdb

Please sign in to comment.