Last active
March 24, 2019 01:56
-
-
Save yyYank/e787bdbbf0db5e46e3f96d1fe4abae24 to your computer and use it in GitHub Desktop.
こんな感じでしょうか?apply使ってるのがちょっと残念、あとインスタンスめっちゃ生成されたり
This file contains hidden or 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
| import com.jayway.jsonassert.JsonAssert | |
| import com.jayway.jsonassert.JsonAsserter | |
| import org.hamcrest.Matcher | |
| import kotlin.test.* | |
| class LibraryTest { | |
| @Test fun testHogeJson() { | |
| val jsonString = """ | |
| { | |
| "foo": "aaa", | |
| "bar": 1, | |
| "baz": true, | |
| "qux": null | |
| "quux": [ "bbb", "ccc" ], | |
| "corge": { | |
| "grault": "ddd", | |
| "garply": "eee" | |
| } | |
| } | |
| """ | |
| JsonAssertK(jsonString).apply{ | |
| jsonPath("$.foo").isEqualTo("aaa") | |
| jsonPath("$.bar").isLessThan(10) | |
| jsonPath("$.baz").isBool().isTrue() | |
| jsonPath("$.qux").isNull() | |
| jsonPath("$.quux").isArray().hasSize(2).containsExactlyInAnyOrder("bbb", "ccc") | |
| jsonPath("$.corge").isObject() | |
| jsonPath("$.corge.grault").isString().containsIgnoringCase("DD") | |
| jsonPath("$.corge.garply").isEqualTo("eee") | |
| } | |
| } | |
| } | |
| class JsonAssertK(val jsonString : String) { | |
| fun jsonPath(path : String) : JsonKAsserter { | |
| return JsonKAsserter(jsonString, path) | |
| } | |
| } | |
| class JsonKAsserter(val jsonString : String, val path : String) : JsonAsserter { | |
| fun isEqualTo(expected: String) { | |
| JsonAssert.with(jsonString).assertEquals(path, expected) | |
| } | |
| fun isLessThan(expected: Int): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isBool(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isTrue(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isFalse(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isArray(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isObject(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun hasSize(size : Int): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isString(): JsonKAsserter { | |
| TODO() | |
| } | |
| fun containsExactlyInAnyOrder(vararg any :String): JsonKAsserter { | |
| TODO() | |
| } | |
| fun containsIgnoringCase(expected : String): JsonKAsserter { | |
| TODO() | |
| } | |
| fun isNull(): JsonKAsserter { | |
| return this.assertNull(path) as JsonKAsserter | |
| } | |
| override fun <T : Any?> assertEquals(path: String?, expected: T): JsonAsserter = JsonAssert.with(jsonString).assertEquals(path, expected) | |
| override fun <T : Any?> assertThat(path: String?, matcher: Matcher<T>?, message: String?): JsonAsserter = JsonAssert.with(jsonString).assertThat(path, matcher,message) | |
| override fun assertNotDefined(path: String?): JsonAsserter = JsonAssert.with(jsonString).assertNotDefined(path) | |
| override fun assertNotDefined(path: String?, message: String?): JsonAsserter = JsonAssert.with(jsonString).assertNotDefined(path, message) | |
| override fun <T : Any?> assertEquals(path: String?, expected: T, message: String?): JsonAsserter = JsonAssert.with(jsonString).assertEquals(path, expected, message) | |
| override fun and(): JsonAsserter = JsonAssert.with(jsonString).and() | |
| override fun assertNull(path: String?): JsonAsserter = JsonAssert.with(jsonString).assertNull(path) | |
| override fun assertNull(path: String?, message: String?): JsonAsserter = JsonAssert.with(jsonString).assertNull(path, message) | |
| override fun <T : Any?> assertNotNull(path: String): JsonAsserter = JsonAssert.with(jsonString).assertNotNull<T>(path) | |
| override fun <T : Any?> assertNotNull(path: String?, message: String?): JsonAsserter = JsonAssert.with(jsonString).assertNotNull<T>(path, message) | |
| override fun <T : Any?> assertThat(path: String?, matcher: Matcher<T>?): JsonAsserter = JsonAssert.with(jsonString).assertThat(path, matcher) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment