-
-
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.
Updating
Two Fer
Analyzer to use our new analyzer convention (#137)
* Updating Two Fer Analyzer to use our new analyzer convention * Deleting whitespace * Updating usage of prefer string concatenation as now it is a general comment
- Loading branch information
1 parent
c392d59
commit a810853
Showing
22 changed files
with
276 additions
and
75 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
src/main/java/analyzer/exercises/twofer/AvoidStringFormat.java
This file was deleted.
Oops, something went wrong.
77 changes: 65 additions & 12 deletions
77
src/main/java/analyzer/exercises/twofer/TwoferAnalyzer.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,86 @@ | ||
package analyzer.exercises.twofer; | ||
|
||
import analyzer.OutputCollector; | ||
|
||
import java.util.List; | ||
|
||
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.expr.StringLiteralExpr; | ||
import com.github.javaparser.ast.stmt.IfStmt; | ||
import com.github.javaparser.ast.stmt.ReturnStmt; | ||
import com.github.javaparser.ast.stmt.Statement; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.Solution; | ||
import analyzer.comments.AvoidHardCodedTestCases; | ||
import analyzer.comments.PreferStringConcatenation; | ||
|
||
/** | ||
* The {@link TwoferAnalyzer} is the analyzer implementation for the {@code two-fer} practice exercise. | ||
* | ||
* @see <a href="https://github.com/exercism/java/tree/main/exercises/practice/two-fer">The two-fer exercise on the Java track</a> | ||
*/ | ||
public class TwoferAnalyzer implements Analyzer { | ||
public class TwoferAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { | ||
|
||
@Override | ||
public void analyze(Solution solution, OutputCollector output) { | ||
TwoferWalker walker = new TwoferWalker(); | ||
|
||
solution.getCompilationUnits().forEach(cu -> cu.walk(walker)); | ||
for (CompilationUnit compilationUnit : solution.getCompilationUnits()) { | ||
compilationUnit.accept(this, output); | ||
} | ||
} | ||
|
||
if (walker.hasHardCodedTestCases) { | ||
@Override | ||
public void visit(MethodDeclaration node, OutputCollector output) { | ||
if (hasHardCodedTestCases(node)) { | ||
output.addComment(new AvoidHardCodedTestCases()); | ||
} else if (walker.usesFormat) { | ||
output.addComment(new AvoidStringFormat()); | ||
} else if (walker.returnCount > 1) { | ||
return; | ||
} | ||
|
||
if (hasIfStatement(node)) { | ||
output.addComment(new UseTernaryOperator()); | ||
} | ||
|
||
if (hasMoreThanOneReturnStatement(node)) { | ||
output.addComment(new UseOneReturn()); | ||
} else { | ||
if (walker.usesIfStatement) { | ||
output.addComment(new UseTernaryOperator()); | ||
} | ||
} | ||
|
||
if (callsFormatMethod(node)) { | ||
output.addComment(new PreferStringConcatenation()); | ||
} | ||
} | ||
|
||
private static boolean hasHardCodedTestCases(MethodDeclaration node) { | ||
List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class, | ||
x -> x.getValue().contains("Alice") || x.getValue().contains("Bob")); | ||
|
||
return hardcodedStrings.size() > 1; | ||
} | ||
|
||
private static boolean hasMoreThanOneReturnStatement(MethodDeclaration node) { | ||
long returnStmtCount = node.getBody() | ||
.map(body -> body.getStatements().stream().filter(TwoferAnalyzer::isReturnStatement).count()) | ||
.orElse(0L); | ||
|
||
return returnStmtCount > 1; | ||
} | ||
|
||
private static boolean callsFormatMethod(MethodDeclaration node) { | ||
return !node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains("format")).isEmpty(); | ||
} | ||
|
||
private static boolean hasIfStatement(MethodDeclaration node) { | ||
return node.getBody().map(body -> body.getStatements().stream().anyMatch(TwoferAnalyzer::isIfStatement)) | ||
.orElse(false); | ||
} | ||
|
||
private static boolean isIfStatement(Statement statement) { | ||
return !statement.findAll(IfStmt.class).isEmpty(); | ||
} | ||
|
||
private static boolean isReturnStatement(Statement statement) { | ||
return !statement.findAll(ReturnStmt.class).isEmpty(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
src/test/resources/analyzer/AnalyzerIntegrationTest.twofer.UsesMultipleReturns.approved.txt
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
4 changes: 2 additions & 2 deletions
4
src/test/resources/analyzer/AnalyzerIntegrationTest.twofer.UsesStringFormat.approved.txt
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
File renamed without changes.
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,14 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.avoid_hard_coded_test_cases", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
File renamed without changes.
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,12 @@ | ||
class Twofer { | ||
public String twofer(String name) { | ||
if (name == null) { | ||
// fall through | ||
} else if (name.equals("Alice")) { | ||
return "One for Alice, one for me."; | ||
} else if (name.equals("Bob")) { | ||
return "One for Bob, one for me."; | ||
} | ||
return "One for you, one for me."; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
{ | ||
"authors": [ | ||
"Smarticles101" | ||
], | ||
"contributors": [ | ||
"FridaTveit", | ||
"ikhadykin", | ||
"jmrunkle", | ||
"jssander", | ||
"kytrinyx", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"rdavid1099", | ||
"sjwarner-bp", | ||
"SleeplessByte", | ||
"sshine", | ||
"stkent", | ||
"uzilan", | ||
"Valkryst", | ||
"ymoskovits" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Twofer.java" | ||
], | ||
"test": [ | ||
"src/test/java/TwoferTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Twofer.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Create a sentence of the form \"One for X, one for me.\".", | ||
"source_url": "https://github.com/exercism/problem-specifications/issues/757" | ||
} |
2 changes: 1 addition & 1 deletion
2
...n-optimal-solution/expected_analysis.json → ...using-if-statement/expected_analysis.json
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,3 @@ | ||
{ | ||
"tags": [] | ||
} |
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,7 @@ | ||
class Twofer { | ||
public String twofer(String rawName) { | ||
String name = rawName; | ||
if (name == null) name = "you"; | ||
return "One for " + name + ", one for me."; | ||
} | ||
} |
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,39 @@ | ||
{ | ||
"authors": [ | ||
"Smarticles101" | ||
], | ||
"contributors": [ | ||
"FridaTveit", | ||
"ikhadykin", | ||
"jmrunkle", | ||
"jssander", | ||
"kytrinyx", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"rdavid1099", | ||
"sjwarner-bp", | ||
"SleeplessByte", | ||
"sshine", | ||
"stkent", | ||
"uzilan", | ||
"Valkryst", | ||
"ymoskovits" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Twofer.java" | ||
], | ||
"test": [ | ||
"src/test/java/TwoferTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Twofer.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Create a sentence of the form \"One for X, one for me.\".", | ||
"source_url": "https://github.com/exercism/problem-specifications/issues/757" | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/two-fer/using-multiple-returns/expected_analysis.json
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 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.two-fer.use_ternary_operator", | ||
"params": {}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.two-fer.use_one_return", | ||
"params": {}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"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,3 @@ | ||
{ | ||
"tags": [] | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/two-fer/using-multiple-returns/src/main/java/TwoFer.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Twofer { | ||
public String twofer(String name) { | ||
if (name == null) return "One for you, one for me."; | ||
return "One for " + name + ", one for me."; | ||
} | ||
} |
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,39 @@ | ||
{ | ||
"authors": [ | ||
"Smarticles101" | ||
], | ||
"contributors": [ | ||
"FridaTveit", | ||
"ikhadykin", | ||
"jmrunkle", | ||
"jssander", | ||
"kytrinyx", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"rdavid1099", | ||
"sjwarner-bp", | ||
"SleeplessByte", | ||
"sshine", | ||
"stkent", | ||
"uzilan", | ||
"Valkryst", | ||
"ymoskovits" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Twofer.java" | ||
], | ||
"test": [ | ||
"src/test/java/TwoferTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Twofer.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Create a sentence of the form \"One for X, one for me.\".", | ||
"source_url": "https://github.com/exercism/problem-specifications/issues/757" | ||
} |
Oops, something went wrong.