Best Kotest code snippet using io.kotest.matchers.optional.matchers.bePresent
RawHttpTest.kt
Source:RawHttpTest.kt
1package rawhttp.core2import io.kotest.matchers.collections.shouldHaveSize3import io.kotest.matchers.optional.bePresent4import io.kotest.matchers.optional.shouldBePresent5import io.kotest.matchers.shouldBe6import io.kotest.matchers.shouldNot7import org.junit.jupiter.api.Test8import java.io.File9import java.net.URI10import java.nio.charset.StandardCharsets.UTF_811fun Any.fileFromResource(resource: String): File {12 val file = File.createTempFile("raw-http", "txt")13 file.writeBytes(this.javaClass.getResource(resource)!!.readBytes())14 return file15}16class SimpleHttpRequestTests {17 @Test18 fun `Should be able to parse simplest HTTP Request`() {19 RawHttp().parseRequest("GET localhost:8080").eagerly().run {20 method shouldBe "GET"21 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default22 uri shouldBe URI.create("http://localhost:8080")23 toString() shouldBe "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"24 headers.asMap() shouldBe mapOf("HOST" to listOf("localhost"))25 body shouldNot bePresent()26 }27 }28 @Test29 fun `Should be able to parse HTTP Request with path and HTTP version`() {30 RawHttp().parseRequest("GET https://localhost:8080/my/resource/234 HTTP/1.0").eagerly().run {31 method shouldBe "GET"32 startLine.httpVersion shouldBe HttpVersion.HTTP_1_033 uri shouldBe URI.create("https://localhost:8080/my/resource/234")34 toString() shouldBe "GET /my/resource/234 HTTP/1.0\r\nHost: localhost\r\n\r\n"35 headers.asMap() shouldBe mapOf("HOST" to listOf("localhost"))36 body shouldNot bePresent()37 }38 }39 @Test40 fun `Should be able to parse HTTP Request with path, query and fragment`() {41 RawHttp().parseRequest("GET https://localhost:8080/resource?start=0&limit=10#blah").eagerly().run {42 method shouldBe "GET"43 startLine.httpVersion shouldBe HttpVersion.HTTP_1_144 uri shouldBe URI.create("https://localhost:8080/resource?start=0&limit=10#blah")45 toString() shouldBe "GET /resource?start=0&limit=10 HTTP/1.1\r\nHost: localhost\r\n\r\n"46 headers.asMap() shouldBe mapOf("HOST" to listOf("localhost"))47 body shouldNot bePresent()48 }49 }50 @Test51 fun `Can parse encoded HTTP Request query`() {52 RawHttp().parseRequest("GET /hello?field=encoded%20value HTTP/1.0\nHost: www.example.com").eagerly().run {53 method shouldBe "GET"54 startLine.httpVersion shouldBe HttpVersion.HTTP_1_055 uri.path shouldBe "/hello"56 uri.query shouldBe "field=encoded value"57 toString() shouldBe "GET /hello?field=encoded%20value HTTP/1.0\r\nHost: www.example.com\r\n\r\n"58 headers.asMap() shouldBe mapOf("HOST" to listOf("www.example.com"))59 body shouldNot bePresent()60 }61 }62 @Test63 fun `Uses Host header to identify target server if missing from method line`() {64 RawHttp().parseRequest("GET /hello\nHost: www.example.com").eagerly().run {65 method shouldBe "GET"66 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default67 uri shouldBe URI.create("http://www.example.com/hello")68 toString() shouldBe "GET /hello HTTP/1.1\r\nHost: www.example.com\r\n\r\n"69 headers.asMap() shouldBe mapOf("HOST" to listOf("www.example.com"))70 body shouldNot bePresent()71 }72 }73 @Test74 fun `Uses request-line URI Host instead of Host header to identify target server if both are given`() {75 RawHttp().parseRequest("GET http://favorite.host/hello\nHost: www.example.com").eagerly().run {76 method shouldBe "GET"77 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default78 uri shouldBe URI.create("http://favorite.host/hello")79 toString() shouldBe "GET /hello HTTP/1.1\r\nHost: www.example.com\r\n\r\n"80 headers.asMap() shouldBe mapOf("HOST" to listOf("www.example.com"))81 body shouldNot bePresent()82 }83 }84 @Test85 fun `Request can have a body`() {86 RawHttp().parseRequest(87 """88 POST http://host.com/myresource/12345689 Content-Type: application/json90 Content-Length: 4891 Accept: text/html92 {93 "hello": true,94 "from": "kotlin-test"95 }96 """.trimIndent()97 ).eagerly().run {98 val expectedBody = "{\n \"hello\": true,\n \"from\": \"kotlin-test\"\n}"99 method shouldBe "POST"100 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1101 uri shouldBe URI.create("http://host.com/myresource/123456")102 toString() shouldBe "POST /myresource/123456 HTTP/1.1\r\n" +103 "Content-Type: application/json\r\n" +104 "Content-Length: 48\r\n" +105 "Accept: text/html\r\n" +106 "Host: host.com\r\n\r\n" +107 expectedBody108 headers.asMap() shouldBe mapOf(109 "HOST" to listOf("host.com"),110 "CONTENT-TYPE" to listOf("application/json"),111 "CONTENT-LENGTH" to listOf("48"),112 "ACCEPT" to listOf("text/html")113 )114 body shouldBePresent {115 it.asRawString(UTF_8) shouldBe expectedBody116 it.asRawBytes().size shouldBe 48117 }118 }119 }120 @Test121 fun `Should be able to parse HTTP Request with path and HTTP version from a file`() {122 val requestFile = fileFromResource("simple.request")123 RawHttp().parseRequest(requestFile).eagerly().run {124 method shouldBe "GET"125 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1126 uri shouldBe URI.create("http://example.com/resources/abcde")127 toString() shouldBe "GET /resources/abcde HTTP/1.1\r\n" +128 "Accept: application/json\r\n" +129 "Host: example.com\r\n\r\n"130 headers.asMap() shouldBe mapOf(131 "ACCEPT" to listOf("application/json"),132 "HOST" to listOf("example.com")133 )134 body shouldNot bePresent()135 }136 }137 @Test138 fun `Should be able to parse HTTP Request with body from a file`() {139 val requestFile = fileFromResource("post.request")140 RawHttp().parseRequest(requestFile).eagerly().run {141 method shouldBe "POST"142 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1143 uri shouldBe URI.create("https://example.com/my-resource/SDFKJWEKLKLKWERLWKEGJGJE")144 headers.asMap() shouldBe mapOf(145 "ACCEPT" to listOf("text/plain", "*/*"),146 "CONTENT-TYPE" to listOf("text/encrypted"),147 "CONTENT-LENGTH" to listOf("765"),148 "USER-AGENT" to listOf("rawhttp"),149 "HOST" to listOf("example.com")150 )151 body shouldBePresent {152 val expectedBody = "BEGIN KEYBASE SALTPACK ENCRYPTED MESSAGE. " +153 "kiOUtMhcc4NXXRb XMxIeCbf5rCmoNO Z9cuk3vFu4WUHGE FbP7OCGjWcildtW gRRS2oOGl0tDgNc " +154 "yZBlB9lxbNQs77O RLN5mMqTNWbKrwQ mSZolwGEonepkkk seiN0mXd8vwWM9S 7ssjvDZGbGjAfdO " +155 "AUJmEHLdsRKrmUX yGqKzFKkG9XuiX9 8odcxJUhBMuUAUT dPpaL3sntmQTWal FfD5rj2o0ysBE92 " +156 "lQjYk9Sok2Ofjod ytMjCDOF0eowY67 TgdmD9xmjC9kt0N v3XJB8FQA6mntYY QvTGvMyEInxfyd0 " +157 "4GnXi1PgbwwH9O4 Ntyrt73xVko2RdV 7yaEPrSxveTEQMh P5RxWbTqXsNNagf UfgvsZlpJFxKlPs " +158 "DxovufvUTamC5G8 Hq5XtAT811RZlro rXjZmgoS2uUinRO 0BCq3LujBBrEzQS vV4ZV6DroIjJ6kz " +159 "fm0sr8nIZ4pdUVS qNi5LhWIgGwPlg1 KKIOuv6aCFLUFtO pYzmPXilv7ntnES 88EnMhI1wPLDiih " +160 "Cy1LQyPzT7gUM3A josP5Nne89rWCD9 QrKxhczapyUSch4 E4qqihxkujRPqEu toCyI5eKEnvVbfn " +161 "ldCLQWSoA7RLYRZ E8x3TY7EqFJpmLP iulp9YqVZj. END KEYBASE SALTPACK ENCRYPTED MESSAGE."162 it.asRawString(UTF_8) shouldBe expectedBody163 it.asRawBytes().size shouldBe 765164 }165 }166 }167 @Test168 fun `Should be able to parse HTTP Request with trailing new-line as recommended for robustness`() {169 RawHttp().parseRequest("\r\nGET localhost:8080").eagerly().run {170 method shouldBe "GET"171 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default172 uri shouldBe URI.create("http://localhost:8080")173 toString() shouldBe "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"174 headers.asMap() shouldBe mapOf("HOST" to listOf("localhost"))175 body shouldNot bePresent()176 }177 RawHttp().parseRequest("\nGET localhost:8080").eagerly().run {178 method shouldBe "GET"179 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default180 uri shouldBe URI.create("http://localhost:8080")181 toString() shouldBe "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"182 headers.asMap() shouldBe mapOf("HOST" to listOf("localhost"))183 body shouldNot bePresent()184 }185 }186}187class SimpleHttpResponseTests {188 @Test189 fun `Should be able to parse simplest HTTP Response`() {190 RawHttp().parseResponse("404").eagerly().run {191 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1 // the default192 startLine.statusCode shouldBe 404193 startLine.reason shouldBe ""194 toString() shouldBe "HTTP/1.1 404\r\n\r\n"195 headers.headerNames shouldHaveSize 0196 body shouldBePresent { it.toString() shouldBe "" }197 }198 }199 @Test200 fun `Should be able to parse simple HTTP Response`() {201 RawHttp().parseResponse("HTTP/1.0 404 NOT FOUND").eagerly().run {202 startLine.httpVersion shouldBe HttpVersion.HTTP_1_0203 startLine.statusCode shouldBe 404204 startLine.reason shouldBe "NOT FOUND"205 toString() shouldBe "HTTP/1.0 404 NOT FOUND\r\n\r\n"206 headers.headerNames shouldHaveSize 0207 body shouldBePresent { it.toString() shouldBe "" }208 }209 }210 @Test211 fun `Should be able to parse HTTP Response that may not have a body`() {212 RawHttp().parseResponse("HTTP/1.1 100 CONTINUE").eagerly().run {213 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1214 startLine.statusCode shouldBe 100215 startLine.reason shouldBe "CONTINUE"216 toString() shouldBe "HTTP/1.1 100 CONTINUE\r\n\r\n"217 headers.headerNames shouldHaveSize 0218 body shouldNot bePresent()219 }220 }221 @Test222 fun `Should be able to parse simple HTTP Response with body`() {223 RawHttp().parseResponse("HTTP/1.1 200 OK\r\nServer: Apache\r\n\r\nHello World!").eagerly().run {224 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1225 startLine.statusCode shouldBe 200226 startLine.reason shouldBe "OK"227 toString() shouldBe "HTTP/1.1 200 OK\r\nServer: Apache\r\n\r\nHello World!"228 headers.asMap() shouldBe mapOf("SERVER" to listOf("Apache"))229 body shouldBePresent {230 it.asRawString(UTF_8) shouldBe "Hello World!"231 }232 }233 }234 @Test235 fun `Should be able to parse longer HTTP Response with invalid line-endings`() {236 RawHttp().parseResponse(237 """238 HTTP/1.1 200 OK239 Date: Mon, 27 Jul 2009 12:28:53 GMT240 Server: Apache241 Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT242 ETag: "34aa387-d-1568eb00"243 Accept-Ranges: bytes244 Content-Length: 39245 Vary: Accept-Encoding246 Content-Type: application/json247 {248 "hello": "world",249 "number": 123250 }251 """.trimIndent()252 ).eagerly().run {253 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1254 startLine.statusCode shouldBe 200255 startLine.reason shouldBe "OK"256 toString() shouldBe "HTTP/1.1 200 OK\r\n" +257 "Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n" +258 "Server: Apache\r\n" +259 "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\n" +260 "ETag: \"34aa387-d-1568eb00\"\r\n" +261 "Accept-Ranges: bytes\r\n" +262 "Content-Length: 39\r\n" +263 "Vary: Accept-Encoding\r\n" +264 "Content-Type: application/json\r\n\r\n" +265 "{\n" +266 " \"hello\": \"world\",\n" +267 " \"number\": 123\n" +268 "}"269 headers.asMap() shouldBe mapOf(270 "DATE" to listOf("Mon, 27 Jul 2009 12:28:53 GMT"),271 "SERVER" to listOf("Apache"),272 "LAST-MODIFIED" to listOf("Wed, 22 Jul 2009 19:15:56 GMT"),273 "ETAG" to listOf("\"34aa387-d-1568eb00\""),274 "ACCEPT-RANGES" to listOf("bytes"),275 "CONTENT-LENGTH" to listOf("39"),276 "VARY" to listOf("Accept-Encoding"),277 "CONTENT-TYPE" to listOf("application/json")278 )279 body shouldBePresent {280 it.asRawString(UTF_8) shouldBe "{\n \"hello\": \"world\",\n \"number\": 123\n}"281 }282 }283 }284 @Test285 fun `Should be able to parse HTTP Response from file`() {286 val responseFile = fileFromResource("simple.response")287 RawHttp().parseResponse(responseFile).eagerly().run {288 val expectedBody = "{\n \"message\": \"Hello World\",\n \"language\": \"EN\"\n}"289 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1290 startLine.statusCode shouldBe 200291 startLine.reason shouldBe "OK"292 toString() shouldBe "HTTP/1.1 200 OK\r\n" +293 "Content-Type: application/json\r\n" +294 "Content-Length: 50\r\n" +295 "Server: super-server\r\n\r\n" +296 expectedBody297 headers.asMap() shouldBe mapOf(298 "SERVER" to listOf("super-server"),299 "CONTENT-TYPE" to listOf("application/json"),300 "CONTENT-LENGTH" to listOf("50")301 )302 body shouldBePresent {303 it.asRawString(UTF_8) shouldBe expectedBody304 }305 }306 }307 @Test308 fun `Should ignore body of HTTP Response that may not have a body`() {309 val stream = "HTTP/1.1 304 Not Modified\r\nETag: 12345\r\n\r\nBODY".byteInputStream()310 RawHttp().parseResponse(stream).eagerly().run {311 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1312 startLine.statusCode shouldBe 304313 startLine.reason shouldBe "Not Modified"314 headers.asMap() shouldBe mapOf("ETAG" to listOf("12345"))315 body shouldNot bePresent()316 }317 // verify that the stream was only consumed until the empty-line after the last header318 String(stream.readBytes()) shouldBe "BODY"319 }320 @Test321 fun `Should be able to parse HTTP Response with trailing new-line as recommended for robustness`() {322 RawHttp().parseResponse("\r\nHTTP/1.0 404 NOT FOUND").eagerly().run {323 startLine.httpVersion shouldBe HttpVersion.HTTP_1_0324 startLine.statusCode shouldBe 404325 startLine.reason shouldBe "NOT FOUND"326 toString() shouldBe "HTTP/1.0 404 NOT FOUND\r\n\r\n"327 headers.headerNames shouldHaveSize 0328 body shouldBePresent { it.toString() shouldBe "" }329 }330 RawHttp().parseResponse("\nHTTP/1.0 404 NOT FOUND").eagerly().run {331 startLine.httpVersion shouldBe HttpVersion.HTTP_1_0332 startLine.statusCode shouldBe 404333 startLine.reason shouldBe "NOT FOUND"334 toString() shouldBe "HTTP/1.0 404 NOT FOUND\r\n\r\n"335 headers.headerNames shouldHaveSize 0336 body shouldBePresent { it.toString() shouldBe "" }337 }338 }339}340class CopyHttpRequestTests {341 @Test342 fun `Can make a copy of a HTTP Request with added headers`() {343 val addedHeaders = RawHttpHeaders.newBuilder()344 .with("Accept", "any/thing")345 .with("User-Agent", "raw-http")346 .build()347 RawHttp().parseRequest("GET /hello\nHost: www.example.com")348 .withHeaders(addedHeaders).run {349 method shouldBe "GET"350 uri.path shouldBe "/hello"351 headers["Host"] shouldBe listOf("www.example.com")352 headers["Accept"] shouldBe listOf("any/thing")353 headers["User-Agent"] shouldBe listOf("raw-http")354 headers.asMap().size shouldBe 3355 senderAddress shouldNot bePresent()356 }357 }358 @Test359 fun `Can make a copy of a HTTP Request with a replaced request line (different host)`() {360 RawHttp().parseRequest("GET /hello HTTP/1.0\nHost: example.org:8080")361 .withRequestLine(RequestLine("GET", URI.create("http://localhost:8999/foo/bar"), HttpVersion.HTTP_1_1))362 .run {363 method shouldBe "GET"364 uri.scheme shouldBe "http"365 uri.host shouldBe "localhost"366 uri.port shouldBe 8999367 uri.path shouldBe "/foo/bar"368 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1369 headers["Host"] shouldBe listOf("localhost:8999")370 headers.asMap().size shouldBe 1371 senderAddress shouldNot bePresent()372 }373 }374 @Test375 fun `Can make a copy of a HTTP Request with a replaced request line (different port)`() {376 RawHttp().parseRequest("GET /hello HTTP/1.0\nHost: localhost:8080")377 .withRequestLine(RequestLine("GET", URI.create("http://localhost:8999/foo/bar"), HttpVersion.HTTP_1_1))378 .run {379 method shouldBe "GET"380 uri.scheme shouldBe "http"381 uri.host shouldBe "localhost"382 uri.port shouldBe 8999383 uri.path shouldBe "/foo/bar"384 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1385 headers["Host"] shouldBe listOf("localhost:8999")386 headers.asMap().size shouldBe 1387 senderAddress shouldNot bePresent()388 }389 }390}391class CopyHttpResponseTests {392 @Test393 fun `Can make a copy of a HTTP Response with added headers`() {394 val addedHeaders = RawHttpHeaders.newBuilder()395 .with("Accept", "any/thing")396 .with("User-Agent", "raw-http")397 .build()398 RawHttp().parseResponse("HTTP/1.1 200 OK\nHost: www.example.com")399 .withHeaders(addedHeaders).run {400 startLine.httpVersion shouldBe HttpVersion.HTTP_1_1401 statusCode shouldBe 200402 startLine.reason shouldBe "OK"403 libResponse shouldNot bePresent()404 headers["Host"] shouldBe listOf("www.example.com")405 headers["Accept"] shouldBe listOf("any/thing")406 headers["User-Agent"] shouldBe listOf("raw-http")407 headers.asMap().size shouldBe 3408 }409 }410}...
EagerBodyReaderTest.kt
Source:EagerBodyReaderTest.kt
1package rawhttp.core.body2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.optional.bePresent4import io.kotest.matchers.optional.shouldBePresent5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.kotest.matchers.shouldNot8import io.kotest.matchers.types.beOfType9import rawhttp.core.HttpMetadataParser10import rawhttp.core.RawHttpHeaders11import rawhttp.core.RawHttpHeaders.Builder.emptyRawHttpHeaders12import rawhttp.core.RawHttpOptions13import rawhttp.core.body.FramedBody.Chunked14import rawhttp.core.body.FramedBody.CloseTerminated15import rawhttp.core.body.FramedBody.ContentLength16import rawhttp.core.body.encoding.ServiceLoaderHttpBodyEncodingRegistry17import rawhttp.core.shouldHaveSameElementsAs18import kotlin.text.Charsets.UTF_819class EagerBodyReaderTest : StringSpec({20 val registry = ServiceLoaderHttpBodyEncodingRegistry()21 val noOpDecoder = BodyDecoder()22 "Can read content-length body" {23 val body = "Hello world"24 val stream = body.byteInputStream()25 val reader = EagerBodyReader(ContentLength(body.length.toLong()), stream)26 reader.run {27 framedBody should beOfType(ContentLength::class)28 isChunked shouldBe false29 asRawString(Charsets.UTF_8) shouldBe body30 asChunkedBodyContents() shouldNot bePresent<ChunkedBodyContents>()31 asRawBytes() shouldHaveSameElementsAs body.toByteArray()32 }33 }34 "Can read empty content-length body" {35 val body = ""36 val stream = body.byteInputStream()37 val reader = EagerBodyReader(ContentLength(body.length.toLong()), stream)38 reader.run {39 framedBody should beOfType<ContentLength>()40 isChunked shouldBe false41 asRawString(Charsets.UTF_8) shouldBe body42 asChunkedBodyContents() shouldNot bePresent()43 asRawBytes() shouldHaveSameElementsAs body.toByteArray()44 }45 }46 "Can read body until EOF" {47 val body = "Hello world"48 val stream = body.byteInputStream()49 val reader = EagerBodyReader(CloseTerminated(noOpDecoder), stream)50 reader.run {51 framedBody shouldBe CloseTerminated(noOpDecoder)52 isChunked shouldBe false53 asRawString(Charsets.UTF_8) shouldBe body54 asChunkedBodyContents() shouldNot bePresent()55 asRawBytes() shouldHaveSameElementsAs body.toByteArray()56 }57 }58 val metadataParser = HttpMetadataParser(RawHttpOptions.defaultInstance())59 "Can read simple chunked body" {60 val body = byteArrayOf(56, 13, 10, 72, 105, 32, 116, 104, 101, 114, 101, 13, 10, 48, 13, 10, 13, 10)61 val stream = body.inputStream()62 val reader = EagerBodyReader(Chunked(BodyDecoder(registry, listOf("chunked")), metadataParser), stream)63 reader.run {64 framedBody shouldBe Chunked(BodyDecoder(registry, listOf("chunked")), metadataParser)65 isChunked shouldBe true66 asChunkedBodyContents() shouldBePresent {67 it.data shouldHaveSameElementsAs "Hi there".toByteArray()68 it.chunks.size shouldBe 2...
OptionalMatchersTest.kt
Source:OptionalMatchersTest.kt
1package com.sksamuel.kotest.matchers.optional2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.ShouldSpec4import io.kotest.matchers.optional.beEmpty5import io.kotest.matchers.optional.bePresent6import io.kotest.matchers.optional.shouldBeEmpty7import io.kotest.matchers.optional.shouldBePresent8import io.kotest.matchers.optional.shouldNotBeEmpty9import io.kotest.matchers.optional.shouldNotBePresent10import io.kotest.matchers.should11import io.kotest.matchers.shouldBe12import io.kotest.matchers.shouldNot13import io.kotest.matchers.throwable.shouldHaveMessage14import java.util.Optional15class OptionalMatchersTest : ShouldSpec({16 context("Empty optional") {17 val optional = Optional.empty<Any>()18 should("Be empty") {19 optional.shouldBeEmpty()20 optional should beEmpty()21 }22 should("Not be present") {23 optional.shouldNotBePresent()24 optional shouldNot bePresent()25 }26 should("Fail to be notEmpty") {27 shouldThrow<AssertionError> { optional.shouldNotBeEmpty() }28 shouldThrow<AssertionError> { optional shouldNot beEmpty() }29 }30 should("Fail to be present") {31 shouldThrow<AssertionError> { optional.shouldBePresent() }32 shouldThrow<AssertionError> { optional should bePresent() }33 }34 }35 context("Present optional") {36 val optional = Optional.of("A")37 should("Be present") {38 optional.shouldBePresent()39 optional should bePresent()40 }41 42 should("Return the present value for usage in more assertions") {43 optional.shouldBePresent() shouldBe "A"44 } 45 should("Allow matchers with present value as a receiver") {46 optional shouldBePresent {47 this shouldBe "A"48 }49 }50 51 should("Allow matchers with present value as parameter") {52 optional shouldBePresent {53 it shouldBe "A"...
matchers.kt
Source:matchers.kt
...71 * ```72 *73 */74fun <T> Optional<T>.shouldBePresent(): T {75 this should bePresent()76 return get()77}78/**79 * Verifies t hat this Optional contains no value80 */81fun <T> Optional<T>.shouldNotBePresent() = this shouldNot bePresent()82/**83 * Matcher to verify whether a matcher contains a value or not84 */85fun <T> bePresent() = object : Matcher<Optional<T>> {86 override fun test(value: Optional<T>) = MatcherResult(87 value.isPresent,88 { "Expected optional to be present, but was empty instead" },89 { "Expected optional to not be present, but was ${value.get()}" }90 )91}...
TestCasesThatShouldJustRun.kt
Source:TestCasesThatShouldJustRun.kt
...7 * as described in the file LICENSE in the Alchemist distribution's top directory.8 */9package it.unibo.alchemist.test10import io.kotest.core.spec.style.StringSpec11import io.kotest.matchers.optional.bePresent12import io.kotest.matchers.shouldNot13import it.unibo.alchemist.loader.LoadAlchemist14import it.unibo.alchemist.testsupport.createSimulation15import it.unibo.alchemist.testsupport.runInCurrentThread16import it.unibo.alchemist.util.ClassPathScanner17class TestCasesThatShouldJustRun : StringSpec(18 {19 ClassPathScanner.resourcesMatching(".+\\.ya?ml", "shouldrun").forEach {20 "simulation at ${it.path} should run" {21 LoadAlchemist.from(it).getDefault<Any, Nothing>()22 .createSimulation(finalStep = 100)23 .runInCurrentThread()24 .error shouldNot bePresent()25 }26 }27 }28)...
bePresent
Using AI Code Generation
1val optional : Optional < String > = Optional . empty () optional . shouldNotBePresent ()2val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldBePresent ()3val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotBeEmpty ()4val optional : Optional < String > = Optional . empty () optional . shouldBeEmpty ()5val optional : Optional < String > = Optional . empty () optional . shouldNotBePresent ()6val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldBePresent ()7val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotBeEmpty ()8val optional : Optional < String > = Optional . empty () optional . shouldBeEmpty ()9val optional : Optional < String > = Optional . empty () optional . shouldNotBePresent ()10val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldBePresent ()11val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotBeEmpty ()12val optional : Optional < String > = Optional . empty () optional . shouldBeEmpty ()13val optional : Optional < String > = Optional . empty () optional . shouldNotBePresent ()
bePresent
Using AI Code Generation
1val optional : Optional < String > = Optional . of ( "Hello" ) optional should bePresent ( "Hello" )2val optional : Optional < String > = Optional . empty () optional should beEmpty ()3val optional : Optional < String > = Optional . empty () optional shouldNot bePresent ()4val optional : Optional < String > = Optional . of ( "Hello" ) optional shouldNot beEmpty ()5val optional : Optional < String > = Optional . of ( "Hello" ) optional should bePresent ()6val optional : Optional < String > = Optional . empty () optional should beEmpty ()7val optional : Optional < String > = Optional . empty () optional shouldNot bePresent ()8val optional : Optional < String > = Optional . of ( "Hello" ) optional shouldNot beEmpty ()9val optional : Optional < String > = Optional . of ( "Hello" ) optional should bePresent ()10val optional : Optional < String > = Optional . empty () optional should beEmpty ()11val optional : Optional < String > = Optional . empty () optional shouldNot bePresent ()12val optional : Optional < String > = Optional . of ( "Hello" ) optional shouldNot beEmpty ()13val optional : Optional < String > = Optional . of ( "Hello" ) optional should bePresent ()
bePresent
Using AI Code Generation
1val optional : Optional < String > = Optional . empty () optional should bePresent ()2val optional : Optional < String > = Optional . of ( "foo" ) optional should bePresent ()3val optional : Optional < String > = Optional . ofNullable ( "foo" ) optional should bePresent ()4val optional : Optional < String > = Optional . of ( "foo" ) optional should bePresent ()5val optional : Optional < String > = Optional . ofNullable ( "foo" ) optional should bePresent ()6val optional : Optional < String > = Optional . empty () optional should bePresent ()7val optional : Optional < String > = Optional . of ( "foo" ) optional should bePresent ()8val optional : Optional < String > = Optional . ofNullable ( "foo" ) optional should bePresent ()9val optional : Optional < String > = Optional . empty () optional should bePresent ()10val optional : Optional < String > = Optional . of ( "foo" ) optional should bePresent ()11val optional : Optional < String > = Optional . ofNullable ( "foo" ) optional should bePresent ()12val optional : Optional < String > = Optional . empty () optional should bePresent ()13val optional : Optional < String > = Optional . of ( "foo" ) optional should bePresent ()
bePresent
Using AI Code Generation
1val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotBePresent ()2val optional : Optional < String > = Optional . empty () optional . shouldNotBePresent ()3val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldBePresent ()4val optional : Optional < String > = Optional . empty () optional . shouldBeEmpty ()5val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotBeEmpty ()6val optional : Optional < String > = Optional . empty () optional . shouldNotBeEmpty ()7val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldHaveValue ( "Hello" )8val optional : Optional < String > = Optional . empty () optional . shouldHaveValue ( "Hello" )9val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldNotHaveValue ( "Hello" )10val optional : Optional < String > = Optional . empty () optional . shouldNotHaveValue ( "Hello" )11val optional : Optional < String > = Optional . of ( "Hello" ) optional . shouldHaveValueSatisfying { it . length > 2 }12val optional : Optional < String > = Optional . empty () optional . shouldHaveValueSatisfying { it . length > 2 }
bePresent
Using AI Code Generation
1val result = Optional.of(10)2val result = Optional.empty()3val result = Optional.empty()4val result = Optional.of(10)5val result = Optional.empty()6val result = Optional.of(10)7val result = Optional.of(10)
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!!