-
-
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.
* Refactor JSON writing behavior * Preserve order of added comments and tags * Rename Exercise to Analyzer * Add javadocs referencing the Exercism documentation * Refactor Analyzer base class into interface * Rename JsonSerializerTest.java to OutputWriterTest.java * Remove unused general comments The `FailedParse` comment was removed because it doesn't make much sense to have the analyzer comment on a submission that does not even compile. The `FileNotFound` comment was removed because the analyzer now analyzes all files in the source root, so there is no need to search for a specific file anymore. * Add golden test verifying that the analyzer doesn't crash on unknown exercises * Add .gitattributes
- Loading branch information
1 parent
4387cac
commit c7c64e1
Showing
48 changed files
with
956 additions
and
944 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* text=auto | ||
|
||
*.bat text eol=crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package analyzer; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @see <a href="https://exercism.org/docs/building/tooling/analyzers/interface">The analyzer interface in the Exercism documentation</a> | ||
*/ | ||
public class Analysis { | ||
private String summary; | ||
private final Set<Comment> comments = new LinkedHashSet<>(); | ||
private final Set<String> tags = new LinkedHashSet<>(); | ||
|
||
public String getSummary() { | ||
return summary; | ||
} | ||
|
||
public void setSummary(String summary) { | ||
this.summary = summary; | ||
} | ||
|
||
public List<Comment> getComments() { | ||
return List.copyOf(comments); | ||
} | ||
|
||
public List<String> getTags() { | ||
return List.copyOf(tags); | ||
} | ||
|
||
public void addComment(Comment comment) { | ||
comments.add(comment); | ||
} | ||
|
||
public void addTag(String tag) { | ||
tags.add(tag); | ||
} | ||
} |
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,9 @@ | ||
package analyzer; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
|
||
import java.util.List; | ||
|
||
public interface Analyzer { | ||
void analyze(List<CompilationUnit> compilationUnits, Analysis analysis); | ||
} |
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,30 @@ | ||
package analyzer; | ||
|
||
import analyzer.exercises.hamming.HammingAnalyzer; | ||
import analyzer.exercises.twofer.TwoferAnalyzer; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AnalyzerRoot { | ||
|
||
public static Analysis analyze(String slug, List<CompilationUnit> compilationUnits) { | ||
var analysis = new Analysis(); | ||
for (Analyzer analyzer : createAnalyzers(slug)) { | ||
analyzer.analyze(compilationUnits, analysis); | ||
} | ||
return analysis; | ||
} | ||
|
||
private static List<Analyzer> createAnalyzers(String slug) { | ||
var analyzers = new ArrayList<Analyzer>(); | ||
|
||
switch (slug) { | ||
case "hamming" -> analyzers.add(new HammingAnalyzer()); | ||
case "two-fer" -> analyzers.add(new TwoferAnalyzer()); | ||
} | ||
|
||
return List.copyOf(analyzers); | ||
} | ||
} |
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,46 @@ | ||
package analyzer; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @see <a href="https://exercism.org/docs/building/tooling/analyzers/interface">The analyzer interface in the Exercism documentation</a> | ||
*/ | ||
public abstract class Comment { | ||
|
||
public abstract String getKey(); | ||
|
||
public Map<String, String> getParameters() { | ||
return Map.of(); | ||
} | ||
|
||
public CommentType getType() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return (obj instanceof Comment other) && equals(other); | ||
} | ||
|
||
public boolean equals(Comment other) { | ||
if (!getKey().equals(other.getKey()) || getType() != other.getType()) { | ||
return false; | ||
} | ||
|
||
var params = this.getParameters().entrySet(); | ||
var otherParams = other.getParameters().entrySet(); | ||
|
||
return params.containsAll(otherParams) && otherParams.containsAll(params); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getKey(), getType(), getParameters()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Comment{key=%s,params=%s,type=%s}", getKey(), getParameters(), getType()); | ||
} | ||
} |
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 @@ | ||
package analyzer; | ||
|
||
/** | ||
* @see <a href="https://exercism.org/docs/building/tooling/analyzers/interface">The analyzer interface in the Exercism documentation</a> | ||
*/ | ||
public enum CommentType { | ||
ESSENTIAL, | ||
ACTIONABLE, | ||
INFORMATIVE, | ||
CELEBRATORY | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package analyzer; | ||
|
||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
/** | ||
* @see <a href="https://exercism.org/docs/building/tooling/analyzers/interface">The analyzer interface in the Exercism documentation</a> | ||
*/ | ||
public class OutputWriter { | ||
private static final int JSON_INDENTATION = 2; | ||
|
||
private final Writer analysisWriter; | ||
private final Writer tagsWriter; | ||
|
||
public OutputWriter(Writer analysisWriter, Writer tagsWriter) { | ||
this.analysisWriter = analysisWriter; | ||
this.tagsWriter = tagsWriter; | ||
} | ||
|
||
public void write(Analysis analysis) throws IOException { | ||
writeAnalysis(analysis); | ||
writeTags(analysis); | ||
} | ||
|
||
private void writeAnalysis(Analysis analysis) throws IOException { | ||
var json = new JSONObject(); | ||
|
||
if (analysis.getSummary() != null) { | ||
json.put("summary", analysis.getSummary()); | ||
} | ||
|
||
for (Comment comment : analysis.getComments()) { | ||
json.append("comments", serialize(comment)); | ||
} | ||
|
||
this.analysisWriter.write(json.toString(JSON_INDENTATION)); | ||
} | ||
|
||
private void writeTags(Analysis analysis) throws IOException { | ||
var json = new JSONObject(); | ||
for (String tag : analysis.getTags()) { | ||
json.append("tags", tag); | ||
} | ||
|
||
this.tagsWriter.write(json.toString(JSON_INDENTATION)); | ||
} | ||
|
||
private static JSONObject serialize(Comment comment) { | ||
var json = new JSONObject(); | ||
json.put("comment", comment.getKey()); | ||
|
||
if (comment.getType() != null) { | ||
json.put("type", comment.getType().name().toLowerCase()); | ||
} | ||
|
||
if (comment.getParameters().isEmpty()) { | ||
return json; | ||
} | ||
|
||
var paramsJson = new JSONObject(); | ||
comment.getParameters().forEach(paramsJson::put); | ||
json.put("params", paramsJson); | ||
return json; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/analyzer/comments/AvoidHardCodedTestCases.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,13 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/avoid_hard_coded_test_cases.md">Markdown Template</a> | ||
*/ | ||
public class AvoidHardCodedTestCases extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.avoid_hard_coded_test_cases"; | ||
} | ||
} |
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,32 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/constructor_too_long.md">Markdown Template</a> | ||
*/ | ||
public class ConstructorTooLong extends Comment { | ||
private final Collection<String> constructorNames; | ||
|
||
public ConstructorTooLong(Collection<String> constructorNames) { | ||
this.constructorNames = constructorNames; | ||
} | ||
|
||
public ConstructorTooLong(String constructorName) { | ||
this(List.of(constructorName)); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.general.constructor_too_long"; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParameters() { | ||
return Map.of("constructorNames", String.join(", ", this.constructorNames)); | ||
} | ||
} |
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,32 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/method_too_long.md">Markdown Template</a> | ||
*/ | ||
public class MethodTooLong extends Comment { | ||
private final Collection<String> methodNames; | ||
|
||
public MethodTooLong(Collection<String> methodNames) { | ||
this.methodNames = methodNames; | ||
} | ||
|
||
public MethodTooLong(String methodName) { | ||
this(List.of(methodName)); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.general.method_too_long"; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParameters() { | ||
return Map.of("methodNames", String.join(", ", this.methodNames)); | ||
} | ||
} |
Oops, something went wrong.