-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/io/toolisticon/testing/jgiven/format/VarargsFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.toolisticon.testing.jgiven.format | ||
|
||
import com.tngtech.jgiven.annotation.Format | ||
import com.tngtech.jgiven.format.ArgumentFormatter | ||
import com.tngtech.jgiven.format.PrintfFormatter | ||
import kotlin.annotation.AnnotationRetention.RUNTIME | ||
import kotlin.annotation.AnnotationTarget.* | ||
|
||
|
||
/** | ||
* Varargs step parameters annotated with this annotation will be put into quotes (" ") in reports. | ||
*/ | ||
@MustBeDocumented | ||
@Format(value = VarargsFormatter::class, args = ["\"%s\""]) | ||
@Target(FIELD, VALUE_PARAMETER, ANNOTATION_CLASS) | ||
@Retention(RUNTIME) | ||
annotation class VarargsQuoted | ||
|
||
/** | ||
* Argument formatter for varargs delegating to [PrintfFormatter] | ||
*/ | ||
class VarargsFormatter @JvmOverloads constructor(private val delimiter: String = ", ") : ArgumentFormatter<Any?> { | ||
companion object { | ||
fun anyToList(instance: Any?): List<Any?>? { | ||
if (instance == null || !instance::class.java.isArray) { | ||
return null | ||
} | ||
|
||
// deal with the various primitive array (int[],...) wrappers in kotlin. | ||
return when (instance) { | ||
is IntArray -> instance.toList() | ||
is ByteArray -> instance.toList() | ||
is CharArray -> instance.toList() | ||
is ShortArray -> instance.toList() | ||
is LongArray -> instance.toList() | ||
is DoubleArray -> instance.toList() | ||
is FloatArray -> instance.toList() | ||
is BooleanArray -> instance.toList() | ||
else -> (instance as Array<*>).toList() | ||
} | ||
} | ||
} | ||
|
||
private val formatter = PrintfFormatter() | ||
|
||
override fun format(argumentToFormat: Any?, vararg formatterArguments: String): String { | ||
val argumentList = anyToList(argumentToFormat) | ||
|
||
return argumentList | ||
?.joinToString(separator = delimiter) { formatter.format(it, *formatterArguments) } | ||
?: formatter.format(argumentToFormat, *formatterArguments) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/kotlin/io/toolisticon/testing/jgiven/format/VarargsFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.toolisticon.testing.jgiven.format | ||
|
||
import io.toolisticon.testing.jgiven.format.VarargsFormatter.Companion.anyToList | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
internal class VarargsFormatterTest { | ||
|
||
private var formatter: VarargsFormatter = VarargsFormatter() | ||
|
||
|
||
@Test | ||
fun `format a single value`() { | ||
assertThat(formatter.format("value", "\"%s\"")).isEqualTo("\"value\"") | ||
} | ||
|
||
@Test | ||
fun `format single array values`() { | ||
val varargs = arrayOf("value") | ||
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value\"") | ||
} | ||
|
||
@Test | ||
fun `format multiple array values`() { | ||
val varargs = arrayOf<Any>("value1", 1L) | ||
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value1\", \"1\"") | ||
} | ||
|
||
@Test | ||
fun `fail without formatterArguments`() { | ||
val varargs = arrayOf("value") | ||
|
||
assertThatThrownBy { formatter.format(varargs) } | ||
.isInstanceOf(ArrayIndexOutOfBoundsException::class.java) | ||
} | ||
|
||
@Test | ||
fun `format null value`() { | ||
val varargs: Array<String>? = null | ||
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"null\"") | ||
} | ||
|
||
@Nested | ||
inner class AnyToList { | ||
|
||
|
||
@Test | ||
fun `null to null`() { | ||
assertThat(anyToList(null)).isNull() | ||
} | ||
|
||
@Test | ||
fun `non-array to null`() { | ||
assertThat(anyToList("")).isNull() | ||
} | ||
|
||
@Test | ||
fun `int to list`() { | ||
val list: List<Any?> = requireNotNull(anyToList(varargInt(1, 2, 3))) | ||
|
||
assertThat(list).containsExactly(1, 2, 3) | ||
} | ||
|
||
@Test | ||
fun `boolean to list`() { | ||
val list = requireNotNull(anyToList(varargBoolean(true, false, null))) | ||
|
||
assertThat(list).containsExactly(true, false, null) | ||
} | ||
} | ||
|
||
private fun varargInt(vararg v: Int): Any? = v as Any? | ||
private fun varargBoolean(vararg v: Boolean?): Any? = v as Any? | ||
} |