-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global analyzer to catch common mistakes (#85)
* Add global analyzer to catch common mistakes * Expand golden test scenarios * Add feedback request if any comments * Add golden test that receives no feedback
- Loading branch information
1 parent
c20b9ed
commit 65325ac
Showing
22 changed files
with
279 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/avoid_print_statements.md">Markdown Template</a> | ||
*/ | ||
public class AvoidPrintStatements extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.avoid_print_statements"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.INFORMATIVE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/do_not_use_main_method.md">Markdown Template</a> | ||
*/ | ||
public class DoNotUseMainMethod extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.do_not_use_main_method"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.ESSENTIAL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/feedback_request.md">Markdown Template</a> | ||
*/ | ||
public class FeedbackRequest extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.feedback_request"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package analyzer.exercises; | ||
|
||
import analyzer.Analysis; | ||
import analyzer.Analyzer; | ||
import analyzer.comments.AvoidPrintStatements; | ||
import analyzer.comments.DoNotUseMainMethod; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import java.util.List; | ||
|
||
public class GlobalAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer { | ||
private Analysis analysis; | ||
|
||
@Override | ||
public void analyze(List<CompilationUnit> compilationUnits, Analysis analysis) { | ||
this.analysis = analysis; | ||
for (CompilationUnit compilationUnit : compilationUnits) { | ||
compilationUnit.accept(this, null); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(MethodDeclaration n, Void arg) { | ||
if (isMainMethod(n)) { | ||
analysis.addComment(new DoNotUseMainMethod()); | ||
} | ||
|
||
super.visit(n, arg); | ||
} | ||
|
||
@Override | ||
public void visit(MethodCallExpr n, Void arg) { | ||
if (isPrintStatement(n)) { | ||
analysis.addComment(new AvoidPrintStatements()); | ||
} | ||
|
||
super.visit(n, arg); | ||
} | ||
|
||
private static boolean isMainMethod(MethodDeclaration node) { | ||
return node.getNameAsString().equals("main") && node.isStatic() && node.getType().isVoidType(); | ||
} | ||
|
||
private static boolean isPrintStatement(MethodCallExpr node) { | ||
return node.getScope() | ||
.map(scope -> scope.toString().matches("System\\.(?:out|err)")) | ||
.orElse(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
...t/java/analyzer/ExerciseAnalyzerTest.java → src/test/java/analyzer/AnalyzerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package analyzer.exercises; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.AnalyzerTest; | ||
import analyzer.comments.AvoidPrintStatements; | ||
import analyzer.comments.DoNotUseMainMethod; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GlobalAnalyzerTest extends AnalyzerTest { | ||
@Override | ||
protected Analyzer getAnalyzer() { | ||
return new GlobalAnalyzer(); | ||
} | ||
|
||
@MethodSource | ||
@ParameterizedTest | ||
public void solutionsWithMainMethod(String solution) { | ||
var actual = analyzeString(solution); | ||
assertThat(actual.getComments()).contains(new DoNotUseMainMethod()); | ||
} | ||
|
||
private static Stream<String> solutionsWithMainMethod() { | ||
return Stream.of( | ||
""" | ||
class Solution { | ||
public static void main(String... args) {} | ||
}""", | ||
""" | ||
class Solution { | ||
public static void main(String ...args) {} | ||
}""", | ||
""" | ||
class Solution { | ||
public static void main(String[] args) {} | ||
}""" | ||
); | ||
} | ||
|
||
@MethodSource | ||
@ParameterizedTest | ||
public void solutionsWithPrintStatements(String solution) { | ||
var actual = analyzeString(solution); | ||
assertThat(actual.getComments()).contains(new AvoidPrintStatements()); | ||
} | ||
|
||
private static Stream<String> solutionsWithPrintStatements() { | ||
return Stream.of( | ||
""" | ||
class Solution { | ||
void method() { | ||
System.out.println("Printing line to stdout"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.err.println("Printing line to stderr"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.out.print("Printing to stdout"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.err.print("Printing to stderr"); | ||
} | ||
}""" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This test is designed to receive no comments at all from the analyzer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Greeter { | ||
String getGreeting() { | ||
return "Hello, World!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This test is designed to receive comments from multiple analyzers: | ||
|
||
- The global analyzer should complain about the use of a `main` method and print statements | ||
- The `two-fer` analyzer should complain about multiple return statements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{"comments": [ | ||
{ | ||
"comment": "java.general.do_not_use_main_method", | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.two-fer.avoid_string_format", | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.avoid_print_statements", | ||
"type": "informative" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"type": "informative" | ||
} | ||
]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Twofer { | ||
public String twofer(String name) { | ||
if (name == null) { | ||
return "Two for you, two for me."; | ||
} | ||
|
||
return String.format("Two for %s, two for me.", name); | ||
} | ||
|
||
public static void main(String[] args) { | ||
TwoFer twoFer = new TwoFer(); | ||
System.out.println(twoFer.twofer(null)); | ||
System.out.println(twoFer.twofer("John")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This test is designed to analyze a solution for which no exercise analyzer is implemented. | ||
It should still receive feedback from the global analyzer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
{} | ||
{"comments": [ | ||
{ | ||
"comment": "java.general.do_not_use_main_method", | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.avoid_print_statements", | ||
"type": "informative" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"type": "informative" | ||
} | ||
]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters