-
-
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.
Implement analyzer for wizards-and-warriors (#145)
* Implement analyzer for wizards-and-warriors * Fixing smoke tests * Making WizardsAndWarriorsClassAnalyzer static * Apllying suggestion to make analyzer that checks for override annotation more sophisticated Adding extra analyzer and scenario to test that
- Loading branch information
1 parent
addcf0c
commit c2628d8
Showing
24 changed files
with
652 additions
and
0 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
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/wizardsandwarriors/UseOverrideAnnotation.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,19 @@ | ||
package analyzer.exercises.wizardsandwarriors; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/wizards-and-warriors/use_override_annotation.md">Markdown Template</a> | ||
*/ | ||
class UseOverrideAnnotation extends Comment { | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.wizards-and-warriors.use_override_annotation"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.INFORMATIVE; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/analyzer/exercises/wizardsandwarriors/WizardsAndWarriorsAnalyzer.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,68 @@ | ||
package analyzer.exercises.wizardsandwarriors; | ||
|
||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.comments.LineComment; | ||
import com.github.javaparser.ast.expr.AnnotationExpr; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.OutputCollector; | ||
import analyzer.Solution; | ||
import analyzer.comments.ExemplarSolution; | ||
import analyzer.comments.RemoveTodoComments; | ||
|
||
/** | ||
* The {@link WizardsAndWarriorsAnalyzer} is the analyzer implementation for the {@code wizards-and-warriors} practice exercise. | ||
* It has a subclass WizardsAndWarriorsClassAnalyzer that extends the {@link VoidVisitorAdapter} and uses the visitor pattern to traverse each compilation unit. | ||
* | ||
* @see <a href="https://github.com/exercism/java/tree/main/exercises/concept/wizards-and-warriors">The wizards-and-warriors exercise on the Java track</a> | ||
*/ | ||
public class WizardsAndWarriorsAnalyzer implements Analyzer { | ||
private static final String EXERCISE_NAME = "Wizards and Warriors"; | ||
private static final String TO_STRING = "toString"; | ||
private static final String IS_VULNERABLE = "isVulnerable"; | ||
private static final String GET_DAMAGE_POINTS = "getDamagePoints"; | ||
|
||
@Override | ||
public void analyze(Solution solution, OutputCollector output) { | ||
var wizardsAndWarriorsClassAnalyzer = new WizardsAndWarriorsClassAnalyzer(); | ||
|
||
for (var compilationUnit : solution.getCompilationUnits()) { | ||
compilationUnit.getClassByName("Wizard").ifPresent(c -> c.accept(wizardsAndWarriorsClassAnalyzer, output)); | ||
compilationUnit.getClassByName("Warrior").ifPresent(c -> c.accept(wizardsAndWarriorsClassAnalyzer, output)); | ||
} | ||
|
||
if (output.getComments().isEmpty()) { | ||
output.addComment(new ExemplarSolution(EXERCISE_NAME)); | ||
} | ||
} | ||
|
||
static class WizardsAndWarriorsClassAnalyzer extends VoidVisitorAdapter<OutputCollector> { | ||
|
||
@Override | ||
public void visit(LineComment node, OutputCollector output) { | ||
if (node.getContent().contains("TODO")) { | ||
output.addComment(new RemoveTodoComments()); | ||
} | ||
|
||
super.visit(node, output); | ||
} | ||
|
||
@Override | ||
public void visit(MethodDeclaration node, OutputCollector output) { | ||
if (itsOverridedMethod(node) && doesNotHaveOverrideAnnotation(node)) { | ||
output.addComment(new UseOverrideAnnotation()); | ||
} | ||
|
||
super.visit(node, output); | ||
} | ||
|
||
private static boolean doesNotHaveOverrideAnnotation(MethodDeclaration node) { | ||
return node.findAll(AnnotationExpr.class).isEmpty(); | ||
} | ||
|
||
private static boolean itsOverridedMethod(MethodDeclaration node) { | ||
return node.getNameAsString().equals(TO_STRING) || node.getNameAsString().equals(IS_VULNERABLE) || node.getNameAsString().equals(GET_DAMAGE_POINTS); | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...sources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors.ExemplarSolution.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.exemplar", | ||
"params": { | ||
"exerciseName": "Wizards and Warriors" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
.../AnalyzerIntegrationTest.wizardsandwarriors.ExemplarSolutionWithTodoComments.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.remove_todo_comments", | ||
"params": {}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
...lyzer/AnalyzerIntegrationTest.wizardsandwarriors.NotUsingOverrideAnnotations.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.wizards-and-warriors.use_override_annotation", | ||
"params": {}, | ||
"type": "informative" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
...alyzerIntegrationTest.wizardsandwarriors.UsingAditionalEqualsMethodOverrided.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.exemplar", | ||
"params": { | ||
"exerciseName": "Wizards and Warriors" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/resources/scenarios/wizards-and-warriors/ExemplarSolution.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,54 @@ | ||
package scenarios.wizardsandwarriors; | ||
|
||
class Fighter { | ||
|
||
boolean isVulnerable() { | ||
return true; | ||
} | ||
|
||
int getDamagePoints(Fighter fighter) { | ||
return 1; | ||
} | ||
} | ||
|
||
class Warrior extends Fighter { | ||
|
||
@Override | ||
public String toString() { | ||
return "Fighter is a Warrior"; | ||
} | ||
|
||
@Override | ||
public boolean isVulnerable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
int getDamagePoints(Fighter target) { | ||
return target.isVulnerable() ? 10 : 6; | ||
} | ||
} | ||
|
||
class Wizard extends Fighter { | ||
|
||
boolean isSpellPrepared = false; | ||
|
||
@Override | ||
public String toString() { | ||
return "Fighter is a Wizard"; | ||
} | ||
|
||
@Override | ||
boolean isVulnerable() { | ||
return !isSpellPrepared; | ||
} | ||
|
||
@Override | ||
int getDamagePoints(Fighter target) { | ||
return isSpellPrepared ? 12 : 3; | ||
} | ||
|
||
void prepareSpell() { | ||
isSpellPrepared = true; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/resources/scenarios/wizards-and-warriors/ExemplarSolutionWithTodoComments.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,56 @@ | ||
package scenarios.wizardsandwarriors; | ||
|
||
class Fighter { | ||
|
||
boolean isVulnerable() { | ||
return true; | ||
} | ||
|
||
int getDamagePoints(Fighter fighter) { | ||
return 1; | ||
} | ||
} | ||
|
||
// TODO: define the Warrior class | ||
class Warrior extends Fighter { | ||
|
||
@Override | ||
public String toString() { | ||
return "Fighter is a Warrior"; | ||
} | ||
|
||
@Override | ||
public boolean isVulnerable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
int getDamagePoints(Fighter target) { | ||
return target.isVulnerable() ? 10 : 6; | ||
} | ||
} | ||
|
||
// TODO: define the Wizard class | ||
class Wizard extends Fighter { | ||
|
||
boolean isSpellPrepared = false; | ||
|
||
@Override | ||
public String toString() { | ||
return "Fighter is a Wizard"; | ||
} | ||
|
||
@Override | ||
boolean isVulnerable() { | ||
return !isSpellPrepared; | ||
} | ||
|
||
@Override | ||
int getDamagePoints(Fighter target) { | ||
return isSpellPrepared ? 12 : 3; | ||
} | ||
|
||
void prepareSpell() { | ||
isSpellPrepared = true; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/resources/scenarios/wizards-and-warriors/NotUsingOverrideAnnotations.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,48 @@ | ||
package scenarios.wizardsandwarriors; | ||
|
||
class Fighter { | ||
|
||
boolean isVulnerable() { | ||
return true; | ||
} | ||
|
||
int getDamagePoints(Fighter fighter) { | ||
return 1; | ||
} | ||
} | ||
|
||
class Warrior extends Fighter { | ||
|
||
public String toString() { | ||
return "Fighter is a Warrior"; | ||
} | ||
|
||
public boolean isVulnerable() { | ||
return false; | ||
} | ||
|
||
int getDamagePoints(Fighter target) { | ||
return target.isVulnerable() ? 10 : 6; | ||
} | ||
} | ||
|
||
class Wizard extends Fighter { | ||
|
||
boolean isSpellPrepared = false; | ||
|
||
public String toString() { | ||
return "Fighter is a Wizard"; | ||
} | ||
|
||
boolean isVulnerable() { | ||
return !isSpellPrepared; | ||
} | ||
|
||
int getDamagePoints(Fighter target) { | ||
return isSpellPrepared ? 12 : 3; | ||
} | ||
|
||
void prepareSpell() { | ||
isSpellPrepared = true; | ||
} | ||
} |
Oops, something went wrong.