From b200561e73bdfcab70d39f1fe19ef69edde6d8ad Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Thu, 10 Apr 2025 10:30:10 +0200 Subject: [PATCH 1/6] solution to wizards-and-warriors-2-analyzer --- src/main/java/analyzer/AnalyzerRoot.java | 4 +- .../UseMethodOverloading.java | 20 +++++ .../WizardsAndWarriors2Analyzer.java | 85 +++++++++++++++++++ .../analyzer/AnalyzerIntegrationTest.java | 16 ++++ ...andwarriors2.ExemplarSolution.approved.txt | 11 +++ ...iors2.NotUseMethodOverloading.approved.txt | 14 +++ ...PartialUseOfMethodOverloading.approved.txt | 14 +++ ...sandwarriors2.UseStringFormat.approved.txt | 14 +++ ...matAndNotUseMethodOverloading.approved.txt | 19 +++++ .../ExemplarSolution.java | 25 ++++++ .../NotUseMethodOverloading.java | 29 +++++++ .../PartialUseOfMethodOverloading.java | 25 ++++++ .../UseStringFormat.java | 28 ++++++ ...tringFormatAndNotUseMethodOverloading.java | 35 ++++++++ .../exemplar-solution/.meta/config.json | 32 +++++++ .../exemplar-solution/expected_analysis.json | 11 +++ .../exemplar-solution/expected_tags.json | 3 + .../src/main/java/GameMaster.java | 25 ++++++ .../.meta/config.json | 32 +++++++ .../expected_analysis.json | 14 +++ .../expected_tags.json | 3 + .../src/main/java/GameMaster.java | 29 +++++++ .../use-string-format/.meta/config.json | 32 +++++++ .../use-string-format/expected_analysis.json | 14 +++ .../use-string-format/expected_tags.json | 3 + .../src/main/java/GameMaster.java | 28 ++++++ 26 files changed, 563 insertions(+), 2 deletions(-) create mode 100644 src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java create mode 100644 src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java create mode 100644 tests/wizards-and-warriors-2/use-string-format/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java diff --git a/src/main/java/analyzer/AnalyzerRoot.java b/src/main/java/analyzer/AnalyzerRoot.java index 6e249ec3..b7cb7a2f 100644 --- a/src/main/java/analyzer/AnalyzerRoot.java +++ b/src/main/java/analyzer/AnalyzerRoot.java @@ -12,6 +12,7 @@ import analyzer.exercises.secrets.SecretsAnalyzer; import analyzer.exercises.twofer.TwoferAnalyzer; import analyzer.exercises.wizardsandwarriors.WizardsAndWarriorsAnalyzer; +import analyzer.exercises.wizardsandwarriors2.WizardsAndWarriors2Analyzer; import java.util.ArrayList; import java.util.List; @@ -33,7 +34,6 @@ private AnalyzerRoot() { */ public static Output analyze(Solution solution) { var outputBuilder = new OutputBuilder(); - for (Analyzer analyzer : createAnalyzers(solution.getSlug())) { analyzer.analyze(solution, outputBuilder); } @@ -49,7 +49,6 @@ private static List createAnalyzers(String slug) { var analyzers = new ArrayList(); analyzers.add(new GlobalAnalyzer()); - switch (slug) { case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer()); case "hamming" -> analyzers.add(new HammingAnalyzer()); @@ -61,6 +60,7 @@ private static List createAnalyzers(String slug) { case "secrets" -> analyzers.add(new SecretsAnalyzer()); case "two-fer" -> analyzers.add(new TwoferAnalyzer()); case "wizards-and-warriors" -> analyzers.add(new WizardsAndWarriorsAnalyzer()); + case "wizards-and-warriors-2" -> analyzers.add(new WizardsAndWarriors2Analyzer()); } return List.copyOf(analyzers); diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java new file mode 100644 index 00000000..7d9aefa1 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java @@ -0,0 +1,20 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Comment; + +/** + * @author: chiarazarrella + */ +public class UseMethodOverloading extends Comment{ + + @Override + public String getKey() { + return "java.wizards-and-warriors-2.use_method_overloading"; + } + + @Override + public Type getType() { + return Type.ACTIONABLE; + } +} + diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java new file mode 100644 index 00000000..8eaed9e2 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -0,0 +1,85 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Analyzer; +import analyzer.OutputCollector; +import analyzer.Solution; +import analyzer.comments.ExemplarSolution; +import analyzer.comments.PreferStringConcatenation; +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; + +/** + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} practice exercise. + * + * @see The wizards-and-warriors exercise on the Java track + */ +public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter implements Analyzer { + + private static final String EXERCISE_NAME = "Wizards and Warriors 2"; + private static final String GAME_MASTER = "GameMaster"; + private static final String DESCRIBE = "describe"; + private static final String FORMAT = "format"; + + @Override + public void analyze(Solution solution, OutputCollector output) { + + for (var compilationUnit : solution.getCompilationUnits()) { + compilationUnit.getClassByName(GAME_MASTER).ifPresent(c -> c.accept(this, output)); + } + + if (output.getComments().isEmpty()) { + output.addComment(new ExemplarSolution(EXERCISE_NAME)); + } + } + + @Override + public void visit(MethodDeclaration node, OutputCollector output) { + + if(!node.getNameAsString().equals(DESCRIBE)) { + return; + } + + if(node.getParameters().size() > 1 && !useOverload(node)) { + + output.addComment(new UseMethodOverloading()); + + } + + if(useStringFormat(node)) { + + output.addComment(new PreferStringConcatenation()); + + } + + + super.visit(node, output); + } + + private static boolean useOverload(MethodDeclaration node) { + + int paramCount = node.getParameters().size(); + + List describeCalls = node.findAll(MethodCallExpr.class).stream() + .filter(m -> m.getNameAsString().equals(DESCRIBE)) + .toList(); + + if (paramCount == 2) { + return describeCalls.size() == 1 || describeCalls.size() == 3; + } + + if (paramCount == 3) { + return describeCalls.size() == 3; + } + + return false; + } + + private static boolean useStringFormat(MethodDeclaration node) { + return node.findAll(MethodCallExpr.class).stream() + .anyMatch(m -> m.getNameAsString().contains(FORMAT)); + } + +} diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index 26315c72..8b732f84 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -205,4 +205,20 @@ void salarycalculator(String scenario) throws IOException { Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); } + + @ParameterizedTest + @ValueSource(strings = { + "ExemplarSolution", + "NotUseMethodOverloading", + "PartialUseOfMethodOverloading", + "UseStringFormat", + "UseStringFormatAndNotUseMethodOverloading", + }) + void wizardsandwarriors2(String scenario) throws IOException { + var path = Path.of("wizards-and-warriors-2", scenario + ".java"); + var solution = new SolutionFromFiles("wizards-and-warriors-2", SCENARIOS.resolve(path)); + var output = AnalyzerRoot.analyze(solution); + + Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); + } } diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt new file mode 100644 index 00000000..a9ee72ff --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt new file mode 100644 index 00000000..96757c7d --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt new file mode 100644 index 00000000..96757c7d --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt new file mode 100644 index 00000000..b01ad7f7 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt new file mode 100644 index 00000000..25d682cc --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java new file mode 100644 index 00000000..e5b69038 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } + } \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java new file mode 100644 index 00000000..6e9efe84 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character) + " " + describe(destination) + " You're traveling to your destination by walking."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java new file mode 100644 index 00000000..f3fc84e3 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java @@ -0,0 +1,35 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level %d %s with %d hit points. ".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()) + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " + : "You're traveling to your destination on horseback. ") + + "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(Character character, Destination destination) { + return "You're a level %d %s with %d hit points. You're traveling to your destination by walking. You've arrived at %s, which has %d inhabitants." + .formatted(character.getLevel(), character.getCharacterClass(), character.getHitPoints(), + destination.getName(), destination.getInhabitants()); + } +} diff --git a/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json new file mode 100644 index 00000000..a9b48769 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java new file mode 100644 index 00000000..8a513f51 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json b/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json new file mode 100644 index 00000000..a2e52b42 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/.meta/config.json b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json new file mode 100644 index 00000000..2b150682 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_tags.json b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} From 42c95419e1104e2da8b89384a043b1456fd31af0 Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Fri, 11 Apr 2025 10:22:18 +0200 Subject: [PATCH 2/6] minor change --- .../wizardsandwarriors2/WizardsAndWarriors2Analyzer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java index 8eaed9e2..4a745e88 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -12,7 +12,7 @@ import java.util.List; /** - * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} practice exercise. + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} concept exercise. * * @see The wizards-and-warriors exercise on the Java track */ From 2b003320426e8a1c62e7c4e7b4ba6376ecdbee9b Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Sun, 27 Apr 2025 19:37:04 +0200 Subject: [PATCH 3/6] modified with hardcoded comments of "Reuse_code" --- ...=> ReuseCodeHardcodedThreeParameters.java} | 8 ++---- .../ReuseCodeHardcodedTwoParameters.java | 16 ++++++++++++ .../WizardsAndWarriors2Analyzer.java | 14 ++++++---- .../analyzer/AnalyzerIntegrationTest.java | 6 ++--- ...dsandwarriors2.NotReuseMethod.approved.txt | 19 ++++++++++++++ ...iors2.NotUseMethodOverloading.approved.txt | 14 ---------- ...arriors2.PartialReuseOfMethod.approved.txt | 26 +++++++++---------- ...PartialUseOfMethodOverloading.approved.txt | 14 ---------- ...tringFormatAndNotReuseMethod.approved.txt} | 7 ++++- ...odOverloading.java => NotReuseMethod.java} | 0 ...loading.java => PartialReuseOfMethod.java} | 0 ... => UseStringFormatAndNotReuseMethod.java} | 0 .../.meta/config.json | 0 .../not-reuse-method/expected_analysis.json | 19 ++++++++++++++ .../expected_tags.json | 0 .../src/main/java/GameMaster.java | 0 16 files changed, 87 insertions(+), 56 deletions(-) rename src/main/java/analyzer/exercises/wizardsandwarriors2/{UseMethodOverloading.java => ReuseCodeHardcodedThreeParameters.java} (57%) create mode 100644 src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt delete mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt rename tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json => src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt (66%) delete mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt rename src/test/resources/analyzer/{AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt => AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt} (55%) rename src/test/resources/scenarios/wizards-and-warriors-2/{NotUseMethodOverloading.java => NotReuseMethod.java} (100%) rename src/test/resources/scenarios/wizards-and-warriors-2/{PartialUseOfMethodOverloading.java => PartialReuseOfMethod.java} (100%) rename src/test/resources/scenarios/wizards-and-warriors-2/{UseStringFormatAndNotUseMethodOverloading.java => UseStringFormatAndNotReuseMethod.java} (100%) rename tests/wizards-and-warriors-2/{not-use-method-overloading => not-reuse-method}/.meta/config.json (100%) create mode 100644 tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json rename tests/wizards-and-warriors-2/{not-use-method-overloading => not-reuse-method}/expected_tags.json (100%) rename tests/wizards-and-warriors-2/{not-use-method-overloading => not-reuse-method}/src/main/java/GameMaster.java (100%) diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java similarity index 57% rename from src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java rename to src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java index 7d9aefa1..73e327bd 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java @@ -2,14 +2,11 @@ import analyzer.Comment; -/** - * @author: chiarazarrella - */ -public class UseMethodOverloading extends Comment{ +public class ReuseCodeHardcodedThreeParameters extends Comment { @Override public String getKey() { - return "java.wizards-and-warriors-2.use_method_overloading"; + return "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters"; } @Override @@ -17,4 +14,3 @@ public Type getType() { return Type.ACTIONABLE; } } - diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java new file mode 100644 index 00000000..878946a9 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java @@ -0,0 +1,16 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Comment; + +public class ReuseCodeHardcodedTwoParameters extends Comment { + + @Override + public String getKey() { + return "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters"; + } + + @Override + public Type getType() { + return Type.ACTIONABLE; + } +} diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java index 4a745e88..7d042ce9 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -42,9 +42,15 @@ public void visit(MethodDeclaration node, OutputCollector output) { return; } - if(node.getParameters().size() > 1 && !useOverload(node)) { + if(node.getParameters().size() == 2 && !reuseMethod(node)) { - output.addComment(new UseMethodOverloading()); + output.addComment(new ReuseCodeHardcodedTwoParameters()); + + } + + if(node.getParameters().size() == 3 && !reuseMethod(node)) { + + output.addComment(new ReuseCodeHardcodedThreeParameters()); } @@ -54,11 +60,10 @@ public void visit(MethodDeclaration node, OutputCollector output) { } - super.visit(node, output); } - private static boolean useOverload(MethodDeclaration node) { + private static boolean reuseMethod(MethodDeclaration node) { int paramCount = node.getParameters().size(); @@ -69,7 +74,6 @@ private static boolean useOverload(MethodDeclaration node) { if (paramCount == 2) { return describeCalls.size() == 1 || describeCalls.size() == 3; } - if (paramCount == 3) { return describeCalls.size() == 3; } diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index 8b732f84..5f193f3c 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -209,10 +209,10 @@ void salarycalculator(String scenario) throws IOException { @ParameterizedTest @ValueSource(strings = { "ExemplarSolution", - "NotUseMethodOverloading", - "PartialUseOfMethodOverloading", + "NotReuseMethod", + "PartialReuseOfMethod", "UseStringFormat", - "UseStringFormatAndNotUseMethodOverloading", + "UseStringFormatAndNotReuseMethod", }) void wizardsandwarriors2(String scenario) throws IOException { var path = Path.of("wizards-and-warriors-2", scenario + ".java"); diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt new file mode 100644 index 00000000..cc5cfba4 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt deleted file mode 100644 index 96757c7d..00000000 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt +++ /dev/null @@ -1,14 +0,0 @@ -{ - "comments": [ - { - "comment": "java.wizards-and-warriors-2.use_method_overloading", - "params": {}, - "type": "actionable" - }, - { - "comment": "java.general.feedback_request", - "params": {}, - "type": "informative" - } - ] -} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt similarity index 66% rename from tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json rename to src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt index a2e52b42..234e21fe 100644 --- a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt @@ -1,14 +1,14 @@ -{ - "comments": [ - { - "comment": "java.wizards-and-warriors-2.use_method_overloading", - "params": {}, - "type": "actionable" - }, - { - "comment": "java.general.feedback_request", - "params": {}, - "type": "informative" - } - ] +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] } \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt deleted file mode 100644 index 96757c7d..00000000 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt +++ /dev/null @@ -1,14 +0,0 @@ -{ - "comments": [ - { - "comment": "java.wizards-and-warriors-2.use_method_overloading", - "params": {}, - "type": "actionable" - }, - { - "comment": "java.general.feedback_request", - "params": {}, - "type": "informative" - } - ] -} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt similarity index 55% rename from src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt rename to src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt index 25d682cc..fac41fcc 100644 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt @@ -1,7 +1,12 @@ { "comments": [ { - "comment": "java.wizards-and-warriors-2.use_method_overloading", + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", "params": {}, "type": "actionable" }, diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/NotReuseMethod.java similarity index 100% rename from src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java rename to src/test/resources/scenarios/wizards-and-warriors-2/NotReuseMethod.java diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/PartialReuseOfMethod.java similarity index 100% rename from src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java rename to src/test/resources/scenarios/wizards-and-warriors-2/PartialReuseOfMethod.java diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotReuseMethod.java similarity index 100% rename from src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java rename to src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotReuseMethod.java diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json b/tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json similarity index 100% rename from tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json rename to tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json diff --git a/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json new file mode 100644 index 00000000..94c8a656 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json b/tests/wizards-and-warriors-2/not-reuse-method/expected_tags.json similarity index 100% rename from tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json rename to tests/wizards-and-warriors-2/not-reuse-method/expected_tags.json diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/not-reuse-method/src/main/java/GameMaster.java similarity index 100% rename from tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java rename to tests/wizards-and-warriors-2/not-reuse-method/src/main/java/GameMaster.java From be1e45ac8a8efc1022ed8353c6769a0ec28afbb4 Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Mon, 5 May 2025 22:51:29 +0200 Subject: [PATCH 4/6] added check on parameter types --- .../WizardsAndWarriors2Analyzer.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java index 7d042ce9..af860661 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -6,6 +6,7 @@ import analyzer.comments.ExemplarSolution; import analyzer.comments.PreferStringConcatenation; import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; @@ -22,6 +23,9 @@ public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter params = node.getParameters().stream().map(Parameter::getTypeAsString).toList(); List describeCalls = node.findAll(MethodCallExpr.class).stream() .filter(m -> m.getNameAsString().equals(DESCRIBE)) .toList(); - if (paramCount == 2) { + if (paramCount == 2 && params.contains(DESTINATION) && params.contains(CHARACTER)) { return describeCalls.size() == 1 || describeCalls.size() == 3; } - if (paramCount == 3) { + + if (paramCount == 3 && params.contains(DESTINATION) && params.contains(TRAVEL_METHOD) && params.contains(CHARACTER)) { return describeCalls.size() == 3; } From 2ad565ca5a8d1e959937b6dc255760b0fb20fb1c Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Fri, 16 May 2025 20:36:53 +0200 Subject: [PATCH 5/6] meaningful name of variables --- ...ava => ReuseCodeFromDescribeThreeParams.java} | 4 ++-- ....java => ReuseCodeFromDescribeTwoParams.java} | 4 ++-- .../WizardsAndWarriors2Analyzer.java | 16 ++++++++-------- 3 files changed, 12 insertions(+), 12 deletions(-) rename src/main/java/analyzer/exercises/wizardsandwarriors2/{ReuseCodeHardcodedThreeParameters.java => ReuseCodeFromDescribeThreeParams.java} (58%) rename src/main/java/analyzer/exercises/wizardsandwarriors2/{ReuseCodeHardcodedTwoParameters.java => ReuseCodeFromDescribeTwoParams.java} (58%) diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeThreeParams.java similarity index 58% rename from src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java rename to src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeThreeParams.java index 73e327bd..47440c7f 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeThreeParams.java @@ -2,11 +2,11 @@ import analyzer.Comment; -public class ReuseCodeHardcodedThreeParameters extends Comment { +public class ReuseCodeFromDescribeThreeParams extends Comment { @Override public String getKey() { - return "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters"; + return "java.wizards-and-warriors-2.reuse_code_from_describe_three_params"; } @Override diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeTwoParams.java similarity index 58% rename from src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java rename to src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeTwoParams.java index 878946a9..0ec1976e 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeFromDescribeTwoParams.java @@ -2,11 +2,11 @@ import analyzer.Comment; -public class ReuseCodeHardcodedTwoParameters extends Comment { +public class ReuseCodeFromDescribeTwoParams extends Comment { @Override public String getKey() { - return "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters"; + return "java.wizards-and-warriors-2.reuse_code_from_describe_two_params"; } @Override diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java index af860661..fc75fd76 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -23,9 +23,9 @@ public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter params = node.getParameters().stream().map(Parameter::getTypeAsString).toList(); + List paramsType = node.getParameters().stream().map(Parameter::getTypeAsString).toList(); List describeCalls = node.findAll(MethodCallExpr.class).stream() .filter(m -> m.getNameAsString().equals(DESCRIBE)) .toList(); - if (paramCount == 2 && params.contains(DESTINATION) && params.contains(CHARACTER)) { + if (paramCount == 2 && paramsType.contains(DESTINATION_TYPE) && paramsType.contains(CHARACTER_TYPE)) { return describeCalls.size() == 1 || describeCalls.size() == 3; } - if (paramCount == 3 && params.contains(DESTINATION) && params.contains(TRAVEL_METHOD) && params.contains(CHARACTER)) { + if (paramCount == 3 && paramsType.contains(DESTINATION_TYPE) && paramsType.contains(TRAVEL_METHOD_TYPE) && paramsType.contains(CHARACTER_TYPE)) { return describeCalls.size() == 3; } From efe28286471d38ebf351fefe3d69c4c7e0ad1502 Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Sat, 17 May 2025 17:31:36 +0200 Subject: [PATCH 6/6] fix tests with new comments name --- ...rationTest.wizardsandwarriors2.NotReuseMethod.approved.txt | 4 ++-- ...Test.wizardsandwarriors2.PartialReuseOfMethod.approved.txt | 2 +- ...andwarriors2.UseStringFormatAndNotReuseMethod.approved.txt | 4 ++-- .../not-reuse-method/expected_analysis.json | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt index cc5cfba4..c989ee5e 100644 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt @@ -1,12 +1,12 @@ { "comments": [ { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_three_params", "params": {}, "type": "actionable" }, { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_two_params", "params": {}, "type": "actionable" }, diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt index 234e21fe..a6f24256 100644 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt @@ -1,7 +1,7 @@ { "comments": [ { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_two_params", "params": {}, "type": "actionable" }, diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt index fac41fcc..b230d7b6 100644 --- a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt @@ -1,12 +1,12 @@ { "comments": [ { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_three_params", "params": {}, "type": "actionable" }, { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_two_params", "params": {}, "type": "actionable" }, diff --git a/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json index 94c8a656..239579fa 100644 --- a/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json +++ b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json @@ -1,12 +1,12 @@ { "comments": [ { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_three_params", "params": {}, "type": "actionable" }, { - "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "comment": "java.wizards-and-warriors-2.reuse_code_from_describe_two_params", "params": {}, "type": "actionable" },