From 2bb90ced032d7c0ae19bb7b034517431de2a0b01 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 3 Apr 2021 00:17:29 +0200 Subject: [PATCH] Refactor 'the response should contain list' --- src/test/kotlin/assert/Range.kt | 7 +++++++ src/test/kotlin/integration/Article routes.kt | 2 +- .../integration/Comment constitutions routes.kt | 2 +- src/test/kotlin/integration/Opinion routes.kt | 2 +- src/test/kotlin/integration/Workgroup routes.kt | 6 +++--- src/test/kotlin/integration/steps/then/request.kt | 15 ++++++--------- 6 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 src/test/kotlin/assert/Range.kt diff --git a/src/test/kotlin/assert/Range.kt b/src/test/kotlin/assert/Range.kt new file mode 100644 index 0000000..a388716 --- /dev/null +++ b/src/test/kotlin/assert/Range.kt @@ -0,0 +1,7 @@ +package assert + +import kotlin.test.assertTrue + +infix fun IntProgression.assertContain(expected: Int) { + assertTrue(this.contains(expected), "Expected $this less than $expected") +} diff --git a/src/test/kotlin/integration/Article routes.kt b/src/test/kotlin/integration/Article routes.kt index 661e4f9..0ed6685 100644 --- a/src/test/kotlin/integration/Article routes.kt +++ b/src/test/kotlin/integration/Article routes.kt @@ -38,7 +38,7 @@ class `Article routes` : BaseTest() { `And the response should contain pattern`("$.result[1].createdBy.name.firstName", "firstName.+") `And the response should contain pattern`("$.result[2].createdBy.name.firstName", "firstName.+") `And the response should not contain`("$.result[3]") - `And the response should contain list`("$.result", 3, 3) + `And the response should contain list`("$.result", 3) } } } diff --git a/src/test/kotlin/integration/Comment constitutions routes.kt b/src/test/kotlin/integration/Comment constitutions routes.kt index f887ce1..a999082 100644 --- a/src/test/kotlin/integration/Comment constitutions routes.kt +++ b/src/test/kotlin/integration/Comment constitutions routes.kt @@ -57,7 +57,7 @@ class `Comment constitutions routes` : BaseTest() { `And the response should contain`("$.limit", 50) `And the response should contain`("$.result[0].createdBy.id", "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5") `And the response should contain`("$.result[0].target.id", "34ddd50a-da00-4a90-a869-08baa2a121be") - `And the response should contain list`("$.result[*]", 1, 1) + `And the response should contain list`("$.result[*]", 1) } } } diff --git a/src/test/kotlin/integration/Opinion routes.kt b/src/test/kotlin/integration/Opinion routes.kt index ec8c7dd..4613372 100644 --- a/src/test/kotlin/integration/Opinion routes.kt +++ b/src/test/kotlin/integration/Opinion routes.kt @@ -137,7 +137,7 @@ class `Opinion routes` : BaseTest() { `authenticated as`("Albert", "Einstein") } `Then the response should be` OK and { `And the response should contain`("$.result[0].name", "Opinion9") - `And the response should contain list`("$.result[*]", 1, 1) + `And the response should contain list`("$.result[*]", 1) } } } diff --git a/src/test/kotlin/integration/Workgroup routes.kt b/src/test/kotlin/integration/Workgroup routes.kt index 0df0a7f..32b69d7 100644 --- a/src/test/kotlin/integration/Workgroup routes.kt +++ b/src/test/kotlin/integration/Workgroup routes.kt @@ -119,7 +119,7 @@ class `Workgroup routes` : BaseTest() { `And the response should contain`("$.description", "Une petite souris") `And have property`("$.members") - `And the response should contain list`("$.members", 3, 3) + `And the response should contain list`("$.members", 3) `And the response should contain`("$.members.[1]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9") `And the response should contain`("$.members.[2]citizen.id", "87909ba3-2069-431c-9924-219fd8411cf2") } @@ -215,7 +215,7 @@ class `Workgroup routes` : BaseTest() { ] """ } `Then the response should be` OK and { - `And the response should contain list`("$", 2, 2) + `And the response should contain list`("$", 2) `And the response should contain`("$.[0]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9") `And the response should contain`("$.[1]citizen.id", "1baf48bb-02bc-4d8f-ac86-33335354f5e7") } @@ -252,7 +252,7 @@ class `Workgroup routes` : BaseTest() { """ ) } `Then the response should be` OK and { - `And the response should contain list`("$", 2, 2) + `And the response should contain list`("$", 2) `And the response should contain`("$.[0]citizen.id", "be3b0926-8628-4426-804a-75188a6eb315") `And the response should contain`("$.[1]citizen.id", "b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1") } diff --git a/src/test/kotlin/integration/steps/then/request.kt b/src/test/kotlin/integration/steps/then/request.kt index c4ed68a..cc5156d 100644 --- a/src/test/kotlin/integration/steps/then/request.kt +++ b/src/test/kotlin/integration/steps/then/request.kt @@ -1,7 +1,6 @@ package integration.steps.then -import assert.assertGreaterThan -import assert.assertLessThan +import assert.assertContain import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.PathNotFoundException import io.ktor.http.HttpStatusCode @@ -85,15 +84,13 @@ fun TestApplicationResponse.`And the response should contain pattern`(path: Stri } } -fun TestApplicationResponse.`And the response should contain list`(path: String, min: Int? = null, max: Int? = null) { +fun TestApplicationResponse.`And the response should contain list`(path: String, exactCount: Int) = + `And the response should contain list`(path, IntRange(exactCount, exactCount)) + +fun TestApplicationResponse.`And the response should contain list`(path: String, range: IntRange) { JsonPath.read(content, path).also { assertNotNull(it) - if (min != null) { - it.size assertGreaterThan min - } - if (max != null) { - it.size assertLessThan max - } + range assertContain it.size } }