-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
1 parent
4f753bc
commit 2b9b18b
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
core/support/src/main/kotlin/au/com/dius/pact/core/support/json/JsonBuilder.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,47 @@ | ||
package au.com.dius.pact.core.support.json | ||
|
||
import au.com.dius.pact.core.support.Json | ||
|
||
sealed class JsonBuilder(open val root: JsonValue) { | ||
class JsonObjectBuilder(override val root: JsonValue.Object = JsonValue.Object()): JsonBuilder(root) { | ||
operator fun set(name: String, value: Any?) { | ||
root[name] = Json.toJson(value) | ||
} | ||
|
||
fun `object`(function: (JsonObjectBuilder) -> Unit): JsonValue.Object { | ||
return build(function) | ||
} | ||
|
||
fun array(function: (JsonArrayBuilder) -> Unit): JsonValue.Array { | ||
val builder = JsonArrayBuilder() | ||
function(builder) | ||
return builder.root | ||
} | ||
} | ||
|
||
class JsonArrayBuilder(override val root: JsonValue.Array = JsonValue.Array()): JsonBuilder(root) { | ||
operator fun set(i: Int, value: Any?) { | ||
root[i] = Json.toJson(value) | ||
} | ||
|
||
fun push(value: Any?) { | ||
root.append(Json.toJson(value)) | ||
} | ||
|
||
operator fun plusAssign(value: Any?) { | ||
push(value) | ||
} | ||
|
||
fun `object`(function: (JsonObjectBuilder) -> Unit): JsonValue.Object { | ||
return build(function) | ||
} | ||
} | ||
|
||
companion object { | ||
fun build(function: (JsonObjectBuilder) -> Unit): JsonValue.Object { | ||
val builder = JsonObjectBuilder() | ||
function(builder) | ||
return builder.root | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
core/support/src/test/kotlin/au/com/dius/pact/core/support/json/JsonBuilderTest.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,102 @@ | ||
package au.com.dius.pact.core.support.json | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.hamcrest.Matchers.`is` | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonBuilderTest { | ||
@Test | ||
fun testEmptyBuilder() { | ||
val json = JsonBuilder.build { | ||
it | ||
} | ||
|
||
val expected = JsonValue.Object() | ||
|
||
assertThat(json, `is`(equalTo(expected))) | ||
} | ||
|
||
@Test | ||
fun testBasicJson() { | ||
val json = JsonBuilder.build { | ||
it["integer"] = 100 | ||
it["decimal"] = 100.22 | ||
it["string"] = "100.22" | ||
it["bool"] = true | ||
it["null"] = null | ||
} | ||
|
||
val expected = JsonValue.Object(mutableMapOf( | ||
"bool" to JsonValue.True, | ||
"decimal" to JsonValue.Decimal(100.22), | ||
"integer" to JsonValue.Integer(100), | ||
"null" to JsonValue.Null, | ||
"string" to JsonValue.StringValue("100.22") | ||
)) | ||
|
||
assertThat(json, `is`(equalTo(expected))) | ||
} | ||
|
||
@Test | ||
fun testChildObjectJson() { | ||
val json = JsonBuilder.build { | ||
it["child"] = it.`object` { child -> | ||
child["integer"] = 100 | ||
child["decimal"] = 100.22 | ||
child["string"] = "100.22" | ||
child["bool"] = true | ||
child["null"] = null | ||
} | ||
} | ||
|
||
val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Object(mutableMapOf( | ||
"bool" to JsonValue.True, | ||
"decimal" to JsonValue.Decimal(100.22), | ||
"integer" to JsonValue.Integer(100), | ||
"null" to JsonValue.Null, | ||
"string" to JsonValue.StringValue("100.22") | ||
)))) | ||
|
||
assertThat(json, `is`(equalTo(expected))) | ||
} | ||
|
||
@Test | ||
fun testChildArrayJson() { | ||
val json = JsonBuilder.build { | ||
it["child"] = it.array { child -> | ||
child.push(100.22) | ||
child += "100.22" | ||
child.push(null) | ||
child[2] = 100 | ||
} | ||
} | ||
|
||
val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Array( | ||
mutableListOf( | ||
JsonValue.Decimal(100.22), | ||
JsonValue.StringValue("100.22"), | ||
JsonValue.Integer(100), | ||
) | ||
))) | ||
|
||
assertThat(json, `is`(equalTo(expected))) | ||
} | ||
|
||
@Test | ||
fun testChildMultiLevel() { | ||
val json = JsonBuilder.build { | ||
it["child"] = it.array { child -> | ||
child.push(child.`object` { child2 -> | ||
child2["term"] = true | ||
}) | ||
} | ||
} | ||
|
||
val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Array( | ||
mutableListOf(JsonValue.Object("term" to JsonValue.True)) | ||
))) | ||
|
||
assertThat(json, `is`(equalTo(expected))) | ||
} | ||
} |