Skip to content

Commit

Permalink
Preserve order of added comments and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Jan 18, 2024
1 parent 3038944 commit 7eaa632
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/main/java/analyzer/Analysis.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package analyzer;

import java.util.Set;
import java.util.List;

public record Analysis(Set<Comment> comments, String summary) {
public record Analysis(List<Comment> comments, String summary) {
public Analysis {
comments = Set.copyOf(comments);
comments = List.copyOf(comments);
}
}
16 changes: 10 additions & 6 deletions src/main/java/analyzer/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;

public abstract class Exercise {
private CompilationUnit compilationUnit;

private String summary;
private final Set<Comment> comments = new HashSet<>();
private final Set<String> tags = new HashSet<>();
private final List<Comment> comments = new ArrayList<>();
private final List<String> tags = new ArrayList<>();

protected Exercise(String directory, String solutionFile) {
this(getSolutionFile(directory, solutionFile));
Expand Down Expand Up @@ -46,15 +46,19 @@ public final void parse() {
abstract public void parse(CompilationUnit compilationUnit);

protected void addComment(Comment comment) {
this.comments.add(comment);
if (!this.comments.contains(comment)) {
this.comments.add(comment);
}
}

protected void setSummary(String summary) {
this.summary = summary;
}

protected void addTag(String tag) {
this.tags.add(tag);
if (!this.tags.contains(tag)) {
this.tags.add(tag);
}
}

public Analysis getAnalysis() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/analyzer/Tags.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package analyzer;

import java.util.Set;
import java.util.List;

public record Tags(Set<String> tags) {
public record Tags(List<String> tags) {
public Tags {
tags = Set.copyOf(tags);
tags = List.copyOf(tags);
}
}
27 changes: 17 additions & 10 deletions src/test/java/analyzer/JsonSerializerTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package analyzer;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -49,11 +48,11 @@ public void serializeComment(Comment comment, String expected) {
private static Stream<Arguments> analyses() {
return Stream.of(
Arguments.of(
new Analysis(Set.of(new TestComment("key")), null),
new Analysis(List.of(new TestComment("key")), null),
"""
{"comments":[{"comment":"key"}]}"""),
Arguments.of(
new Analysis(Set.of(new TestComment("key")), "Lorum ipsum"),
new Analysis(List.of(new TestComment("key")), "Lorum ipsum"),
"""
{"summary":"Lorum ipsum","comments":[{"comment":"key"}]}""")
);
Expand All @@ -66,12 +65,20 @@ public void serializeAnalysis(Analysis analysis, String expected) {
assertThat(actual).isEqualTo(expected);
}

@Test
public void serializeTags() {
var tags = new Tags(Set.of("tag1", "tag2", "tag3"));
var actual = JsonSerializer.serialize(tags);
assertThat(actual.keySet()).containsExactlyInAnyOrder("tags");
assertThat(actual.getJSONArray("tags")).containsExactlyInAnyOrder("tag1", "tag2", "tag3");
private static Stream<Arguments> tags() {
return Stream.of(
Arguments.of(new Tags(List.of()), "{}"),
Arguments.of(new Tags(List.of("tag1", "tag2", "tag3")),
"""
{"tags":["tag1","tag2","tag3"]}""")
);
}

@MethodSource("tags")
@ParameterizedTest(name = "{0}")
public void serializeTags(Tags tags, String expected) {
var actual = JsonSerializer.serialize(tags).toString();
assertThat(actual).isEqualTo(expected);
}

private static class TestComment extends Comment {
Expand Down
4 changes: 2 additions & 2 deletions tests/hamming/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"comments": [
{"comment": "java.hamming.should_use_string_is_empty"},
{"comment": "java.hamming.calculate_distance_in_constructor"}
{"comment": "java.hamming.calculate_distance_in_constructor"},
{"comment": "java.hamming.should_use_string_is_empty"}
]}

0 comments on commit 7eaa632

Please sign in to comment.