diff options
Diffstat (limited to 'examples/10-transient-0/output/java/src')
| -rw-r--r-- | examples/10-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/examples/10-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/10-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java new file mode 100644 index 0000000..e28e926 --- /dev/null +++ b/examples/10-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java @@ -0,0 +1,185 @@ +package org.zwobble.example; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.data.EnumWithVariants; +import org.zwobble.example.types.data.HobgoblinTransient0Data; +import org.zwobble.example.types.data.InnerStruct; +import org.zwobble.example.types.data.OuterStruct; +import org.zwobble.example.types.data.StructWithBool; +import org.zwobble.example.types.data.StructWithInt32; +import org.zwobble.example.types.data.StructWithInt64; +import org.zwobble.example.types.data.StructWithList; +import org.zwobble.example.types.data.StructWithOption; +import org.zwobble.example.types.data.StructWithString; +import org.zwobble.example.types.data.VariantOne; +import org.zwobble.example.types.data.VariantTwo; + +public class Transient0Tests { + @Test + public void structWithBool() throws IOException { + var value = new StructWithBool(true, false); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithBool, + HobgoblinTransient0Data::decodeStructWithBool + ); + } + + @Test + public void structWithInt32() throws IOException { + var value = new StructWithInt32(10, 25); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithInt32, + HobgoblinTransient0Data::decodeStructWithInt32 + ); + } + + @Test + public void structWithInt64() throws IOException { + var value = new StructWithInt64(10, 25); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithInt64, + HobgoblinTransient0Data::decodeStructWithInt64 + ); + } + + @Test + public void structWithString() throws IOException { + var value = new StructWithString("abc", "def"); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithString, + HobgoblinTransient0Data::decodeStructWithString + ); + } + + @Test + public void nestedStruct() throws IOException { + var value = new OuterStruct(new InnerStruct(10, 25), 42); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeOuterStruct, + HobgoblinTransient0Data::decodeOuterStruct + ); + } + + @Test + public void structWithList() throws IOException { + var value = new StructWithList( + List.of(10L, 25L), + List.of( + new InnerStruct(42, 47), + new InnerStruct(52, 57), + new InnerStruct(62, 67) + ) + ); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithList, + HobgoblinTransient0Data::decodeStructWithList + ); + } + + @Test + public void structWithOptionNone() throws IOException { + var value = new StructWithOption( + Optional.empty(), + Optional.empty() + ); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void structWithOptionSome() throws IOException { + var value = new StructWithOption( + Optional.of(10L), + Optional.of(new InnerStruct(42, 47)) + ); + + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void enumWithVariants() throws IOException { + assertRoundTripEncoding( + EnumWithVariants.ZERO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + EnumWithVariants.ONE, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + EnumWithVariants.TWO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + } + + @Test + public void sumWithVariants() throws IOException { + assertRoundTripEncoding( + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + + assertRoundTripEncoding( + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + } + + private <T> void assertRoundTripEncoding( + T value, + Encoder<T> encoder, + Decoder<T> decoder + ) throws IOException { + var outputStream = new ByteArrayOutputStream(); + encoder.encode(value, outputStream); + var bytes = outputStream.toByteArray(); + + var inputStream = new ByteArrayInputStream(bytes); + var decodedValue = decoder.decode(inputStream); + + Assertions.assertEquals(value, decodedValue); + } + + private interface Encoder<T> { + void encode(T value, OutputStream outputStream) throws IOException; + } + + private interface Decoder<T> { + T decode(InputStream inputStream) throws IOException; + } +} |
