-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
- Loading branch information
1 parent
136383d
commit 2e329f4
Showing
22 changed files
with
578 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; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
package analyzer.exercises.wizardsandwarriors; | ||
|
||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; | ||
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; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 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"; | ||
|
||
@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)); | ||
} | ||
} | ||
|
||
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(ClassOrInterfaceDeclaration node, OutputCollector output) { | ||
if (doesNotHasOverrideAnnotationForEveryOverridedMethod(node)) { | ||
output.addComment(new UseOverrideAnnotation()); | ||
} | ||
|
||
super.visit(node, output); | ||
} | ||
|
||
private static boolean doesNotHasOverrideAnnotationForEveryOverridedMethod(ClassOrInterfaceDeclaration node) { | ||
List<AnnotationExpr> annotations = node.findAll(AnnotationExpr.class); | ||
|
||
return annotations.size() < 3; | ||
} | ||
} | ||
} |
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" | ||
} | ||
] | ||
} |
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/wizards-and-warriors/exemplar-solution/.meta/config.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,27 @@ | ||
{ | ||
"authors": [ | ||
"himanshugoyal1065" | ||
], | ||
"contributors": [ | ||
"manumafe98", | ||
"sanderploegsma" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Fighter.java" | ||
], | ||
"test": [ | ||
"src/test/java/FighterTest.java" | ||
], | ||
"exemplar": [ | ||
".meta/src/reference/java/Fighter.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"forked_from": [ | ||
"csharp/wizards-and-warriors" | ||
], | ||
"blurb": "Learn about inheritance by creating an RPG." | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/wizards-and-warriors/exemplar-solution/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,11 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.exemplar", | ||
"params": { | ||
"exerciseName": "Wizards and Warriors" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/wizards-and-warriors/exemplar-solution/expected_tags.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,3 @@ | ||
{ | ||
"tags": [] | ||
} |
Oops, something went wrong.