Skip to content

Commit

Permalink
Replace .assertForEach {} with parameterize(...) {}`
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWoodworth committed Oct 3, 2022
1 parent eaa99d7 commit 9278b40
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 146 deletions.
40 changes: 29 additions & 11 deletions src/commonTest/kotlin/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,40 @@ fun ByteArray.asSource(): Source = object : Source {
override fun timeout(): Timeout = Timeout.NONE
}

fun <T> Iterable<T>.assertForEach(assert: (element: T) -> Unit) {
val throws = mutableListOf<Throwable>()

forEach { element ->
try {
assert(element)
inline fun <T> parameterize(
parameters: List<T>,
description: T.() -> Any? = { this },
assert: T.() -> Unit,
) {
val errors = parameters.map { parameter ->
val error = try {
parameter.assert()
null
} catch (t: Throwable) {
throws += t
t.printStackTrace()
println()
t
}

parameter to error
}

if (throws.any()) {
throw AssertionError("Exceptions thrown: ${throws.size}")
errors.forEach { (parameter, error) ->
val passOrFail = if (error == null) "PASS" else "FAIL"

println("[$passOrFail] ${description(parameter)}")
if (error != null) {
if (error is AssertionError) {
println("- ${error.message}")
} else {
error.printStackTrace()
}
}
if (error != null) println()
}

val failCount = errors.count { (_, error) -> error != null }
println("Passed: ${parameters.size - failCount}/${parameters.size}")

if (failCount > 0) fail()
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/commonTest/kotlin/file/NbtTestFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class NbtTestFile<T>(
fun toByteArray(): ByteArray = bytes.copyOf()

fun asSource(): Source = bytes.asSource()

override fun toString(): String = description
}

val nbtFiles = listOf(
Expand Down
80 changes: 36 additions & 44 deletions src/commonTest/kotlin/internal/BinaryNbtReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,48 @@ import kotlin.test.*
@OptIn(OkioApi::class)
class BinaryNbtReaderTest {
@Test
fun Should_decode_to_class_correctly() {
nbtFiles.assertForEach { file ->
assertEquals(
expected = file.value,
actual = file.asSource().use { source ->
file.nbt.decodeFromSource(file.valueSerializer, source)
},
message = "Read class incorrectly while decoding ${file.description}"
)
}
fun Should_decode_to_class_correctly() = parameterize(nbtFiles) {
assertEquals(
expected = value,
actual = asSource().use { source ->
nbt.decodeFromSource(valueSerializer, source)
},
)
}

@Test
fun Should_decode_to_NbtTag_correctly() {
nbtFiles.assertForEach { file ->
assertEquals(
expected = file.nbtTag,
actual = file.asSource().use { source ->
file.nbt.decodeFromSource(NbtTag.serializer(), source)
},
message = "Read NbtTag incorrectly while decoding ${file.description}"
)
}
fun Should_decode_to_NbtTag_correctly() = parameterize(nbtFiles) {
assertEquals(
expected = nbtTag,
actual = asSource().use { source ->
nbt.decodeFromSource(NbtTag.serializer(), source)
},
)
}

@Test
fun Should_not_read_more_from_source_than_necessary() {
nbtFiles.assertForEach { file ->
TestSource(file.asSource()).use { source ->
file.nbt.decodeFromSource(NbtTag.serializer(), source)
assertFalse(source.readPastEnd, "Source read past end while decoding ${file.description}")
}
fun Should_not_read_more_from_source_than_necessary() = parameterize(nbtFiles) {
TestSource(asSource()).use { source ->
nbt.decodeFromSource(NbtTag.serializer(), source)
assertFalse(source.readPastEnd)
}
}

@Test
fun Should_not_close_source() {
nbtFiles.assertForEach { file ->
TestSource(file.asSource()).use { source ->
file.nbt.decodeFromSource(NbtTag.serializer(), source)
assertFalse(source.isClosed, "Source closed while decoding ${file.description}")
}
fun Should_not_close_source() = parameterize(nbtFiles) {
TestSource(asSource()).use { source ->
nbt.decodeFromSource(NbtTag.serializer(), source)
assertFalse(source.isClosed)
}
}

@Test
fun Should_fail_with_incorrect_NbtCompression_and_specify_mismatched_compressions() {
data class Parameters(
val configuredCompression: NbtCompression,
val fileCompression: NbtCompression,
)

val data = buildNbtCompound("root") {
put("string", "String!")
}
Expand All @@ -68,14 +63,18 @@ class BinaryNbtReaderTest {
NbtCompression.Zlib,
)

fun test(configured: NbtCompression, actual: NbtCompression) {
val mismatchedCompressions = compressions
.flatMap { a -> compressions.map { b -> Parameters(a, b) } }
.filter { (a, b) -> a !== b }

parameterize(mismatchedCompressions, { "Configured: $configuredCompression, File: $fileCompression" }) {
val decodingNbt = Nbt {
variant = Java
compression = configured
compression = configuredCompression
}

val encodingNbt = Nbt(decodingNbt) {
compression = actual
compression = fileCompression
}

val encoded = encodingNbt.encodeToByteArray(NbtTag.serializer(), data)
Expand All @@ -88,19 +87,12 @@ class BinaryNbtReaderTest {
assertNotNull(errorMessage)
assertContains(
errorMessage,
configured.toString(),
message = "Error message should contain configured compression name"
configuredCompression.toString(),
)
assertContains(
errorMessage,
actual.toString(),
message = "Error message should contain actual compression name"
fileCompression.toString(),
)
}

compressions
.flatMap { a -> compressions.map { b -> a to b } }
.filter { (a, b) -> a !== b }
.assertForEach { (a, b) -> test(a, b) }
}
}
98 changes: 46 additions & 52 deletions src/commonTest/kotlin/internal/BinaryNbtWriterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,54 @@ class BinaryNbtWriterTest {
}

@Test
fun Should_encode_from_class_correctly() {
nbtFiles.assertForEach { file ->
@Suppress("UNCHECKED_CAST")
val out = file.nbt.encodeToByteArray(file.valueSerializer as KSerializer<Any>, file.value)

val outCompression = try {
NbtCompression.detect(out.asSource().buffer())
} catch (t: Throwable) {
throw Exception("Unable to check compression type", t)
}

assertEquals(
file.nbt.configuration.compression,
outCompression,
"Encoded with wrong compression: ${file.description}",
)
fun Should_encode_from_class_correctly() = parameterize(nbtFiles) {
@Suppress("UNCHECKED_CAST")
val out = nbt.encodeToByteArray(valueSerializer as KSerializer<Any>, value)

val outCompression = try {
NbtCompression.detect(out.asSource().buffer())
} catch (t: Throwable) {
throw Exception("Unable to check compression type", t)
}

val tag = try {
file.nbt.decodeFromByteArray(NbtTag.serializer(), out)
} catch (t: Throwable) {
throw Exception("Unable to decode compressed value", t)
}
assertEquals(
nbt.configuration.compression,
outCompression,
"Encoded with wrong compression",
)

assertEquals(file.nbtTag, tag, "Unable to decode encoded data correctly: ${file.description}")
val tag = try {
nbt.decodeFromByteArray(NbtTag.serializer(), out)
} catch (t: Throwable) {
throw Exception("Unable to decode compressed value", t)
}

assertEquals(nbtTag, tag, "Unable to decode encoded data correctly")
}

@Test
fun Should_encode_from_NbtTag_correctly() {
nbtFiles.assertForEach { file ->
val out = file.nbt.encodeToByteArray(NbtTag.serializer(), file.nbtTag)

val outCompression = try {
NbtCompression.detect(out.asSource().buffer())
} catch (t: Throwable) {
throw Exception("Unable to check compression type", t)
}

assertEquals(
file.nbt.configuration.compression,
outCompression,
"Encoded with wrong compression: ${file.description}",
)

val tag = try {
file.nbt.decodeFromByteArray(NbtTag.serializer(), out)
} catch (t: Throwable) {
throw Exception("Unable to decode compressed value", t)
}

assertEquals(file.nbtTag, tag, "Unable to decode encoded data correctly: ${file.description}")
fun Should_encode_from_NbtTag_correctly() = parameterize(nbtFiles) {
val out = nbt.encodeToByteArray(NbtTag.serializer(), nbtTag)

val outCompression = try {
NbtCompression.detect(out.asSource().buffer())
} catch (t: Throwable) {
throw Exception("Unable to check compression type", t)
}

assertEquals(
nbt.configuration.compression,
outCompression,
"Encoded with wrong compression",
)

val tag = try {
nbt.decodeFromByteArray(NbtTag.serializer(), out)
} catch (t: Throwable) {
throw Exception("Unable to decode compressed value", t)
}

assertEquals(nbtTag, tag, "Unable to decode encoded data correctly")
}

@Test
Expand Down Expand Up @@ -154,13 +150,11 @@ class BinaryNbtWriterTest {
}

@Test
fun Should_not_close_sink() {
nbtFiles.assertForEach { file ->
TestSink(blackholeSink()).use { sink ->
@Suppress("UNCHECKED_CAST")
file.nbt.encodeToSink(file.valueSerializer as KSerializer<Any>, file.value, sink)
assertFalse(sink.isClosed, "Sink closed while decoding ${file.description}")
}
fun Should_not_close_sink() = parameterize(nbtFiles) {
TestSink(blackholeSink()).use { sink ->
@Suppress("UNCHECKED_CAST")
nbt.encodeToSink(valueSerializer as KSerializer<Any>, value, sink)
assertFalse(sink.isClosed, "Sink closed while decoding")
}
}
}
Loading

0 comments on commit 9278b40

Please sign in to comment.