Skip to content

Commit

Permalink
Can serialize record with List<Suggestion>
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Feb 23, 2023
1 parent f413b62 commit 958cc78
Showing 1 changed file with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.enso.compiler.test.context;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.scala.DefaultScalaModule;
import java.util.List;
import org.enso.polyglot.Suggestion;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;
import scala.Option;
Expand All @@ -14,11 +17,11 @@ public class JacksonTest {
@Test
public void testSerdeOfSuggestion() throws Exception {
Object shape = new Suggestion.Module(
"SampleModule",
Option.apply("doc"),
Option.apply("html"),
Option.empty(),
Option.empty()
"SampleModule",
Option.apply("doc"),
Option.apply("html"),
Option.empty(),
Option.empty()
);
final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule());
String result = m
Expand All @@ -34,10 +37,10 @@ public void testSerdeOfSuggestion() throws Exception {
@Test
public void testArraySerdeOfSuggestion() throws Exception {
Object shape = new Suggestion[]{new Suggestion.Module(
"SampleModule",
Option.apply("doc"),
Option.apply("html"),
Option.empty(),
"SampleModule",
Option.apply("doc"),
Option.apply("html"),
Option.empty(),
Option.empty()
)};
final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule());
Expand All @@ -51,6 +54,38 @@ public void testArraySerdeOfSuggestion() throws Exception {
if (suggestion instanceof Suggestion.Module module) {
assertEquals("SampleModule", module.name());
assertEquals("doc", module.documentation().get());
} else {
fail("Expecting Suggestion.Module: " + suggestion);
}
}

@Test
public void testRecordSerdeOfSuggestion() throws Exception {
Object shape = new SuggestionCache(11, List.of(new Suggestion.Module(
"SampleModule",
Option.apply("doc"),
Option.apply("html"),
Option.empty(),
Option.empty()
)));
final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule());
String result = m
.writerWithDefaultPrettyPrinter()
.writeValueAsString(shape);

var cache = (SuggestionCache) m.readerFor(SuggestionCache.class).readValue(result);
assertEquals("One suggestion", 1, cache.suggestions.size());
if (cache.suggestions().get(0) instanceof Suggestion.Module module) {
assertEquals("SampleModule", module.name());
assertEquals("doc", module.documentation().get());
} else {
fail("Expecting Suggestion.Module: " + cache);
}
}

public record SuggestionCache(
@JsonProperty("version") int version,
@JsonProperty("suggestions") List<Suggestion> suggestions
) {
}
}

0 comments on commit 958cc78

Please sign in to comment.