Skip to content

Implement Analyzer for wizards-and-warriors-2 #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- name: Set up JDK
uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12
uses: actions/setup-java@7a6d8a8234af8eb26422e24e3006232cccaa061b
with:
java-version: "21"
distribution: "temurin"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- name: Set up JDK
uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12
uses: actions/setup-java@7a6d8a8234af8eb26422e24e3006232cccaa061b
with:
java-version: "21"
distribution: "temurin"
- name: Test with Gradle
run: ./gradlew --no-daemon --continue test
- name: Upload test coverage
uses: coverallsapp/github-action@648a8eb78e6d50909eff900e4ec85cab4524a45b
uses: coverallsapp/github-action@cfd0633edbd2411b532b808ba7a8b5e04f76d2c8

smoke_tests:
name: Smoke tests
Expand All @@ -32,12 +32,12 @@ jobs:
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5
with:
install: true

- name: Build Docker image and store in cache
uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355
with:
context: .
push: false
Expand Down
2 changes: 1 addition & 1 deletion bin/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env sh

java -jar /opt/analyzer/java-analyzer.jar "$@"
java -jar /opt/analyzer/java-analyzer.jar "$@"
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ repositories {
}

dependencies {
implementation "com.google.code.gson:gson:2.12.1"
implementation "com.google.code.gson:gson:2.11.0"
implementation "com.github.javaparser:javaparser-core:3.26.3"

testImplementation platform("org.junit:junit-bom:5.11.4")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.27.0"
testImplementation "com.approvaltests:approvaltests:24.15.0"
testImplementation "com.approvaltests:approvaltests:24.7.0"
}

shadowJar {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
7 changes: 5 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/analyzer/AnalyzerRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -49,7 +49,6 @@ private static List<Analyzer> createAnalyzers(String slug) {
var analyzers = new ArrayList<Analyzer>();

analyzers.add(new GlobalAnalyzer());

switch (slug) {
case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer());
case "hamming" -> analyzers.add(new HammingAnalyzer());
Expand All @@ -61,6 +60,7 @@ private static List<Analyzer> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/exercism/java/tree/main/exercises/concept/wizards-and-warriors-2">The wizards-and-warriors exercise on the Java track</a>
*/
public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter<OutputCollector> 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<MethodCallExpr> 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));
}

}
16 changes: 16 additions & 0 deletions src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"comments": [
{
"comment": "java.general.exemplar",
"params": {
"exerciseName": "Wizards and Warriors 2"
},
"type": "celebratory"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "informative"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading