Skip to content

Commit adae5bb

Browse files
author
Ronald Holshausen
authoredMar 21, 2020
Merge pull request #1048 from gmariotti/master
feat: create Kotlin friendly extension for arrays
2 parents d38fda1 + 045499f commit adae5bb

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
dependencies {
22
api project(path: ":consumer:pact-jvm-consumer", configuration: 'default')
33

4-
testCompile "org.junit.jupiter:junit-jupiter-api:${project.junit5Version}"
5-
testRuntime "org.junit.vintage:junit-vintage-engine:${project.junit5Version}"
4+
testImplementation "org.junit.jupiter:junit-jupiter:${project.junit5Version}"
65
testRuntime "ch.qos.logback:logback-classic:${project.logbackVersion}"
7-
testCompile "junit:junit:${project.junitVersion}"
8-
testCompile "org.codehaus.groovy:groovy:${project.groovyVersion}"
9-
testCompile('org.spockframework:spock-core:2.0-M2-groovy-3.0') {
6+
testImplementation "org.codehaus.groovy:groovy:${project.groovyVersion}"
7+
testImplementation('org.spockframework:spock-core:2.0-M2-groovy-3.0') {
108
exclude group: 'org.codehaus.groovy'
119
}
1210
}

‎consumer/pact-jvm-consumer-java8/src/main/kotlin/io/pactfoundation/consumer/dsl/Extensions.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ fun LambdaDslObject.newObject(name: String, nestedObject: LambdaDslObject.() ->
2323
return `object`(name) { it.nestedObject() }
2424
}
2525

26+
/**
27+
* Extension function to make [LambdaDslObject.array] Kotlin friendly.
28+
*/
29+
fun LambdaDslObject.newArray(name: String, body: LambdaDslJsonArray.() -> (Unit)): LambdaDslObject {
30+
return array(name) { it.body() }
31+
}
32+
2633
/**
2734
* Extension function to make [LambdaDslJsonArray.array] Kotlin friendly.
2835
*/
@@ -35,4 +42,4 @@ fun LambdaDslJsonArray.newArray(body: LambdaDslJsonArray.() -> (Unit)): LambdaDs
3542
*/
3643
fun LambdaDslJsonArray.newObject(body: LambdaDslObject.() -> (Unit)): LambdaDslJsonArray {
3744
return `object` { it.body() }
38-
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
package io.pactfoundation.consumer.dsl
22

3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.junit.Assert.assertThat
35
import org.junit.jupiter.api.Test
46

57
class ExtensionsTest {
6-
@Test
7-
fun `can use LambdaDslJsonArray#newObject`() {
8-
newJsonArray { newObject { stringType("foo") } }
9-
}
8+
@Test
9+
fun `can use Kotlin DSL to create a Json Array`() {
10+
val expectedJson = """[
11+
|{"key":"value"},
12+
|{"key_1":"value_1"}
13+
|]""".trimMargin().replace("\n", "")
1014

11-
@Test
12-
fun `can use LambdaDslObject#newObject`() {
13-
newJsonObject {
14-
newObject("object") {
15-
stringType("field")
16-
}
15+
val actualJson = newJsonArray {
16+
newObject { stringValue("key", "value") }
17+
newObject { stringValue("key_1", "value_1") }
18+
}.body.toString()
19+
20+
assertThat(actualJson, equalTo(expectedJson))
21+
}
22+
23+
@Test
24+
fun `can use Kotlin DSL to create a Json`() {
25+
val expectedJson = """{
26+
|"array":[{"key":"value"}],
27+
|"object":{"property":"value"}
28+
|}""".trimMargin().replace("\n", "")
29+
30+
val actualJson = newJsonObject {
31+
newArray("array") {
32+
newObject { stringValue("key", "value") }
33+
}
34+
newObject("object") {
35+
stringValue("property", "value")
36+
}
37+
}.body.toString()
38+
39+
assertThat(actualJson, equalTo(expectedJson))
1740
}
18-
}
1941
}

0 commit comments

Comments
 (0)
Please sign in to comment.