From 8e3727afb7145eb9e1a72ffa148b0c133a1eaeef Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Jul 2026 15:59:25 +0100 Subject: Extract assertRoundTripEncoding() --- .../java/org/zwobble/example/Transient0Tests.java | 117 +++++++++++---------- 1 file changed, 59 insertions(+), 58 deletions(-) (limited to 'examples') diff --git a/examples/10-transient-0/output/java-junit/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/10-transient-0/output/java-junit/src/test/java/org/zwobble/example/Transient0Tests.java index 40acde4..cb3ac61 100644 --- a/examples/10-transient-0/output/java-junit/src/test/java/org/zwobble/example/Transient0Tests.java +++ b/examples/10-transient-0/output/java-junit/src/test/java/org/zwobble/example/Transient0Tests.java @@ -3,6 +3,8 @@ 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; @@ -22,70 +24,55 @@ public class Transient0Tests { public void structWithBool() throws IOException { var value = new StructWithBool(true, false); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithBool(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithBool(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithBool, + HobgoblinTransient0Data::decodeStructWithBool + ); } @Test public void structWithInt32() throws IOException { var value = new StructWithInt32(10, 25); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithInt32(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithInt32(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithInt32, + HobgoblinTransient0Data::decodeStructWithInt32 + ); } @Test public void structWithInt64() throws IOException { var value = new StructWithInt64(10, 25); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithInt64(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithInt64(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithInt64, + HobgoblinTransient0Data::decodeStructWithInt64 + ); } @Test public void structWithString() throws IOException { var value = new StructWithString("abc", "def"); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithString(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithString(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithString, + HobgoblinTransient0Data::decodeStructWithString + ); } @Test public void nestedStruct() throws IOException { var value = new OuterStruct(new InnerStruct(10, 25), 42); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeOuterStruct(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeOuterStruct(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeOuterStruct, + HobgoblinTransient0Data::decodeOuterStruct + ); } @Test @@ -99,14 +86,11 @@ public class Transient0Tests { ) ); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithList(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithList(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithList, + HobgoblinTransient0Data::decodeStructWithList + ); } @Test @@ -116,14 +100,11 @@ public class Transient0Tests { Optional.empty() ); - var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithOption(value, outputStream); - var bytes = outputStream.toByteArray(); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithOption(inputStream); - - Assertions.assertEquals(value, decodedValue); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); } @Test @@ -133,13 +114,33 @@ public class Transient0Tests { Optional.of(new InnerStruct(42, 47)) ); + assertRoundTripEncoding( + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + private void assertRoundTripEncoding( + T value, + Encoder encoder, + Decoder decoder + ) throws IOException { var outputStream = new ByteArrayOutputStream(); - HobgoblinTransient0Data.encodeStructWithOption(value, outputStream); + encoder.encode(value, outputStream); var bytes = outputStream.toByteArray(); var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = HobgoblinTransient0Data.decodeStructWithOption(inputStream); + var decodedValue = decoder.decode(inputStream); Assertions.assertEquals(value, decodedValue); } + + private interface Encoder { + void encode(T value, OutputStream outputStream) throws IOException; + } + + private interface Decoder { + T decode(InputStream inputStream) throws IOException; + } } -- cgit v1.2.3