forked from uzabase/playtest
-
Notifications
You must be signed in to change notification settings - Fork 0
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
e695dc1
commit b1d83e2
Showing
8 changed files
with
82 additions
and
15 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
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
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
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 |
---|---|---|
@@ -1,14 +1,6 @@ | ||
package com.uzabase | ||
|
||
import com.nfeld.jsonpathkt.JsonPath | ||
import com.nfeld.jsonpathkt.extension.read | ||
|
||
interface JsonReader { | ||
fun getByJsonPath(json: String, path: String): String | ||
fun getStringByJsonPath(json: String, path: String): String | ||
fun getBooleanByJsonPath(json: String, path: String): Boolean | ||
} | ||
|
||
class JsonReaderImpl : JsonReader { | ||
override fun getByJsonPath(json: String, path: String): String { | ||
return JsonPath.parse(json)?.read(path)!! | ||
} | ||
} |
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,19 @@ | ||
package com.uzabase | ||
|
||
import com.nfeld.jsonpathkt.JsonPath | ||
import com.nfeld.jsonpathkt.extension.read | ||
|
||
|
||
class JsonReaderImpl : JsonReader { | ||
override fun getStringByJsonPath(json: String, path: String): String { | ||
return getValueByJsonPath(json, path) | ||
} | ||
|
||
override fun getBooleanByJsonPath(json: String, path: String): Boolean { | ||
return getValueByJsonPath(json, path) | ||
} | ||
|
||
inline fun <reified T : Any> getValueByJsonPath(json: String, path: String): T { | ||
return JsonPath.parse(json)?.read(path) ?: throw NotFoundJsonValueException(json, path) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
json-assert/src/main/kotlin/com/uzabase/NotFoundJsonValueException.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,6 @@ | ||
package com.uzabase | ||
|
||
data class NotFoundJsonValueException( | ||
val json: String, | ||
private val jsonPath: String | ||
): Exception("Not found value in $json by: $jsonPath") |
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 |
---|---|---|
@@ -1,13 +1,34 @@ | ||
package com.uzabase | ||
|
||
import org.amshove.kluent.AnyException | ||
import org.amshove.kluent.internal.ComparisonFailedException | ||
import org.amshove.kluent.invoking | ||
import org.amshove.kluent.shouldNotThrow | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
internal class JsonAssertTest : JsonAssert { | ||
@Test | ||
fun JSONPathを指定して文字列をアサーションできる() { | ||
fun 指定したJSONPathの値が期待した文字列の場合アサートの例外をスローしない() { | ||
val json = """{ "test": "test" }""" | ||
invoking { json.assertByJsonPath("test", "$.test") } shouldNotThrow AnyException | ||
} | ||
|
||
@Test | ||
fun 指定したJSONPathの値が期待した文字列の場合アサートの例外をスローする() { | ||
val json = """{ "test": "test" }""" | ||
assertThrows<ComparisonFailedException> { json.assertByJsonPath("xxxx", "$.test") } | ||
} | ||
|
||
@Test | ||
fun 指定したJSONPathの値が期待した真偽値の場合アサートの例外をスローしない() { | ||
val json = """{ "test": true }""" | ||
invoking { json.assertByJsonPath(true, "$.test") } shouldNotThrow AnyException | ||
} | ||
|
||
@Test | ||
fun 指定したJSONPathの値が期待していない真偽値の場合アサートの例外をスローする() { | ||
val json = """{ "test": false }""" | ||
assertThrows<ComparisonFailedException> { json.assertByJsonPath(true, "$.test") } | ||
} | ||
} |
15 changes: 14 additions & 1 deletion
15
json-assert/src/test/kotlin/com/uzabase/JsonReaderImplTest.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 |
---|---|---|
@@ -1,13 +1,26 @@ | ||
package com.uzabase | ||
|
||
import org.amshove.kluent.invoking | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.amshove.kluent.shouldThrow | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class JsonReaderImplTest { | ||
|
||
@Test | ||
fun JSONPathで指定したKeyの値が存在しないとき例外をスローする() { | ||
invoking { JsonReaderImpl().getValueByJsonPath<String>("""{"test": "test"}""", "$.xxx") } shouldThrow NotFoundJsonValueException::class | ||
} | ||
|
||
@Test | ||
fun JSONPathで指定したKeyの文字列を取得する() { | ||
val actual = JsonReaderImpl().getByJsonPath("""{"test": "test"}""", "$.test") | ||
val actual = JsonReaderImpl().getStringByJsonPath("""{"test": "test"}""", "$.test") | ||
"test" shouldBeEqualTo actual | ||
} | ||
|
||
@Test | ||
fun JSONPathで指定したKeyの真偽値を取得する() { | ||
val actual = JsonReaderImpl().getBooleanByJsonPath("""{"test": true}""", "$.test") | ||
true shouldBeEqualTo actual | ||
} | ||
} |