diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2023-12-10 20:29:35 +0100 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2023-12-10 20:32:21 +0100 |
commit | df853ef46a9c12d319bf824ac106a411f5eddabd (patch) | |
tree | f7e0112d7ff160c12186ba7d7ec4671a6487aeeb /jgvariant-ostree | |
parent | 3ad12086acde4cfa9d346938e876947bde6305dc (diff) |
Add property-based tests and fix the bugs discovered.
Change-Id: I8deb1a7d75078c037714541d8f6f656052c2476c
Diffstat (limited to 'jgvariant-ostree')
3 files changed, 76 insertions, 9 deletions
diff --git a/jgvariant-ostree/pom.xml b/jgvariant-ostree/pom.xml index 79f5f48..e5aebc3 100644 --- a/jgvariant-ostree/pom.xml +++ b/jgvariant-ostree/pom.xml @@ -79,6 +79,11 @@ SPDX-License-Identifier: LGPL-3.0-or-later <artifactId>inject-resources-junit-jupiter</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>net.jqwik</groupId> + <artifactId>jqwik</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java new file mode 100644 index 0000000..acd11c4 --- /dev/null +++ b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java @@ -0,0 +1,66 @@ +package eu.mulk.jgvariant.ostree; + +import eu.mulk.jgvariant.core.Decoder; +import java.util.Map; +import net.jqwik.api.*; + +@SuppressWarnings("java:S2187") +class OstreeDecoderPropertyTest { + + @Group + class SummaryRoundtripLaw implements RoundtripLaw<Summary> { + + @Override + public Decoder<Summary> decoder() { + return Summary.decoder(); + } + + @Override + public Arbitrary<Summary> anyT() { + return anySummary(); + } + } + + interface RoundtripLaw<T> { + + @Property + default boolean roundtripsWell(@ForAll(value = "anyT") T entityLeft) { + var decoder = decoder(); + var bytes = decoder.encode(entityLeft); + var entityRight = decoder.decode(bytes); + return entityLeft.equals(entityRight); + } + + Decoder<T> decoder(); + + @Provide + Arbitrary<T> anyT(); + } + + @Provide + Arbitrary<Summary> anySummary() { + return Combinators.combine(anySummaryEntry().list(), anyMetadata()).as(Summary::new); + } + + @Provide + Arbitrary<Metadata> anyMetadata() { + return Arbitraries.of(new Metadata(Map.of())); + } + + @Provide + Arbitrary<Summary.Entry> anySummaryEntry() { + return Combinators.combine(Arbitraries.strings(), anySummaryEntryValue()) + .as(Summary.Entry::new); + } + + @Provide + Arbitrary<Summary.Entry.Value> anySummaryEntryValue() { + return Combinators.combine(Arbitraries.integers(), anyChecksum(), anyMetadata()) + .as(Summary.Entry.Value::new); + } + + @Provide + Arbitrary<Checksum> anyChecksum() { + return Arbitraries.of(new Checksum(new ByteString(new byte[32]))); + } +} diff --git a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java index 4465d02..793c5a5 100644 --- a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java +++ b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java @@ -110,10 +110,9 @@ class OstreeDecoderTest { summary.metadata().fields())); var encoded = decoder.encode(summary); + input.rewind(); assertEquals(input, encoded); - - System.out.println(summary); } @Test @@ -123,10 +122,9 @@ class OstreeDecoderTest { var commit = decoder.decode(input); var encoded = decoder.encode(commit); + input.rewind(); assertEquals(input, encoded); - - System.out.println(commit); } @Test @@ -136,10 +134,9 @@ class OstreeDecoderTest { var dirTree = decoder.decode(input); var encoded = decoder.encode(dirTree); + input.rewind(); assertEquals(input, encoded); - - System.out.println(dirTree); } @Test @@ -149,10 +146,9 @@ class OstreeDecoderTest { var dirMeta = decoder.decode(ByteBuffer.wrap(dirMetaBytes)); var encoded = decoder.encode(dirMeta); + input.rewind(); assertEquals(input, encoded); - - System.out.println(dirMeta); } @Test @@ -160,9 +156,9 @@ class OstreeDecoderTest { var decoder = DeltaSuperblock.decoder(); var input = ByteBuffer.wrap(deltaSuperblockBytes); var deltaSuperblock = decoder.decode(input); - System.out.println(deltaSuperblock); var encoded = decoder.encode(deltaSuperblock); + input.rewind(); assertEquals(input, encoded); } |