Skip to content
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

Ft/doc codec #81

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ dependencies {
implementation 'org.slf4j:slf4j-api:1.8.0-beta4'
implementation 'org.apache.commons:commons-lang3:3.12.0'

testImplementation 'junit:junit-dep:4.10'
testImplementation "org.slf4j:slf4j-simple:1.8.0-beta4"
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
}

task sourcesJar(type: Jar) {
Expand Down Expand Up @@ -67,6 +67,8 @@ artifacts {
}

test {
useJUnitPlatform()

testLogging {
events "failed", "skipped"
showStandardStreams = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mojang.serialization;

import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

// Suggested reading material
// http://scodec.org

record PersonRecord(String name, int age) {}

public class IntroCodecsRecordReaderTest {
public IntroCodecsRecordReaderTest() {}

static final Codec<PersonRecord> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.STRING.fieldOf("name").forGetter(PersonRecord::name),
Codec.INT.fieldOf("age").forGetter(PersonRecord::age)
).apply(i, PersonRecord::new));

private Path GetResourcePath(String resource_name) {
final ClassLoader classLoader = getClass().getClassLoader();
return Path.of(Objects.requireNonNull(classLoader.getResource(resource_name)).getPath());
}

@Test
public void CanDeserializeAPersonRecord() throws IOException {
final Path path = GetResourcePath("IntroCodecsRecordReader_00.json");
try (final JsonReader reader = new JsonReader(Files.newBufferedReader(path, StandardCharsets.UTF_8))) {
reader.setLenient(true);
final DataResult<PersonRecord> dr_pr = CODEC.parse(JsonOps.INSTANCE, Streams.parse(reader));
assertThat("We were expecting a PersonRecord here", dr_pr.result().isPresent());

final PersonRecord pr = dr_pr.result().get();
assertThat("Person age should be 77", pr.age(), is(77));
}
}

@Test
public void CanSerializeAPersonRecord() {
final PersonRecord pr = new PersonRecord("name person", 66);
final var enc_pr = CODEC.encodeStart(JsonOps.INSTANCE, pr);
assertThat("Should be possible to encode a person record", enc_pr.result().isPresent());
}
}
9 changes: 5 additions & 4 deletions src/test/java/com/mojang/serialization/RoundtripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
Expand All @@ -13,7 +13,8 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class RoundtripTest {
// Constructors, equals and hashcode are auto-generated
Expand Down Expand Up @@ -249,7 +250,7 @@ private <T> void testWriteRead(final DynamicOps<T> ops) {
final DataResult<T> encoded = TestData.CODEC.encodeStart(ops, data);
final DataResult<TestData> decoded = encoded.flatMap(r -> TestData.CODEC.parse(ops, r));

assertEquals("read(write(x)) == x", DataResult.success(data), decoded);
assertThat("read(write(x)) == x", DataResult.success(data), equalTo(decoded));
}

private <T> void testReadWrite(final DynamicOps<T> ops) {
Expand All @@ -259,7 +260,7 @@ private <T> void testReadWrite(final DynamicOps<T> ops) {
final DataResult<TestData> decoded = encoded.flatMap(r -> TestData.CODEC.parse(ops, r));
final DataResult<T> reEncoded = decoded.flatMap(r -> TestData.CODEC.encodeStart(ops, r));

assertEquals("write(read(x)) == x", encoded, reEncoded);
assertThat("write(read(x)) == x", encoded, equalTo(reEncoded));
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/IntroCodecsRecordReader_00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name" : "person name",
"age": 77
}