How to use strings class of io.kotest.property.exhaustive package

Best Kotest code snippet using io.kotest.property.exhaustive.strings

ReadOnlyJSONValTests.kt

Source:ReadOnlyJSONValTests.kt Github

copy

Full Screen

1/*2 * Copyright (c) 2022 OneAndOlaf3 *4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated5 * documentation files (the “Software”), to deal in the Software without restriction, including without limitation the6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to7 * permit persons to whom the Software is furnished to do so, subject to the following conditions:8 *9 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the10 * Software.11 *12 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE13 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR14 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR15 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.16 */17package com.github.oneandolaf.jsonext.readonly18import com.github.oneandolaf.jsonext.extensions.asReadOnly19import com.github.oneandolaf.jsonext.impl.Conversions20import com.github.oneandolaf.jsonext.testutils.*21import io.kotest.core.spec.style.FunSpec22import io.kotest.matchers.booleans.shouldBeFalse23import io.kotest.matchers.booleans.shouldBeTrue24import io.kotest.matchers.nulls.shouldBeNull25import io.kotest.matchers.shouldBe26import io.kotest.matchers.string.shouldBeEmpty27import io.kotest.matchers.types.shouldBeInstanceOf28import io.kotest.property.Exhaustive29import io.kotest.property.checkAll30import io.kotest.property.exhaustive.enum31import io.kotest.property.exhaustive.times32import org.json.JSONArray33import org.json.JSONObject34import java.math.BigDecimal35import java.math.BigInteger36class ReadOnlyJSONValTests : FunSpec({37 test("orNull") {38 checkAll(JSONGenerators.values) {39 val value = ReadOnlyJSONVal(it)40 value.orNull() shouldBeSimilarTo it41 if (it is JSONObject) {42 value.orNull().shouldBeInstanceOf<ReadOnlyJSONObject>()43 } else if (it is JSONArray) {44 value.orNull().shouldBeInstanceOf<ReadOnlyJSONArray>()45 }46 }47 ReadOnlyJSONVal(null).orNull().shouldBeNull()48 }49 test("orDefault") {50 checkAll(JSONGenerators.values * JSONGenerators.values) { data ->51 val value = ReadOnlyJSONVal(data.first)52 value.orDefault(data.second) shouldBeSimilarTo data.first53 ReadOnlyJSONVal(null).orDefault(data.second) shouldBeSimilarTo data.second54 }55 }56 test("orElse") {57 checkAll(JSONGenerators.values * JSONGenerators.values) { data ->58 val value = ReadOnlyJSONVal(data.first)59 value.orElse { data.second } shouldBeSimilarTo data.first60 ReadOnlyJSONVal(null).orElse { data.second } shouldBeSimilarTo data.second61 }62 }63 test("isArray") {64 checkAll(JSONGenerators.arrays) {65 ReadOnlyJSONVal(it).isArray().shouldBeTrue()66 }67 checkAll(JSONGenerators.nonArrays) {68 ReadOnlyJSONVal(it).isArray().shouldBeFalse()69 }70 ReadOnlyJSONVal(null).isArray().shouldBeFalse()71 }72 test("asArrayOrNull") {73 checkAll(JSONGenerators.arrays) {74 ReadOnlyJSONVal(it).asArrayOrNull() shouldBeSimilarTo it75 }76 checkAll(JSONGenerators.nonArrays) {77 ReadOnlyJSONVal(it).asArrayOrNull().shouldBeNull()78 }79 ReadOnlyJSONVal(null).asArrayOrNull().shouldBeNull()80 }81 test("asArrayOrDefault") {82 checkAll(JSONGenerators.arrays * JSONGenerators.arrays) { data ->83 ReadOnlyJSONVal(data.first).asArrayOrDefault(data.second.asReadOnly()) shouldBeSimilarTo data.first84 }85 checkAll(JSONGenerators.nonArrays * JSONGenerators.arrays) { data ->86 ReadOnlyJSONVal(data.first).asArrayOrDefault(data.second.asReadOnly()) shouldBeSimilarTo data.second87 }88 checkAll(JSONGenerators.arrays) {89 ReadOnlyJSONVal(null).asArrayOrDefault(it.asReadOnly()) shouldBeSimilarTo it90 }91 }92 test("asArrayOrEmpty") {93 checkAll(JSONGenerators.arrays) {94 ReadOnlyJSONVal(it).asArrayOrEmpty() shouldBeSimilarTo it95 }96 checkAll(JSONGenerators.nonArrays) {97 ReadOnlyJSONVal(it).asArrayOrEmpty() shouldBeSimilarTo ReadOnlyJSONArray.EMPTY98 }99 ReadOnlyJSONVal(null).asArrayOrEmpty() shouldBeSimilarTo ReadOnlyJSONArray.EMPTY100 }101 test("asArrayOrElse") {102 checkAll(JSONGenerators.arrays * JSONGenerators.arrays) { data ->103 ReadOnlyJSONVal(data.first).asArrayOrElse { data.second.asReadOnly() } shouldBeSimilarTo data.first104 }105 checkAll(JSONGenerators.nonArrays * JSONGenerators.arrays) { data ->106 ReadOnlyJSONVal(data.first).asArrayOrElse { data.second.asReadOnly() } shouldBeSimilarTo data.second107 }108 checkAll(JSONGenerators.arrays) {109 ReadOnlyJSONVal(null).asArrayOrElse { it.asReadOnly() } shouldBeSimilarTo it110 }111 }112 test("isBigDecimal") {113 checkAll(JSONGenerators.numberLikes) {114 ReadOnlyJSONVal(it).isBigDecimal().shouldBeTrue()115 }116 checkAll(JSONGenerators.nonNumberLikes) {117 ReadOnlyJSONVal(it).isBigDecimal().shouldBeFalse()118 }119 ReadOnlyJSONVal(null).isBigDecimal().shouldBeFalse()120 }121 test("asBigDecimalOrNull") {122 checkAll(JSONGenerators.numberLikes) {123 ReadOnlyJSONVal(it).asBigDecimalOrNull() shouldBe Conversions.toBigDecimal(it)!!124 }125 checkAll(JSONGenerators.nonNumberLikes) {126 ReadOnlyJSONVal(it).asBigDecimalOrNull().shouldBeNull()127 }128 ReadOnlyJSONVal(null).asBigDecimalOrNull().shouldBeNull()129 }130 test("asBigDecimalOrDefault") {131 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigDecimals) { data ->132 ReadOnlyJSONVal(data.first).asBigDecimalOrDefault(data.second) shouldBe Conversions.toBigDecimal(133 data.first134 )!!135 }136 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigDecimals) { data ->137 ReadOnlyJSONVal(data.first).asBigDecimalOrDefault(data.second) shouldBe data.second138 }139 checkAll(JSONGenerators.bigDecimals) {140 ReadOnlyJSONVal(null).asBigDecimalOrDefault(it) shouldBe it141 }142 }143 test("asBigDecimalOrZero") {144 checkAll(JSONGenerators.numberLikes) {145 ReadOnlyJSONVal(it).asBigDecimalOrZero() shouldBe Conversions.toBigDecimal(it)!!146 }147 checkAll(JSONGenerators.nonNumberLikes) {148 ReadOnlyJSONVal(it).asBigDecimalOrZero() shouldBe BigDecimal.ZERO149 }150 ReadOnlyJSONVal(null).asBigDecimalOrZero() shouldBe BigDecimal.ZERO151 }152 test("asBigDecimalOrOne") {153 checkAll(JSONGenerators.numberLikes) {154 ReadOnlyJSONVal(it).asBigDecimalOrOne() shouldBe Conversions.toBigDecimal(it)!!155 }156 checkAll(JSONGenerators.nonNumberLikes) {157 ReadOnlyJSONVal(it).asBigDecimalOrOne() shouldBe BigDecimal.ONE158 }159 ReadOnlyJSONVal(null).asBigDecimalOrOne() shouldBe BigDecimal.ONE160 }161 test("asBigDecimalOrMinusOne") {162 checkAll(JSONGenerators.numberLikes) {163 ReadOnlyJSONVal(it).asBigDecimalOrMinusOne() shouldBe Conversions.toBigDecimal(it)!!164 }165 checkAll(JSONGenerators.nonNumberLikes) {166 ReadOnlyJSONVal(it).asBigDecimalOrMinusOne() shouldBe BigDecimal.valueOf(-1L)167 }168 ReadOnlyJSONVal(null).asBigDecimalOrMinusOne() shouldBe BigDecimal.valueOf(-1L)169 }170 test("asBigDecimalOrElse") {171 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigDecimals) { data ->172 ReadOnlyJSONVal(data.first).asBigDecimalOrElse { data.second } shouldBe Conversions.toBigDecimal(173 data.first174 )!!175 }176 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigDecimals) { data ->177 ReadOnlyJSONVal(data.first).asBigDecimalOrElse { data.second } shouldBe data.second178 }179 checkAll(JSONGenerators.bigDecimals) {180 ReadOnlyJSONVal(null).asBigDecimalOrElse { it } shouldBe it181 }182 }183 test("isBigInteger") {184 checkAll(JSONGenerators.numberLikes) {185 ReadOnlyJSONVal(it).isBigInteger().shouldBeTrue()186 }187 checkAll(JSONGenerators.nonNumberLikes) {188 ReadOnlyJSONVal(it).isBigInteger().shouldBeFalse()189 }190 ReadOnlyJSONVal(null).isBigInteger().shouldBeFalse()191 }192 test("asBigIntegerOrNull") {193 checkAll(JSONGenerators.numberLikes) {194 ReadOnlyJSONVal(it).asBigIntegerOrNull() shouldBe Conversions.toBigInteger(it)!!195 }196 checkAll(JSONGenerators.nonNumberLikes) {197 ReadOnlyJSONVal(it).asBigIntegerOrNull().shouldBeNull()198 }199 ReadOnlyJSONVal(null).asBigIntegerOrNull().shouldBeNull()200 }201 test("asBigIntegerOrDefault") {202 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigIntegers) { data ->203 ReadOnlyJSONVal(data.first).asBigIntegerOrDefault(data.second) shouldBe Conversions.toBigInteger(204 data.first205 )!!206 }207 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigIntegers) { data ->208 ReadOnlyJSONVal(data.first).asBigIntegerOrDefault(data.second) shouldBe data.second209 }210 checkAll(JSONGenerators.bigIntegers) {211 ReadOnlyJSONVal(null).asBigIntegerOrDefault(it) shouldBe it212 }213 }214 test("asBigIntegerOrZero") {215 checkAll(JSONGenerators.numberLikes) {216 ReadOnlyJSONVal(it).asBigIntegerOrZero() shouldBe Conversions.toBigInteger(it)!!217 }218 checkAll(JSONGenerators.nonNumberLikes) {219 ReadOnlyJSONVal(it).asBigIntegerOrZero() shouldBe BigInteger.ZERO220 }221 ReadOnlyJSONVal(null).asBigIntegerOrZero() shouldBe BigInteger.ZERO222 }223 test("asBigIntegerOrOne") {224 checkAll(JSONGenerators.numberLikes) {225 ReadOnlyJSONVal(it).asBigIntegerOrOne() shouldBe Conversions.toBigInteger(it)!!226 }227 checkAll(JSONGenerators.nonNumberLikes) {228 ReadOnlyJSONVal(it).asBigIntegerOrOne() shouldBe BigInteger.ONE229 }230 ReadOnlyJSONVal(null).asBigIntegerOrOne() shouldBe BigInteger.ONE231 }232 test("asBigIntegerOrMinusOne") {233 checkAll(JSONGenerators.numberLikes) {234 ReadOnlyJSONVal(it).asBigIntegerOrMinusOne() shouldBe Conversions.toBigInteger(it)!!235 }236 checkAll(JSONGenerators.nonNumberLikes) {237 ReadOnlyJSONVal(it).asBigIntegerOrMinusOne() shouldBe BigInteger.valueOf(-1L)238 }239 ReadOnlyJSONVal(null).asBigIntegerOrMinusOne() shouldBe BigInteger.valueOf(-1L)240 }241 test("asBigIntegerOrElse") {242 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigIntegers) { data ->243 ReadOnlyJSONVal(data.first).asBigIntegerOrElse { data.second } shouldBe Conversions.toBigInteger(244 data.first245 )!!246 }247 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigIntegers) { data ->248 ReadOnlyJSONVal(data.first).asBigIntegerOrElse { data.second } shouldBe data.second249 }250 checkAll(JSONGenerators.bigIntegers) {251 ReadOnlyJSONVal(null).asBigIntegerOrElse { it } shouldBe it252 }253 }254 test("isBoolean") {255 checkAll(JSONGenerators.booleanLikes) {256 ReadOnlyJSONVal(it).isBoolean().shouldBeTrue()257 }258 checkAll(JSONGenerators.nonBooleanLikes) {259 ReadOnlyJSONVal(it).isBoolean().shouldBeFalse()260 }261 ReadOnlyJSONVal(null).isBoolean().shouldBeFalse()262 }263 test("asBooleanOrNull") {264 checkAll(JSONGenerators.booleanLikes) {265 ReadOnlyJSONVal(it).asBooleanOrNull() shouldBe Conversions.toBoolean(it)!!266 }267 checkAll(JSONGenerators.nonBooleanLikes) {268 ReadOnlyJSONVal(it).asBooleanOrNull().shouldBeNull()269 }270 ReadOnlyJSONVal(null).asBooleanOrNull().shouldBeNull()271 }272 test("asBooleanOrDefault") {273 checkAll(JSONGenerators.booleanLikes * JSONGenerators.bools) { data ->274 ReadOnlyJSONVal(data.first).asBooleanOrDefault(data.second) shouldBe Conversions.toBoolean(data.first)!!275 }276 checkAll(JSONGenerators.nonBooleanLikes * JSONGenerators.bools) { data ->277 ReadOnlyJSONVal(data.first).asBooleanOrDefault(data.second) shouldBe data.second278 }279 checkAll(JSONGenerators.bools) {280 ReadOnlyJSONVal(null).asBooleanOrDefault(it) shouldBe it281 }282 }283 test("asBooleanOrTrue") {284 checkAll(JSONGenerators.booleanLikes) {285 ReadOnlyJSONVal(it).asBooleanOrTrue() shouldBe Conversions.toBoolean(it)!!286 }287 checkAll(JSONGenerators.nonBooleanLikes) {288 ReadOnlyJSONVal(it).asBooleanOrTrue().shouldBeTrue()289 }290 ReadOnlyJSONVal(null).asBooleanOrTrue().shouldBeTrue()291 }292 test("asBooleanOrFalse") {293 checkAll(JSONGenerators.booleanLikes) {294 ReadOnlyJSONVal(it).asBooleanOrFalse() shouldBe Conversions.toBoolean(it)!!295 }296 checkAll(JSONGenerators.nonBooleanLikes) {297 ReadOnlyJSONVal(it).asBooleanOrFalse().shouldBeFalse()298 }299 ReadOnlyJSONVal(null).asBooleanOrFalse().shouldBeFalse()300 }301 test("asBooleanOrElse") {302 checkAll(JSONGenerators.booleanLikes * JSONGenerators.bools) { data ->303 ReadOnlyJSONVal(data.first).asBooleanOrElse { data.second } shouldBe Conversions.toBoolean(data.first)!!304 }305 checkAll(JSONGenerators.nonBooleanLikes * JSONGenerators.bools) { data ->306 ReadOnlyJSONVal(data.first).asBooleanOrElse { data.second } shouldBe data.second307 }308 checkAll(JSONGenerators.bools) {309 ReadOnlyJSONVal(null).asBooleanOrElse { it } shouldBe it310 }311 }312 test("isDouble") {313 checkAll(JSONGenerators.numberLikes) {314 ReadOnlyJSONVal(it).isDouble().shouldBeTrue()315 }316 checkAll(JSONGenerators.nonNumberLikes) {317 ReadOnlyJSONVal(it).isDouble().shouldBeFalse()318 }319 ReadOnlyJSONVal(null).isDouble().shouldBeFalse()320 }321 test("asDoubleOrNull") {322 checkAll(JSONGenerators.numberLikes) {323 ReadOnlyJSONVal(it).asDoubleOrNull() shouldBe Conversions.toDouble(it)!!324 }325 checkAll(JSONGenerators.nonNumberLikes) {326 ReadOnlyJSONVal(it).asDoubleOrNull().shouldBeNull()327 }328 ReadOnlyJSONVal(null).asDoubleOrNull().shouldBeNull()329 }330 test("asDoubleOrDefault") {331 checkAll(JSONGenerators.numberLikes * JSONGenerators.doubles) { data ->332 ReadOnlyJSONVal(data.first).asDoubleOrDefault(data.second) shouldBe Conversions.toDouble(data.first)!!333 }334 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.doubles) { data ->335 ReadOnlyJSONVal(data.first).asDoubleOrDefault(data.second) shouldBe data.second336 }337 checkAll(JSONGenerators.doubles) {338 ReadOnlyJSONVal(null).asDoubleOrDefault(it) shouldBe it339 }340 }341 test("asDoubleOrZero") {342 checkAll(JSONGenerators.numberLikes) {343 ReadOnlyJSONVal(it).asDoubleOrZero() shouldBe Conversions.toDouble(it)!!344 }345 checkAll(JSONGenerators.nonNumberLikes) {346 ReadOnlyJSONVal(it).asDoubleOrZero() shouldBe 0.0347 }348 ReadOnlyJSONVal(null).asDoubleOrZero() shouldBe 0.0349 }350 test("asDoubleOrOne") {351 checkAll(JSONGenerators.numberLikes) {352 ReadOnlyJSONVal(it).asDoubleOrOne() shouldBe Conversions.toDouble(it)!!353 }354 checkAll(JSONGenerators.nonNumberLikes) {355 ReadOnlyJSONVal(it).asDoubleOrOne() shouldBe 1.0356 }357 ReadOnlyJSONVal(null).asDoubleOrOne() shouldBe 1.0358 }359 test("asDoubleOrMinusOne") {360 checkAll(JSONGenerators.numberLikes) {361 ReadOnlyJSONVal(it).asDoubleOrMinusOne() shouldBe Conversions.toDouble(it)!!362 }363 checkAll(JSONGenerators.nonNumberLikes) {364 ReadOnlyJSONVal(it).asDoubleOrMinusOne() shouldBe -1.0365 }366 ReadOnlyJSONVal(null).asDoubleOrMinusOne() shouldBe -1.0367 }368 test("asDoubleOrNaN") {369 checkAll(JSONGenerators.numberLikes) {370 ReadOnlyJSONVal(it).asDoubleOrNaN() shouldBe Conversions.toDouble(it)!!371 }372 checkAll(JSONGenerators.nonNumberLikes) {373 ReadOnlyJSONVal(it).asDoubleOrNaN() shouldBe Double.NaN374 }375 ReadOnlyJSONVal(null).asDoubleOrNaN() shouldBe Double.NaN376 }377 test("asDoubleOrElse") {378 checkAll(JSONGenerators.numberLikes * JSONGenerators.doubles) { data ->379 ReadOnlyJSONVal(data.first).asDoubleOrElse { data.second } shouldBe Conversions.toDouble(data.first)!!380 }381 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.doubles) { data ->382 ReadOnlyJSONVal(data.first).asDoubleOrElse { data.second } shouldBe data.second383 }384 checkAll(JSONGenerators.doubles) {385 ReadOnlyJSONVal(null).asDoubleOrElse { it } shouldBe it386 }387 }388 test("isEnum") {389 checkAll(Exhaustive.enum<TestEnum>()) {390 ReadOnlyJSONVal(it).isEnum<TestEnum>().shouldBeTrue()391 ReadOnlyJSONVal(it).isEnum(TestEnum::class.java).shouldBeTrue()392 }393 checkAll(JSONGenerators.values) {394 ReadOnlyJSONVal(it).isEnum<TestEnum>().shouldBeFalse()395 ReadOnlyJSONVal(it).isEnum(TestEnum::class.java).shouldBeFalse()396 }397 ReadOnlyJSONVal(null).isEnum<TestEnum>().shouldBeFalse()398 ReadOnlyJSONVal(null).isEnum(TestEnum::class.java).shouldBeFalse()399 }400 test("asEnumOrNull") {401 checkAll(Exhaustive.enum<TestEnum>()) {402 ReadOnlyJSONVal(it).asEnumOrNull<TestEnum>() shouldBe it403 ReadOnlyJSONVal(it).asEnumOrNull(TestEnum::class.java) shouldBe it404 }405 checkAll(JSONGenerators.values) {406 ReadOnlyJSONVal(it).asEnumOrNull<TestEnum>().shouldBeNull()407 ReadOnlyJSONVal(it).asEnumOrNull(TestEnum::class.java).shouldBeNull()408 }409 ReadOnlyJSONVal(null).asEnumOrNull<TestEnum>().shouldBeNull()410 ReadOnlyJSONVal(null).asEnumOrNull(TestEnum::class.java).shouldBeNull()411 }412 test("asEnumOrDefault") {413 checkAll(Exhaustive.enum<TestEnum>() cross Exhaustive.enum<TestEnum>()) {414 ReadOnlyJSONVal(it.first).asEnumOrDefault(it.second) shouldBe it.first415 }416 checkAll(JSONGenerators.values cross Exhaustive.enum<TestEnum>()) {417 ReadOnlyJSONVal(it.first).asEnumOrDefault(it.second) shouldBe it.second418 }419 checkAll(Exhaustive.enum<TestEnum>()) {420 ReadOnlyJSONVal(null).asEnumOrDefault(it) shouldBe it421 }422 }423 test("asEnumOrElse") {424 checkAll(Exhaustive.enum<TestEnum>() cross Exhaustive.enum<TestEnum>()) {425 ReadOnlyJSONVal(it.first).asEnumOrElse { it.second } shouldBe it.first426 ReadOnlyJSONVal(it.first).asEnumOrElse(TestEnum::class.java) { it.second } shouldBe it.first427 }428 checkAll(JSONGenerators.values cross Exhaustive.enum<TestEnum>()) {429 ReadOnlyJSONVal(it.first).asEnumOrElse { it.second } shouldBe it.second430 ReadOnlyJSONVal(it.first).asEnumOrElse(TestEnum::class.java) { it.second } shouldBe it.second431 }432 checkAll(Exhaustive.enum<TestEnum>()) {433 ReadOnlyJSONVal(null).asEnumOrElse { it } shouldBe it434 ReadOnlyJSONVal(null).asEnumOrElse(TestEnum::class.java) { it } shouldBe it435 }436 }437 test("isFloat") {438 checkAll(JSONGenerators.numberLikes) {439 ReadOnlyJSONVal(it).isFloat().shouldBeTrue()440 }441 checkAll(JSONGenerators.nonNumberLikes) {442 ReadOnlyJSONVal(it).isFloat().shouldBeFalse()443 }444 ReadOnlyJSONVal(null).isFloat().shouldBeFalse()445 }446 test("asFloatOrNull") {447 checkAll(JSONGenerators.numberLikes) {448 ReadOnlyJSONVal(it).asFloatOrNull() shouldBe Conversions.toFloat(it)!!449 }450 checkAll(JSONGenerators.nonNumberLikes) {451 ReadOnlyJSONVal(it).asFloatOrNull().shouldBeNull()452 }453 ReadOnlyJSONVal(null).asFloatOrNull().shouldBeNull()454 }455 test("asFloatOrDefault") {456 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) { data ->457 ReadOnlyJSONVal(data.first).asFloatOrDefault(data.second) shouldBe Conversions.toFloat(data.first)!!458 }459 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) { data ->460 ReadOnlyJSONVal(data.first).asFloatOrDefault(data.second) shouldBe data.second461 }462 checkAll(JSONGenerators.floats) {463 ReadOnlyJSONVal(null).asFloatOrDefault(it) shouldBe it464 }465 }466 test("asFloatOrZero") {467 checkAll(JSONGenerators.numberLikes) {468 ReadOnlyJSONVal(it).asFloatOrZero() shouldBe Conversions.toFloat(it)!!469 }470 checkAll(JSONGenerators.nonNumberLikes) {471 ReadOnlyJSONVal(it).asFloatOrZero() shouldBe 0.0f472 }473 ReadOnlyJSONVal(null).asFloatOrZero() shouldBe 0.0f474 }475 test("asFloatOrOne") {476 checkAll(JSONGenerators.numberLikes) {477 ReadOnlyJSONVal(it).asFloatOrOne() shouldBe Conversions.toFloat(it)!!478 }479 checkAll(JSONGenerators.nonNumberLikes) {480 ReadOnlyJSONVal(it).asFloatOrOne() shouldBe 1.0f481 }482 ReadOnlyJSONVal(null).asFloatOrOne() shouldBe 1.0f483 }484 test("asFloatOrMinusOne") {485 checkAll(JSONGenerators.numberLikes) {486 ReadOnlyJSONVal(it).asFloatOrMinusOne() shouldBe Conversions.toFloat(it)!!487 }488 checkAll(JSONGenerators.nonNumberLikes) {489 ReadOnlyJSONVal(it).asFloatOrMinusOne() shouldBe -1.0f490 }491 ReadOnlyJSONVal(null).asFloatOrMinusOne() shouldBe -1.0f492 }493 test("asFloatOrNaN") {494 checkAll(JSONGenerators.numberLikes) {495 ReadOnlyJSONVal(it).asFloatOrNaN() shouldBe Conversions.toFloat(it)!!496 }497 checkAll(JSONGenerators.nonNumberLikes) {498 ReadOnlyJSONVal(it).asFloatOrNaN() shouldBe Float.NaN499 }500 ReadOnlyJSONVal(null).asFloatOrNaN() shouldBe Float.NaN501 }502 test("asFloatOrElse") {503 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) { data ->504 ReadOnlyJSONVal(data.first).asFloatOrElse { data.second } shouldBe Conversions.toFloat(data.first)!!505 }506 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) { data ->507 ReadOnlyJSONVal(data.first).asFloatOrElse { data.second } shouldBe data.second508 }509 checkAll(JSONGenerators.floats) {510 ReadOnlyJSONVal(null).asFloatOrElse { it } shouldBe it511 }512 }513 test("isInt") {514 checkAll(JSONGenerators.intLikes) {515 ReadOnlyJSONVal(it).isInt().shouldBeTrue()516 }517 checkAll(JSONGenerators.nonIntLikes) {518 ReadOnlyJSONVal(it).isInt().shouldBeFalse()519 }520 ReadOnlyJSONVal(null).isInt().shouldBeFalse()521 }522 test("asIntOrNull") {523 checkAll(JSONGenerators.intLikes) {524 ReadOnlyJSONVal(it).asIntOrNull() shouldBe Conversions.toInt(it)!!525 }526 checkAll(JSONGenerators.nonIntLikes) {527 ReadOnlyJSONVal(it).asIntOrNull().shouldBeNull()528 }529 ReadOnlyJSONVal(null).asIntOrNull().shouldBeNull()530 }531 test("asIntOrDefault") {532 checkAll(JSONGenerators.intLikes * JSONGenerators.ints) { data ->533 ReadOnlyJSONVal(data.first).asIntOrDefault(data.second) shouldBe Conversions.toInt(data.first)!!534 }535 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.ints) { data ->536 ReadOnlyJSONVal(data.first).asIntOrDefault(data.second) shouldBe data.second537 }538 checkAll(JSONGenerators.ints) {539 ReadOnlyJSONVal(null).asIntOrDefault(it) shouldBe it540 }541 }542 test("asIntOrZero") {543 checkAll(JSONGenerators.intLikes) {544 ReadOnlyJSONVal(it).asIntOrZero() shouldBe Conversions.toInt(it)!!545 }546 checkAll(JSONGenerators.nonIntLikes) {547 ReadOnlyJSONVal(it).asIntOrZero() shouldBe 0548 }549 ReadOnlyJSONVal(null).asIntOrZero() shouldBe 0550 }551 test("asIntOrOne") {552 checkAll(JSONGenerators.intLikes) {553 ReadOnlyJSONVal(it).asIntOrOne() shouldBe Conversions.toInt(it)!!554 }555 checkAll(JSONGenerators.nonIntLikes) {556 ReadOnlyJSONVal(it).asIntOrOne() shouldBe 1557 }558 ReadOnlyJSONVal(null).asIntOrOne() shouldBe 1559 }560 test("asIntOrMinusOne") {561 checkAll(JSONGenerators.intLikes) {562 ReadOnlyJSONVal(it).asIntOrMinusOne() shouldBe Conversions.toInt(it)!!563 }564 checkAll(JSONGenerators.nonIntLikes) {565 ReadOnlyJSONVal(it).asIntOrMinusOne() shouldBe -1566 }567 ReadOnlyJSONVal(null).asIntOrMinusOne() shouldBe -1568 }569 test("asIntOrElse") {570 checkAll(JSONGenerators.intLikes * JSONGenerators.ints) { data ->571 ReadOnlyJSONVal(data.first).asIntOrElse { data.second } shouldBe Conversions.toInt(data.first)!!572 }573 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.ints) { data ->574 ReadOnlyJSONVal(data.first).asIntOrElse { data.second } shouldBe data.second575 }576 checkAll(JSONGenerators.ints) {577 ReadOnlyJSONVal(null).asIntOrElse { it } shouldBe it578 }579 }580 test("isLong") {581 checkAll(JSONGenerators.intLikes) {582 ReadOnlyJSONVal(it).isLong().shouldBeTrue()583 }584 checkAll(JSONGenerators.nonIntLikes) {585 ReadOnlyJSONVal(it).isLong().shouldBeFalse()586 }587 ReadOnlyJSONVal(null).isLong().shouldBeFalse()588 }589 test("asLongOrNull") {590 checkAll(JSONGenerators.intLikes) {591 ReadOnlyJSONVal(it).asLongOrNull() shouldBe Conversions.toLong(it)!!592 }593 checkAll(JSONGenerators.nonIntLikes) {594 ReadOnlyJSONVal(it).asLongOrNull().shouldBeNull()595 }596 ReadOnlyJSONVal(null).asLongOrNull().shouldBeNull()597 }598 test("asLongOrDefault") {599 checkAll(JSONGenerators.intLikes * JSONGenerators.longs) { data ->600 ReadOnlyJSONVal(data.first).asLongOrDefault(data.second) shouldBe Conversions.toLong(data.first)!!601 }602 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.longs) { data ->603 ReadOnlyJSONVal(data.first).asLongOrDefault(data.second) shouldBe data.second604 }605 checkAll(JSONGenerators.longs) {606 ReadOnlyJSONVal(null).asLongOrDefault(it) shouldBe it607 }608 }609 test("asLongOrZero") {610 checkAll(JSONGenerators.intLikes) {611 ReadOnlyJSONVal(it).asLongOrZero() shouldBe Conversions.toLong(it)!!612 }613 checkAll(JSONGenerators.nonIntLikes) {614 ReadOnlyJSONVal(it).asLongOrZero() shouldBe 0L615 }616 ReadOnlyJSONVal(null).asLongOrZero() shouldBe 0L617 }618 test("asLongOrOne") {619 checkAll(JSONGenerators.intLikes) {620 ReadOnlyJSONVal(it).asLongOrOne() shouldBe Conversions.toLong(it)!!621 }622 checkAll(JSONGenerators.nonIntLikes) {623 ReadOnlyJSONVal(it).asLongOrOne() shouldBe 1L624 }625 ReadOnlyJSONVal(null).asLongOrOne() shouldBe 1L626 }627 test("asLongOrMinusOne") {628 checkAll(JSONGenerators.intLikes) {629 ReadOnlyJSONVal(it).asLongOrMinusOne() shouldBe Conversions.toLong(it)!!630 }631 checkAll(JSONGenerators.nonIntLikes) {632 ReadOnlyJSONVal(it).asLongOrMinusOne() shouldBe -1L633 }634 ReadOnlyJSONVal(null).asLongOrMinusOne() shouldBe -1L635 }636 test("asLongOrElse") {637 checkAll(JSONGenerators.intLikes * JSONGenerators.longs) { data ->638 ReadOnlyJSONVal(data.first).asLongOrElse { data.second } shouldBe Conversions.toLong(data.first)!!639 }640 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.longs) { data ->641 ReadOnlyJSONVal(data.first).asLongOrElse { data.second } shouldBe data.second642 }643 checkAll(JSONGenerators.longs) {644 ReadOnlyJSONVal(null).asLongOrElse { it } shouldBe it645 }646 }647 test("isNumber") {648 checkAll(JSONGenerators.numberLikes) {649 ReadOnlyJSONVal(it).isNumber().shouldBeTrue()650 }651 checkAll(JSONGenerators.nonNumberLikes) {652 ReadOnlyJSONVal(it).isNumber().shouldBeFalse()653 }654 ReadOnlyJSONVal(null).isNumber().shouldBeFalse()655 }656 test("asNumberOrNull") {657 checkAll(JSONGenerators.numberLikes) {658 ReadOnlyJSONVal(it).asNumberOrNull() shouldBe Conversions.toNumber(it)!!659 }660 checkAll(JSONGenerators.nonNumberLikes) {661 ReadOnlyJSONVal(it).asNumberOrNull().shouldBeNull()662 }663 ReadOnlyJSONVal(null).asNumberOrNull().shouldBeNull()664 }665 test("asNumberOrDefault") {666 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) { data ->667 ReadOnlyJSONVal(data.first).asNumberOrDefault(data.second) shouldBe Conversions.toNumber(data.first)!!668 }669 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) { data ->670 ReadOnlyJSONVal(data.first).asNumberOrDefault(data.second) shouldBe data.second671 }672 checkAll(JSONGenerators.floats) {673 ReadOnlyJSONVal(null).asNumberOrDefault(it) shouldBe it674 }675 }676 test("asNumberOrElse") {677 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) { data ->678 ReadOnlyJSONVal(data.first).asNumberOrElse { data.second } shouldBe Conversions.toNumber(data.first)!!679 }680 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) { data ->681 ReadOnlyJSONVal(data.first).asNumberOrElse { data.second } shouldBe data.second682 }683 checkAll(JSONGenerators.floats) {684 ReadOnlyJSONVal(null).asNumberOrElse { it } shouldBe it685 }686 }687 test("isObject") {688 checkAll(JSONGenerators.objects) {689 ReadOnlyJSONVal(it).isObject().shouldBeTrue()690 }691 checkAll(JSONGenerators.nonObjects) {692 ReadOnlyJSONVal(it).isObject().shouldBeFalse()693 }694 ReadOnlyJSONVal(null).isObject().shouldBeFalse()695 }696 test("asObjectOrNull") {697 checkAll(JSONGenerators.objects) {698 ReadOnlyJSONVal(it).asObjectOrNull() shouldBeSimilarTo it699 }700 checkAll(JSONGenerators.nonObjects) {701 ReadOnlyJSONVal(it).asObjectOrNull().shouldBeNull()702 }703 ReadOnlyJSONVal(null).asObjectOrNull().shouldBeNull()704 }705 test("asObjectOrDefault") {706 checkAll(JSONGenerators.objects * JSONGenerators.objects) { data ->707 ReadOnlyJSONVal(data.first).asObjectOrDefault(data.second.asReadOnly()) shouldBeSimilarTo data.first708 }709 checkAll(JSONGenerators.nonObjects * JSONGenerators.objects) { data ->710 ReadOnlyJSONVal(data.first).asObjectOrDefault(data.second.asReadOnly()) shouldBeSimilarTo data.second711 }712 checkAll(JSONGenerators.objects) {713 ReadOnlyJSONVal(null).asObjectOrDefault(it.asReadOnly()) shouldBeSimilarTo it714 }715 }716 test("asObjectOrEmpty") {717 checkAll(JSONGenerators.objects) {718 ReadOnlyJSONVal(it).asObjectOrEmpty() shouldBeSimilarTo it719 }720 checkAll(JSONGenerators.nonObjects) {721 ReadOnlyJSONVal(it).asObjectOrEmpty() shouldBeSimilarTo ReadOnlyJSONObject.EMPTY722 }723 ReadOnlyJSONVal(null).asObjectOrEmpty() shouldBeSimilarTo ReadOnlyJSONObject.EMPTY724 }725 test("asObjectOrElse") {726 checkAll(JSONGenerators.objects * JSONGenerators.objects) { data ->727 ReadOnlyJSONVal(data.first).asObjectOrElse { data.second.asReadOnly() } shouldBeSimilarTo data.first728 }729 checkAll(JSONGenerators.nonObjects * JSONGenerators.objects) { data ->730 ReadOnlyJSONVal(data.first).asObjectOrElse { data.second.asReadOnly() } shouldBeSimilarTo data.second731 }732 checkAll(JSONGenerators.objects) {733 ReadOnlyJSONVal(null).asObjectOrElse { it.asReadOnly() } shouldBeSimilarTo it734 }735 }736 test("isString") {737 checkAll(JSONGenerators.allStrings) {738 ReadOnlyJSONVal(it).isString().shouldBeTrue()739 ReadOnlyJSONVal(it).isString(false).shouldBeTrue()740 ReadOnlyJSONVal(it).isString(true).shouldBeTrue()741 }742 checkAll(JSONGenerators.nonStrings) {743 ReadOnlyJSONVal(it).isString().shouldBeFalse()744 ReadOnlyJSONVal(it).isString(false).shouldBeFalse()745 ReadOnlyJSONVal(it).isString(true).shouldBeTrue()746 }747 ReadOnlyJSONVal(null).isString().shouldBeFalse()748 ReadOnlyJSONVal(null).isString(false).shouldBeFalse()749 ReadOnlyJSONVal(null).isString(true).shouldBeFalse()750 }751 test("asStringOrNull") {752 checkAll(JSONGenerators.allStrings) {753 ReadOnlyJSONVal(it).asStringOrNull() shouldBe it754 ReadOnlyJSONVal(it).asStringOrNull(false) shouldBe it755 ReadOnlyJSONVal(it).asStringOrNull(true) shouldBe it756 }757 checkAll(JSONGenerators.nonStrings) {758 ReadOnlyJSONVal(it).asStringOrNull().shouldBeNull()759 ReadOnlyJSONVal(it).asStringOrNull(false).shouldBeNull()760 ReadOnlyJSONVal(it).asStringOrNull(true) shouldBeSimilarStringAs it761 }762 ReadOnlyJSONVal(null).asStringOrNull().shouldBeNull()763 ReadOnlyJSONVal(null).asStringOrNull(false).shouldBeNull()764 ReadOnlyJSONVal(null).asStringOrNull(true).shouldBeNull()765 }766 test("asStringOrDefault") {767 checkAll(JSONGenerators.allStrings cross JSONGenerators.basicStrings) {768 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second) shouldBe it.first769 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second, false) shouldBe it.first770 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second, true) shouldBe it.first771 }772 checkAll(JSONGenerators.nonStrings cross JSONGenerators.basicStrings) {773 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second) shouldBe it.second774 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second, false) shouldBe it.second775 ReadOnlyJSONVal(it.first).asStringOrDefault(it.second, true) shouldBe Conversions.toString(it.first, true)776 }777 checkAll(JSONGenerators.basicStrings) {778 ReadOnlyJSONVal(null).asStringOrDefault(it) shouldBe it779 ReadOnlyJSONVal(null).asStringOrDefault(it, false) shouldBe it780 ReadOnlyJSONVal(null).asStringOrDefault(it, true) shouldBe it781 }782 }783 test("asStringOrEmpty") {784 checkAll(JSONGenerators.allStrings) {785 ReadOnlyJSONVal(it).asStringOrEmpty() shouldBe it786 ReadOnlyJSONVal(it).asStringOrEmpty(false) shouldBe it787 ReadOnlyJSONVal(it).asStringOrEmpty(true) shouldBe it788 }789 checkAll(JSONGenerators.nonStrings) {790 ReadOnlyJSONVal(it).asStringOrEmpty().shouldBeEmpty()791 ReadOnlyJSONVal(it).asStringOrEmpty(false).shouldBeEmpty()792 ReadOnlyJSONVal(it).asStringOrEmpty(true) shouldBeSimilarStringAs it793 }794 ReadOnlyJSONVal(null).asStringOrEmpty().shouldBeEmpty()795 ReadOnlyJSONVal(null).asStringOrEmpty().shouldBeEmpty()796 ReadOnlyJSONVal(null).asStringOrEmpty().shouldBeEmpty()797 }798 test("asStringOrElse") {799 checkAll(JSONGenerators.allStrings cross JSONGenerators.basicStrings) {800 ReadOnlyJSONVal(it.first).asStringOrElse { it.second } shouldBe it.first801 ReadOnlyJSONVal(it.first).asStringOrElse({ it.second }, false) shouldBe it.first802 ReadOnlyJSONVal(it.first).asStringOrElse(false) { it.second } shouldBe it.first803 ReadOnlyJSONVal(it.first).asStringOrElse({ it.second }, true) shouldBe Conversions.toString(it.first, true)804 ReadOnlyJSONVal(it.first).asStringOrElse(true) { it.second } shouldBe Conversions.toString(it.first, true)805 }806 checkAll(JSONGenerators.nonStrings cross JSONGenerators.basicStrings) {807 ReadOnlyJSONVal(it.first).asStringOrElse { it.second } shouldBe it.second808 ReadOnlyJSONVal(it.first).asStringOrElse({ it.second }, false) shouldBe it.second809 ReadOnlyJSONVal(it.first).asStringOrElse(false) { it.second } shouldBe it.second810 ReadOnlyJSONVal(it.first).asStringOrElse({ it.second }, true) shouldBe Conversions.toString(it.first, true)811 ReadOnlyJSONVal(it.first).asStringOrElse(true) { it.second } shouldBe Conversions.toString(it.first, true)812 }813 checkAll(JSONGenerators.basicStrings) {814 ReadOnlyJSONVal(null).asStringOrElse { it } shouldBe it815 ReadOnlyJSONVal(null).asStringOrElse({ it }, false) shouldBe it816 ReadOnlyJSONVal(null).asStringOrElse(false) { it } shouldBe it817 ReadOnlyJSONVal(null).asStringOrElse({ it }, true) shouldBe it818 ReadOnlyJSONVal(null).asStringOrElse(true) { it } shouldBe it819 }820 }821})...

Full Screen

Full Screen

ReadOnlyJSONArrayGetterTests.kt

Source:ReadOnlyJSONArrayGetterTests.kt Github

copy

Full Screen

1/*2 * Copyright (c) 2021 OneAndOlaf3 *4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated5 * documentation files (the “Software”), to deal in the Software without restriction, including without limitation the6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to7 * permit persons to whom the Software is furnished to do so, subject to the following conditions:8 *9 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the10 * Software.11 *12 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE13 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR14 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR15 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.16 */17package com.github.oneandolaf.jsonext.readonly18import com.github.oneandolaf.jsonext.extensions.deepCopy19import com.github.oneandolaf.jsonext.impl.Conversions20import com.github.oneandolaf.jsonext.testutils.JSONGenerators21import com.github.oneandolaf.jsonext.testutils.TestEnum22import com.github.oneandolaf.jsonext.testutils.shouldBeSimilarStringAs23import com.github.oneandolaf.jsonext.testutils.shouldBeSimilarTo24import io.kotest.core.spec.style.FunSpec25import io.kotest.matchers.nulls.shouldBeNull26import io.kotest.matchers.shouldBe27import io.kotest.matchers.string.shouldBeEmpty28import io.kotest.matchers.types.shouldBeInstanceOf29import io.kotest.property.Exhaustive30import io.kotest.property.checkAll31import io.kotest.property.exhaustive.enum32import io.kotest.property.exhaustive.times33import org.json.JSONArray34import org.json.JSONObject35import java.math.BigDecimal36import java.math.BigInteger37class ReadOnlyJSONArrayGetterTests : FunSpec({38 test("getOrNull") {39 checkAll(JSONGenerators.values) {40 val arr = JSONArray().put(it)41 val orig = arr.deepCopy()42 val ro = ReadOnlyJSONArray.create(arr)43 ro.getOrNull(0) shouldBeSimilarTo it44 if (it is JSONObject) {45 ro.getOrNull(0).shouldBeInstanceOf<ReadOnlyJSONObject>()46 } else if (it is JSONArray) {47 ro.getOrNull(0).shouldBeInstanceOf<ReadOnlyJSONArray>()48 }49 ro.getOrNull(1).shouldBeNull()50 ro.getOrNull(2).shouldBeNull()51 ro.getOrNull(42).shouldBeNull()52 ro.getOrNull(-1).shouldBeNull()53 ro.getOrNull(-10).shouldBeNull()54 arr shouldBeSimilarTo orig55 }56 }57 test("getOrElse") {58 checkAll(JSONGenerators.values * JSONGenerators.values) { data ->59 val arr = JSONArray().put(data.first)60 val orig = arr.deepCopy()61 val ro = ReadOnlyJSONArray.create(arr)62 ro.getOrElse(0) { data.second } shouldBeSimilarTo data.first63 ro.getOrElse(1) { data.second } shouldBeSimilarTo data.second64 arr shouldBeSimilarTo orig65 }66 }67 test("getArrayOrNull") {68 checkAll(JSONGenerators.arrays) {69 val arr = JSONArray().put(it)70 checkWithReadOnly(arr) {71 getArrayOrNull(0) shouldBeSimilarTo it72 }73 }74 checkAll(JSONGenerators.nonArrays) {75 val arr = JSONArray().put(it)76 checkWithReadOnly(arr) {77 getArrayOrNull(0).shouldBeNull()78 }79 }80 }81 test("getArrayOrDefault") {82 checkAll(JSONGenerators.arrays * JSONGenerators.arrays) {83 val arr = JSONArray().put(it.first)84 checkWithReadOnly(arr) {85 getArrayOrDefault(0, ReadOnlyJSONArray.create(it.second)) shouldBeSimilarTo it.first86 }87 }88 checkAll(JSONGenerators.nonArrays * JSONGenerators.arrays) {89 val arr = JSONArray().put(it.first)90 checkWithReadOnly(arr) {91 getArrayOrDefault(0, ReadOnlyJSONArray.create(it.second)) shouldBeSimilarTo it.second92 }93 }94 }95 test("getArrayOrEmpty") {96 checkAll(JSONGenerators.arrays) {97 val arr = JSONArray().put(it)98 checkWithReadOnly(arr) {99 getArrayOrEmpty(0) shouldBeSimilarTo it100 }101 }102 checkAll(JSONGenerators.nonArrays) {103 val arr = JSONArray().put(it)104 checkWithReadOnly(arr) {105 getArrayOrEmpty(0) shouldBeSimilarTo JSONArray()106 }107 }108 }109 test("getArrayOrElse") {110 checkAll(JSONGenerators.arrays * JSONGenerators.arrays) { data ->111 val arr = JSONArray().put(data.first)112 checkWithReadOnly(arr) {113 getArrayOrElse(0) {114 ReadOnlyJSONArray.create(data.second)115 } shouldBeSimilarTo data.first116 }117 }118 checkAll(JSONGenerators.nonArrays * JSONGenerators.arrays) { data ->119 val arr = JSONArray().put(data.first)120 checkWithReadOnly(arr) {121 getArrayOrElse(0) {122 ReadOnlyJSONArray.create(data.second)123 } shouldBeSimilarTo data.second124 }125 }126 }127 test("getBigDecimalOrNull") {128 checkAll(JSONGenerators.numberLikes) {129 val arr = JSONArray().put(it)130 checkWithReadOnly(arr) {131 getBigDecimalOrNull(0) shouldBe BigDecimal(it.toString())132 }133 }134 checkAll(JSONGenerators.nonNumberLikes) {135 val arr = JSONArray().put(it)136 checkWithReadOnly(arr) {137 getBigDecimalOrNull(0).shouldBeNull()138 }139 }140 }141 test("getBigDecimalOrDefault") {142 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigDecimals) {143 val arr = JSONArray().put(it.first)144 checkWithReadOnly(arr) {145 getBigDecimalOrDefault(0, it.second) shouldBe BigDecimal(it.first.toString())146 }147 }148 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigDecimals) {149 val arr = JSONArray().put(it.first)150 checkWithReadOnly(arr) {151 getBigDecimalOrDefault(0, it.second) shouldBe it.second152 }153 }154 }155 test("getBigDecimalOrZero") {156 checkAll(JSONGenerators.numberLikes) {157 val arr = JSONArray().put(it)158 checkWithReadOnly(arr) {159 getBigDecimalOrZero(0) shouldBe BigDecimal(it.toString())160 }161 }162 checkAll(JSONGenerators.nonNumberLikes) {163 val arr = JSONArray().put(it)164 checkWithReadOnly(arr) {165 getBigDecimalOrZero(0) shouldBe BigDecimal.ZERO166 }167 }168 }169 test("getBigDecimalOrOne") {170 checkAll(JSONGenerators.numberLikes) {171 val arr = JSONArray().put(it)172 checkWithReadOnly(arr) {173 getBigDecimalOrOne(0) shouldBe BigDecimal(it.toString())174 }175 }176 checkAll(JSONGenerators.nonNumberLikes) {177 val arr = JSONArray().put(it)178 checkWithReadOnly(arr) {179 getBigDecimalOrOne(0) shouldBe BigDecimal.ONE180 }181 }182 }183 test("getBigDecimalOrMinusOne") {184 checkAll(JSONGenerators.numberLikes) {185 val arr = JSONArray().put(it)186 checkWithReadOnly(arr) {187 getBigDecimalOrMinusOne(0) shouldBe BigDecimal(it.toString())188 }189 }190 checkAll(JSONGenerators.nonNumberLikes) {191 val arr = JSONArray().put(it)192 checkWithReadOnly(arr) {193 getBigDecimalOrMinusOne(0) shouldBe BigDecimal.valueOf(-1L)194 }195 }196 }197 test("getBigDecimalOrElse") {198 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigDecimals) { data ->199 val arr = JSONArray().put(data.first)200 checkWithReadOnly(arr) {201 getBigDecimalOrElse(0) { data.second } shouldBe BigDecimal(data.first.toString())202 }203 }204 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigDecimals) { data ->205 val arr = JSONArray().put(data.first)206 checkWithReadOnly(arr) {207 getBigDecimalOrElse(0) { data.second } shouldBe data.second208 }209 }210 }211 test("getBigIntegerOrNull") {212 checkAll(JSONGenerators.numberLikes) {213 val arr = JSONArray().put(it)214 checkWithReadOnly(arr) {215 getBigIntegerOrNull(0) shouldBe BigDecimal(it.toString()).toBigInteger()216 }217 }218 checkAll(JSONGenerators.nonNumberLikes) {219 val arr = JSONArray().put(it)220 checkWithReadOnly(arr) {221 getBigIntegerOrNull(0).shouldBeNull()222 }223 }224 }225 test("getBigIntegerOrDefault") {226 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigIntegers) {227 val arr = JSONArray().put(it.first)228 checkWithReadOnly(arr) {229 getBigIntegerOrDefault(0, it.second) shouldBe BigDecimal(it.first.toString()).toBigInteger()230 }231 }232 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigIntegers) {233 val arr = JSONArray().put(it.first)234 checkWithReadOnly(arr) {235 getBigIntegerOrDefault(0, it.second) shouldBe it.second236 }237 }238 }239 test("getBigIntegerOrZero") {240 checkAll(JSONGenerators.numberLikes) {241 val arr = JSONArray().put(it)242 checkWithReadOnly(arr) {243 getBigIntegerOrZero(0) shouldBe BigDecimal(it.toString()).toBigInteger()244 }245 }246 checkAll(JSONGenerators.nonNumberLikes) {247 val arr = JSONArray().put(it)248 checkWithReadOnly(arr) {249 getBigIntegerOrZero(0) shouldBe BigInteger.ZERO250 }251 }252 }253 test("getBigIntegerOrOne") {254 checkAll(JSONGenerators.numberLikes) {255 val arr = JSONArray().put(it)256 checkWithReadOnly(arr) {257 getBigIntegerOrOne(0) shouldBe BigDecimal(it.toString()).toBigInteger()258 }259 }260 checkAll(JSONGenerators.nonNumberLikes) {261 val arr = JSONArray().put(it)262 checkWithReadOnly(arr) {263 getBigIntegerOrOne(0) shouldBe BigInteger.ONE264 }265 }266 }267 test("getBigIntegerOrMinusOne") {268 checkAll(JSONGenerators.numberLikes) {269 val arr = JSONArray().put(it)270 checkWithReadOnly(arr) {271 getBigIntegerOrMinusOne(0) shouldBe BigDecimal(it.toString()).toBigInteger()272 }273 }274 checkAll(JSONGenerators.nonNumberLikes) {275 val arr = JSONArray().put(it)276 checkWithReadOnly(arr) {277 getBigIntegerOrMinusOne(0) shouldBe BigInteger.valueOf(-1L)278 }279 }280 }281 test("getBigIntegerOrElse") {282 checkAll(JSONGenerators.numberLikes * JSONGenerators.bigIntegers) { data ->283 val arr = JSONArray().put(data.first)284 checkWithReadOnly(arr) {285 getBigIntegerOrElse(0) { data.second } shouldBe BigDecimal(data.first.toString()).toBigInteger()286 }287 }288 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.bigIntegers) { data ->289 val arr = JSONArray().put(data.first)290 checkWithReadOnly(arr) {291 getBigIntegerOrElse(0) { data.second } shouldBe data.second292 }293 }294 }295 test("getBooleanOrNull") {296 checkAll(JSONGenerators.booleanLikes) {297 val arr = JSONArray().put(it)298 checkWithReadOnly(arr) {299 getBooleanOrNull(0) shouldBe Conversions.toBoolean(it)!!300 }301 }302 checkAll(JSONGenerators.nonBooleanLikes) {303 val arr = JSONArray().put(it)304 checkWithReadOnly(arr) {305 getBooleanOrNull(0).shouldBeNull()306 }307 }308 }309 test("getBooleanOrDefault") {310 checkAll(JSONGenerators.booleanLikes * JSONGenerators.bools) {311 val arr = JSONArray().put(it.first)312 checkWithReadOnly(arr) {313 getBooleanOrDefault(0, it.second) shouldBe Conversions.toBoolean(it.first)!!314 }315 }316 checkAll(JSONGenerators.nonBooleanLikes * JSONGenerators.bools) {317 val arr = JSONArray().put(it.first)318 checkWithReadOnly(arr) {319 getBooleanOrDefault(0, it.second) shouldBe it.second320 }321 }322 }323 test("getBooleanOrTrue") {324 checkAll(JSONGenerators.booleanLikes) {325 val arr = JSONArray().put(it)326 checkWithReadOnly(arr) {327 getBooleanOrTrue(0) shouldBe Conversions.toBoolean(it)!!328 }329 }330 checkAll(JSONGenerators.nonBooleanLikes) {331 val arr = JSONArray().put(it)332 checkWithReadOnly(arr) {333 getBooleanOrTrue(0) shouldBe true334 }335 }336 }337 test("getBooleanOrFalse") {338 checkAll(JSONGenerators.booleanLikes) {339 val arr = JSONArray().put(it)340 checkWithReadOnly(arr) {341 getBooleanOrFalse(0) shouldBe Conversions.toBoolean(it)!!342 }343 }344 checkAll(JSONGenerators.nonBooleanLikes) {345 val arr = JSONArray().put(it)346 checkWithReadOnly(arr) {347 getBooleanOrFalse(0) shouldBe false348 }349 }350 }351 test("getBooleanOrElse") {352 checkAll(JSONGenerators.booleanLikes * JSONGenerators.bools) { data ->353 val arr = JSONArray().put(data.first)354 checkWithReadOnly(arr) {355 getBooleanOrElse(0) { data.second } shouldBe Conversions.toBoolean(data.first)!!356 }357 }358 checkAll(JSONGenerators.nonBooleanLikes * JSONGenerators.bools) { data ->359 val arr = JSONArray().put(data.first)360 checkWithReadOnly(arr) {361 getBooleanOrElse(0) { data.second } shouldBe data.second362 }363 }364 }365 test("getDoubleOrNull") {366 checkAll(JSONGenerators.numberLikes) {367 val arr = JSONArray().put(it)368 checkWithReadOnly(arr) {369 getDoubleOrNull(0) shouldBe Conversions.toDouble(it)370 }371 }372 checkAll(JSONGenerators.nonNumberLikes) {373 val arr = JSONArray().put(it)374 checkWithReadOnly(arr) {375 getDoubleOrNull(0).shouldBeNull()376 }377 }378 }379 test("getDoubleOrDefault") {380 checkAll(JSONGenerators.numberLikes * JSONGenerators.doubles) {381 val arr = JSONArray().put(it.first)382 checkWithReadOnly(arr) {383 getDoubleOrDefault(0, it.second) shouldBe Conversions.toDouble(it.first)384 }385 }386 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.doubles) {387 val arr = JSONArray().put(it.first)388 checkWithReadOnly(arr) {389 getDoubleOrDefault(0, it.second) shouldBe it.second390 }391 }392 }393 test("getDoubleOrZero") {394 checkAll(JSONGenerators.numberLikes) {395 val arr = JSONArray().put(it)396 checkWithReadOnly(arr) {397 getDoubleOrZero(0) shouldBe Conversions.toDouble(it)398 }399 }400 checkAll(JSONGenerators.nonNumberLikes) {401 val arr = JSONArray().put(it)402 checkWithReadOnly(arr) {403 getDoubleOrZero(0) shouldBe 0.0404 }405 }406 }407 test("getDoubleOrOne") {408 checkAll(JSONGenerators.numberLikes) {409 val arr = JSONArray().put(it)410 checkWithReadOnly(arr) {411 getDoubleOrOne(0) shouldBe Conversions.toDouble(it)412 }413 }414 checkAll(JSONGenerators.nonNumberLikes) {415 val arr = JSONArray().put(it)416 checkWithReadOnly(arr) {417 getDoubleOrOne(0) shouldBe 1.0418 }419 }420 }421 test("getDoubleOrMinusOne") {422 checkAll(JSONGenerators.numberLikes) {423 val arr = JSONArray().put(it)424 checkWithReadOnly(arr) {425 getDoubleOrMinusOne(0) shouldBe Conversions.toDouble(it)426 }427 }428 checkAll(JSONGenerators.nonNumberLikes) {429 val arr = JSONArray().put(it)430 checkWithReadOnly(arr) {431 getDoubleOrMinusOne(0) shouldBe -1.0432 }433 }434 }435 test("getDoubleOrNaN") {436 checkAll(JSONGenerators.numberLikes) {437 val arr = JSONArray().put(it)438 checkWithReadOnly(arr) {439 getDoubleOrNaN(0) shouldBe Conversions.toDouble(it)440 }441 }442 checkAll(JSONGenerators.nonNumberLikes) {443 val arr = JSONArray().put(it)444 checkWithReadOnly(arr) {445 getDoubleOrNaN(0) shouldBe Double.NaN446 }447 }448 }449 test("getDoubleOrElse") {450 checkAll(JSONGenerators.numberLikes * JSONGenerators.doubles) { data ->451 val arr = JSONArray().put(data.first)452 checkWithReadOnly(arr) {453 getDoubleOrElse(0) { data.second } shouldBe Conversions.toDouble(data.first)454 }455 }456 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.doubles) { data ->457 val arr = JSONArray().put(data.first)458 checkWithReadOnly(arr) {459 getDoubleOrElse(0) { data.second } shouldBe data.second460 }461 }462 }463 test("getEnumOrNull") {464 checkAll(Exhaustive.enum<TestEnum>()) {465 val arr = JSONArray().put(it.name)466 checkWithReadOnly(arr) {467 getEnumOrNull(TestEnum::class.java, 0) shouldBe it468 getEnumOrNull<TestEnum>(0) shouldBe it469 }470 }471 checkAll(JSONGenerators.values) {472 val arr = JSONArray().put(it)473 checkWithReadOnly(arr) {474 getEnumOrNull(TestEnum::class.java, 0).shouldBeNull()475 getEnumOrNull<TestEnum>(0).shouldBeNull()476 }477 }478 }479 test("getEnumOrDefault") {480 checkAll(Exhaustive.enum<TestEnum>() * Exhaustive.enum()) {481 val arr = JSONArray().put(it.first.name)482 checkWithReadOnly(arr) {483 getEnumOrDefault(TestEnum::class.java, 0, it.second) shouldBe it.first484 getEnumOrDefault(0, it.second) shouldBe it.first485 }486 }487 checkAll(JSONGenerators.values * Exhaustive.enum<TestEnum>()) {488 val arr = JSONArray().put(it)489 checkWithReadOnly(arr) {490 getEnumOrDefault(TestEnum::class.java, 0, it.second) shouldBe it.second491 getEnumOrDefault(0, it.second) shouldBe it.second492 }493 }494 }495 test("getEnumOrElse") {496 checkAll(Exhaustive.enum<TestEnum>() * Exhaustive.enum()) { data ->497 val arr = JSONArray().put(data.first.name)498 checkWithReadOnly(arr) {499 getEnumOrElse(TestEnum::class.java, 0) { data.second } shouldBe data.first500 getEnumOrElse(0) { data.second } shouldBe data.first501 }502 }503 checkAll(JSONGenerators.values * Exhaustive.enum<TestEnum>()) { data ->504 val arr = JSONArray().put(data)505 checkWithReadOnly(arr) {506 getEnumOrElse(TestEnum::class.java, 0) { data.second } shouldBe data.second507 getEnumOrElse(0) { data.second } shouldBe data.second508 }509 }510 }511 test("getFloatOrNull") {512 checkAll(JSONGenerators.numberLikes) {513 val arr = JSONArray().put(it)514 checkWithReadOnly(arr) {515 getFloatOrNull(0) shouldBe Conversions.toFloat(it)516 }517 }518 checkAll(JSONGenerators.nonNumberLikes) {519 val arr = JSONArray().put(it)520 checkWithReadOnly(arr) {521 getFloatOrNull(0).shouldBeNull()522 }523 }524 }525 test("getFloatOrDefault") {526 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) {527 val arr = JSONArray().put(it.first)528 checkWithReadOnly(arr) {529 getFloatOrDefault(0, it.second) shouldBe Conversions.toFloat(it.first)530 }531 }532 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) {533 val arr = JSONArray().put(it.first)534 checkWithReadOnly(arr) {535 getFloatOrDefault(0, it.second) shouldBe it.second536 }537 }538 }539 test("getFloatOrZero") {540 checkAll(JSONGenerators.numberLikes) {541 val arr = JSONArray().put(it)542 checkWithReadOnly(arr) {543 getFloatOrZero(0) shouldBe Conversions.toFloat(it)544 }545 }546 checkAll(JSONGenerators.nonNumberLikes) {547 val arr = JSONArray().put(it)548 checkWithReadOnly(arr) {549 getFloatOrZero(0) shouldBe 0.0f550 }551 }552 }553 test("getFloatOrOne") {554 checkAll(JSONGenerators.numberLikes) {555 val arr = JSONArray().put(it)556 checkWithReadOnly(arr) {557 getFloatOrOne(0) shouldBe Conversions.toFloat(it)558 }559 }560 checkAll(JSONGenerators.nonNumberLikes) {561 val arr = JSONArray().put(it)562 checkWithReadOnly(arr) {563 getFloatOrOne(0) shouldBe 1.0f564 }565 }566 }567 test("getFloatOrMinusOne") {568 checkAll(JSONGenerators.numberLikes) {569 val arr = JSONArray().put(it)570 checkWithReadOnly(arr) {571 getFloatOrMinusOne(0) shouldBe Conversions.toFloat(it)572 }573 }574 checkAll(JSONGenerators.nonNumberLikes) {575 val arr = JSONArray().put(it)576 checkWithReadOnly(arr) {577 getFloatOrMinusOne(0) shouldBe -1.0f578 }579 }580 }581 test("getFloatOrNaN") {582 checkAll(JSONGenerators.numberLikes) {583 val arr = JSONArray().put(it)584 checkWithReadOnly(arr) {585 getFloatOrNaN(0) shouldBe Conversions.toFloat(it)586 }587 }588 checkAll(JSONGenerators.nonNumberLikes) {589 val arr = JSONArray().put(it)590 checkWithReadOnly(arr) {591 getFloatOrNaN(0) shouldBe Float.NaN592 }593 }594 }595 test("getFloatOrElse") {596 checkAll(JSONGenerators.numberLikes * JSONGenerators.floats) { data ->597 val arr = JSONArray().put(data.first)598 checkWithReadOnly(arr) {599 getFloatOrElse(0) { data.second } shouldBe Conversions.toFloat(data.first)600 }601 }602 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.floats) { data ->603 val arr = JSONArray().put(data.first)604 checkWithReadOnly(arr) {605 getFloatOrElse(0) { data.second } shouldBe data.second606 }607 }608 }609 test("getIntOrNull") {610 checkAll(JSONGenerators.intLikes) {611 val arr = JSONArray().put(it)612 checkWithReadOnly(arr) {613 getIntOrNull(0) shouldBe Conversions.toInt(it)!!614 }615 }616 checkAll(JSONGenerators.nonIntLikes) {617 val arr = JSONArray().put(it)618 checkWithReadOnly(arr) {619 getIntOrNull(0).shouldBeNull()620 }621 }622 }623 test("getIntOrDefault") {624 checkAll(JSONGenerators.intLikes * JSONGenerators.ints) {625 val arr = JSONArray().put(it.first)626 checkWithReadOnly(arr) {627 getIntOrDefault(0, it.second) shouldBe Conversions.toInt(it.first)628 }629 }630 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.ints) {631 val arr = JSONArray().put(it.first)632 checkWithReadOnly(arr) {633 getIntOrDefault(0, it.second) shouldBe it.second634 }635 }636 }637 test("getIntOrZero") {638 checkAll(JSONGenerators.intLikes) {639 val arr = JSONArray().put(it)640 checkWithReadOnly(arr) {641 getIntOrZero(0) shouldBe Conversions.toInt(it)642 }643 }644 checkAll(JSONGenerators.nonIntLikes) {645 val arr = JSONArray().put(it)646 checkWithReadOnly(arr) {647 getIntOrZero(0) shouldBe 0648 }649 }650 }651 test("getIntOrOne") {652 checkAll(JSONGenerators.intLikes) {653 val arr = JSONArray().put(it)654 checkWithReadOnly(arr) {655 getIntOrOne(0) shouldBe Conversions.toInt(it)656 }657 }658 checkAll(JSONGenerators.nonIntLikes) {659 val arr = JSONArray().put(it)660 checkWithReadOnly(arr) {661 getIntOrOne(0) shouldBe 1662 }663 }664 }665 test("getIntOrMinusOne") {666 checkAll(JSONGenerators.intLikes) {667 val arr = JSONArray().put(it)668 checkWithReadOnly(arr) {669 getIntOrMinusOne(0) shouldBe Conversions.toInt(it)670 }671 }672 checkAll(JSONGenerators.nonIntLikes) {673 val arr = JSONArray().put(it)674 checkWithReadOnly(arr) {675 getIntOrMinusOne(0) shouldBe -1676 }677 }678 }679 test("getIntOrElse") {680 checkAll(JSONGenerators.intLikes * JSONGenerators.ints) { data ->681 val arr = JSONArray().put(data.first)682 checkWithReadOnly(arr) {683 getIntOrElse(0) { data.second } shouldBe Conversions.toInt(data.first)684 }685 }686 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.ints) { data ->687 val arr = JSONArray().put(data.first)688 checkWithReadOnly(arr) {689 getIntOrElse(0) { data.second } shouldBe data.second690 }691 }692 }693 test("getLongOrNull") {694 checkAll(JSONGenerators.intLikes) {695 val arr = JSONArray().put(it)696 checkWithReadOnly(arr) {697 getLongOrNull(0) shouldBe Conversions.toLong(it)698 }699 }700 checkAll(JSONGenerators.nonIntLikes) {701 val arr = JSONArray().put(it)702 checkWithReadOnly(arr) {703 getLongOrNull(0).shouldBeNull()704 }705 }706 }707 test("getLongOrDefault") {708 checkAll(JSONGenerators.intLikes * JSONGenerators.longs) {709 val arr = JSONArray().put(it.first)710 checkWithReadOnly(arr) {711 getLongOrDefault(0, it.second) shouldBe Conversions.toLong(it.first)712 }713 }714 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.longs) {715 val arr = JSONArray().put(it.first)716 checkWithReadOnly(arr) {717 getLongOrDefault(0, it.second) shouldBe it.second718 }719 }720 }721 test("getLongOrZero") {722 checkAll(JSONGenerators.intLikes) {723 val arr = JSONArray().put(it)724 checkWithReadOnly(arr) {725 getLongOrZero(0) shouldBe Conversions.toLong(it)726 }727 }728 checkAll(JSONGenerators.nonIntLikes) {729 val arr = JSONArray().put(it)730 checkWithReadOnly(arr) {731 getLongOrZero(0) shouldBe 0L732 }733 }734 }735 test("getLongOrOne") {736 checkAll(JSONGenerators.intLikes) {737 val arr = JSONArray().put(it)738 checkWithReadOnly(arr) {739 getLongOrOne(0) shouldBe Conversions.toLong(it)740 }741 }742 checkAll(JSONGenerators.nonIntLikes) {743 val arr = JSONArray().put(it)744 checkWithReadOnly(arr) {745 getLongOrOne(0) shouldBe 1L746 }747 }748 }749 test("getLongOrMinusOne") {750 checkAll(JSONGenerators.intLikes) {751 val arr = JSONArray().put(it)752 checkWithReadOnly(arr) {753 getLongOrMinusOne(0) shouldBe Conversions.toLong(it)754 }755 }756 checkAll(JSONGenerators.nonIntLikes) {757 val arr = JSONArray().put(it)758 checkWithReadOnly(arr) {759 getLongOrMinusOne(0) shouldBe -1L760 }761 }762 }763 test("getLongOrElse") {764 checkAll(JSONGenerators.intLikes * JSONGenerators.longs) { data ->765 val arr = JSONArray().put(data.first)766 checkWithReadOnly(arr) {767 getLongOrElse(0) { data.second } shouldBe Conversions.toLong(data.first)768 }769 }770 checkAll(JSONGenerators.nonIntLikes * JSONGenerators.longs) { data ->771 val arr = JSONArray().put(data.first)772 checkWithReadOnly(arr) {773 getLongOrElse(0) { data.second } shouldBe data.second774 }775 }776 }777 test("getNumberOrNull") {778 checkAll(JSONGenerators.numberLikes) {779 val arr = JSONArray().put(it)780 checkWithReadOnly(arr) {781 getNumberOrNull(0)!!.toString() shouldBe it.toString()782 }783 }784 checkAll(JSONGenerators.nonNumberLikes) {785 val arr = JSONArray().put(it)786 checkWithReadOnly(arr) {787 getNumberOrNull(0).shouldBeNull()788 }789 }790 }791 test("getNumberOrDefault") {792 checkAll(JSONGenerators.numberLikes * JSONGenerators.numbers) {793 val arr = JSONArray().put(it.first)794 checkWithReadOnly(arr) {795 getNumberOrDefault(0, it.second).toString() shouldBe it.first.toString()796 }797 }798 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.numbers) {799 val arr = JSONArray().put(it.first)800 checkWithReadOnly(arr) {801 getNumberOrDefault(0, it.second) shouldBe it.second802 }803 }804 }805 test("getNumberOrElse") {806 checkAll(JSONGenerators.numberLikes * JSONGenerators.numbers) { data ->807 val arr = JSONArray().put(data.first)808 checkWithReadOnly(arr) {809 getNumberOrElse(0) { data.second }.toString() shouldBe data.first.toString()810 }811 }812 checkAll(JSONGenerators.nonNumberLikes * JSONGenerators.numbers) { data ->813 val arr = JSONArray().put(data.first)814 checkWithReadOnly(arr) {815 getNumberOrElse(0) { data.second } shouldBe data.second816 }817 }818 }819 test("getObjectOrNull") {820 checkAll(JSONGenerators.objects) {821 val arr = JSONArray().put(it)822 checkWithReadOnly(arr) {823 getObjectOrNull(0) shouldBeSimilarTo it824 }825 }826 checkAll(JSONGenerators.nonObjects) {827 val arr = JSONArray().put(it)828 checkWithReadOnly(arr) {829 getObjectOrNull(0).shouldBeNull()830 }831 }832 }833 test("getObjectOrDefault") {834 checkAll(JSONGenerators.objects * JSONGenerators.objects) {835 val arr = JSONArray().put(it.first)836 checkWithReadOnly(arr) {837 getObjectOrDefault(0, ReadOnlyJSONObject.create(it.second)) shouldBeSimilarTo it.first838 }839 }840 checkAll(JSONGenerators.nonObjects * JSONGenerators.objects) {841 val arr = JSONArray().put(it.first)842 checkWithReadOnly(arr) {843 getObjectOrDefault(0, ReadOnlyJSONObject.create(it.second)) shouldBeSimilarTo it.second844 }845 }846 }847 test("getObjectOrEmpty") {848 checkAll(JSONGenerators.objects) {849 val arr = JSONArray().put(it)850 checkWithReadOnly(arr) {851 getObjectOrEmpty(0) shouldBeSimilarTo it852 }853 }854 checkAll(JSONGenerators.nonObjects) {855 val arr = JSONArray().put(it)856 checkWithReadOnly(arr) {857 getObjectOrEmpty(0) shouldBeSimilarTo JSONObject()858 }859 }860 }861 test("getObjectOrElse") {862 checkAll(JSONGenerators.objects * JSONGenerators.objects) { data ->863 val arr = JSONArray().put(data.first)864 checkWithReadOnly(arr) {865 getObjectOrElse(0) {866 ReadOnlyJSONObject.create(data.second)867 } shouldBeSimilarTo data.first868 }869 }870 checkAll(JSONGenerators.nonObjects * JSONGenerators.objects) { data ->871 val arr = JSONArray().put(data.first)872 checkWithReadOnly(arr) {873 getObjectOrElse(0) {874 ReadOnlyJSONObject.create(data.second)875 } shouldBeSimilarTo data.second876 }877 }878 }879 test("getStringOrNull") {880 checkAll(JSONGenerators.allStrings) {881 val arr = JSONArray().put(it)882 checkWithReadOnly(arr) {883 getStringOrNull(0) shouldBe it884 getStringOrNull(0, false) shouldBe it885 getStringOrNull(0, true) shouldBe it886 }887 }888 checkAll(JSONGenerators.nonStrings) {889 val arr = JSONArray().put(it)890 checkWithReadOnly(arr) {891 getStringOrNull(0).shouldBeNull()892 getStringOrNull(0, false).shouldBeNull()893 getStringOrNull(0, true) shouldBeSimilarStringAs it894 }895 }896 }897 test("getStringOrDefault") {898 checkAll(JSONGenerators.allStrings * JSONGenerators.allStrings) {899 val arr = JSONArray().put(it.first)900 checkWithReadOnly(arr) {901 getStringOrDefault(0, it.second) shouldBe it.first902 getStringOrDefault(0, it.second, false) shouldBe it.first903 getStringOrDefault(0, it.second, true) shouldBe it.first904 }905 }906 checkAll(JSONGenerators.nonStrings * JSONGenerators.allStrings) {907 val arr = JSONArray().put(it.first)908 checkWithReadOnly(arr) {909 getStringOrDefault(0, it.second) shouldBe it.second910 getStringOrDefault(0, it.second, false) shouldBe it.second911 getStringOrDefault(0, it.second, true) shouldBeSimilarStringAs it.first912 }913 }914 }915 test("getStringOrEmpty") {916 checkAll(JSONGenerators.allStrings) {917 val arr = JSONArray().put(it)918 checkWithReadOnly(arr) {919 getStringOrEmpty(0) shouldBe it920 getStringOrEmpty(0, false) shouldBe it921 getStringOrEmpty(0, true) shouldBe it922 }923 }924 checkAll(JSONGenerators.nonStrings) {925 val arr = JSONArray().put(it)926 checkWithReadOnly(arr) {927 getStringOrEmpty(0).shouldBeEmpty()928 getStringOrEmpty(0, false).shouldBeEmpty()929 getStringOrEmpty(0, true) shouldBeSimilarStringAs it930 }931 }932 }933 test("getStringOrElse") {934 checkAll(JSONGenerators.allStrings * JSONGenerators.allStrings) { data ->935 val arr = JSONArray().put(data.first)936 checkWithReadOnly(arr) {937 getStringOrElse(0) { data.second } shouldBe data.first938 getStringOrElse(0, false) { data.second } shouldBe data.first939 getStringOrElse(0, true) { data.second } shouldBe data.first940 getStringOrElse(0, { data.second }, false) shouldBe data.first941 getStringOrElse(0, { data.second }, true) shouldBe data.first942 }943 }944 checkAll(JSONGenerators.nonStrings * JSONGenerators.allStrings) { data ->945 val arr = JSONArray().put(data.first)946 checkWithReadOnly(arr) {947 getStringOrElse(0) { data.second } shouldBe data.second948 getStringOrElse(0, false) { data.second } shouldBe data.second949 getStringOrElse(0, true) { data.second } shouldBeSimilarStringAs data.first950 getStringOrElse(0, { data.second }, false) shouldBe data.second951 getStringOrElse(0, { data.second }, true) shouldBeSimilarStringAs data.first952 }953 }954 }955})956private fun checkWithReadOnly(orig: JSONArray, func: ReadOnlyJSONArray.() -> Unit) {957 val ro = ReadOnlyJSONArray.snapshot(orig)958 ro.func()959 ro shouldBeSimilarTo orig960}...

Full Screen

Full Screen

ConversionsTests.kt

Source:ConversionsTests.kt Github

copy

Full Screen

1/*2 * Copyright (c) 2021 OneAndOlaf3 *4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated5 * documentation files (the “Software”), to deal in the Software without restriction, including without limitation the6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to7 * permit persons to whom the Software is furnished to do so, subject to the following conditions:8 *9 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the10 * Software.11 *12 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE13 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR14 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR15 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.16 */17package com.github.oneandolaf.jsonext.impl18import com.github.oneandolaf.jsonext.testutils.JSONGenerators19import com.github.oneandolaf.jsonext.testutils.TestEnum20import com.github.oneandolaf.jsonext.testutils.minus21import io.kotest.core.spec.style.FunSpec22import io.kotest.matchers.nulls.shouldBeNull23import io.kotest.matchers.nulls.shouldNotBeNull24import io.kotest.property.Exhaustive25import io.kotest.property.checkAll26import io.kotest.property.exhaustive.enum27import io.kotest.property.exhaustive.map28class ConversionsTests : FunSpec({29 test("toBigDecimal") {30 Conversions.toBigDecimal(null).shouldBeNull()31 checkAll(JSONGenerators.bigDecimals) {32 Conversions.toBigDecimal(it).shouldNotBeNull()33 }34 checkAll(JSONGenerators.bigIntegers) {35 Conversions.toBigDecimal(it).shouldNotBeNull()36 }37 checkAll(JSONGenerators.numbers) {38 Conversions.toBigDecimal(it).shouldNotBeNull()39 }40 Conversions.toBigDecimal(Double.NaN).shouldBeNull()41 Conversions.toBigDecimal(Double.POSITIVE_INFINITY).shouldBeNull()42 Conversions.toBigDecimal(Double.NEGATIVE_INFINITY).shouldBeNull()43 Conversions.toBigDecimal(Float.NaN).shouldBeNull()44 Conversions.toBigDecimal(Float.POSITIVE_INFINITY).shouldBeNull()45 Conversions.toBigDecimal(Float.NEGATIVE_INFINITY).shouldBeNull()46 checkAll(JSONGenerators.numericStrings) {47 Conversions.toBigDecimal(it).shouldNotBeNull()48 }49 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {50 Conversions.toBigDecimal(it).shouldBeNull()51 }52 checkAll(JSONGenerators.bools) {53 Conversions.toBigDecimal(it).shouldBeNull()54 }55 }56 test("toBigInteger") {57 Conversions.toBigInteger(null).shouldBeNull()58 checkAll(JSONGenerators.bigDecimals) {59 Conversions.toBigInteger(it).shouldNotBeNull()60 }61 checkAll(JSONGenerators.bigIntegers) {62 Conversions.toBigInteger(it).shouldNotBeNull()63 }64 checkAll(JSONGenerators.numbers) {65 Conversions.toBigInteger(it).shouldNotBeNull()66 }67 Conversions.toBigInteger(Double.NaN).shouldBeNull()68 Conversions.toBigInteger(Double.POSITIVE_INFINITY).shouldBeNull()69 Conversions.toBigInteger(Double.NEGATIVE_INFINITY).shouldBeNull()70 Conversions.toBigInteger(Float.NaN).shouldBeNull()71 Conversions.toBigInteger(Float.POSITIVE_INFINITY).shouldBeNull()72 Conversions.toBigInteger(Float.NEGATIVE_INFINITY).shouldBeNull()73 checkAll(JSONGenerators.numericStrings) {74 Conversions.toBigInteger(it).shouldNotBeNull()75 }76 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {77 Conversions.toBigInteger(it).shouldBeNull()78 }79 checkAll(JSONGenerators.bools) {80 Conversions.toBigInteger(it).shouldBeNull()81 }82 }83 test("toBoolean") {84 Conversions.toBoolean(null).shouldBeNull()85 checkAll(JSONGenerators.booleanLikes) {86 Conversions.toBoolean(it).shouldNotBeNull()87 }88 checkAll(JSONGenerators.nonBooleanLikes) {89 Conversions.toBoolean(it).shouldBeNull()90 }91 }92 test("toDouble") {93 Conversions.toDouble(null).shouldBeNull()94 checkAll(JSONGenerators.bigDecimals) {95 Conversions.toDouble(it).shouldNotBeNull()96 }97 checkAll(JSONGenerators.bigIntegers) {98 Conversions.toDouble(it).shouldNotBeNull()99 }100 checkAll(JSONGenerators.numbers) {101 Conversions.toDouble(it).shouldNotBeNull()102 }103 Conversions.toDouble(Double.NaN).shouldNotBeNull()104 Conversions.toDouble(Double.POSITIVE_INFINITY).shouldNotBeNull()105 Conversions.toDouble(Double.NEGATIVE_INFINITY).shouldNotBeNull()106 Conversions.toDouble(Float.NaN).shouldNotBeNull()107 Conversions.toDouble(Float.POSITIVE_INFINITY).shouldNotBeNull()108 Conversions.toDouble(Float.NEGATIVE_INFINITY).shouldNotBeNull()109 checkAll(JSONGenerators.numericStrings) {110 Conversions.toDouble(it).shouldNotBeNull()111 }112 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {113 Conversions.toDouble(it).shouldBeNull()114 }115 checkAll(JSONGenerators.bools) {116 Conversions.toDouble(it).shouldBeNull()117 }118 }119 test("toEnum") {120 Conversions.toEnum(null, TestEnum::class.java).shouldBeNull()121 Conversions.toEnum<TestEnum>(null).shouldBeNull()122 checkAll(JSONGenerators.values) {123 Conversions.toEnum(it, TestEnum::class.java).shouldBeNull()124 Conversions.toEnum<TestEnum>(it).shouldBeNull()125 }126 checkAll(Exhaustive.enum<TestEnum>()) {127 Conversions.toEnum(it, TestEnum::class.java).shouldNotBeNull()128 Conversions.toEnum<TestEnum>(it).shouldNotBeNull()129 }130 checkAll(Exhaustive.enum<TestEnum>().map { it.name }) {131 Conversions.toEnum(it, TestEnum::class.java).shouldNotBeNull()132 Conversions.toEnum<TestEnum>(it).shouldNotBeNull()133 }134 checkAll(Exhaustive.enum<TestEnum>().map { it.name.lowercase() }) {135 Conversions.toEnum(it, TestEnum::class.java).shouldBeNull()136 Conversions.toEnum<TestEnum>(it).shouldBeNull()137 }138 }139 test("toFloat") {140 Conversions.toFloat(null).shouldBeNull()141 checkAll(JSONGenerators.bigDecimals) {142 Conversions.toFloat(it).shouldNotBeNull()143 }144 checkAll(JSONGenerators.bigIntegers) {145 Conversions.toFloat(it).shouldNotBeNull()146 }147 checkAll(JSONGenerators.numbers) {148 Conversions.toFloat(it).shouldNotBeNull()149 }150 Conversions.toFloat(Double.NaN).shouldNotBeNull()151 Conversions.toFloat(Double.POSITIVE_INFINITY).shouldNotBeNull()152 Conversions.toFloat(Double.NEGATIVE_INFINITY).shouldNotBeNull()153 Conversions.toFloat(Float.NaN).shouldNotBeNull()154 Conversions.toFloat(Float.POSITIVE_INFINITY).shouldNotBeNull()155 Conversions.toFloat(Float.NEGATIVE_INFINITY).shouldNotBeNull()156 checkAll(JSONGenerators.numericStrings) {157 Conversions.toFloat(it).shouldNotBeNull()158 }159 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {160 Conversions.toFloat(it).shouldBeNull()161 }162 checkAll(JSONGenerators.bools) {163 Conversions.toFloat(it).shouldBeNull()164 }165 }166 test("toInt") {167 Conversions.toInt(null).shouldBeNull()168 checkAll(JSONGenerators.bigDecimals) {169 Conversions.toInt(it).shouldNotBeNull()170 }171 checkAll(JSONGenerators.bigIntegers) {172 Conversions.toInt(it).shouldNotBeNull()173 }174 checkAll(JSONGenerators.numbers) {175 Conversions.toInt(it).shouldNotBeNull()176 }177 Conversions.toInt(Double.NaN).shouldNotBeNull()178 Conversions.toInt(Double.POSITIVE_INFINITY).shouldNotBeNull()179 Conversions.toInt(Double.NEGATIVE_INFINITY).shouldNotBeNull()180 Conversions.toInt(Float.NaN).shouldNotBeNull()181 Conversions.toInt(Float.POSITIVE_INFINITY).shouldNotBeNull()182 Conversions.toInt(Float.NEGATIVE_INFINITY).shouldNotBeNull()183 checkAll(JSONGenerators.intStrings) {184 Conversions.toInt(it).shouldNotBeNull()185 }186 checkAll(JSONGenerators.doubleStrings) {187 Conversions.toInt(it).shouldBeNull()188 }189 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {190 Conversions.toInt(it).shouldBeNull()191 }192 checkAll(JSONGenerators.bools) {193 Conversions.toInt(it).shouldBeNull()194 }195 }196 test("toLong") {197 Conversions.toLong(null).shouldBeNull()198 checkAll(JSONGenerators.bigDecimals) {199 Conversions.toLong(it).shouldNotBeNull()200 }201 checkAll(JSONGenerators.bigIntegers) {202 Conversions.toLong(it).shouldNotBeNull()203 }204 checkAll(JSONGenerators.numbers) {205 Conversions.toLong(it).shouldNotBeNull()206 }207 Conversions.toLong(Double.NaN).shouldNotBeNull()208 Conversions.toLong(Double.POSITIVE_INFINITY).shouldNotBeNull()209 Conversions.toLong(Double.NEGATIVE_INFINITY).shouldNotBeNull()210 Conversions.toLong(Float.NaN).shouldNotBeNull()211 Conversions.toLong(Float.POSITIVE_INFINITY).shouldNotBeNull()212 Conversions.toLong(Float.NEGATIVE_INFINITY).shouldNotBeNull()213 checkAll(JSONGenerators.intStrings) {214 Conversions.toLong(it).shouldNotBeNull()215 }216 checkAll(JSONGenerators.doubleStrings) {217 Conversions.toLong(it).shouldBeNull()218 }219 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {220 Conversions.toLong(it).shouldBeNull()221 }222 checkAll(JSONGenerators.bools) {223 Conversions.toLong(it).shouldBeNull()224 }225 }226 test("toNumber") {227 Conversions.toNumber(null).shouldBeNull()228 checkAll(JSONGenerators.bigDecimals) {229 Conversions.toNumber(it).shouldNotBeNull()230 }231 checkAll(JSONGenerators.bigIntegers) {232 Conversions.toNumber(it).shouldNotBeNull()233 }234 checkAll(JSONGenerators.numbers) {235 Conversions.toNumber(it).shouldNotBeNull()236 }237 Conversions.toNumber(Double.NaN).shouldNotBeNull()238 Conversions.toNumber(Double.POSITIVE_INFINITY).shouldNotBeNull()239 Conversions.toNumber(Double.NEGATIVE_INFINITY).shouldNotBeNull()240 Conversions.toNumber(Float.NaN).shouldNotBeNull()241 Conversions.toNumber(Float.POSITIVE_INFINITY).shouldNotBeNull()242 Conversions.toNumber(Float.NEGATIVE_INFINITY).shouldNotBeNull()243 checkAll(JSONGenerators.numericStrings) {244 Conversions.toNumber(it).shouldNotBeNull()245 }246 checkAll(JSONGenerators.allStrings - JSONGenerators.numericStrings) {247 Conversions.toNumber(it).shouldBeNull()248 }249 checkAll(JSONGenerators.bools) {250 Conversions.toNumber(it).shouldBeNull()251 }252 }253 test("toString") {254 Conversions.toString(null, false).shouldBeNull()255 Conversions.toString(null, true).shouldBeNull()256 checkAll(JSONGenerators.allStrings) {257 Conversions.toString(it, false).shouldNotBeNull()258 Conversions.toString(it, true).shouldNotBeNull()259 }260 checkAll(JSONGenerators.nonStrings) {261 Conversions.toString(it, false).shouldBeNull()262 Conversions.toString(it, true).shouldNotBeNull()263 }264 }265})...

Full Screen

Full Screen

JSONGenerators.kt

Source:JSONGenerators.kt Github

copy

Full Screen

...100 * Strings that are representations of numbers.101 */102 val numericStrings = intStrings + doubleStrings103 /**104 * All strings.105 */106 val allStrings = basicStrings + booleanStrings + numericStrings107 val arrays = mutableExhaustive {108 listOf(109 JSONArray(),110 jsonArrayOf(""),111 jsonArrayOf("foo"),112 jsonArrayOf(true),113 jsonArrayOf(false),114 jsonArrayOf(42),115 jsonArrayOf(5.0),116 jsonArrayOf(emptyList<Any>()),117 jsonArrayOf(jsonArrayOf("foo")),118 jsonArrayOf(jsonObjectOf()),...

Full Screen

Full Screen

NbtEncodingTest.kt

Source:NbtEncodingTest.kt Github

copy

Full Screen

...97 element.shouldBeInstanceOf<LongArrayTag>()98 element.asLongArray shouldBe it.toLongArray()99 }100 }101 "enums should encode to strings" {102 checkAll(Exhaustive.enum<TestEnum>()) {103 val element = Nbt.encodeToNbtElement(it)104 element.shouldBeInstanceOf<StringTag>()105 element.asString shouldBe it.name106 }107 }108 "closed polymorphism should encode correctly" {109 val value = SealedChild1(1f, 2.5)110 val element = Nbt.encodeToNbtElement<SealedBase>(value)111 element.shouldBeInstanceOf<CompoundTag>()112 element.getString("type") shouldBe "child1"113 with(element.getCompound("value")) {114 getFloat("baseVal") shouldBe value.baseVal115 getDouble("childProp") shouldBe value.childProp...

Full Screen

Full Screen

NbtDecodingTest.kt

Source:NbtDecodingTest.kt Github

copy

Full Screen

...76 val nbt = it.toList().toNbt()77 Nbt.decodeFromNbtElement<Set<Long>>(nbt) shouldBe it78 }79 }80 "strings should decode to enums" {81 forAll(Exhaustive.enum<TestEnum>()) {82 Nbt.decodeFromNbtElement<TestEnum>(it.name.toNbt()) == it83 }84 }85 "closed polymorphism should decode correctly" {86 val value = SealedChild1(1f, 2.5)87 val compound = nbtCompound {88 put("type", "child1")89 compound("value") {90 put("baseVal", value.baseVal)91 put("childProp", value.childProp)92 }93 }94 Nbt.decodeFromNbtElement<SealedBase>(compound) shouldBe value...

Full Screen

Full Screen

ParserTest.kt

Source:ParserTest.kt Github

copy

Full Screen

...13import io.kotest.property.exhaustive.cartesian14import io.kotest.property.exhaustive.exhaustive15import io.kotest.property.exhaustive.merge16class ParserTest: StringSpec({17 val strings = listOf("\"hello\"", "\"hello world\"", "\"\"", "\")(\"", "\"()\"").exhaustive()18 val symbols = listOf("abc", "x", "x1", " a", " b").exhaustive()19 val atoms = symbols.merge(listOf("-1", "1", "1.0", "-1.0").exhaustive())20 val lists = Exhaustive.cartesian(atoms, atoms) { a, b -> listOf(a, b) }21 "parse token" {22 checkAll(symbols) { s ->23 val result = tokenize(s)24 result.forAll {25 it.shouldNotContain(" ")26 it.shouldNotBeEmpty()27 }28 result.forExactly(1) { it.shouldBe(s.trim()) }29 }30 }31 "parse with parens" {32 checkAll(symbols) { s ->33 val result = tokenize("($s)")34 result.forAll {35 it.shouldNotContain(" ")36 it.shouldNotBeEmpty()37 }38 result.forExactly(1) { it.shouldBe(s.trim()) }39 }40 }41 "parse with unbalanced parens" {42 checkAll(symbols) { s ->43 shouldThrow<SyntaxErr> {44 readFromTokens(tokenize("($s $s ($s ($s $s))")) // no closing paren45 }46 }47 }48 "parse strings" {49 val exp = readFromTokens(tokenize("\"well this is silly\""))50 exp.shouldBe(LString("well this is silly"))51 }52 "parse lists of strings" {53 checkAll(strings) { str ->54 val bare = str.substring(1, str.length - 1)55 val exp = readFromTokens(tokenize("($str ($str $str))"))56 exp.shouldBe(listExp(LString(bare), listExp(LString(bare), LString(bare))))57 }58 }59 "parse complex expressions" {60 val tokens = tokenize("(begin (define r 10.0) (* pi (* r r)))")61 tokens.shouldBe(listOf("(", "begin", "(", "define", "r", "10.0", ")", "(", "*", "pi", "(", "*", "r", "r", ")", ")", ")"))62 }63 "readFromTokens fails if empty" {64 shouldThrow<SyntaxErr> {65 readFromTokens(emptyList())66 }67 }68 "readFromTokens returns int" {69 checkAll<Int> { i ->70 readFromTokens(listOf(i.toString())).shouldBe(Num(i))71 }72 }73 "readFromTokens returns float" {74 checkAll<Float> { f ->75 readFromTokens(listOf(f.toString())).shouldBe(Num(f))76 }77 }78 "readFromTokens returns boolean" {79 checkAll<Boolean> { b ->80 readFromTokens(listOf(b.toString())).shouldBe(Bool(b))81 }82 }83 "readFromTokens returns string" {84 checkAll(strings) { str ->85 readFromTokens(listOf(str)).shouldBe(LString(str.substring(1, str.length - 1)))86 }87 }88 "readFromTokens returns symbol" {89 checkAll(symbols) { s ->90 readFromTokens(listOf(s)).shouldBe(Symbol(s))91 }92 }93 "readFromTokens fails on unmatched closing paren" {94 shouldThrow<SyntaxErr> {95 readFromTokens(listOf(")"))96 }97 }98 "readFromTokens returns list of atoms" {...

Full Screen

Full Screen

RomanNumeralsConverterTest.kt

Source:RomanNumeralsConverterTest.kt Github

copy

Full Screen

...26 fun `Conversion Roman to Arabic`(arabic: Int, roman: String) {27 converter.roman2arabic(roman) shouldBeExactly arabic28 }29 @ParameterizedTest(name = "{index} ==> {0} should throw")30 @ValueSource(strings = ["N", "ABC", "IL", "CDC"])31 fun `Roman to Arabic should fail if number is not a roman numeral`(n: String) {32 assertThrows<IllegalArgumentException> {33 converter.roman2arabic(n)34 }35 }36 @ParameterizedTest(name = "{index} ==> {0} should be {1}")37 @CsvFileSource(resources = ["RomanNumerals.csv"])38 fun `Conversion Arabic to Roman`(arabic: Int, roman: String) {39 converter.arabic2roman(arabic) shouldBe roman40 }41 @ParameterizedTest(name = "{index} ==> {0} should throw")42 @ValueSource(ints = [0, 4000])43 fun `Arabic to Roman should fail if number out of bounds`(n: Int) {44 assertThrows<IllegalArgumentException> {...

Full Screen

Full Screen

strings

Using AI Code Generation

copy

Full Screen

1val stringGen = strings()2val stringGen = strings(1, 10)3val stringGen = strings(1, 10, 'a', 'z')4val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z')5val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9')6val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ')7val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!')8val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#')9val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#', '$', '$')10val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#', '$', '$', '%', '%')11val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#', '$', '$', '%', '%', '&', '&')12val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#', '$', '$', '%', '%', '&', '&', '(', ')')13val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!', '#', '#', '$', '$', '%', '%', '&', '&', '(', ')', '*', '*')14val stringGen = strings(1, 10, 'a', 'z', 'A', 'Z', '0', '9', ' ', ' ', '!', '!',

Full Screen

Full Screen

strings

Using AI Code Generation

copy

Full Screen

1 val stringGen = strings(1..10, chars = ('a'..'z') + ('A'..'Z') + ('0'..'9'))2 val stringProperty = forAll(stringGen) { s ->3 }4 stringProperty.check(1000)5 val stringGen = string(1..10, chars = ('a'..'z') + ('A'..'Z') + ('0'..'9'))6 val stringProperty = forAll(stringGen) { s ->7 }8 stringProperty.check(1000)9 val intGen = ints(1..100)10 val intProperty = forAll(intGen) { i ->11 }12 intProperty.check(1000)13 val intGen = int(1..100)14 val intProperty = forAll(intGen) { i ->15 }16 intProperty.check(1000)17}18The check() function is used to run the test cases. The number of test cases is specified in the arguments of the check() function. The check()

Full Screen

Full Screen

strings

Using AI Code Generation

copy

Full Screen

1You can also use the strings() function to generate all strings of a given length within a given character set. For example, to generate all strings of length 2 within the character set "ab", we can use the following code:2The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:3The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:4The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:5The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:6The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:7The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:8The strings() function can also be used to generate random strings. For example, to generate random strings of length 2 within the character set "ab", we can use the following code:9The strings() function can also be used to generate random strings. For

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in strings

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful