Skip to content

Commit 7aac70c

Browse files
committed
Merge branch 'release/1.2.2.1'
2 parents 12924c6 + 78fc743 commit 7aac70c

File tree

6 files changed

+168
-8
lines changed

6 files changed

+168
-8
lines changed

pom.xml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>io.toolisticon.testing</groupId>
66
<artifactId>jgiven-kotlin</artifactId>
7-
<version>1.2.2</version>
7+
<version>1.2.2.1</version>
88

99
<name>${project.artifactId}</name>
1010
<description>jgiven kotlin extension</description>
@@ -16,12 +16,14 @@
1616
<java.version>11</java.version>
1717
<maven.compiler.source>${java.version}</maven.compiler.source>
1818
<maven.compiler.target>${java.version}</maven.compiler.target>
19-
<kotlin.version>1.6.21</kotlin.version>
19+
<kotlin.version>1.7.10</kotlin.version>
2020
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
2121

2222
<jgiven.version>1.2.2</jgiven.version>
2323
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
2424

25+
<!-- TEST -->
26+
<assertj.version>3.23.1</assertj.version>
2527
</properties>
2628

2729
<dependencyManagement>
@@ -63,6 +65,13 @@
6365
<scope>test</scope>
6466
</dependency>
6567

68+
<dependency>
69+
<groupId>org.assertj</groupId>
70+
<artifactId>assertj-core</artifactId>
71+
<version>${assertj.version}</version>
72+
<scope>test</scope>
73+
</dependency>
74+
6675
<dependency>
6776
<groupId>org.slf4j</groupId>
6877
<artifactId>slf4j-simple</artifactId>
@@ -262,7 +271,7 @@
262271
<plugin>
263272
<groupId>org.jetbrains.dokka</groupId>
264273
<artifactId>dokka-maven-plugin</artifactId>
265-
<version>1.6.21</version>
274+
<version>1.7.10</version>
266275
<executions>
267276
<execution>
268277
<id>attach-javadocs</id>
@@ -377,7 +386,7 @@
377386
<!-- Deploy -->
378387
<plugin>
379388
<artifactId>maven-deploy-plugin</artifactId>
380-
<version>3.0.0-M2</version>
389+
<version>3.0.0</version>
381390
<configuration>
382391
<skip>true</skip>
383392
</configuration>
@@ -408,14 +417,14 @@
408417
<!-- Install -->
409418
<plugin>
410419
<artifactId>maven-install-plugin</artifactId>
411-
<version>2.5.2</version>
420+
<version>3.0.0</version>
412421
</plugin>
413422

414423
<!-- Enforce -->
415424
<plugin>
416425
<groupId>org.apache.maven.plugins</groupId>
417426
<artifactId>maven-enforcer-plugin</artifactId>
418-
<version>3.0.0</version>
427+
<version>3.1.0</version>
419428
<executions>
420429
<execution>
421430
<id>enforce-maven</id>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.toolisticon.testing.jgiven.format
2+
3+
import com.tngtech.jgiven.annotation.Format
4+
import com.tngtech.jgiven.format.ArgumentFormatter
5+
import com.tngtech.jgiven.format.PrintfFormatter
6+
import kotlin.annotation.AnnotationRetention.RUNTIME
7+
import kotlin.annotation.AnnotationTarget.*
8+
9+
10+
/**
11+
* Varargs step parameters annotated with this annotation will be put into quotes (" ") in reports.
12+
*/
13+
@MustBeDocumented
14+
@Format(value = VarargsFormatter::class, args = ["\"%s\""])
15+
@Target(FIELD, VALUE_PARAMETER, ANNOTATION_CLASS)
16+
@Retention(RUNTIME)
17+
annotation class VarargsQuoted
18+
19+
/**
20+
* Argument formatter for varargs delegating to [PrintfFormatter]
21+
*/
22+
class VarargsFormatter @JvmOverloads constructor(private val delimiter: String = ", ") : ArgumentFormatter<Any?> {
23+
companion object {
24+
fun anyToList(instance: Any?): List<Any?>? {
25+
if (instance == null || !instance::class.java.isArray) {
26+
return null
27+
}
28+
29+
// deal with the various primitive array (int[],...) wrappers in kotlin.
30+
return when (instance) {
31+
is IntArray -> instance.toList()
32+
is ByteArray -> instance.toList()
33+
is CharArray -> instance.toList()
34+
is ShortArray -> instance.toList()
35+
is LongArray -> instance.toList()
36+
is DoubleArray -> instance.toList()
37+
is FloatArray -> instance.toList()
38+
is BooleanArray -> instance.toList()
39+
else -> (instance as Array<*>).toList()
40+
}
41+
}
42+
}
43+
44+
private val formatter = PrintfFormatter()
45+
46+
override fun format(argumentToFormat: Any?, vararg formatterArguments: String): String {
47+
val argumentList = anyToList(argumentToFormat)
48+
49+
return argumentList
50+
?.joinToString(separator = delimiter) { formatter.format(it, *formatterArguments) }
51+
?: formatter.format(argumentToFormat, *formatterArguments)
52+
}
53+
}

src/test/kotlin/CalculatorExampleTest.kt renamed to src/test/kotlin/io/toolisticon/testing/jgiven/CalculatorExampleTest.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import com.tngtech.jgiven.Stage
44
import com.tngtech.jgiven.annotation.ScenarioState
55
import com.tngtech.jgiven.annotation.ScenarioState.Resolution.NAME
66
import com.tngtech.jgiven.junit5.SimpleScenarioTest
7+
import io.toolisticon.testing.jgiven.format.VarargsQuoted
78
import org.junit.jupiter.api.Test
89
import kotlin.test.assertEquals
910

1011
/**
1112
* Simple example using GIVEN/WHEN/THEN extensions, the step() lambda and the all open annotation.
1213
*/
13-
class CalculatorExampleTest : SimpleScenarioTest<CalculatorStage>() {
14+
internal class CalculatorExampleTest : SimpleScenarioTest<CalculatorStage>() {
1415

1516
@Test
16-
internal fun `calculator adds two numbers`() {
17+
fun `calculator adds two numbers`() {
1718
GIVEN
1819
.`the first number is $`(4)
1920
.AND
@@ -25,6 +26,18 @@ class CalculatorExampleTest : SimpleScenarioTest<CalculatorStage>() {
2526
THEN
2627
.`the sum is $`(11)
2728
}
29+
30+
@Test
31+
fun `calculator adds two numbers as varargs`() {
32+
GIVEN
33+
.`numbers are $`(5, 4)
34+
35+
WHEN
36+
.`both numbers are added`()
37+
38+
THEN
39+
.`the sum is $`(9)
40+
}
2841
}
2942

3043
@JGivenKotlinStage
@@ -39,6 +52,14 @@ class CalculatorStage : Stage<CalculatorStage>() {
3952
@ScenarioState(resolution = NAME)
4053
var sum: Int = 0
4154

55+
/**
56+
* Sets both numbers via vararg, use to verify Vararg formatter.
57+
*/
58+
fun `numbers are $`(@VarargsQuoted vararg nums: Int) = step {
59+
require(nums.size == 2) { "need to pass two numbers" }
60+
`the first number is $`(nums[0]).`the second number is $`(nums[1])
61+
}
62+
4263
fun `the first number is $`(n: Int) = step {
4364
this.firstNumber = n
4465
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.toolisticon.testing.jgiven.format
2+
3+
import io.toolisticon.testing.jgiven.format.VarargsFormatter.Companion.anyToList
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.assertj.core.api.Assertions.assertThatThrownBy
6+
import org.junit.jupiter.api.Nested
7+
import org.junit.jupiter.api.Test
8+
9+
10+
internal class VarargsFormatterTest {
11+
12+
private var formatter: VarargsFormatter = VarargsFormatter()
13+
14+
15+
@Test
16+
fun `format a single value`() {
17+
assertThat(formatter.format("value", "\"%s\"")).isEqualTo("\"value\"")
18+
}
19+
20+
@Test
21+
fun `format single array values`() {
22+
val varargs = arrayOf("value")
23+
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value\"")
24+
}
25+
26+
@Test
27+
fun `format multiple array values`() {
28+
val varargs = arrayOf<Any>("value1", 1L)
29+
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value1\", \"1\"")
30+
}
31+
32+
@Test
33+
fun `fail without formatterArguments`() {
34+
val varargs = arrayOf("value")
35+
36+
assertThatThrownBy { formatter.format(varargs) }
37+
.isInstanceOf(ArrayIndexOutOfBoundsException::class.java)
38+
}
39+
40+
@Test
41+
fun `format null value`() {
42+
val varargs: Array<String>? = null
43+
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"null\"")
44+
}
45+
46+
@Nested
47+
inner class AnyToList {
48+
49+
50+
@Test
51+
fun `null to null`() {
52+
assertThat(anyToList(null)).isNull()
53+
}
54+
55+
@Test
56+
fun `non-array to null`() {
57+
assertThat(anyToList("")).isNull()
58+
}
59+
60+
@Test
61+
fun `int to list`() {
62+
val list: List<Any?> = requireNotNull(anyToList(varargInt(1, 2, 3)))
63+
64+
assertThat(list).containsExactly(1, 2, 3)
65+
}
66+
67+
@Test
68+
fun `boolean to list`() {
69+
val list = requireNotNull(anyToList(varargBoolean(true, false, null)))
70+
71+
assertThat(list).containsExactly(true, false, null)
72+
}
73+
}
74+
75+
private fun varargInt(vararg v: Int): Any? = v as Any?
76+
private fun varargBoolean(vararg v: Boolean?): Any? = v as Any?
77+
}

0 commit comments

Comments
 (0)