Best Kotest code snippet using io.kotest.matchers.longs.between.between
BasicEventTest.kt
Source:BasicEventTest.kt
1package dev.akkinoc.spring.boot.logback.access2import dev.akkinoc.spring.boot.logback.access.test.assertion.Assertions.assertLogbackAccessEventsEventually3import dev.akkinoc.spring.boot.logback.access.test.extension.EventsCapture4import dev.akkinoc.spring.boot.logback.access.test.extension.EventsCaptureExtension5import dev.akkinoc.spring.boot.logback.access.test.type.JettyReactiveWebTest6import dev.akkinoc.spring.boot.logback.access.test.type.JettyServletWebTest7import dev.akkinoc.spring.boot.logback.access.test.type.TomcatReactiveWebTest8import dev.akkinoc.spring.boot.logback.access.test.type.TomcatServletWebTest9import dev.akkinoc.spring.boot.logback.access.test.type.UndertowReactiveWebTest10import dev.akkinoc.spring.boot.logback.access.test.type.UndertowServletWebTest11import io.kotest.assertions.throwables.shouldThrowUnit12import io.kotest.matchers.booleans.shouldBeFalse13import io.kotest.matchers.booleans.shouldBeTrue14import io.kotest.matchers.collections.shouldBeSingleton15import io.kotest.matchers.collections.shouldContainAll16import io.kotest.matchers.collections.shouldContainExactly17import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder18import io.kotest.matchers.collections.shouldNotContainAnyOf19import io.kotest.matchers.longs.shouldBeBetween20import io.kotest.matchers.longs.shouldBeGreaterThanOrEqual21import io.kotest.matchers.longs.shouldBePositive22import io.kotest.matchers.longs.shouldBeZero23import io.kotest.matchers.maps.shouldBeEmpty24import io.kotest.matchers.nulls.shouldBeNull25import io.kotest.matchers.nulls.shouldNotBeNull26import io.kotest.matchers.shouldBe27import io.kotest.matchers.shouldNotBe28import io.kotest.matchers.string.shouldBeEmpty29import io.kotest.matchers.string.shouldNotBeEmpty30import io.kotest.matchers.string.shouldStartWith31import org.junit.jupiter.api.Test32import org.junit.jupiter.api.extension.ExtendWith33import org.springframework.beans.factory.annotation.Autowired34import org.springframework.boot.test.web.client.TestRestTemplate35import org.springframework.boot.test.web.client.exchange36import org.springframework.boot.web.server.LocalServerPort37import org.springframework.http.RequestEntity38import org.springframework.test.context.TestPropertySource39import java.lang.System.currentTimeMillis40import java.util.concurrent.TimeUnit.MILLISECONDS41/**42 * Tests the appended Logback-access event in the case where the configuration is the default.43 *44 * @property supportsRequestParametersByFormData Whether to support request parameters by form data.45 * @property supportsRequestAttributes Whether to support request attributes.46 * @property supportsSessionIDs Whether to support session IDs.47 * @property canForwardRequests Whether the web server can forward requests.48 */49@ExtendWith(EventsCaptureExtension::class)50@TestPropertySource(properties = ["logback.access.config=classpath:logback-access-test.capture.xml"])51sealed class BasicEventTest(52 private val supportsRequestParametersByFormData: Boolean,53 private val supportsRequestAttributes: Boolean,54 private val supportsSessionIDs: Boolean,55 private val canForwardRequests: Boolean,56) {57 @Test58 fun `Appends a Logback-access event`(59 @Autowired rest: TestRestTemplate,60 @LocalServerPort port: Int,61 capture: EventsCapture,62 ) {63 val request = RequestEntity.get("/mock-controller/text").build()64 val started = currentTimeMillis()65 val response = rest.exchange<String>(request)66 response.statusCodeValue.shouldBe(200)67 response.body.shouldBe("mock-text")68 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }69 val finished = currentTimeMillis()70 event.request.shouldBeNull()71 event.response.shouldBeNull()72 event.serverAdapter.shouldBeNull()73 event.timeStamp.shouldBeBetween(started, finished)74 event.elapsedTime.shouldBeBetween(0L, finished - started)75 event.elapsedSeconds.shouldBeBetween(0L, MILLISECONDS.toSeconds(finished - started))76 event.threadName.shouldNotBeEmpty()77 shouldThrowUnit<UnsupportedOperationException> { event.threadName = "changed-thread-name" }78 event.serverName.shouldBe("localhost")79 event.localPort.shouldBe(port)80 event.remoteAddr.shouldBe("127.0.0.1")81 event.remoteHost.shouldBe("127.0.0.1")82 event.remoteUser.shouldBe("-")83 event.protocol.shouldBe("HTTP/1.1")84 event.method.shouldBe("GET")85 event.requestURI.shouldBe("/mock-controller/text")86 event.queryString.shouldBeEmpty()87 event.requestURL.shouldBe("GET /mock-controller/text HTTP/1.1")88 event.requestHeaderMap.shouldNotBeNull()89 event.requestHeaderNames.shouldNotBeNull()90 event.getRequestHeader("x").shouldBe("-")91 event.getCookie("x").shouldBe("-")92 event.requestParameterMap.shouldBeEmpty()93 event.getRequestParameter("x").shouldContainExactly("-")94 event.getAttribute("x").shouldBe("-")95 event.sessionID.shouldBe("-")96 event.requestContent.shouldBeEmpty()97 event.statusCode.shouldBe(200)98 event.responseHeaderMap.shouldNotBeNull()99 event.responseHeaderNameList.shouldNotBeNull()100 event.getResponseHeader("x").shouldBe("-")101 event.contentLength.shouldBe(9L)102 event.responseContent.shouldBeEmpty()103 }104 @Test105 fun `Appends a Logback-access event with request headers`(106 @Autowired rest: TestRestTemplate,107 capture: EventsCapture,108 ) {109 val request = RequestEntity.get("/mock-controller/text")110 .header("a", "value @a")111 .header("b", "value1 @b", "value2 @b")112 .header("c", "")113 .build()114 val response = rest.exchange<String>(request)115 response.statusCodeValue.shouldBe(200)116 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }117 event.requestHeaderMap["a"].shouldBe("value @a")118 event.requestHeaderMap["b"].shouldBe("value1 @b")119 event.requestHeaderMap["c"].shouldBeEmpty()120 event.requestHeaderNames.toList().shouldContainAll("a", "b", "c")121 event.requestHeaderNames.toList().shouldNotContainAnyOf("A", "B", "C")122 event.getRequestHeader("a").shouldBe("value @a")123 event.getRequestHeader("A").shouldBe("value @a")124 event.getRequestHeader("b").shouldBe("value1 @b")125 event.getRequestHeader("B").shouldBe("value1 @b")126 event.getRequestHeader("c").shouldBeEmpty()127 event.getRequestHeader("C").shouldBeEmpty()128 }129 @Test130 fun `Appends a Logback-access event with request cookies`(131 @Autowired rest: TestRestTemplate,132 capture: EventsCapture,133 ) {134 val request = RequestEntity.get("/mock-controller/text")135 .header("cookie", "a=value+%40a; b=")136 .build()137 val response = rest.exchange<String>(request)138 response.statusCodeValue.shouldBe(200)139 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }140 event.getCookie("a").shouldBe("value+%40a")141 event.getCookie("b").shouldBeEmpty()142 }143 @Test144 fun `Appends a Logback-access event with request parameters by query string`(145 @Autowired rest: TestRestTemplate,146 capture: EventsCapture,147 ) {148 val request = RequestEntity.get("/mock-controller/text?a=value+@a&b=value1+@b&b=value2+@b&c=").build()149 val response = rest.exchange<String>(request)150 response.statusCodeValue.shouldBe(200)151 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }152 event.method.shouldBe("GET")153 event.requestURI.shouldBe("/mock-controller/text")154 event.queryString.shouldBe("?a=value+@a&b=value1+@b&b=value2+@b&c=")155 event.requestURL.shouldBe("GET /mock-controller/text?a=value+@a&b=value1+@b&b=value2+@b&c= HTTP/1.1")156 event.requestParameterMap.keys.shouldContainExactlyInAnyOrder("a", "b", "c")157 event.requestParameterMap["a"].shouldContainExactly("value @a")158 event.requestParameterMap["b"].shouldContainExactly("value1 @b", "value2 @b")159 event.requestParameterMap["c"].shouldContainExactly("")160 event.getRequestParameter("a").shouldContainExactly("value @a")161 event.getRequestParameter("b").shouldContainExactly("value1 @b", "value2 @b")162 event.getRequestParameter("c").shouldContainExactly("")163 }164 @Test165 fun `Appends a Logback-access event with request parameters by form data`(166 @Autowired rest: TestRestTemplate,167 capture: EventsCapture,168 ) {169 val request = RequestEntity.post("/mock-controller/form-data")170 .header("content-type", "application/x-www-form-urlencoded")171 .body("a=value+%40a&b=value1+%40b&b=value2+%40b&c=")172 val response = rest.exchange<String>(request)173 response.statusCodeValue.shouldBe(200)174 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }175 event.method.shouldBe("POST")176 event.requestURI.shouldBe("/mock-controller/form-data")177 event.queryString.shouldBeEmpty()178 event.requestURL.shouldBe("POST /mock-controller/form-data HTTP/1.1")179 if (supportsRequestParametersByFormData) {180 event.requestParameterMap.keys.shouldContainExactlyInAnyOrder("a", "b", "c")181 event.requestParameterMap["a"].shouldContainExactly("value @a")182 event.requestParameterMap["b"].shouldContainExactly("value1 @b", "value2 @b")183 event.requestParameterMap["c"].shouldContainExactly("")184 event.getRequestParameter("a").shouldContainExactly("value @a")185 event.getRequestParameter("b").shouldContainExactly("value1 @b", "value2 @b")186 event.getRequestParameter("c").shouldContainExactly("")187 } else {188 event.requestParameterMap.shouldBeEmpty()189 event.getRequestParameter("a").shouldContainExactly("-")190 event.getRequestParameter("b").shouldContainExactly("-")191 event.getRequestParameter("c").shouldContainExactly("-")192 }193 }194 @Test195 fun `Appends a Logback-access event with request attributes`(196 @Autowired rest: TestRestTemplate,197 capture: EventsCapture,198 ) {199 val request = RequestEntity.get("/mock-controller/text-with-request-attributes").build()200 val response = rest.exchange<String>(request)201 response.statusCodeValue.shouldBe(200)202 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }203 if (supportsRequestAttributes) {204 event.getAttribute("a").shouldBe("value @a")205 event.getAttribute("b").shouldBe("[value1 @b, value2 @b]")206 event.getAttribute("c").shouldBe("")207 event.getAttribute("d").shouldStartWith("java.lang.Object@")208 } else {209 event.getAttribute("a").shouldBe("-")210 event.getAttribute("b").shouldBe("-")211 event.getAttribute("c").shouldBe("-")212 event.getAttribute("d").shouldBe("-")213 }214 }215 @Test216 fun `Appends a Logback-access event with a session`(217 @Autowired rest: TestRestTemplate,218 capture: EventsCapture,219 ) {220 val request = RequestEntity.get("/mock-controller/text-with-session").build()221 val response = rest.exchange<String>(request)222 response.statusCodeValue.shouldBe(200)223 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }224 if (supportsSessionIDs) event.sessionID.shouldNotBeEmpty().shouldNotBe("-")225 else event.sessionID.shouldBe("-")226 }227 @Test228 fun `Appends a Logback-access event with response headers`(229 @Autowired rest: TestRestTemplate,230 capture: EventsCapture,231 ) {232 val request = RequestEntity.get("/mock-controller/text-with-response-headers").build()233 val response = rest.exchange<String>(request)234 response.statusCodeValue.shouldBe(200)235 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }236 event.responseHeaderMap["a"].shouldBe("value @a")237 event.responseHeaderMap["b"].shouldBe("value1 @b")238 event.responseHeaderMap["c"].shouldBeEmpty()239 event.responseHeaderNameList.shouldContainAll("a", "b", "c")240 event.responseHeaderNameList.shouldNotContainAnyOf("A", "B", "C")241 event.getResponseHeader("a").shouldBe("value @a")242 event.getResponseHeader("A").shouldBe("value @a")243 event.getResponseHeader("b").shouldBe("value1 @b")244 event.getResponseHeader("B").shouldBe("value1 @b")245 event.getResponseHeader("c").shouldBeEmpty()246 event.getResponseHeader("C").shouldBeEmpty()247 }248 @Test249 fun `Appends a Logback-access event with an empty response`(250 @Autowired rest: TestRestTemplate,251 capture: EventsCapture,252 ) {253 val request = RequestEntity.get("/mock-controller/empty-text").build()254 val response = rest.exchange<String>(request)255 response.statusCodeValue.shouldBe(200)256 response.hasBody().shouldBeFalse()257 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }258 event.contentLength.shouldBeZero()259 }260 @Test261 fun `Appends a Logback-access event with an asynchronous response`(262 @Autowired rest: TestRestTemplate,263 capture: EventsCapture,264 ) {265 val request = RequestEntity.get("/mock-controller/text-asynchronously").build()266 val response = rest.exchange<String>(request)267 response.statusCodeValue.shouldBe(200)268 response.body.shouldBe("mock-text")269 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }270 event.threadName.shouldNotBeEmpty()271 event.contentLength.shouldBe(9L)272 }273 @Test274 fun `Appends a Logback-access event with a chunked response`(275 @Autowired rest: TestRestTemplate,276 capture: EventsCapture,277 ) {278 val request = RequestEntity.get("/mock-controller/text-with-chunked-transfer-encoding").build()279 val response = rest.exchange<String>(request)280 response.statusCodeValue.shouldBe(200)281 response.headers["transfer-encoding"].shouldBe(listOf("chunked"))282 response.headers["content-length"].shouldBeNull()283 response.body.shouldBe("mock-text")284 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }285 event.contentLength.shouldBeGreaterThanOrEqual(9L)286 }287 @Test288 fun `Appends a Logback-access event with a forward response`(289 @Autowired rest: TestRestTemplate,290 capture: EventsCapture,291 ) {292 if (!canForwardRequests) return293 val request = RequestEntity.get("/mock-controller/text-with-forward?a=value+@a").build()294 val response = rest.exchange<String>(request)295 response.statusCodeValue.shouldBe(200)296 response.body.shouldBe("mock-text")297 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }298 event.protocol.shouldBe("HTTP/1.1")299 event.method.shouldBe("GET")300 event.requestURI.shouldBe("/mock-controller/text-with-forward")301 event.queryString.shouldBe("?a=value+@a")302 event.requestURL.shouldBe("GET /mock-controller/text-with-forward?a=value+@a HTTP/1.1")303 event.statusCode.shouldBe(200)304 event.contentLength.shouldBe(9L)305 }306 @Test307 fun `Appends a Logback-access event with an error response`(308 @Autowired rest: TestRestTemplate,309 capture: EventsCapture,310 ) {311 val request = RequestEntity.get("/mock-controller/unknown?a=value+@a").build()312 val response = rest.exchange<String>(request)313 response.statusCodeValue.shouldBe(404)314 response.hasBody().shouldBeTrue()315 val event = assertLogbackAccessEventsEventually { capture.shouldBeSingleton().single() }316 event.protocol.shouldBe("HTTP/1.1")317 event.method.shouldBe("GET")318 event.requestURI.shouldBe("/mock-controller/unknown")319 event.queryString.shouldBe("?a=value+@a")320 event.requestURL.shouldBe("GET /mock-controller/unknown?a=value+@a HTTP/1.1")321 event.statusCode.shouldBe(404)322 event.contentLength.shouldBePositive()323 }324}325/**326 * Tests the [BasicEventTest] using the Tomcat servlet web server.327 */328@TomcatServletWebTest329class TomcatServletWebBasicEventTest : BasicEventTest(330 supportsRequestParametersByFormData = true,331 supportsRequestAttributes = true,332 supportsSessionIDs = true,333 canForwardRequests = true,334)335/**336 * Tests the [BasicEventTest] using the Tomcat reactive web server.337 */338@TomcatReactiveWebTest339class TomcatReactiveWebBasicEventTest : BasicEventTest(340 supportsRequestParametersByFormData = false,341 supportsRequestAttributes = false,342 supportsSessionIDs = false,343 canForwardRequests = false,344)345/**346 * Tests the [BasicEventTest] using the Jetty servlet web server.347 */348@JettyServletWebTest349class JettyServletWebBasicEventTest : BasicEventTest(350 supportsRequestParametersByFormData = true,351 supportsRequestAttributes = true,352 supportsSessionIDs = true,353 canForwardRequests = true,354)355/**356 * Tests the [BasicEventTest] using the Jetty reactive web server.357 */358@JettyReactiveWebTest359class JettyReactiveWebBasicEventTest : BasicEventTest(360 supportsRequestParametersByFormData = false,361 supportsRequestAttributes = false,362 supportsSessionIDs = false,363 canForwardRequests = false,364)365/**366 * Tests the [BasicEventTest] using the Undertow servlet web server.367 */368@UndertowServletWebTest369class UndertowServletWebBasicEventTest : BasicEventTest(370 supportsRequestParametersByFormData = true,371 supportsRequestAttributes = true,372 supportsSessionIDs = true,373 canForwardRequests = true,374)375/**376 * Tests the [BasicEventTest] using the Undertow reactive web server.377 */378@UndertowReactiveWebTest379class UndertowReactiveWebBasicEventTest : BasicEventTest(380 supportsRequestParametersByFormData = false,381 supportsRequestAttributes = false,382 supportsSessionIDs = false,383 canForwardRequests = false,384)...
GooglePlayReviewRepositoryTest.kt
Source:GooglePlayReviewRepositoryTest.kt
1package io.appvox.googleplay.review2import io.appvox.core.configuration.RequestConfiguration3import io.appvox.googleplay.BaseGooglePlayMockTest4import io.appvox.googleplay.review.GooglePlayReviewRepository.Companion.REQUEST_URL_DOMAIN5import io.appvox.googleplay.review.GooglePlayReviewRepository.Companion.REQUEST_URL_PATH6import io.appvox.googleplay.review.constant.GooglePlayLanguage7import io.appvox.googleplay.review.constant.GooglePlaySortType8import io.appvox.googleplay.review.domain.GooglePlayReviewRequestParameters9import io.appvox.core.review.ReviewRequest10import io.kotest.assertions.assertSoftly11import io.kotest.inspectors.forExactly12import io.kotest.matchers.ints.shouldBeBetween13import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual14import io.kotest.matchers.longs.shouldBeBetween15import io.kotest.matchers.string.shouldContain16import io.kotest.matchers.string.shouldNotBeEmpty17import io.kotest.matchers.string.shouldStartWith18import kotlinx.coroutines.ExperimentalCoroutinesApi19import org.junit.jupiter.params.ParameterizedTest20import org.junit.jupiter.params.provider.CsvSource21class GooglePlayReviewRepositoryTest : BaseGooglePlayMockTest() {22 private val repository = GooglePlayReviewRepository(RequestConfiguration(delay = 3000))23 @ExperimentalCoroutinesApi24 @ParameterizedTest25 @CsvSource(26 "com.twitter.android, en, 1, 40, 40"27 )28 fun `get Google Play reviews`(29 appId: String,30 language: String,31 sortType: Int,32 batchSize: Int,33 expectedReviewCount: Int34 ) {35 REQUEST_URL_DOMAIN = httpMockServerDomain36 val mockData = javaClass.getResource(37 "/review/com.twitter.android/relevant" +38 "/review_google_play_com.twitter.android_relevant_1.json"39 ).readText()40 stubHttpUrl(REQUEST_URL_PATH, mockData)41 val request = GooglePlayReviewRequestParameters(42 appId = appId,43 language = GooglePlayLanguage.fromValue(language),44 sortType = GooglePlaySortType.fromValue(sortType),45 batchSize = batchSize,46 fetchHistory = false47 )48 val response = repository.getReviewsByAppId(ReviewRequest(request))49 response.results?.forExactly(expectedReviewCount) { result ->50 assertSoftly(result) {51 reviewId shouldStartWith "gp:"52 userName.shouldNotBeEmpty()53 userProfilePicUrl shouldStartWith "https://play-lh.googleusercontent.com/"54 rating.shouldBeBetween(1, 5)55 userCommentText.shouldNotBeEmpty()56 userCommentTime.shouldBeBetween(0, Long.MAX_VALUE)57 likeCount.shouldBeGreaterThanOrEqual(0)58 reviewUrl shouldContain result.reviewId59 developerCommentText?.let { it.shouldNotBeEmpty() }60 developerCommentTime?.shouldBeBetween(0, Long.MAX_VALUE)61 }62 }63 response.nextToken.shouldNotBeEmpty()64 }65 @ExperimentalCoroutinesApi66 @ParameterizedTest67 @CsvSource(68 "com.twitter.android, " +69 "gp:AOqpTOFeIsixb5qcUUUvJLSz_JudDjdKvngeHbfbUNGh7ch4H3KYh6NFVObMQkdes5HXbLkp3x5iyEiyRsTpuw, " +70 "en, " +71 "40, " +72 "2"73 )74 fun `get Google Play review history`(75 appId: String,76 reviewId: String,77 language: String,78 batchSize: Int,79 expectedReviewCount: Int80 ) {81 REQUEST_URL_DOMAIN = httpMockServerDomain82 val mockData = javaClass.getResource(83 "/review/com.twitter.android/history" +84 "/review_google_play_com.twitter.android_history.json"85 ).readText()86 stubHttpUrl(REQUEST_URL_PATH, mockData)87 val request = GooglePlayReviewRequestParameters(88 appId = appId,89 language = GooglePlayLanguage.fromValue(language),90 // Sort Type is not used when fetching a review's history91 sortType = GooglePlaySortType.RECENT,92 batchSize = batchSize,93 fetchHistory = true94 )95 val response = repository.getReviewHistoryById(reviewId, ReviewRequest(request))96 response.forExactly(expectedReviewCount) { result ->97 assertSoftly(result) {98 reviewId shouldStartWith "gp:"99 userName.shouldNotBeEmpty()100 userProfilePicUrl shouldStartWith "https://play-lh.googleusercontent.com/"101 rating.shouldBeBetween(1, 5)102 userCommentText.shouldNotBeEmpty()103 userCommentTime.shouldBeBetween(0, Long.MAX_VALUE)104 likeCount.shouldBeGreaterThanOrEqual(0)105 reviewUrl shouldContain result.reviewId106 }107 }108 }109}...
StampsAndCoinsGeneratorKtTest.kt
Source:StampsAndCoinsGeneratorKtTest.kt
...20 main(arrayOf())21 }22 }23 "getRandomDimMM" should {24 "Get dim between considered range" {25 getRandomDimMM(1, 10) shouldBeInRange (LongRange(1, 10))26 }27 }28 "getRandomDiameterMM" should {29 getRandomDiameterMM(1, 10) shouldBeInRange (LongRange(1, 10))30 }31 "getRandomCurrency" should {32 getRandomCurrency() shouldBeIn Currency.values()33 }34 "getRandomValueUpTo" should {35 getRandomValueUpTo(10) shouldBeInRange LongRange(1, 10)36 }37 "getRandomYearFrom" should {38 getRandomYearFrom(1900) shouldBeGreaterThanOrEqual 1900...
LongMatchersTest.kt
Source:LongMatchersTest.kt
...3import io.kotest.matchers.comparables.beGreaterThan4import io.kotest.matchers.comparables.beGreaterThanOrEqualTo5import io.kotest.matchers.comparables.beLessThan6import io.kotest.matchers.comparables.beLessThanOrEqualTo7import io.kotest.matchers.longs.between8import io.kotest.matchers.longs.shouldBeNegative9import io.kotest.matchers.longs.shouldBePositive10import io.kotest.matchers.longs.shouldBeZero11import io.kotest.matchers.longs.shouldNotBeZero12import io.kotest.core.spec.style.StringSpec13import io.kotest.matchers.should14import io.kotest.matchers.shouldBe15import io.kotest.data.forAll16import io.kotest.data.forNone17import io.kotest.data.headers18import io.kotest.data.row19import io.kotest.data.table20class LongMatchersTest : StringSpec() {21 init {22 "be positive" {23 1L.shouldBePositive()24 shouldThrow<AssertionError> {25 (-1L).shouldBePositive()26 }.message shouldBe "-1 should be > 0"27 shouldThrow<AssertionError> {28 (0L).shouldBePositive()29 }.message shouldBe "0 should be > 0"30 }31 "be negative" {32 (-1L).shouldBeNegative()33 shouldThrow<AssertionError> {34 1L.shouldBeNegative()35 }.message shouldBe "1 should be < 0"36 shouldThrow<AssertionError> {37 0L.shouldBeNegative()38 }.message shouldBe "0 should be < 0"39 }40 "Ge should be valid" {41 1L should beGreaterThan(0L)42 }43 "beGreaterThan" {44 1L should beGreaterThan(0L)45 shouldThrow<AssertionError> {46 2L should beGreaterThan(3L)47 }48 }49 "beLessThan" {50 1L should beLessThan(2L)51 shouldThrow<AssertionError> {52 2L should beLessThan(1L)53 }54 }55 "beLessThanOrEqualTo" {56 1L should beLessThanOrEqualTo(2L)57 shouldThrow<AssertionError> {58 2L should beLessThanOrEqualTo(1L)59 }60 }61 "greaterThan" {62 1L should beGreaterThanOrEqualTo(0L)63 shouldThrow<AssertionError> {64 2L should beGreaterThanOrEqualTo(3L)65 }66 }67 "between should test for valid interval" {68 val table = table(69 headers("a", "b"),70 row(0L, 2L),71 row(1L, 2L),72 row(0L, 1L),73 row(1L, 1L)74 )75 forAll(table) { a, b ->76 1 shouldBe between(a, b)77 }78 }79 "between should test for invalid interval" {80 val table = table(81 headers("a", "b"),82 row(0L, 2L),83 row(2L, 2L),84 row(4L, 5L),85 row(4L, 6L)86 )87 forNone(table) { a, b ->88 3 shouldBe between(a, b)89 }90 }91 "shouldBeZero" {92 (0L).shouldBeZero()93 (1L).shouldNotBeZero()94 Long.MIN_VALUE.shouldNotBeZero()95 Long.MAX_VALUE.shouldNotBeZero()96 }97 }98}...
FlowTest.kt
Source:FlowTest.kt
...53 .retry(Schedule.recurs<Throwable>(2) and Schedule.spaced(delayMs.milliseconds))54 .collect()55 }56 timestamps.size shouldBe 357 // total run should be between start time + delay * 3 AND start + tolerance %58 val min = start + (delayMs * 2)59 val max = min + delayMs / 1060 timestamps.last() shouldBeGreaterThanOrEqual min61 timestamps.last() shouldBeLessThan max62 }63 }64 }65 }66)...
RepeatWhileTest.kt
Source:RepeatWhileTest.kt
1//package com.sksamuel.tabby.effects2//3//import io.kotest.core.spec.style.FunSpec4//import io.kotest.matchers.longs.shouldBeBetween5//import io.kotest.matchers.shouldBe6//import java.util.concurrent.atomic.AtomicInteger7//import kotlin.time.measureTime8//import kotlin.time.milliseconds9//10//class RepeatWhileTest : FunSpec() {11// init {12//13// test("IO.repeatWhile(schedule, predicate) should contine until predicate is false") {14// var counter = 015// IO { counter++ }.repeatWhile(Schedule.forever) { counter < 10 }.run()16// counter shouldBe 1017// }18//19// test("IO.repeatWhile(schedule, predicate) should contine until schedule expires") {20// val counter = AtomicInteger(0)21// IO { counter.incrementAndGet() }.repeatWhile(Schedule.iterations(5)) { counter.get() < 10 }.run()22// counter.get() shouldBe 623// }24//25// test("IO.repeatWhile(f) should contine while function returns a predicate") {26// measureTime {27// val counter = AtomicInteger(0)28// IO { counter.incrementAndGet() }.repeatWhile { if (it.index < 10) Schedule.delay(25.milliseconds) else Schedule.never }.run()29// counter.get() shouldBe 1130// }.toLongMilliseconds().shouldBeBetween(250, 325)31// }32// }33//}...
RetryTest.kt
Source:RetryTest.kt
1package com.sksamuel.tabby.effects2import com.sksamuel.tabby.results.exceptionOrThrow3import com.sksamuel.tabby.results.failure4import com.sksamuel.tabby.results.success5import io.kotest.core.spec.style.FunSpec6import io.kotest.matchers.longs.shouldBeBetween7import io.kotest.matchers.shouldBe8import kotlin.time.Duration.Companion.milliseconds9import kotlin.time.ExperimentalTime10import kotlin.time.measureTime11@OptIn(ExperimentalTime::class)12class RetryTest : FunSpec() {13 init {14 test("retry should invoke up to Schedule.iterations") {15 var iterations = 016 retry(Schedule.iterations(5)) {17 iterations++18 "boom".failure()19 }.exceptionOrThrow().message shouldBe "boom"20 iterations shouldBe 621 }22 test("retry should return once successful") {23 var iterations = 024 retry(Schedule.forever) {25 iterations++26 if (iterations == 5) "foo".success() else "boom".failure()27 }.getOrThrow() shouldBe "foo"28 iterations shouldBe 529 }30 test("retry with delayed schedule") {31 measureTime {32 retry(Schedule.iterations(3).delay(100.milliseconds)) { "boom".failure() }33 }.inWholeMilliseconds.shouldBeBetween(300, 400)34 }35 }36}...
ScheduleNTest.kt
Source:ScheduleNTest.kt
1package com.sksamuel.tabby.effects2import com.sksamuel.tabby.results.success3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.longs.shouldBeBetween5import io.kotest.matchers.shouldBe6import kotlin.time.Duration.Companion.milliseconds7class ScheduleNTest : FunSpec() {8 init {9 test("Schedule.iterations should run the effect the given number of times") {10 var counter = 011 repeat(Schedule.iterations(8)) {12 counter++13 counter.success()14 }15 counter shouldBe 916 }17 test("Schedule.iterations with delay") {18 var counter = 019 val start = System.currentTimeMillis()20 repeat(Schedule.iterations(5).delay(75.milliseconds)) {21 counter++22 counter.success()23 }24 counter shouldBe 625 val duration = System.currentTimeMillis() - start26 duration.shouldBeBetween(375, 475)27 }28 }29}...
between
Using AI Code Generation
1 fun between(start: Long, endInclusive: Long) = object : Matcher<Long> {2 override fun test(value: Long) = Result(3 }4}5 fun between(start: Long, endInclusive: Long) = object : Matcher<Long> {6 override fun test(value: Long) = Result(7 }8}9 fun between(start: Long, endInclusive: Long) = object : Matcher<Long> {10 override fun test(value: Long) = Result(11 }12}13 fun between(start: Long, endInclusive: Long) = object : Matcher<Long> {14 override fun test(value: Long) = Result(15 }16}17 fun between(start: Long, endInclusive: Long) = object : Matcher<Long> {18 override fun test(value: Long) = Result(19 }20}21 fun between(start
between
Using AI Code Generation
1 fun between() {2 5 should beBetween(1, 10)3 5 shouldNot beBetween(6, 10)4 }5}6 fun between() {7 5 should between(1, 10)8 5 shouldNot between(6, 10)9 }10}11 fun between() {12 5 should beBetween(1, 10)13 5 shouldNot beBetween(6, 10)14 }15}16 fun between() {17 5 should between(1, 10)18 5 shouldNot between(6, 10)19 }20}21 fun between() {22 5 should beBetween(1, 10)23 5 shouldNot beBetween(6, 10)24 }25}26 fun between() {27 5 should between(1, 10)28 5 shouldNot between(6, 10)29 }30}31 fun between() {32 5 should beBetween(1, 10)33 5 shouldNot beBetween(6, 10)34 }35}36 fun between() {37 5 should between(1, 10)38 5 shouldNot between(6, 10)39 }40}41 fun between() {42 5 should beBetween(1, 10)43 5 shouldNot beBetween(6, 10)44 }45}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!