From 404c276ad3eec12b3aba8850b5588b9c0b88ad27 Mon Sep 17 00:00:00 2001 From: Peter Wall Date: Wed, 11 Dec 2024 19:08:19 +1100 Subject: [PATCH] Added asNumber, switched to should-test --- CHANGELOG.md | 7 + README.md | 51 +- pom.xml | 8 +- src/main/kotlin/io/kjson/JSON.kt | 138 +- src/main/kotlin/io/kjson/JSONArray.kt | 2 +- src/main/kotlin/io/kjson/JSONTypeException.kt | 8 +- src/test/kotlin/io/kjson/JSONArrayTest.kt | 163 +- src/test/kotlin/io/kjson/JSONBooleanTest.kt | 52 +- src/test/kotlin/io/kjson/JSONDecimalTest.kt | 352 ++--- src/test/kotlin/io/kjson/JSONIntTest.kt | 202 +-- src/test/kotlin/io/kjson/JSONLongTest.kt | 242 +-- src/test/kotlin/io/kjson/JSONNumberTest.kt | 25 +- src/test/kotlin/io/kjson/JSONObjectTest.kt | 778 +++++----- src/test/kotlin/io/kjson/JSONStringTest.kt | 40 +- src/test/kotlin/io/kjson/JSONStructureTest.kt | 400 ++--- src/test/kotlin/io/kjson/JSONTest.kt | 1346 +++++++++-------- src/test/kotlin/io/kjson/JSONValueTest.kt | 47 +- .../io/kjson/parser/ParseOptionsTest.kt | 29 +- .../kotlin/io/kjson/parser/ParserArrayTest.kt | 79 +- .../io/kjson/parser/ParserKeywordTest.kt | 34 +- .../kotlin/io/kjson/parser/ParserLinesTest.kt | 84 +- .../io/kjson/parser/ParserNumberTest.kt | 150 +- .../io/kjson/parser/ParserObjectTest.kt | 155 +- .../io/kjson/parser/ParserStringTest.kt | 66 +- .../kotlin/io/kjson/parser/ParserUtilTest.kt | 54 +- .../kotlin/io/kjson/testutil/ImportTest.kt | 5 +- .../kotlin/io/kjson/util/LookupSetTest.kt | 14 +- 27 files changed, 2320 insertions(+), 2211 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf5c931..d0125e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). +## [9.2] - 2024-12-11 +### Changed +- `JSON`: added `asNumber`, `asNumberOrNull`, `asNumberOr()` and `asNumberOrError()` extension values / functions +- `JSON`, `JSONTypeException`: renamed parameter `target` to `expected` +- `JSON`: cosmetic changes to comments +- tests : switched to `should-test` library + ## [9.1] - 2024-08-17 ### Changed - `JSONString`: fixed bug in `toJSON()` diff --git a/README.md b/README.md index 1432f39..2295866 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ JSON Kotlin core library ## Background -The input of JSON data generally consists of two main phases – parsing the input text and converting the -human-readable form into an easily navigated internal representation, and then mapping that internal form into -pre-existing data types. +The input of JSON data generally consists of two main phases: +1. parsing the input text and converting the human-readable form into an easily navigated internal structure, and +2. mapping that internal form into pre-existing data types. Output may similarly use an intermediate form, but it is on the input side that the converted form is most useful – it allows, for example, all of the properties of an object to be analysed before the determination of the -appropriate representation for the object. +appropriate internal representation for the object. There are also many types of JSON processing functions that do not require mapping to a target class – they simply require an internal representation of the JSON data. @@ -25,12 +25,14 @@ The `kjson-core` library provides the basic functionality required to represent - output functions to create valid JSON representations from the internal form The library is an evolution of the [`jsonutil`](https://github.com/pwall567/jsonutil) Java library; it makes better use -of Kotlin-specific functionality like controlled nullability. +of Kotlin-specific functionality like controlled nullability, and it adds functions to simplify the navigation of the +internal structure. ## User Guide -All JSON values are represented by Kotlin objects of type `JSONValue?` – that is, they are all instances of -classes that implement the `JSONValue` interface, or in the case of the JSON “`null`” value they are `null`. +All JSON values are represented by Kotlin objects of type “`JSONValue?`” – that is, they are all +instances of classes that implement the `JSONValue` interface, or in the case of the JSON “`null`” value +they are `null`. ### `JSONValue` @@ -38,8 +40,8 @@ The `JSONValue` interface specifies four functions: - `appendTo()` – this appends the JSON text form of the object to a specified `Appendable`, _e.g._ a `Writer` (when outputting JSON, it is more efficient to append to a single `Appendable`, as opposed to creating strings for each element) -- `toJSON()` – this outputs the value in syntactically-correct JSON (a default implementation makes use of the - above `appendTo()` function) +- `toJSON()` – this creates a `String` representation of the value in syntactically-correct JSON (a default + implementation makes use of the above `appendTo()` function) - `outputTo()` – this outputs the JSON text form of the object using an `IntConsumer` (similar to `appendTo()`, but allowing a greater choice of output mechanism) - `coOutputTo()` (suspend function) – non-blocking version of `outputTo()`, suitable for use in a coroutine-based @@ -83,7 +85,7 @@ The value is never `null`. In addition to implementing [`JSONPrimitive`](#jsonprimitive), the number value classes [`JSONInt`](#jsonint), [`JSONLong`](#jsonlong) and [`JSONDecimal`](#jsondecimal) all derive from the sealed class `JSONNumber`, which itself -derives from the system class `Number`. +derives from the system class `java.lang.Number`. This means that these classes may be used without conversion anywhere a `Number` is called for. The `Number` class provides a set of `toInt()`, `toLong()` _etc._ functions, to which `JSONNumber` adds the following: @@ -337,12 +339,13 @@ The `JSONTypeException` provides a way of reporting such errors in a consistent the human-readable node name, the expected type, the actual value and an optional key (as described [above](#jsonexception)). -The `JSONTypeException` constructor takes the following parameters: +The `JSONTypeException` constructor takes the following parameters, all of which are accessible as values of the +exception object: | Name | Type | Default | Description | |------------|--------------|----------|-----------------------------------------------------| | `nodeName` | `String` | `"Node"` | The name of the field, _e.g._ `"Surname"` | -| `target` | `String` | | The expected type, _e.g._ `"string"` | +| `expected` | `String` | | The expected type, _e.g._ `"string"` | | `value` | `JSONValue?` | | The actual value found | | `key` | `Any?` | `null` | The “key” (the location in a structure) | @@ -456,7 +459,7 @@ It takes the following parameters: | Name | Type | Default | Description | |------------|----------|----------|-----------------------------------------------------| -| `target` | `String` | | The expected type, _e.g._ `"string"` | +| `expected` | `String` | | The expected type, _e.g._ `"string"` | | `key` | `Any?` | `null` | The “key” (the location in a structure) | | `nodeName` | `String` | `"Node"` | The name of the field, _e.g._ `"Surname"` | @@ -482,6 +485,7 @@ The conversion may be combined with the error reporting using the `asXxxxOrError | `JSONValue?.asUShortOrError()` | `UShort` | | `JSONValue?.asUByteOrError()` | `UByte` | | `JSONValue?.asDecimalOrError()` | `BigDecimal` | +| `JSONValue?.asNumberOrError()` | `Number` | | `JSONValue?.asBooleanOrError()` | `Boolean` | | `JSONValue?.asArrayOrError()` | `JSONArray` | | `JSONValue?.asObjectOrError()` | `JSONObject` | @@ -491,8 +495,8 @@ The `asArrayOrError()` and `asObjectOrError()` functions return `JSONArray` and of course be used as the underlying implementation types (`List` and `Map`). The functions all take the same parameters as the `typeError()` function (which they all call if the type is not -correct), but in the case of these functions, the `target` parameter also has a default value, a string representing the -target type. +correct), but in the case of these functions, the `expected` parameter also has a default value, a string representing +the expected type. Using these functions, the above example (for the use of `typeError`) may be written: ```kotlin @@ -525,6 +529,8 @@ To simplify casting a `JSONValue` to the expected type, the `JSON` object provid | `JSONValue?.asUByteOrNull` | `UByte?` | return `null` | | `JSONValue?.asDecimal` | `BigDecimal` | throw `JSONTypeException` | | `JSONValue?.asDecimalOrNull` | `BigDecimal?` | return `null` | +| `JSONValue?.asNumber` | `Number` | throw `JSONTypeException` | +| `JSONValue?.asNumberOrNull` | `Number?` | return `null` | | `JSONValue?.asBoolean` | `Boolean` | throw `JSONTypeException` | | `JSONValue?.asBooleanOrNull` | `Boolean?` | return `null` | | `JSONValue?.asArray` | `JSONArray` | throw `JSONTypeException` | @@ -533,7 +539,7 @@ To simplify casting a `JSONValue` to the expected type, the `JSON` object provid | `JSONValue?.asObjectOrNull` | `JSONObject?` | return `null` | The [`JSONTypeException`](#jsontypeexception) will use the default value `"Node"` for the `nodeName`, and the class name -of the target type as the default for `target`. +of the expected type as the default for `expected`. The default value for `key` is `null`. As with the `asXxxxOrError()` functions, the extension values representing a simple value return the actual value type, @@ -556,6 +562,7 @@ A further way of casting a `JSONValue` to the expected type is provided by the ` | `JSONValue?.asUShortOr()` | `UShort` | | `JSONValue?.asUByteOr()` | `UByte` | | `JSONValue?.asDecimalOr()` | `BigDecimal` | +| `JSONValue?.asNumberOr()` | `Number` | | `JSONValue?.asBooleanOr()` | `Boolean` | | `JSONValue?.asArrayOr()` | `JSONArray` | | `JSONValue?.asObjectOr()` | `JSONObject` | @@ -566,7 +573,7 @@ This may be used to provide a default value, silently ignoring the type error, b throw an exception. For example: ```kotlin - node.asStringOr { typeError(target = "string", key = "/person/surname", nodeName = "Surname") } + node.asStringOr { typeError(expected = "string", key = "/person/surname", nodeName = "Surname") } ``` The advantage of using these functions as compared to `asXxxxOrError()`, is that these functions are inline, and the @@ -694,25 +701,25 @@ The diagram was produced by [Dia](https://wiki.gnome.org/Apps/Dia/); the diagram ## Dependency Specification -The latest version of the library is 9.1, and it may be obtained from the Maven Central repository. +The latest version of the library is 9.2, and it may be obtained from the Maven Central repository. ### Maven ```xml io.kjson kjson-core - 9.1 + 9.2 ``` ### Gradle ```groovy - implementation "io.kjson:kjson-core:9.1" + implementation "io.kjson:kjson-core:9.2" ``` ### Gradle (kts) ```kotlin - implementation("io.kjson:kjson-core:9.1") + implementation("io.kjson:kjson-core:9.2") ``` Peter Wall -2024-08-17 +2024-12-11 diff --git a/pom.xml b/pom.xml index 87d3fc6..683f69e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 kjson-core - 9.1 + 9.2 JSON Kotlin core functionality JSON Kotlin core functionality jar @@ -99,6 +99,12 @@ org.jetbrains.kotlin kotlin-stdlib-jdk8 + + io.kstuff + should-test + 4.0 + test + net.pwall.json json-simple diff --git a/src/main/kotlin/io/kjson/JSON.kt b/src/main/kotlin/io/kjson/JSON.kt index 208eba5..924819f 100644 --- a/src/main/kotlin/io/kjson/JSON.kt +++ b/src/main/kotlin/io/kjson/JSON.kt @@ -138,21 +138,21 @@ object JSON { */ fun CharSequence.parseJSONValue( options: ParseOptions = ParseOptions.DEFAULT, - ): JSONValue = parseNonNull(this.toString(), options) + ): JSONValue = parseNonNull(toString(), options) /** * Parse the receiver [CharSequence] (_e.g._ [String]) to a [JSONArray]. */ fun CharSequence.parseJSONArray( options: ParseOptions = ParseOptions.DEFAULT, - ): JSONArray = parseArray(this.toString(), options) + ): JSONArray = parseArray(toString(), options) /** * Parse the receiver [CharSequence] (_e.g._ [String]) to a [JSONObject]. */ fun CharSequence.parseJSONObject( options: ParseOptions = ParseOptions.DEFAULT, - ): JSONObject = parseObject(this.toString(), options) + ): JSONObject = parseObject(toString(), options) /** * Convert the receiver [JSONValue] to a JSON string (`"null"` if the receiver is `null`). @@ -294,107 +294,115 @@ object JSON { } } - /** The value of the receiver [JSONValue] as a [String]. */ + /** The value of the receiver [JSONValue] as a [String]. */ val JSONValue?.asString: String get() = if (this is JSONString) value else typeError("String") - /** The value of the receiver [JSONValue] as a [String] or `null`. */ + /** The value of the receiver [JSONValue] as a [String] or `null`. */ val JSONValue?.asStringOrNull: String? get() = if (this is JSONString) value else null - /** The value of the receiver [JSONValue] as a [Long]. */ + /** The value of the receiver [JSONValue] as a [Long]. */ val JSONValue?.asLong: Long get() = if (this is JSONNumber && isLong()) toLong() else typeError("Long") - /** The value of the receiver [JSONValue] as a [Long] or `null`. */ + /** The value of the receiver [JSONValue] as a [Long] or `null`. */ val JSONValue?.asLongOrNull: Long? get() = if (this is JSONNumber && isLong()) toLong() else null - /** The value of the receiver [JSONValue] as an [Int]. */ + /** The value of the receiver [JSONValue] as an [Int]. */ val JSONValue?.asInt: Int get() = if (this is JSONNumber && isInt()) toInt() else typeError("Int") - /** The value of the receiver [JSONValue] as an [Int] or `null`. */ + /** The value of the receiver [JSONValue] as an [Int] or `null`. */ val JSONValue?.asIntOrNull: Int? get() = if (this is JSONNumber && isInt()) toInt() else null - /** The value of the receiver [JSONValue] as a [Short]. */ + /** The value of the receiver [JSONValue] as a [Short]. */ val JSONValue?.asShort: Short get() = if (this is JSONNumber && isShort()) toShort() else typeError("Short") - /** The value of the receiver [JSONValue] as a [Short] or `null`. */ + /** The value of the receiver [JSONValue] as a [Short] or `null`. */ val JSONValue?.asShortOrNull: Short? get() = if (this is JSONNumber && isShort()) toShort() else null - /** The value of the receiver [JSONValue] as a [Byte]. */ + /** The value of the receiver [JSONValue] as a [Byte]. */ val JSONValue?.asByte: Byte get() = if (this is JSONNumber && isByte()) toByte() else typeError("Byte") - /** The value of the receiver [JSONValue] as a [Byte] or `null`. */ + /** The value of the receiver [JSONValue] as a [Byte] or `null`. */ val JSONValue?.asByteOrNull: Byte? get() = if (this is JSONNumber && isByte()) toByte() else null - /** The value of the receiver [JSONValue] as a [ULong]. */ + /** The value of the receiver [JSONValue] as a [ULong]. */ val JSONValue?.asULong: ULong get() = if (this is JSONNumber && isULong()) toULong() else typeError("ULong") - /** The value of the receiver [JSONValue] as a [ULong] or `null`. */ + /** The value of the receiver [JSONValue] as a [ULong] or `null`. */ val JSONValue?.asULongOrNull: ULong? get() = if (this is JSONNumber && isULong()) toULong() else null - /** The value of the receiver [JSONValue] as a [UInt]. */ + /** The value of the receiver [JSONValue] as a [UInt]. */ val JSONValue?.asUInt: UInt get() = if (this is JSONNumber && isUInt()) toUInt() else typeError("UInt") - /** The value of the receiver [JSONValue] as a [UInt] or `null`. */ + /** The value of the receiver [JSONValue] as a [UInt] or `null`. */ val JSONValue?.asUIntOrNull: UInt? get() = if (this is JSONNumber && isUInt()) toUInt() else null - /** The value of the receiver [JSONValue] as a [UShort]. */ + /** The value of the receiver [JSONValue] as a [UShort]. */ val JSONValue?.asUShort: UShort get() = if (this is JSONNumber && isUShort()) toUShort() else typeError("UShort") - /** The value of the receiver [JSONValue] as a [UShort] or `null`. */ + /** The value of the receiver [JSONValue] as a [UShort] or `null`. */ val JSONValue?.asUShortOrNull: UShort? get() = if (this is JSONNumber && isUShort()) toUShort() else null - /** The value of the receiver [JSONValue] as a [UByte]. */ + /** The value of the receiver [JSONValue] as a [UByte]. */ val JSONValue?.asUByte: UByte get() = if (this is JSONNumber && isUByte()) toUByte() else typeError("UByte") - /** The value of the receiver [JSONValue] as a [UByte] or `null`. */ + /** The value of the receiver [JSONValue] as a [UByte] or `null`. */ val JSONValue?.asUByteOrNull: UByte? get() = if (this is JSONNumber && isUByte()) toUByte() else null - /** The value of the receiver [JSONValue] as a [BigDecimal]. */ + /** The value of the receiver [JSONValue] as a [BigDecimal]. */ val JSONValue?.asDecimal: BigDecimal get() = if (this is JSONNumber) toDecimal() else typeError("BigDecimal") - /** The value of the receiver [JSONValue] as a [BigDecimal] or `null`. */ + /** The value of the receiver [JSONValue] as a [BigDecimal] or `null`. */ val JSONValue?.asDecimalOrNull: BigDecimal? get() = if (this is JSONNumber) toDecimal() else null - /** The value of the receiver [JSONValue] as a [Boolean]. */ + /** The value of the receiver [JSONValue] as a [Number]. */ + val JSONValue?.asNumber: Number + get() = if (this is JSONNumber) this else typeError("Number") + + /** The value of the receiver [JSONValue] as a [Number] or `null`. */ + val JSONValue?.asNumberOrNull: Number? + get() = if (this is JSONNumber) this else null + + /** The value of the receiver [JSONValue] as a [Boolean]. */ val JSONValue?.asBoolean: Boolean get() = if (this is JSONBoolean) value else typeError("Boolean") - /** The value of the receiver [JSONValue] as a [Boolean] or `null`. */ + /** The value of the receiver [JSONValue] as a [Boolean] or `null`. */ val JSONValue?.asBooleanOrNull: Boolean? get() = if (this is JSONBoolean) value else null - /** The value of the receiver [JSONValue] as a [JSONArray]. */ + /** The value of the receiver [JSONValue] as a [JSONArray]. */ val JSONValue?.asArray: JSONArray get() = if (this is JSONArray) this else typeError("JSONArray") - /** The value of the receiver [JSONValue] as a [JSONArray] or `null`. */ + /** The value of the receiver [JSONValue] as a [JSONArray] or `null`. */ val JSONValue?.asArrayOrNull: JSONArray? get() = this as? JSONArray - /** The value of the receiver [JSONValue] as a [JSONObject]. */ + /** The value of the receiver [JSONValue] as a [JSONObject]. */ val JSONValue?.asObject: JSONObject get() = if (this is JSONObject) this else typeError("JSONObject") - /** The value of the receiver [JSONValue] as a [JSONObject] or `null`. */ + /** The value of the receiver [JSONValue] as a [JSONObject] or `null`. */ val JSONValue?.asObjectOrNull: JSONObject? get() = this as? JSONObject @@ -468,6 +476,13 @@ object JSON { inline fun JSONValue?.asDecimalOr(alternative: JSONValue?.() -> BigDecimal): BigDecimal = if (this is JSONNumber) toDecimal() else alternative() + /** + * Get the receiver [JSONValue] as a [Number] if compatible, or if not, return the result of the evaluation of + * the lambda (which may supply an alternative or throw an exception). + */ + inline fun JSONValue?.asNumberOr(alternative: JSONValue?.() -> Number): Number = + if (this is JSONNumber) this else alternative() + /** * Get the receiver [JSONValue] as a [Boolean] if compatible, or if not, return the result of the evaluation of the * lambda (which may supply an alternative or throw an exception). @@ -493,130 +508,139 @@ object JSON { * Get the receiver [JSONValue] as a [String], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asStringOrError( - target: String = "String", + expected: String = "String", key: Any? = null, nodeName: String = "Node", - ): String = if (this is JSONString) value else typeError(target, key, nodeName) + ): String = if (this is JSONString) value else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [Long], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asLongOrError( - target: String = "Long", + expected: String = "Long", key: Any? = null, nodeName: String = "Node", - ): Long = if (this is JSONNumber && isLong()) toLong() else typeError(target, key, nodeName) + ): Long = if (this is JSONNumber && isLong()) toLong() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as an [Int], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asIntOrError( - target: String = "Int", + expected: String = "Int", key: Any? = null, nodeName: String = "Node", - ): Int = if (this is JSONNumber && isInt()) toInt() else typeError(target, key, nodeName) + ): Int = if (this is JSONNumber && isInt()) toInt() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [Short], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asShortOrError( - target: String = "Short", + expected: String = "Short", key: Any? = null, nodeName: String = "Node", - ): Short = if (this is JSONNumber && isShort()) toShort() else typeError(target, key, nodeName) + ): Short = if (this is JSONNumber && isShort()) toShort() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [Byte], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asByteOrError( - target: String = "Byte", + expected: String = "Byte", key: Any? = null, nodeName: String = "Node", - ): Byte = if (this is JSONNumber && isByte()) toByte() else typeError(target, key, nodeName) + ): Byte = if (this is JSONNumber && isByte()) toByte() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [ULong], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asULongOrError( - target: String = "ULong", + expected: String = "ULong", key: Any? = null, nodeName: String = "Node", - ): ULong = if (this is JSONNumber && isULong()) toULong() else typeError(target, key, nodeName) + ): ULong = if (this is JSONNumber && isULong()) toULong() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [UInt], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asUIntOrError( - target: String = "UInt", + expected: String = "UInt", key: Any? = null, nodeName: String = "Node", - ): UInt = if (this is JSONNumber && isUInt()) toUInt() else typeError(target, key, nodeName) + ): UInt = if (this is JSONNumber && isUInt()) toUInt() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [UShort], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asUShortOrError( - target: String = "UShort", + expected: String = "UShort", key: Any? = null, nodeName: String = "Node", - ): UShort = if (this is JSONNumber && isUShort()) toUShort() else typeError(target, key, nodeName) + ): UShort = if (this is JSONNumber && isUShort()) toUShort() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [UByte], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asUByteOrError( - target: String = "UByte", + expected: String = "UByte", key: Any? = null, nodeName: String = "Node", - ): UByte = if (this is JSONNumber && isUByte()) toUByte() else typeError(target, key, nodeName) + ): UByte = if (this is JSONNumber && isUByte()) toUByte() else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [BigDecimal], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asDecimalOrError( - target: String = "BigDecimal", + expected: String = "BigDecimal", + key: Any? = null, + nodeName: String = "Node", + ): BigDecimal = if (this is JSONNumber) toDecimal() else typeError(expected, key, nodeName) + + /** + * Get the receiver [JSONValue] as a [Number], or throw a [JSONTypeException] if incorrect. + */ + fun JSONValue?.asNumberOrError( + expected: String = "Number", key: Any? = null, nodeName: String = "Node", - ): BigDecimal = if (this is JSONNumber) toDecimal() else typeError(target, key, nodeName) + ): Number = if (this is JSONNumber) this else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [Boolean], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asBooleanOrError( - target: String = "Boolean", + expected: String = "Boolean", key: Any? = null, nodeName: String = "Node", - ): Boolean = if (this is JSONBoolean) value else typeError(target, key, nodeName) + ): Boolean = if (this is JSONBoolean) value else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [JSONArray], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asArrayOrError( - target: String = "JSONArray", + expected: String = "JSONArray", key: Any? = null, nodeName: String = "Node", - ): JSONArray = if (this is JSONArray) this else typeError(target, key, nodeName) + ): JSONArray = if (this is JSONArray) this else typeError(expected, key, nodeName) /** * Get the receiver [JSONValue] as a [JSONObject], or throw a [JSONTypeException] if incorrect. */ fun JSONValue?.asObjectOrError( - target: String = "JSONObject", + expected: String = "JSONObject", key: Any? = null, nodeName: String = "Node", - ): JSONObject = if (this is JSONObject) this else typeError(target, key, nodeName) + ): JSONObject = if (this is JSONObject) this else typeError(expected, key, nodeName) /** * Throw a [JSONTypeException] with the given parameters. */ fun JSONValue?.typeError( - target: String, + expected: String, key: Any? = null, nodeName: String = "Node", ): Nothing { throw JSONTypeException( nodeName = nodeName, - target = target, + expected = expected, value = this, key = key, ) diff --git a/src/main/kotlin/io/kjson/JSONArray.kt b/src/main/kotlin/io/kjson/JSONArray.kt index b23b591..5686a46 100644 --- a/src/main/kotlin/io/kjson/JSONArray.kt +++ b/src/main/kotlin/io/kjson/JSONArray.kt @@ -274,7 +274,7 @@ class JSONArray internal constructor (private val array: Array, /** * Get the hash code for the array, applying the rule in Java for `List` hash codes. */ - override fun hashCode(): Int = this.fold(1) { a, b -> 31 * a + b.hashCode() } + override fun hashCode(): Int = fold(1) { a, b -> 31 * a + b.hashCode() } /** * Convert to a [String] (converts to JSON). diff --git a/src/main/kotlin/io/kjson/JSONTypeException.kt b/src/main/kotlin/io/kjson/JSONTypeException.kt index 768a6d3..a5bd636 100644 --- a/src/main/kotlin/io/kjson/JSONTypeException.kt +++ b/src/main/kotlin/io/kjson/JSONTypeException.kt @@ -2,7 +2,7 @@ * @(#) JSONTypeException.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2022 Peter Wall + * Copyright (c) 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,14 +28,14 @@ package io.kjson import io.kjson.JSON.displayValue /** - * Exception class to represent "incorrect type" errors; thrown when a specific type is requested as a return value and + * Exception class to represent "incorrect type" errors; thrown when a specific type is expected as a return value and * the actual value was not of that type. * * @author Peter Wall */ class JSONTypeException( val nodeName: String = "Node", - val target: String, + val expected: String, val value: JSONValue?, key: Any? = null, -) : JSONException("$nodeName not correct type ($target), was ${value.displayValue()}", key) +) : JSONException("$nodeName not correct type ($expected), was ${value.displayValue()}", key) diff --git a/src/test/kotlin/io/kjson/JSONArrayTest.kt b/src/test/kotlin/io/kjson/JSONArrayTest.kt index 692d4d2..671de78 100644 --- a/src/test/kotlin/io/kjson/JSONArrayTest.kt +++ b/src/test/kotlin/io/kjson/JSONArrayTest.kt @@ -26,13 +26,12 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertFalse -import kotlin.test.assertTrue -import kotlin.test.expect + import kotlinx.coroutines.runBlocking +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldThrow + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -40,63 +39,63 @@ class JSONArrayTest { @Test fun `should create JSONArray`() { val testArray = JSONArray(arrayOf(JSONInt(123), JSONInt(456)), 2) - expect(2) { testArray.size } - expect(JSONInt(123)) { testArray[0] } - expect(JSONInt(456)) { testArray[1] } - expect("[123,456]") { testArray.toString() } - expect("[123,456]") { testArray.toJSON() } - assertFalse(testArray.isEmpty()) - assertTrue(testArray.isNotEmpty()) + testArray.size shouldBe 2 + testArray[0] shouldBe JSONInt(123) + testArray[1] shouldBe JSONInt(456) + testArray.toString() shouldBe "[123,456]" + testArray.toJSON() shouldBe "[123,456]" + testArray.isEmpty() shouldBe false + testArray.isNotEmpty() shouldBe true } @Test fun `should create empty JSONArray`() { val testArray = JSONArray(emptyArray(), 0) - expect(0) { testArray.size } - expect("[]") { testArray.toString() } - expect("[]") { testArray.toJSON() } - assertTrue(testArray.isEmpty()) - assertFalse(testArray.isNotEmpty()) + testArray.size shouldBe 0 + testArray.toString() shouldBe "[]" + testArray.toJSON() shouldBe "[]" + testArray.isEmpty() shouldBe true + testArray.isNotEmpty() shouldBe false } @Test fun `should create JSONArray using of`() { val testArray = JSONArray.of(JSONInt(9999), JSONInt(8888)) - expect(2) { testArray.size } - expect(JSONInt(9999)) { testArray[0] } - expect(JSONInt(8888)) { testArray[1] } - expect("[9999,8888]") { testArray.toJSON() } + testArray.size shouldBe 2 + testArray[0] shouldBe JSONInt(9999) + testArray[1] shouldBe JSONInt(8888) + testArray.toJSON() shouldBe "[9999,8888]" } @Test fun `should create JSONArray using JSONArray function`() { val testArray = JSONArray(JSONInt(9999), JSONInt(8888)) - expect(2) { testArray.size } - expect(JSONInt(9999)) { testArray[0] } - expect(JSONInt(8888)) { testArray[1] } - expect("[9999,8888]") { testArray.toJSON() } + testArray.size shouldBe 2 + testArray[0] shouldBe JSONInt(9999) + testArray[1] shouldBe JSONInt(8888) + testArray.toJSON() shouldBe "[9999,8888]" } @Test fun `should create JSONArray using List`() { val testArray = JSONArray.from(listOf(JSONString("Hello"), JSONString("World"))) - expect(2) { testArray.size } - expect(JSONString("Hello")) { testArray[0] } - expect(JSONString("World")) { testArray[1] } - expect("[\"Hello\",\"World\"]") { testArray.toJSON() } + testArray.size shouldBe 2 + testArray[0] shouldBe JSONString("Hello") + testArray[1] shouldBe JSONString("World") + testArray.toJSON() shouldBe "[\"Hello\",\"World\"]" } @Test fun `should create JSONArray using List extension function`() { val testArray = listOf(JSONString("Hello"), JSONString("World")).toJSONArray() - expect(2) { testArray.size } - expect(JSONString("Hello")) { testArray[0] } - expect(JSONString("World")) { testArray[1] } - expect("[\"Hello\",\"World\"]") { testArray.toJSON() } + testArray.size shouldBe 2 + testArray[0] shouldBe JSONString("Hello") + testArray[1] shouldBe JSONString("World") + testArray.toJSON() shouldBe "[\"Hello\",\"World\"]" } @Test fun `should compare to other List`() { val list = listOf(JSONInt(123), JSONInt(456)) val testArray = JSONArray.from(list) - expect(2) { testArray.size } - assertEquals>(testArray, list) - assertEquals>(list, testArray) - assertEquals(testArray.hashCode(), list.hashCode()) + testArray.size shouldBe 2 + list.shouldBe>(testArray) + testArray.shouldBe>(list) + list.hashCode() shouldBe testArray.hashCode() } @Test fun `should build JSONArray using Builder`() { @@ -105,10 +104,10 @@ class JSONArrayTest { add(JSONInt(456)) add(JSONInt(789)) }.build() - expect(3) { json.size } - expect(JSONInt(123)) { json[0] } - expect(JSONInt(456)) { json[1] } - expect(JSONInt(789)) { json[2] } + json.size shouldBe 3 + json[0] shouldBe JSONInt(123) + json[1] shouldBe JSONInt(456) + json[2] shouldBe JSONInt(789) } @Test fun `should build JSONArray using build`() { @@ -117,10 +116,10 @@ class JSONArrayTest { add(JSONInt(456)) add(JSONInt(789)) } - expect(3) { json.size } - expect(JSONInt(123)) { json[0] } - expect(JSONInt(456)) { json[1] } - expect(JSONInt(789)) { json[2] } + json.size shouldBe 3 + json[0] shouldBe JSONInt(123) + json[1] shouldBe JSONInt(456) + json[2] shouldBe JSONInt(789) } @Test fun `should build JSONArray using build with non-JSON classes`() { @@ -131,12 +130,12 @@ class JSONArrayTest { add("1.456".toBigDecimal()) add(true) } - expect(5) { json.size } - expect(JSONInt(123)) { json[0] } - expect(JSONString("abc")) { json[1] } - expect(JSONLong(112233445566778899)) { json[2] } - expect(JSONDecimal("1.456".toBigDecimal())) { json[3] } - expect(JSONBoolean.TRUE) { json[4] } + json.size shouldBe 5 + json[0] shouldBe JSONInt(123) + json[1] shouldBe JSONString("abc") + json[2] shouldBe JSONLong(112233445566778899) + json[3] shouldBe JSONDecimal("1.456".toBigDecimal()) + json[4] shouldBe JSONBoolean.TRUE } @Test fun `should limit Builder to single use`() { @@ -144,35 +143,33 @@ class JSONArrayTest { builder.add(JSONInt(111)) builder.add(JSONInt(222)) builder.add(JSONInt(333)) - expect(3) { builder.size } + builder.size shouldBe 3 val json = builder.build() - expect(3) { json.size } - expect(JSONInt(111)) { json[0] } - expect(JSONInt(222)) { json[1] } - expect(JSONInt(333)) { json[2] } - assertFailsWith { builder.add(JSONInt(444)) }.let { - expect("Builder is closed") { it.message } - } + json.size shouldBe 3 + json[0] shouldBe JSONInt(111) + json[1] shouldBe JSONInt(222) + json[2] shouldBe JSONInt(333) + shouldThrow("Builder is closed") { builder.add(JSONInt(444)) } } @Test fun `should return JSONArray from subList`() { val array = JSONArray.of(JSONInt(111), JSONInt(222), JSONInt(333), JSONInt(444), JSONInt(555)) val subList = array.subList(2, 4) - expect("[333,444]") { subList.toJSON() } + subList.toJSON() shouldBe "[333,444]" } @Test fun `should format JSONArray using output`() { val capture = OutputCapture(64) JSONArray.from(listOf(JSONString("Hello"), JSONInt(123), JSONBoolean.TRUE, - JSONDecimal("1.5".toBigDecimal()), null, JSONLong(0))).outputTo(capture) - expect("[\"Hello\",123,true,1.5,null,0]") { capture.toString() } + JSONDecimal("1.5".toBigDecimal()), null, JSONLong(0))).outputTo(capture) + capture.toString() shouldBe "[\"Hello\",123,true,1.5,null,0]" } @Test fun `should format JSONArray using coOutput`() = runBlocking { val capture = CoOutputCapture(64) JSONArray.from(listOf(JSONString("Hello"), JSONInt(123), JSONBoolean.TRUE, - JSONDecimal("1.5".toBigDecimal()), null, JSONLong(0))).coOutputTo(capture) - expect("[\"Hello\",123,true,1.5,null,0]") { capture.toString() } + JSONDecimal("1.5".toBigDecimal()), null, JSONLong(0))).coOutputTo(capture) + capture.toString() shouldBe "[\"Hello\",123,true,1.5,null,0]" } @Test fun `should iterate over array`() { @@ -186,14 +183,14 @@ class JSONArrayTest { var count = 0 json.forEachItem { when (count++) { - 0 -> expect(JSONInt(123)) { it } - 1 -> expect(JSONString("abc")) { it } - 2 -> expect(JSONLong(112233445566778899)) { it } - 3 -> expect(JSONDecimal("1.456")) { it } - 4 -> expect(JSONBoolean.TRUE) { it } + 0 -> it shouldBe JSONInt(123) + 1 -> it shouldBe JSONString("abc") + 2 -> it shouldBe JSONLong(112233445566778899) + 3 -> it shouldBe JSONDecimal("1.456") + 4 -> it shouldBe JSONBoolean.TRUE } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over array including index`() { @@ -206,51 +203,51 @@ class JSONArrayTest { } var count = 0 json.forEachItemIndexed { index, item -> - expect(count) { index } + index shouldBe count when (count++) { - 0 -> expect(JSONInt(123)) { item } - 1 -> expect(JSONString("abc")) { item } - 2 -> expect(JSONLong(112233445566778899)) { item } - 3 -> expect(JSONDecimal("1.456")) { item } - 4 -> expect(JSONBoolean.TRUE) { item } + 0 -> item shouldBe JSONInt(123) + 1 -> item shouldBe JSONString("abc") + 2 -> item shouldBe JSONLong(112233445566778899) + 3 -> item shouldBe JSONDecimal("1.456") + 4 -> item shouldBe JSONBoolean.TRUE } } - expect(5) { count } + count shouldBe 5 } @Test fun `should append in JSON Lines format`() { val json = createJSONLines() val sb = StringBuilder() json.appendJSONLinesTo(sb) - expect("{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n") { sb.toString() } + sb.toString() shouldBe "{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n" } @Test fun `should append empty JSONArray in JSON Lines format`() { val sb = StringBuilder() JSONArray.EMPTY.appendJSONLinesTo(sb) - expect("") { sb.toString() } + sb.toString() shouldBe "" } @Test fun `should create JSON Lines string`() { val json = createJSONLines() - expect("{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n") { json.toJSONLines() } + json.toJSONLines() shouldBe "{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n" } @Test fun `should output empty string for toJSONLines of empty JSONArray`() { val json = JSONArray.EMPTY - expect("") { json.toJSONLines() } + json.toJSONLines() shouldBe "" } @Test fun `should output JSON Lines using output`() { val capture = OutputCapture(64) createJSONLines().outputJSONLinesTo(capture) - expect("{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n") { capture.toString() } + capture.toString() shouldBe "{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n" } @Test fun `should output JSON Lines using coOutput`() = runBlocking { val capture = CoOutputCapture(64) createJSONLines().coOutputJSONLinesTo(capture) - expect("{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n") { capture.toString() } + capture.toString() shouldBe "{\"a\":1,\"b\":2}\n{\"a\":21,\"b\":34}\n{\"a\":55,\"b\":66}\n" } companion object { diff --git a/src/test/kotlin/io/kjson/JSONBooleanTest.kt b/src/test/kotlin/io/kjson/JSONBooleanTest.kt index 37b7897..7735baf 100644 --- a/src/test/kotlin/io/kjson/JSONBooleanTest.kt +++ b/src/test/kotlin/io/kjson/JSONBooleanTest.kt @@ -2,7 +2,7 @@ * @(#) JSONBooleanTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021 Peter Wall + * Copyright (c) 2021, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,11 +26,13 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertIs -import kotlin.test.assertSame -import kotlin.test.expect + import kotlinx.coroutines.runBlocking +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance +import io.kstuff.test.shouldBeType + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -38,50 +40,50 @@ class JSONBooleanTest { @Test fun `should return a JSONBoolean`() { val test1 = JSONBoolean.of(true) - assertSame(JSONBoolean.TRUE, test1) - expect(true) { test1.value } - expect("true") { test1.toJSON() } - expect("true") { test1.toString() } + test1 shouldBeSameInstance JSONBoolean.TRUE + test1.value shouldBe true + test1.toJSON() shouldBe "true" + test1.toString() shouldBe "true" val test2 = JSONBoolean.of(false) - assertSame(JSONBoolean.FALSE, test2) - expect(false) { test2.value } - expect("false") { test2.toJSON() } - expect("false") { test2.toString() } + test2 shouldBeSameInstance JSONBoolean.FALSE + test2.value shouldBe false + test2.toJSON() shouldBe "false" + test2.toString() shouldBe "false" } @Test fun `should handle JSONBoolean in an array`() { val test1 = JSON.parse("[12,true,false]") - assertIs(test1) - expect(3) { test1.size } - expect(JSONInt(12)) { test1[0] } - expect(JSONBoolean.TRUE) { test1[1] } - expect(JSONBoolean.FALSE) { test1[2] } + test1.shouldBeType() + test1.size shouldBe 3 + test1[0] shouldBe JSONInt(12) + test1[1] shouldBe JSONBoolean.TRUE + test1[2] shouldBe JSONBoolean.FALSE } @Test fun `should handle JSONBoolean in an object`() { val test1 = JSON.parse("""{"a":true,"b":false}""") - assertIs(test1) - expect(2) { test1.size } - expect(JSONBoolean.TRUE) { test1["a"] } - expect(JSONBoolean.FALSE) { test1["b"] } + test1.shouldBeType() + test1.size shouldBe 2 + test1["a"] shouldBe JSONBoolean.TRUE + test1["b"] shouldBe JSONBoolean.FALSE } @Test fun `should format JSONBoolean using output`() { val capture = OutputCapture(8) JSONBoolean.TRUE.outputTo(capture) - expect("true") { capture.toString() } + capture.toString() shouldBe "true" capture.reset() JSONBoolean.FALSE.outputTo(capture) - expect("false") { capture.toString() } + capture.toString() shouldBe "false" } @Test fun `should format JSONBoolean using coOutput`() = runBlocking { val capture = CoOutputCapture(8) JSONBoolean.TRUE.coOutputTo(capture) - expect("true") { capture.toString() } + capture.toString() shouldBe "true" capture.reset() JSONBoolean.FALSE.coOutputTo(capture) - expect("false") { capture.toString() } + capture.toString() shouldBe "false" } } diff --git a/src/test/kotlin/io/kjson/JSONDecimalTest.kt b/src/test/kotlin/io/kjson/JSONDecimalTest.kt index 52438f0..9157403 100644 --- a/src/test/kotlin/io/kjson/JSONDecimalTest.kt +++ b/src/test/kotlin/io/kjson/JSONDecimalTest.kt @@ -26,14 +26,14 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertSame -import kotlin.test.assertTrue -import kotlin.test.expect + import kotlinx.coroutines.runBlocking import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -41,271 +41,271 @@ class JSONDecimalTest { @Test fun `should create JSONDecimal`() { JSONDecimal(BigDecimal.ZERO).let { - expect(BigDecimal.ZERO) { it.value } - expect("0") { it.toJSON() } - expect(JSONDecimal("0.00")) { it } - expect(JSONInt(0)) { it } - expect(JSONLong(0)) { it } + it.value shouldBe BigDecimal.ZERO + it.toJSON() shouldBe "0" + it shouldBe JSONDecimal("0.00") + it.shouldBe(JSONInt(0)) + it.shouldBe(JSONLong(0)) } } @Test fun `should create JSONDecimal from Long`() { JSONDecimal(123456789123456789).let { - expect(123456789123456789.toBigDecimal()) { it.value } - expect("123456789123456789") { it.toJSON() } - expect(JSONLong(123456789123456789)) { it } + it.value shouldBe 123456789123456789.toBigDecimal() + it.toJSON() shouldBe "123456789123456789" + it.shouldBe(JSONLong(123456789123456789)) } } @Test fun `should create JSONDecimal from Int`() { JSONDecimal(12345).let { - expect(12345.toBigDecimal()) { it.value } - expect("12345") { it.toJSON() } - expect(JSONInt(12345)) { it } + it.value shouldBe 12345.toBigDecimal() + it.toJSON() shouldBe "12345" + it.shouldBe(JSONInt(12345)) } } @Test fun `should create JSONDecimal using of`() { JSONDecimal.of(BigDecimal.ZERO).let { - assertSame(JSONDecimal.ZERO, it) - expect(BigDecimal.ZERO) { it.value } - expect("0") { it.toJSON() } - expect(JSONDecimal("0.00")) { it } + it shouldBeSameInstance JSONDecimal.ZERO + it.value shouldBe BigDecimal.ZERO + it.toJSON() shouldBe "0" + it shouldBe JSONDecimal("0.00") } JSONDecimal.of(BigDecimal.ONE).let { - expect(BigDecimal.ONE) { it.value } - expect("1") { it.toJSON() } - expect(JSONDecimal("1.00")) { it } + it.value shouldBe BigDecimal.ONE + it.toJSON() shouldBe "1" + it shouldBe JSONDecimal("1.00") } - assertSame(JSONDecimal.ZERO, JSONDecimal.of(0)) + JSONDecimal.of(0) shouldBeSameInstance JSONDecimal.ZERO JSONDecimal.of(23456).let { - expect("23456".toBigDecimal()) { it.value } - expect("23456") { it.toJSON() } - expect(JSONDecimal("23456")) { it } + it.value shouldBe "23456".toBigDecimal() + it.toJSON() shouldBe "23456" + it shouldBe JSONDecimal("23456") } - assertSame(JSONDecimal.ZERO, JSONDecimal.of(0L)) + JSONDecimal.of(0L) shouldBeSameInstance JSONDecimal.ZERO JSONDecimal.of(9876543210).let { - expect("9876543210".toBigDecimal()) { it.value } - expect("9876543210") { it.toJSON() } - expect(JSONDecimal("9876543210")) { it } + it.value shouldBe "9876543210".toBigDecimal() + it.toJSON() shouldBe "9876543210" + it shouldBe JSONDecimal("9876543210") } - assertSame(JSONDecimal.ZERO, JSONDecimal.of("0")) + JSONDecimal.of("0") shouldBeSameInstance JSONDecimal.ZERO JSONDecimal.of("333.5").let { - expect("333.5".toBigDecimal()) { it.value } - expect("333.5") { it.toJSON() } - expect(JSONDecimal("333.5")) { it } + it.value shouldBe "333.5".toBigDecimal() + it.toJSON() shouldBe "333.5" + it shouldBe JSONDecimal("333.5") } } @Test fun `should implement isXxxx functions`() { JSONDecimal(123456789123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal(-123456789123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal(123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal(-123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal(12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe false } JSONDecimal(-12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal(123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertTrue(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe true } JSONDecimal(-123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal("0.123").let { - assertFalse(it.isIntegral()) - assertFalse(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe false + it.isLong() shouldBe false + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal("-0.123").let { - assertFalse(it.isIntegral()) - assertFalse(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe false + it.isLong() shouldBe false + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONDecimal("888.00").let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe false } JSONDecimal("-888.00").let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } } @Test fun `should implement isZero etc functions`() { JSONDecimal.ZERO.let { - assertTrue(it.isZero()) - assertFalse(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe true + it.isPositive() shouldBe false + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe true } JSONDecimal(-123).let { - assertFalse(it.isZero()) - assertFalse(it.isPositive()) - assertTrue(it.isNegative()) - assertFalse(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe false + it.isNegative() shouldBe true + it.isNotNegative() shouldBe false + it.isNotPositive() shouldBe true } JSONDecimal(123).let { - assertFalse(it.isZero()) - assertTrue(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertFalse(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe true + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe false } } @Test fun `should implement toDecimal`() { - expect(BigDecimal.ZERO) { JSONDecimal.ZERO.toDecimal() } - expect(12345.toBigDecimal()) { JSONDecimal(12345).toDecimal() } - expect((-9).toBigDecimal()) { JSONDecimal(-9).toDecimal() } - expect("567.89".toBigDecimal()) { JSONDecimal("567.89").toDecimal() } + JSONDecimal.ZERO.toDecimal() shouldBe BigDecimal.ZERO + JSONDecimal(12345).toDecimal() shouldBe 12345.toBigDecimal() + JSONDecimal(-9).toDecimal() shouldBe (-9).toBigDecimal() + JSONDecimal("567.89").toDecimal() shouldBe "567.89".toBigDecimal() } @Test fun `should implement toULong`() { - expect(0U) { JSONDecimal.ZERO.toULong() } - expect(123456789123456789U) { JSONDecimal(123456789123456789).toULong() } + JSONDecimal.ZERO.toULong() shouldBe 0U + JSONDecimal(123456789123456789).toULong() shouldBe 123456789123456789U } @Test fun `should implement toUInt`() { - expect(0U) { JSONDecimal.ZERO.toUInt() } - expect(12345U) { JSONDecimal(12345).toUInt() } - expect(2147483648U) { JSONDecimal(2147483648).toUInt() } + JSONDecimal.ZERO.toUInt() shouldBe 0U + JSONDecimal(12345).toUInt() shouldBe 12345U + JSONDecimal(2147483648).toUInt() shouldBe 2147483648U } @Test fun `should implement toUShort`() { - expect(0U) { JSONDecimal.ZERO.toUShort() } - expect(32768U) { JSONDecimal(32768).toUShort() } + JSONDecimal.ZERO.toUShort() shouldBe 0U + JSONDecimal(32768).toUShort() shouldBe 32768U } @Test fun `should implement toUByte`() { - expect(0U) { JSONDecimal.ZERO.toUByte() } - expect(129U) { JSONDecimal(129).toUByte() } + JSONDecimal.ZERO.toUByte() shouldBe 0U + JSONDecimal(129).toUByte() shouldBe 129U } @Test fun `should format JSONDecimal using output`() { val capture = OutputCapture(16) JSONDecimal.ZERO.outputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONDecimal(12345).outputTo(capture) - expect("12345") { capture.toString() } + capture.toString() shouldBe "12345" capture.reset() JSONDecimal("-527.5").outputTo(capture) - expect("-527.5") { capture.toString() } + capture.toString() shouldBe "-527.5" } @Test fun `should format JSONDecimal using coOutput`() = runBlocking { val capture = CoOutputCapture(16) JSONDecimal.ZERO.coOutputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONDecimal(12345).coOutputTo(capture) - expect("12345") { capture.toString() } + capture.toString() shouldBe "12345" capture.reset() JSONDecimal("-527.5").coOutputTo(capture) - expect("-527.5") { capture.toString() } + capture.toString() shouldBe "-527.5" } } diff --git a/src/test/kotlin/io/kjson/JSONIntTest.kt b/src/test/kotlin/io/kjson/JSONIntTest.kt index a2edcb2..d6d1839 100644 --- a/src/test/kotlin/io/kjson/JSONIntTest.kt +++ b/src/test/kotlin/io/kjson/JSONIntTest.kt @@ -26,14 +26,14 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertSame -import kotlin.test.assertTrue -import kotlin.test.expect + import kotlinx.coroutines.runBlocking import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -41,168 +41,168 @@ class JSONIntTest { @Test fun `should create JSONInt`() { JSONInt(12345).let { - expect(12345) { it.value } - expect("12345") { it.toJSON() } - expect("12345") { it.toString() } - expect(JSONLong(12345)) { it } - expect(JSONDecimal(12345.toBigDecimal())) { it } + it.value shouldBe 12345 + it.toJSON() shouldBe "12345" + it.toString() shouldBe "12345" + it.shouldBe(JSONLong(12345)) + it.shouldBe(JSONDecimal(12345.toBigDecimal())) } JSONInt(-888).let { - expect(-888) { it.value } - expect("-888") { it.toJSON() } - expect("-888") { it.toString() } + it.value shouldBe -888 + it.toJSON() shouldBe "-888" + it.toString() shouldBe "-888" } } @Test fun `should create JSONInt using of`() { JSONInt.of(12345).let { - expect(12345) { it.value } - expect("12345") { it.toJSON() } - expect("12345") { it.toString() } + it.value shouldBe 12345 + it.toJSON() shouldBe "12345" + it.toString() shouldBe "12345" } - assertSame(JSONInt.ZERO, JSONInt.of(0)) + JSONInt.of(0) shouldBeSameInstance JSONInt.ZERO } @Test fun `should implement isXxxx functions`() { JSONInt(123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONInt(-123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONInt(12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe false } JSONInt(-12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONInt(123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertTrue(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe true } JSONInt(-123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } } @Test fun `should implement isZero etc functions`() { JSONInt.ZERO.let { - assertTrue(it.isZero()) - assertFalse(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe true + it.isPositive() shouldBe false + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe true } JSONInt(-123).let { - assertFalse(it.isZero()) - assertFalse(it.isPositive()) - assertTrue(it.isNegative()) - assertFalse(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe false + it.isNegative() shouldBe true + it.isNotNegative() shouldBe false + it.isNotPositive() shouldBe true } JSONInt(123).let { - assertFalse(it.isZero()) - assertTrue(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertFalse(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe true + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe false } } @Test fun `should implement toDecimal`() { - expect(BigDecimal.ZERO) { JSONInt.ZERO.toDecimal() } - expect(12345.toBigDecimal()) { JSONInt(12345).toDecimal() } - expect((-9).toBigDecimal()) { JSONInt(-9).toDecimal() } + JSONInt.ZERO.toDecimal() shouldBe BigDecimal.ZERO + JSONInt(12345).toDecimal() shouldBe 12345.toBigDecimal() + JSONInt(-9).toDecimal() shouldBe (-9).toBigDecimal() } @Test fun `should implement toULong`() { - expect(0U) { JSONInt.ZERO.toULong() } - expect(12345U) { JSONInt(12345).toULong() } + JSONInt.ZERO.toULong() shouldBe 0U + JSONInt(12345).toULong() shouldBe 12345U } @Test fun `should implement toUInt`() { - expect(0U) { JSONInt.ZERO.toUInt() } - expect(12345U) { JSONInt(12345).toUInt() } + JSONInt.ZERO.toUInt() shouldBe 0U + JSONInt(12345).toUInt() shouldBe 12345U } @Test fun `should implement toUShort`() { - expect(0U) { JSONInt.ZERO.toUShort() } - expect(32768U) { JSONInt(32768).toUShort() } + JSONInt.ZERO.toUShort() shouldBe 0U + JSONInt(32768).toUShort() shouldBe 32768U } @Test fun `should implement toUByte`() { - expect(0U) { JSONInt.ZERO.toUByte() } - expect(129U) { JSONInt(129).toUByte() } + JSONInt.ZERO.toUByte() shouldBe 0U + JSONInt(129).toUByte() shouldBe 129U } @Test fun `should format JSONInt using output`() { val capture = OutputCapture(16) JSONInt.ZERO.outputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONInt(1234567).outputTo(capture) - expect("1234567") { capture.toString() } + capture.toString() shouldBe "1234567" capture.reset() JSONInt(-888).outputTo(capture) - expect("-888") { capture.toString() } + capture.toString() shouldBe "-888" } @Test fun `should format JSONInt using coOutput`() = runBlocking { val capture = CoOutputCapture(16) JSONInt.ZERO.coOutputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONInt(1234567).coOutputTo(capture) - expect("1234567") { capture.toString() } + capture.toString() shouldBe "1234567" capture.reset() JSONInt(-888).coOutputTo(capture) - expect("-888") { capture.toString() } + capture.toString() shouldBe "-888" } } diff --git a/src/test/kotlin/io/kjson/JSONLongTest.kt b/src/test/kotlin/io/kjson/JSONLongTest.kt index f7cd80b..2063f17 100644 --- a/src/test/kotlin/io/kjson/JSONLongTest.kt +++ b/src/test/kotlin/io/kjson/JSONLongTest.kt @@ -26,14 +26,14 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertSame -import kotlin.test.assertTrue -import kotlin.test.expect + import kotlinx.coroutines.runBlocking import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -41,194 +41,194 @@ class JSONLongTest { @Test fun `should create a JSONLong`() { JSONLong(123).let { - expect(123L) { it.value } - expect("123") { it.toJSON() } - expect("123") { it.toString() } + it.value shouldBe 123L + it.toJSON() shouldBe "123" + it.toString() shouldBe "123" } JSONLong(-1234567812345678).let { - expect(-1234567812345678) { it.value } - expect("-1234567812345678") { it.toJSON() } - expect("-1234567812345678") { it.toString() } + it.value shouldBe -1234567812345678 + it.toJSON() shouldBe "-1234567812345678" + it.toString() shouldBe "-1234567812345678" } } @Test fun `should create a JSONLong using of`() { JSONLong.of(1122334455667788).let { - expect(1122334455667788) { it.value } - expect("1122334455667788") { it.toJSON() } - expect("1122334455667788") { it.toString() } + it.value shouldBe 1122334455667788 + it.toJSON() shouldBe "1122334455667788" + it.toString() shouldBe "1122334455667788" } JSONLong.of(-1234567812345678).let { - expect(-1234567812345678) { it.value } - expect("-1234567812345678") { it.toJSON() } - expect("-1234567812345678") { it.toString() } + it.value shouldBe -1234567812345678 + it.toJSON() shouldBe "-1234567812345678" + it.toString() shouldBe "-1234567812345678" } - assertSame(JSONLong.ZERO, JSONLong.of(0)) + JSONLong.of(0) shouldBeSameInstance JSONLong.ZERO } @Test fun `should implement isXxxx functions`() { JSONLong(123456789123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONLong(-123456789123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertFalse(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe false + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONLong(123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONLong(-123456789).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertFalse(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe false + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONLong(12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe false } JSONLong(-12345).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertFalse(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe false + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } JSONLong(123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertTrue(it.isULong()) - assertTrue(it.isUInt()) - assertTrue(it.isUShort()) - assertTrue(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe true + it.isUInt() shouldBe true + it.isUShort() shouldBe true + it.isUByte() shouldBe true } JSONLong(-123).let { - assertTrue(it.isIntegral()) - assertTrue(it.isLong()) - assertTrue(it.isInt()) - assertTrue(it.isShort()) - assertTrue(it.isByte()) - assertFalse(it.isULong()) - assertFalse(it.isUInt()) - assertFalse(it.isUShort()) - assertFalse(it.isUByte()) + it.isIntegral() shouldBe true + it.isLong() shouldBe true + it.isInt() shouldBe true + it.isShort() shouldBe true + it.isByte() shouldBe true + it.isULong() shouldBe false + it.isUInt() shouldBe false + it.isUShort() shouldBe false + it.isUByte() shouldBe false } } @Test fun `should implement isZero etc functions`() { JSONLong.ZERO.let { - assertTrue(it.isZero()) - assertFalse(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe true + it.isPositive() shouldBe false + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe true } JSONLong(-123).let { - assertFalse(it.isZero()) - assertFalse(it.isPositive()) - assertTrue(it.isNegative()) - assertFalse(it.isNotNegative()) - assertTrue(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe false + it.isNegative() shouldBe true + it.isNotNegative() shouldBe false + it.isNotPositive() shouldBe true } JSONLong(123).let { - assertFalse(it.isZero()) - assertTrue(it.isPositive()) - assertFalse(it.isNegative()) - assertTrue(it.isNotNegative()) - assertFalse(it.isNotPositive()) + it.isZero() shouldBe false + it.isPositive() shouldBe true + it.isNegative() shouldBe false + it.isNotNegative() shouldBe true + it.isNotPositive() shouldBe false } } @Test fun `should implement toDecimal`() { - expect(BigDecimal.ZERO) { JSONLong.ZERO.toDecimal() } - expect(123456789123456789.toBigDecimal()) { JSONLong(123456789123456789).toDecimal() } - expect((-9).toBigDecimal()) { JSONLong(-9).toDecimal() } + JSONLong.ZERO.toDecimal() shouldBe BigDecimal.ZERO + JSONLong(123456789123456789).toDecimal() shouldBe 123456789123456789.toBigDecimal() + JSONLong(-9).toDecimal() shouldBe (-9).toBigDecimal() } @Test fun `should implement toULong`() { - expect(0U) { JSONLong.ZERO.toULong() } - expect(123456789123456789U) { JSONLong(123456789123456789).toULong() } + JSONLong.ZERO.toULong() shouldBe 0U + JSONLong(123456789123456789).toULong() shouldBe 123456789123456789U } @Test fun `should implement toUInt`() { - expect(0U) { JSONLong.ZERO.toUInt() } - expect(12345U) { JSONLong(12345).toUInt() } - expect(2147483648U) { JSONLong(2147483648).toUInt() } + JSONLong.ZERO.toUInt() shouldBe 0U + JSONLong(12345).toUInt() shouldBe 12345U + JSONLong(2147483648).toUInt() shouldBe 2147483648U } @Test fun `should implement toUShort`() { - expect(0U) { JSONLong.ZERO.toUShort() } - expect(32768U) { JSONLong(32768).toUShort() } + JSONLong.ZERO.toUShort() shouldBe 0U + JSONLong(32768).toUShort() shouldBe 32768U } @Test fun `should implement toUByte`() { - expect(0U) { JSONLong.ZERO.toUByte() } - expect(129U) { JSONLong(129).toUByte() } + JSONLong.ZERO.toUByte() shouldBe 0U + JSONLong(129).toUByte() shouldBe 129U } @Test fun `should format JSONLong using output`() { val capture = OutputCapture(32) JSONLong.ZERO.outputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONLong(1234567890123456789).outputTo(capture) - expect("1234567890123456789") { capture.toString() } + capture.toString() shouldBe "1234567890123456789" capture.reset() JSONLong(-998877665544332211).outputTo(capture) - expect("-998877665544332211") { capture.toString() } + capture.toString() shouldBe "-998877665544332211" } @Test fun `should format JSONLong using coOutput`() = runBlocking { val capture = CoOutputCapture(32) JSONLong.ZERO.coOutputTo(capture) - expect("0") { capture.toString() } + capture.toString() shouldBe "0" capture.reset() JSONLong(1234567890123456789).coOutputTo(capture) - expect("1234567890123456789") { capture.toString() } + capture.toString() shouldBe "1234567890123456789" capture.reset() JSONLong(-998877665544332211).coOutputTo(capture) - expect("-998877665544332211") { capture.toString() } + capture.toString() shouldBe "-998877665544332211" } } diff --git a/src/test/kotlin/io/kjson/JSONNumberTest.kt b/src/test/kotlin/io/kjson/JSONNumberTest.kt index 8621e1c..4e556b8 100644 --- a/src/test/kotlin/io/kjson/JSONNumberTest.kt +++ b/src/test/kotlin/io/kjson/JSONNumberTest.kt @@ -26,30 +26,31 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertIs -import kotlin.test.assertSame -import kotlin.test.expect import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance +import io.kstuff.test.shouldBeType + class JSONNumberTest { @Test fun `should create JSONNumber of correct type using JSONNumber function`() { JSONNumber(123).let { - assertIs(it) - expect(123) { it.value } + it.shouldBeType() + it.value shouldBe 123 } JSONNumber(1234567890123456789).let { - assertIs(it) - expect(1234567890123456789) { it.value } + it.shouldBeType() + it.value shouldBe 1234567890123456789 } JSONNumber(BigDecimal.ONE).let { - assertIs(it) - expect(BigDecimal.ONE) { it.value } + it.shouldBeType() + it.value shouldBe BigDecimal.ONE } - assertSame(JSONInt.ZERO, JSONNumber(0)) - assertSame(JSONLong.ZERO, JSONNumber(0L)) - assertSame(JSONDecimal.ZERO, JSONNumber(BigDecimal.ZERO)) + JSONNumber(0) shouldBeSameInstance JSONInt.ZERO + JSONNumber(0L) shouldBeSameInstance JSONLong.ZERO + JSONNumber(BigDecimal.ZERO) shouldBeSameInstance JSONDecimal.ZERO } } diff --git a/src/test/kotlin/io/kjson/JSONObjectTest.kt b/src/test/kotlin/io/kjson/JSONObjectTest.kt index a931445..d5f4c5e 100644 --- a/src/test/kotlin/io/kjson/JSONObjectTest.kt +++ b/src/test/kotlin/io/kjson/JSONObjectTest.kt @@ -26,14 +26,12 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertFalse -import kotlin.test.assertNull -import kotlin.test.assertTrue -import kotlin.test.expect + import kotlinx.coroutines.runBlocking +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldThrow + import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -41,13 +39,13 @@ class JSONObjectTest { @Test fun `should create JSONObject using of`() { val jsonObject = JSONObject.of("abc" to JSONInt(12345), "def" to JSONString("X")) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using of with duplicateKeyOption`() { @@ -57,33 +55,29 @@ class JSONObjectTest { "def" to JSONString("X"), duplicateKeyOption = duplicateKeyOption, ) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } } @Test fun `should report duplicate key error creating JSONObject using of`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.of("abc" to JSONInt(12345), "abc" to JSONString("X")) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should report duplicate key error creating JSONObject using of with duplicateKeyOption ERROR`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.of( "abc" to JSONInt(12345), "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR, ) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -93,12 +87,12 @@ class JSONObjectTest { "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_FIRST, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept last key creating JSONObject using of with duplicateKeyOption TAKE_LAST`() { @@ -107,12 +101,12 @@ class JSONObjectTest { "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_LAST, ) - expect(1) { jsonObject.size } - expect(JSONString("X")) { jsonObject["abc"] } - expect("""{"abc":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":"X"}""" + jsonObject.toString() shouldBe """{"abc":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept matching duplicate creating JSONObject using of with CHECK_IDENTICAL`() { @@ -121,35 +115,33 @@ class JSONObjectTest { "abc" to JSONInt(12345), duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should report duplicate key error creating JSONObject using of with CHECK_IDENTICAL`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.of( "abc" to JSONInt(12345), "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should create JSONObject using JSONObject function`() { val jsonObject = JSONObject("abc" refersTo JSONInt(12345), "def" refersTo JSONString("X")) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using JSONObject function with duplicateKeyOption`() { @@ -159,33 +151,29 @@ class JSONObjectTest { "def" refersTo JSONString("X"), duplicateKeyOption = duplicateKeyOption ) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } } @Test fun `should report duplicate key error creating JSONObject using JSONObject function`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject("abc" to JSONInt(12345), "abc" to JSONString("X")) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should report duplicate key error creating JSONObject using JSONObject function with option ERROR`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject( "abc" to JSONInt(12345), "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR, ) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -195,12 +183,12 @@ class JSONObjectTest { "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_FIRST, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept last key creating JSONObject using JSONObject function with option TAKE_LAST`() { @@ -209,12 +197,12 @@ class JSONObjectTest { "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_LAST, ) - expect(1) { jsonObject.size } - expect(JSONString("X")) { jsonObject["abc"] } - expect("""{"abc":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":"X"}""" + jsonObject.toString() shouldBe """{"abc":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept matching duplicate creating JSONObject using JSONObject function with CHECK_IDENTICAL`() { @@ -223,133 +211,125 @@ class JSONObjectTest { "abc" to JSONInt(12345), duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should report duplicate key error creating JSONObject using JSONObject function with CHECK_IDENTICAL`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject( "abc" to JSONInt(12345), "abc" to JSONString("X"), duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should create JSONObject using from Map`() { val map = mapOf("abc" to JSONInt(12345), "def" to JSONString("X")) val jsonObject = JSONObject.from(map) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using from Map extension function`() { val map = mapOf("abc" to JSONInt(12345), "def" to JSONString("X")) val jsonObject = map.toJSONObject() - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using from List`() { val list = listOf>("abc" to JSONInt(12345), "def" to JSONString("X")) val jsonObject = JSONObject.from(list) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using from List with duplicateKeyOption`() { for (duplicateKeyOption in JSONObject.DuplicateKeyOption.entries) { val list = listOf>("abc" to JSONInt(12345), "def" to JSONString("X")) val jsonObject = JSONObject.from(list, duplicateKeyOption = duplicateKeyOption) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } } @Test fun `should report duplicate key error creating JSONObject using from List`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONString("X")) JSONObject.from(list) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should report duplicate key error creating JSONObject using from List with duplicateKeyOption ERROR`() { - assertFailsWith { + shouldThrow("Duplicate key - abc") { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONString("X")) JSONObject.from(list, duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR) - }.let { - expect("Duplicate key - abc") { it.message } } } @Test fun `should accept first key creating JSONObject using from List with duplicateKeyOption TAKE_FIRST`() { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONString("X")) val jsonObject = JSONObject.from(list, duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_FIRST) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept last key creating JSONObject using from List with duplicateKeyOption TAKE_LAST`() { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONString("X")) val jsonObject = JSONObject.from(list, duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_LAST) - expect(1) { jsonObject.size } - expect(JSONString("X")) { jsonObject["abc"] } - expect("""{"abc":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":"X"}""" + jsonObject.toString() shouldBe """{"abc":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept matching duplicate creating JSONObject using from List with CHECK_IDENTICAL`() { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONInt(12345)) val jsonObject = JSONObject.from(list, duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should report duplicate key error creating JSONObject using from List with CHECK_IDENTICAL`() { val list = listOf>("abc" to JSONInt(12345), "abc" to JSONString("X")) - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.from(list, duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -359,13 +339,13 @@ class JSONObjectTest { "def" refersTo JSONString("X"), ) val jsonObject = JSONObject.fromProperties(properties) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using fromProperties with duplicateKeyOption`() { @@ -375,13 +355,13 @@ class JSONObjectTest { "def" refersTo JSONString("X"), ) val jsonObject = JSONObject.fromProperties(properties, duplicateKeyOption = duplicateKeyOption) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } } @@ -390,10 +370,8 @@ class JSONObjectTest { "abc" refersTo JSONInt(12345), "abc" refersTo JSONString("X"), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.fromProperties(properties) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -402,10 +380,8 @@ class JSONObjectTest { "abc" refersTo JSONInt(12345), "abc" refersTo JSONString("X"), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.fromProperties(properties, duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -418,12 +394,12 @@ class JSONObjectTest { properties, duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_FIRST, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept last key creating JSONObject using fromProperties with duplicateKeyOption TAKE_LAST`() { @@ -435,12 +411,12 @@ class JSONObjectTest { properties, duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_LAST, ) - expect(1) { jsonObject.size } - expect(JSONString("X")) { jsonObject["abc"] } - expect("""{"abc":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":"X"}""" + jsonObject.toString() shouldBe """{"abc":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept matching duplicate creating JSONObject using fromProperties with CHECK_IDENTICAL`() { @@ -452,12 +428,12 @@ class JSONObjectTest { properties, duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should report duplicate key error creating JSONObject using fromProperties with CHECK_IDENTICAL`() { @@ -465,13 +441,11 @@ class JSONObjectTest { "abc" refersTo JSONInt(12345), "abc" refersTo JSONString("X"), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { JSONObject.fromProperties( properties, duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL, ) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -481,13 +455,13 @@ class JSONObjectTest { JSONObject.Property("def", JSONString("X")), ) val jsonObject = properties.toJSONObject() - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should create JSONObject using extension function with duplicateKeyOption`() { @@ -497,13 +471,13 @@ class JSONObjectTest { JSONObject.Property("def", JSONString("X")), ) val jsonObject = properties.toJSONObject(duplicateKeyOption = duplicateKeyOption) - expect(2) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect(JSONString("X")) { jsonObject["def"] } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":12345,"def":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 2 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject["def"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.toString() shouldBe """{"abc":12345,"def":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } } @@ -512,10 +486,8 @@ class JSONObjectTest { JSONObject.Property("abc", JSONInt(12345)), JSONObject.Property("abc", JSONString("X")), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { properties.toJSONObject() - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -524,10 +496,8 @@ class JSONObjectTest { JSONObject.Property("abc", JSONInt(12345)), JSONObject.Property("abc", JSONString("X")), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { properties.toJSONObject(duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -537,12 +507,12 @@ class JSONObjectTest { JSONObject.Property("abc", JSONString("X")), ) val jsonObject = properties.toJSONObject(duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_FIRST) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept last key creating JSONObject using extension function with TAKE_LAST`() { @@ -551,12 +521,12 @@ class JSONObjectTest { JSONObject.Property("abc", JSONString("X")), ) val jsonObject = properties.toJSONObject(duplicateKeyOption = JSONObject.DuplicateKeyOption.TAKE_LAST) - expect(1) { jsonObject.size } - expect(JSONString("X")) { jsonObject["abc"] } - expect("""{"abc":"X"}""") { jsonObject.toJSON() } - expect("""{"abc":"X"}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONString("X") + jsonObject.toJSON() shouldBe """{"abc":"X"}""" + jsonObject.toString() shouldBe """{"abc":"X"}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should accept matching duplicate creating JSONObject using extension function with CHECK_IDENTICAL`() { @@ -565,12 +535,12 @@ class JSONObjectTest { JSONObject.Property("abc", JSONInt(12345)), ) val jsonObject = properties.toJSONObject(duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) - expect(1) { jsonObject.size } - expect(JSONInt(12345)) { jsonObject["abc"] } - expect("""{"abc":12345}""") { jsonObject.toJSON() } - expect("""{"abc":12345}""") { jsonObject.toString() } - assertFalse(jsonObject.isEmpty()) - assertTrue(jsonObject.isNotEmpty()) + jsonObject.size shouldBe 1 + jsonObject["abc"] shouldBe JSONInt(12345) + jsonObject.toJSON() shouldBe """{"abc":12345}""" + jsonObject.toString() shouldBe """{"abc":12345}""" + jsonObject.isEmpty() shouldBe false + jsonObject.isNotEmpty() shouldBe true } @Test fun `should report duplicate key error creating JSONObject using extension function with CHECK_IDENTICAL`() { @@ -578,10 +548,8 @@ class JSONObjectTest { JSONObject.Property("abc", JSONInt(12345)), JSONObject.Property("abc", JSONString("X")), ) - assertFailsWith { + shouldThrow("Duplicate key - abc") { properties.toJSONObject(duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) - }.let { - expect("Duplicate key - abc") { it.message } } } @@ -589,12 +557,12 @@ class JSONObjectTest { val json = JSONObject.Builder { add("first", JSONInt(123)) add("second", JSONString("dummy")) - assertTrue(containsKey("first")) - assertTrue(containsKey("second")) + containsKey("first") shouldBe true + containsKey("second") shouldBe true }.build() - expect(2) { json.size } - expect(JSONInt(123)) { json["first"] } - expect(JSONString("dummy")) { json["second"] } + json.size shouldBe 2 + json["first"] shouldBe JSONInt(123) + json["second"] shouldBe JSONString("dummy") } @Test fun `should build JSONObject using build`() { @@ -602,9 +570,9 @@ class JSONObjectTest { add("first", JSONInt(123)) add("second", JSONString("dummy")) } - expect(2) { json.size } - expect(JSONInt(123)) { json["first"] } - expect(JSONString("dummy")) { json["second"] } + json.size shouldBe 2 + json["first"] shouldBe JSONInt(123) + json["second"] shouldBe JSONString("dummy") } @Test fun `should build JSONObject using build and Property`() { @@ -612,9 +580,9 @@ class JSONObjectTest { add(JSONObject.Property("first", JSONInt(123))) add(JSONObject.Property("second", JSONString("dummy"))) } - expect(2) { json.size } - expect(JSONInt(123)) { json["first"] } - expect(JSONString("dummy")) { json["second"] } + json.size shouldBe 2 + json["first"] shouldBe JSONInt(123) + json["second"] shouldBe JSONString("dummy") } @Test fun `should build JSONObject using build with non-JSON classes`() { @@ -625,40 +593,38 @@ class JSONObjectTest { add("fourth", "0.123".toBigDecimal()) add("fifth", true) } - expect(5) { json.size } - expect(JSONInt(123)) { json["first"] } - expect(JSONString("dummy")) { json["second"] } - expect(JSONLong(123456789123456789)) { json["third"] } - expect(JSONDecimal("0.123".toBigDecimal())) { json["fourth"] } - expect(JSONBoolean.TRUE) { json["fifth"] } + json.size shouldBe 5 + json["first"] shouldBe JSONInt(123) + json["second"] shouldBe JSONString("dummy") + json["third"] shouldBe JSONLong(123456789123456789) + json["fourth"] shouldBe JSONDecimal("0.123".toBigDecimal()) + json["fifth"] shouldBe JSONBoolean.TRUE } @Test fun `should limit Builder to single use`() { val builder = JSONObject.Builder() builder.add("first", JSONInt(123)) builder.add("second", JSONString("dummy")) - assertTrue(builder.containsKey("second")) - assertFailsWith { builder.add("second", JSONString("another")) }.let { - expect("Duplicate key - second") { it.message } + builder.containsKey("second") shouldBe true + shouldThrow("Duplicate key - second") { + builder.add("second", JSONString("another")) } - expect(2) { builder.size } + builder.size shouldBe 2 val json = builder.build() - expect(2) { json.size } - assertFailsWith { builder.build() }.let { - expect("Builder is closed") { it.message } - } + json.size shouldBe 2 + shouldThrow("Builder is closed") { builder.build() } } @Test fun `should correctly report containsKey`() { - assertTrue(simpleObject.containsKey("abc")) - assertFalse(simpleObject.containsKey("xyz")) + simpleObject.containsKey("abc") shouldBe true + simpleObject.containsKey("xyz") shouldBe false } @Test fun `should correctly report containsValue`() { - assertTrue(mixedObject.containsValue(JSONInt(123))) - assertFalse(mixedObject.containsValue(JSONInt(456))) - assertFalse(mixedObject.containsValue(null)) - assertTrue(mixedObjectWithNull.containsValue(null)) + mixedObject.containsValue(JSONInt(123)) shouldBe true + mixedObject.containsValue(JSONInt(456)) shouldBe false + mixedObject.containsValue(null) shouldBe false + mixedObjectWithNull.containsValue(null) shouldBe true } @Test fun `should compare to other Map`() { @@ -667,45 +633,45 @@ class JSONObjectTest { add("beta", JSONString("hello")) }.build() val map = mapOf("alpha" to JSONInt(1111), "beta" to JSONString("hello")) - assertEquals>(json, map) - assertEquals>(map, json) - assertEquals(json.hashCode(), map.hashCode()) + map.shouldBe>(json) + json.shouldBe>(map) + map.hashCode() shouldBe json.hashCode() } @Test fun `should allow use of keys`() { val jsonObject = JSONObject.of("abc" to JSONInt(12345), "def" to JSONString("X")) - expect(2) { jsonObject.size } + jsonObject.size shouldBe 2 val keysIterator = jsonObject.keys.iterator() - expect("abc") { keysIterator.next() } - expect("def") { keysIterator.next() } - assertFalse { keysIterator.hasNext() } + keysIterator.next() shouldBe "abc" + keysIterator.next() shouldBe "def" + keysIterator.hasNext() shouldBe false } @Test fun `should allow use of values`() { val jsonObject = JSONObject.of("abc" to JSONInt(12345), "def" to JSONString("X")) - expect(2) { jsonObject.size } + jsonObject.size shouldBe 2 val valuesIterator = jsonObject.values.iterator() - expect(JSONInt(12345)) { valuesIterator.next() } - expect(JSONString("X")) { valuesIterator.next() } - assertFalse { valuesIterator.hasNext() } + valuesIterator.next() shouldBe JSONInt(12345) + valuesIterator.next() shouldBe JSONString("X") + valuesIterator.hasNext() shouldBe false } @Test fun `should allow use of entries`() { val jsonObject = JSONObject.of("abc" to JSONInt(12345), "def" to JSONString("X")) - expect(2) { jsonObject.size } + jsonObject.size shouldBe 2 val entriesIterator = jsonObject.entries.iterator() val entry1 = entriesIterator.next() - expect("abc") { entry1.key } - expect(JSONInt(12345)) { entry1.value } + entry1.key shouldBe "abc" + entry1.value shouldBe JSONInt(12345) val entry2 = entriesIterator.next() - expect("def") { entry2.key } - expect(JSONString("X")) { entry2.value } - assertFalse { entriesIterator.hasNext() } + entry2.key shouldBe "def" + entry2.value shouldBe JSONString("X") + entriesIterator.hasNext() shouldBe false for ((a, b) in jsonObject.entries) { // check that destructuring works if (a == "abc") - expect(JSONInt(12345)) { b } + b shouldBe JSONInt(12345) if (a == "def") - expect(JSONString("X")) { b } + b shouldBe JSONString("X") } } @@ -713,56 +679,54 @@ class JSONObjectTest { val string = buildString { simpleObject.appendTo(this) } - expect("""{"abc":12345,"def":"X"}""") { string } + string shouldBe """{"abc":12345,"def":"X"}""" } @Test fun `should format JSONObject using output`() { val capture = OutputCapture(64) JSONObject.EMPTY.outputTo(capture) - expect("{}") { capture.toString() } + capture.toString() shouldBe "{}" capture.reset() simpleObject.outputTo(capture) - expect("""{"abc":12345,"def":"X"}""") { capture.toString() } + capture.toString() shouldBe """{"abc":12345,"def":"X"}""" } @Test fun `should format JSONObject using coOutput`() = runBlocking { val capture = CoOutputCapture(64) JSONObject.EMPTY.coOutputTo(capture) - expect("{}") { capture.toString() } + capture.toString() shouldBe "{}" capture.reset() simpleObject.coOutputTo(capture) - expect("""{"abc":12345,"def":"X"}""") { capture.toString() } + capture.toString() shouldBe """{"abc":12345,"def":"X"}""" } @Test fun `should get existing Builder entries`() { val builder = JSONObject.Builder() builder.add("first", JSONInt(123)) builder.add("second", JSONString("dummy")) - expect(JSONInt(123)) { builder.get("first") } - expect(JSONString("dummy")) { builder.get("second") } - assertNull(builder.get("third")) + builder.get("first") shouldBe JSONInt(123) + builder.get("second") shouldBe JSONString("dummy") + builder.get("third") shouldBe null } @Test fun `should remove existing Builder entries`() { val builder = JSONObject.Builder() builder.add("first", JSONInt(123)) builder.add("second", JSONString("dummy")) - expect(JSONInt(123)) { builder.get("first") } - expect(JSONString("dummy")) { builder.get("second") } + builder.get("first") shouldBe JSONInt(123) + builder.get("second") shouldBe JSONString("dummy") builder.remove("first") - assertNull(builder.get("first")) + builder.get("first") shouldBe null val result = builder.build() - expect(1) { result.size } - expect(JSONString("dummy")) { result["second"] } + result.size shouldBe 1 + result["second"] shouldBe JSONString("dummy") } @Test fun `should fail on incorrect remove of existing Builder entries`() { val builder = JSONObject.Builder() builder.add("first", JSONInt(123)) builder.add("second", JSONString("dummy")) - assertFailsWith { builder.remove("third") }.let { - expect("Key not found - third") { it.message } - } + shouldThrow("Key not found - third") { builder.remove("third") } } @Test fun `should iterate over object entries`() { @@ -770,28 +734,28 @@ class JSONObjectTest { mixedObject.forEachEntry { k, v -> when (count++) { 0 -> { - expect("first") { k } - expect(JSONInt(123)) { v } + k shouldBe "first" + v shouldBe JSONInt(123) } 1 -> { - expect("second") { k } - expect(JSONString("dummy")) { v } + k shouldBe "second" + v shouldBe JSONString("dummy") } 2 -> { - expect("third") { k } - expect(JSONLong(123456789123456789)) { v } + k shouldBe "third" + v shouldBe JSONLong(123456789123456789) } 3 -> { - expect("fourth") { k } - expect(JSONDecimal("0.123")) { v } + k shouldBe "fourth" + v shouldBe JSONDecimal("0.123") } 4 -> { - expect("fifth") { k } - expect(JSONBoolean.TRUE) { v } + k shouldBe "fifth" + v shouldBe JSONBoolean.TRUE } } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over object properties`() { @@ -799,56 +763,56 @@ class JSONObjectTest { mixedObject.forEachProperty { when (count++) { 0 -> { - expect("first") { it.name } - expect(JSONInt(123)) { it.value } + it.name shouldBe "first" + it.value shouldBe JSONInt(123) } 1 -> { - expect("second") { it.name } - expect(JSONString("dummy")) { it.value } + it.name shouldBe "second" + it.value shouldBe JSONString("dummy") } 2 -> { - expect("third") { it.name } - expect(JSONLong(123456789123456789)) { it.value } + it.name shouldBe "third" + it.value shouldBe JSONLong(123456789123456789) } 3 -> { - expect("fourth") { it.name } - expect(JSONDecimal("0.123")) { it.value } + it.name shouldBe "fourth" + it.value shouldBe JSONDecimal("0.123") } 4 -> { - expect("fifth") { it.name } - expect(JSONBoolean.TRUE) { it.value } + it.name shouldBe "fifth" + it.value shouldBe JSONBoolean.TRUE } } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over object keys`() { var count = 0 mixedObject.forEachKey { when (count++) { - 0 -> expect("first") { it } - 1 -> expect("second") { it } - 2 -> expect("third") { it } - 3 -> expect("fourth") { it } - 4 -> expect("fifth") { it } + 0 -> it shouldBe "first" + 1 -> it shouldBe "second" + 2 -> it shouldBe "third" + 3 -> it shouldBe "fourth" + 4 -> it shouldBe "fifth" } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over object values`() { var count = 0 mixedObject.forEachValue { when (count++) { - 0 -> expect(JSONInt(123)) { it } - 1 -> expect(JSONString("dummy")) { it } - 2 -> expect(JSONLong(123456789123456789)) { it } - 3 -> expect(JSONDecimal("0.123")) { it } - 4 -> expect(JSONBoolean.TRUE) { it } + 0 -> it shouldBe JSONInt(123) + 1 -> it shouldBe JSONString("dummy") + 2 -> it shouldBe JSONLong(123456789123456789) + 3 -> it shouldBe JSONDecimal("0.123") + 4 -> it shouldBe JSONBoolean.TRUE } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over object as List`() { @@ -856,142 +820,140 @@ class JSONObjectTest { for (property in mixedObject) { when (count++) { 0 -> { - expect("first") { property.name } - expect(JSONInt(123)) { property.value } + property.name shouldBe "first" + property.value shouldBe JSONInt(123) } 1 -> { - expect("second") { property.name } - expect(JSONString("dummy")) { property.value } + property.name shouldBe "second" + property.value shouldBe JSONString("dummy") } 2 -> { - expect("third") { property.name } - expect(JSONLong(123456789123456789)) { property.value } + property.name shouldBe "third" + property.value shouldBe JSONLong(123456789123456789) } 3 -> { - expect("fourth") { property.name } - expect(JSONDecimal("0.123")) { property.value } + property.name shouldBe "fourth" + property.value shouldBe JSONDecimal("0.123") } 4 -> { - expect("fifth") { property.name } - expect(JSONBoolean.TRUE) { property.value } + property.name shouldBe "fifth" + property.value shouldBe JSONBoolean.TRUE } } } - expect(5) { count } + count shouldBe 5 } @Test fun `should iterate over object using ListIterator`() { val iterator = mixedObject.listIterator() - assertTrue(iterator.hasNext()) - assertFalse(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe false with(iterator.next()) { - expect("first") { name } - expect(JSONInt(123)) { value } + name shouldBe "first" + value shouldBe JSONInt(123) } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.next()) { - expect("second") { name } - expect(JSONString("dummy")) { value } + name shouldBe "second" + value shouldBe JSONString("dummy") } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.next()) { - expect("third") { name } - expect(JSONLong(123456789123456789)) { value } + name shouldBe "third" + value shouldBe JSONLong(123456789123456789) } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.next()) { - expect("fourth") { name } - expect(JSONDecimal("0.123")) { value } + name shouldBe "fourth" + value shouldBe JSONDecimal("0.123") } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.next()) { - expect("fifth") { name } - expect(JSONBoolean.TRUE) { value } + name shouldBe "fifth" + value shouldBe JSONBoolean.TRUE } - assertFalse(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe false + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("fifth") { name } - expect(JSONBoolean.TRUE) { value } + name shouldBe "fifth" + value shouldBe JSONBoolean.TRUE } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("fourth") { name } - expect(JSONDecimal("0.123")) { value } + name shouldBe "fourth" + value shouldBe JSONDecimal("0.123") } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("third") { name } - expect(JSONLong(123456789123456789)) { value } + name shouldBe "third" + value shouldBe JSONLong(123456789123456789) } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("second") { name } - expect(JSONString("dummy")) { value } + name shouldBe "second" + value shouldBe JSONString("dummy") } - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("first") { name } - expect(JSONInt(123)) { value } + name shouldBe "first" + value shouldBe JSONInt(123) } - assertTrue(iterator.hasNext()) - assertFalse(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe false } @Test fun `should iterate over object using ListIterator with start index`() { val iterator = mixedObject.listIterator(1) - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe true with(iterator.previous()) { - expect("first") { name } - expect(JSONInt(123)) { value } + name shouldBe "first" + value shouldBe JSONInt(123) } - assertTrue(iterator.hasNext()) - assertFalse(iterator.hasPrevious()) + iterator.hasNext() shouldBe true + iterator.hasPrevious() shouldBe false } @Test fun `should not iterate over empty object using ListIterator`() { val iterator = JSONObject.EMPTY.listIterator(1) - assertFalse(iterator.hasNext()) - assertTrue(iterator.hasPrevious()) + iterator.hasNext() shouldBe false + iterator.hasPrevious() shouldBe true } @Test fun `should create subset object using subList`() { val sub = mixedObject.subList(2, 4) - expect(2) { sub.size } - expect(JSONObject.Property("third", JSONLong(123456789123456789))) { sub[0] } - expect(JSONObject.Property("fourth", JSONDecimal("0.123"))) { sub[1] } + sub.size shouldBe 2 + sub[0] shouldBe JSONObject.Property("third", JSONLong(123456789123456789)) + sub[1] shouldBe JSONObject.Property("fourth", JSONDecimal("0.123")) } @Test fun `should create Property`() { val property = JSONObject.Property("propertyName", JSONInt(12345)) - expect("propertyName") { property.name } - expect(JSONInt(12345)) { property.value } - expect("propertyName=12345") { property.toString() } + property.name shouldBe "propertyName" + property.value shouldBe JSONInt(12345) + property.toString() shouldBe "propertyName=12345" } @Test fun `should allow destructuring operations on Property`() { val property = JSONObject.Property("propertyName", JSONInt(12345)) val (aaa, bbb) = property - expect("propertyName") { aaa } - expect(JSONInt(12345)) { bbb } + aaa shouldBe "propertyName" + bbb shouldBe JSONInt(12345) } @Test fun `should respect DuplicateKeyOption ERROR`() { val builder = JSONObject.Builder(duplicateKeyOption = JSONObject.DuplicateKeyOption.ERROR) builder.add("first", 111) builder.add("second", 222) - assertFailsWith { builder.add("second", 333) }.let { - expect("Duplicate key - second") { it.message } - } + shouldThrow("Duplicate key - second") { builder.add("second", 333) } } @Test fun `should respect DuplicateKeyOption TAKE_FIRST`() { @@ -1000,9 +962,9 @@ class JSONObjectTest { builder.add("second", 222) builder.add("second", 333) with(builder.build()) { - expect(2) { size } - expect(JSONInt(111)) { this["first"] } - expect(JSONInt(222)) { this["second"] } + size shouldBe 2 + this["first"] shouldBe JSONInt(111) + this["second"] shouldBe JSONInt(222) } } @@ -1012,9 +974,9 @@ class JSONObjectTest { builder.add("second", 222) builder.add("second", 333) with(builder.build()) { - expect(2) { size } - expect(JSONInt(111)) { this["first"] } - expect(JSONInt(333)) { this["second"] } + size shouldBe 2 + this["first"] shouldBe JSONInt(111) + this["second"] shouldBe JSONInt(333) } } @@ -1024,9 +986,9 @@ class JSONObjectTest { builder.add("second", 222) builder.add("second", 222) with(builder.build()) { - expect(2) { size } - expect(JSONInt(111)) { this["first"] } - expect(JSONInt(222)) { this["second"] } + size shouldBe 2 + this["first"] shouldBe JSONInt(111) + this["second"] shouldBe JSONInt(222) } } @@ -1034,9 +996,7 @@ class JSONObjectTest { val builder = JSONObject.Builder(duplicateKeyOption = JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) builder.add("first", 111) builder.add("second", 222) - assertFailsWith { builder.add("second", 333) }.let { - expect("Duplicate key - second") { it.message } - } + shouldThrow("Duplicate key - second") { builder.add("second", 333) } } companion object { diff --git a/src/test/kotlin/io/kjson/JSONStringTest.kt b/src/test/kotlin/io/kjson/JSONStringTest.kt index 32725fb..64b9ee3 100644 --- a/src/test/kotlin/io/kjson/JSONStringTest.kt +++ b/src/test/kotlin/io/kjson/JSONStringTest.kt @@ -26,10 +26,12 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertSame -import kotlin.test.expect + import kotlinx.coroutines.runBlocking +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance + import io.kjson.JSON.asString import io.kjson.testutil.CoOutputCapture import io.kjson.testutil.OutputCapture @@ -38,52 +40,52 @@ class JSONStringTest { @Test fun `should create JSONString`() { val testString = JSONString("ab\u2014c\n") - expect("ab\u2014c\n") { testString.value } - expect("\"ab\\u2014c\\n\"") { testString.toJSON() } - expect("ab\u2014c\n") { testString.toString() } + testString.value shouldBe "ab\u2014c\n" + testString.toJSON() shouldBe "\"ab\\u2014c\\n\"" + testString.toString() shouldBe "ab\u2014c\n" } @Test fun `should create JSONString using of`() { val testString = JSONString.of("Hello!") - expect("Hello!") { testString.value } - expect("\"Hello!\"") { testString.toJSON() } - expect("Hello!") { testString.toString() } + testString.value shouldBe "Hello!" + testString.toJSON() shouldBe "\"Hello!\"" + testString.toString() shouldBe "Hello!" } @Test fun `should use EMPTY`() { val testString = JSONString.of("") - assertSame(JSONString.EMPTY, testString) - expect(JSONString.EMPTY_STRING) { testString.toString() } - expect("\"\"") { testString.toJSON() } + testString shouldBeSameInstance JSONString.EMPTY + testString.toString() shouldBe JSONString.EMPTY_STRING + testString.toJSON() shouldBe "\"\"" } @Test fun `should get value using stringValue`() { val json = JSON.parse("\"abc\"") - expect("abc") { json.asString } + json.asString shouldBe "abc" } @Test fun `should return JSONString from subSequence`() { val json = JSONString.of("irrational") val substring = json.subSequence(2, 7) - expect("\"ratio\"") { substring.toJSON() } + substring.toJSON() shouldBe "\"ratio\"" } @Test fun `should format JSONString using output`() { val capture = OutputCapture(16) JSONString.EMPTY.outputTo(capture) - expect("\"\"") { capture.toString() } + capture.toString() shouldBe "\"\"" capture.reset() JSONString("Kia ora").outputTo(capture) - expect("\"Kia ora\"") { capture.toString() } + capture.toString() shouldBe "\"Kia ora\"" } @Test fun `should format JSONString using coOutput`() = runBlocking { val capture = CoOutputCapture(16) JSONString.EMPTY.coOutputTo(capture) - expect("\"\"") { capture.toString() } + capture.toString() shouldBe "\"\"" capture.reset() JSONString("Kia ora").coOutputTo(capture) - expect("\"Kia ora\"") { capture.toString() } + capture.toString() shouldBe "\"Kia ora\"" } @Test fun `should build a JSONString using build function`() { @@ -92,12 +94,12 @@ class JSONStringTest { append("able") append(99) } - expect(JSONString("Cable99")) { json } + json shouldBe JSONString("Cable99") } @Test fun `should return EMPTY when using build function with no content`() { val json = JSONString.build {} - assertSame(JSONString.EMPTY, json) + json shouldBeSameInstance JSONString.EMPTY } } diff --git a/src/test/kotlin/io/kjson/JSONStructureTest.kt b/src/test/kotlin/io/kjson/JSONStructureTest.kt index eca76c6..2a8b9eb 100644 --- a/src/test/kotlin/io/kjson/JSONStructureTest.kt +++ b/src/test/kotlin/io/kjson/JSONStructureTest.kt @@ -26,10 +26,9 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertFalse -import kotlin.test.assertTrue -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldThrow import io.kjson.JSON.asString @@ -39,19 +38,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("data", "something") } - expect("something") { outer.getString("data") } + outer.getString("data") shouldBe "something" } @Test fun `should fail on getString from object when not a string`() { val outer = JSONObject.build { add("data", true) } - assertFailsWith { outer.getString("data") }.let { - expect("Node") { it.nodeName } - expect("String") { it.target } - expect("data") { it.key } - expect(JSONBoolean.TRUE) { it.value } - expect("Node not correct type (String), was true, at data") { it.message } + shouldThrow("Node not correct type (String), was true, at data") { + outer.getString("data") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "String" + it.key shouldBe "data" + it.value shouldBe JSONBoolean.TRUE } } @@ -59,19 +59,20 @@ class JSONStructureTest { val outer = JSONArray.build { add("nice") } - expect("nice") { outer.getString(0) } + outer.getString(0) shouldBe "nice" } @Test fun `should fail on getString from array when not a string`() { val outer = JSONArray.build { add(42) } - assertFailsWith { outer.getString(0) }.let { - expect("Node") { it.nodeName } - expect("String") { it.target } - expect(0) { it.key } - expect(JSONInt(42)) { it.value } - expect("Node not correct type (String), was 42, at 0") { it.message } + shouldThrow("Node not correct type (String), was 42, at 0") { + outer.getString(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "String" + it.key shouldBe 0 + it.value shouldBe JSONInt(42) } } @@ -79,19 +80,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("lots", 1234567812345678) } - expect(1234567812345678) { outer.getLong("lots") } + outer.getLong("lots") shouldBe 1234567812345678 } @Test fun `should fail on getLong from object when not a long`() { val outer = JSONObject.build { add("lots", "millions") } - assertFailsWith { outer.getLong("lots") }.let { - expect("Node") { it.nodeName } - expect("Long") { it.target } - expect("lots") { it.key } - expect(JSONString("millions")) { it.value } - expect("Node not correct type (Long), was \"millions\", at lots") { it.message } + shouldThrow("Node not correct type (Long), was \"millions\", at lots") { + outer.getLong("lots") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Long" + it.key shouldBe "lots" + it.value shouldBe JSONString("millions") } } @@ -99,19 +101,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(-2244668822446688) } - expect(-2244668822446688) { outer.getLong(0) } + outer.getLong(0) shouldBe -2244668822446688 } @Test fun `should fail on getLong from array when not a long`() { val outer = JSONArray.build { add("1.555".toBigDecimal()) } - assertFailsWith { outer.getLong(0) }.let { - expect("Node") { it.nodeName } - expect("Long") { it.target } - expect(0) { it.key } - expect(JSONDecimal("1.555")) { it.value } - expect("Node not correct type (Long), was 1.555, at 0") { it.message } + shouldThrow("Node not correct type (Long), was 1.555, at 0") { + outer.getLong(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Long" + it.key shouldBe 0 + it.value shouldBe JSONDecimal("1.555") } } @@ -119,19 +122,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("number", 12345678) } - expect(12345678) { outer.getInt("number") } + outer.getInt("number") shouldBe 12345678 } @Test fun `should fail on getInt from object when not an int`() { val outer = JSONObject.build { add("number", "trouble") } - assertFailsWith { outer.getInt("number") }.let { - expect("Node") { it.nodeName } - expect("Int") { it.target } - expect("number") { it.key } - expect(JSONString("trouble")) { it.value } - expect("Node not correct type (Int), was \"trouble\", at number") { it.message } + shouldThrow("Node not correct type (Int), was \"trouble\", at number") { + outer.getInt("number") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Int" + it.key shouldBe "number" + it.value shouldBe JSONString("trouble") } } @@ -139,19 +143,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(-22446688) } - expect(-22446688) { outer.getInt(0) } + outer.getInt(0) shouldBe -22446688 } @Test fun `should fail on getInt from array when not an int`() { val outer = JSONArray.build { add("1.5".toBigDecimal()) } - assertFailsWith { outer.getInt(0) }.let { - expect("Node") { it.nodeName } - expect("Int") { it.target } - expect(0) { it.key } - expect(JSONDecimal("1.5")) { it.value } - expect("Node not correct type (Int), was 1.5, at 0") { it.message } + shouldThrow("Node not correct type (Int), was 1.5, at 0") { + outer.getInt(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Int" + it.key shouldBe 0 + it.value shouldBe JSONDecimal("1.5") } } @@ -159,19 +164,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("mini", 12345) } - expect(12345) { outer.getShort("mini") } + outer.getShort("mini") shouldBe 12345 } @Test fun `should fail on getShort from object when not a short`() { val outer = JSONObject.build { add("mini", 123456) } - assertFailsWith { outer.getShort("mini") }.let { - expect("Node") { it.nodeName } - expect("Short") { it.target } - expect("mini") { it.key } - expect(JSONInt(123456)) { it.value } - expect("Node not correct type (Short), was 123456, at mini") { it.message } + shouldThrow("Node not correct type (Short), was 123456, at mini") { + outer.getShort("mini") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Short" + it.key shouldBe "mini" + it.value shouldBe JSONInt(123456) } } @@ -179,19 +185,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(-20000) } - expect(-20000) { outer.getShort(0) } + outer.getShort(0) shouldBe -20000 } @Test fun `should fail on getShort from array when not a short`() { val outer = JSONArray.build { add(false) } - assertFailsWith { outer.getShort(0) }.let { - expect("Node") { it.nodeName } - expect("Short") { it.target } - expect(0) { it.key } - expect(JSONBoolean.FALSE) { it.value } - expect("Node not correct type (Short), was false, at 0") { it.message } + shouldThrow("Node not correct type (Short), was false, at 0") { + outer.getShort(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Short" + it.key shouldBe 0 + it.value shouldBe JSONBoolean.FALSE } } @@ -199,19 +206,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("little", 123) } - expect(123) { outer.getByte("little") } + outer.getByte("little") shouldBe 123 } @Test fun `should fail on getByte from object when not a byte`() { val outer = JSONObject.build { add("little", "why?") } - assertFailsWith { outer.getByte("little") }.let { - expect("Node") { it.nodeName } - expect("Byte") { it.target } - expect("little") { it.key } - expect(JSONString("why?")) { it.value } - expect("Node not correct type (Byte), was \"why?\", at little") { it.message } + shouldThrow("Node not correct type (Byte), was \"why?\", at little") { + outer.getByte("little") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Byte" + it.key shouldBe "little" + it.value shouldBe JSONString("why?") } } @@ -219,19 +227,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(-99) } - expect(-99) { outer.getByte(0) } + outer.getByte(0) shouldBe -99 } @Test fun `should fail on getByte from array when not a byte`() { val outer = JSONArray.build { add("mushroom") } - assertFailsWith { outer.getByte(0) }.let { - expect("Node") { it.nodeName } - expect("Byte") { it.target } - expect(0) { it.key } - expect(JSONString("mushroom")) { it.value } - expect("Node not correct type (Byte), was \"mushroom\", at 0") { it.message } + shouldThrow("Node not correct type (Byte), was \"mushroom\", at 0") { + outer.getByte(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Byte" + it.key shouldBe 0 + it.value shouldBe JSONString("mushroom") } } @@ -239,19 +248,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("big", 123456789123456789) } - expect(123456789123456789U) { outer.getULong("big") } + outer.getULong("big") shouldBe 123456789123456789U } @Test fun `should fail on getULong from object when not a ULong`() { val outer = JSONObject.build { add("big", true) } - assertFailsWith { outer.getULong("big") }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - expect("big") { it.key } - expect(JSONBoolean.TRUE) { it.value } - expect("Node not correct type (ULong), was true, at big") { it.message } + shouldThrow("Node not correct type (ULong), was true, at big") { + outer.getULong("big") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe "big" + it.value shouldBe JSONBoolean.TRUE } } @@ -259,19 +269,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(99) } - expect(99U) { outer.getULong(0) } + outer.getULong(0) shouldBe 99U } @Test fun `should fail on getULong from array when not a ULong`() { val outer = JSONArray.build { add("1.1".toBigDecimal()) } - assertFailsWith { outer.getULong(0) }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - expect(0) { it.key } - expect(JSONDecimal("1.1")) { it.value } - expect("Node not correct type (ULong), was 1.1, at 0") { it.message } + shouldThrow("Node not correct type (ULong), was 1.1, at 0") { + outer.getULong(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe 0 + it.value shouldBe JSONDecimal("1.1") } } @@ -279,19 +290,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("number", 123456) } - expect(123456U) { outer.getUInt("number") } + outer.getUInt("number") shouldBe 123456U } @Test fun `should fail on getUInt from object when not a UInt`() { val outer = JSONObject.build { add("number", -123456) } - assertFailsWith { outer.getUInt("number") }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - expect("number") { it.key } - expect(JSONInt(-123456)) { it.value } - expect("Node not correct type (UInt), was -123456, at number") { it.message } + shouldThrow("Node not correct type (UInt), was -123456, at number") { + outer.getUInt("number") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe "number" + it.value shouldBe JSONInt(-123456) } } @@ -299,19 +311,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(99) } - expect(99U) { outer.getUInt(0) } + outer.getUInt(0) shouldBe 99U } @Test fun `should fail on getUInt from array when not a UInt`() { val outer = JSONArray.build { add("incorrect") } - assertFailsWith { outer.getUInt(0) }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - expect(0) { it.key } - expect(JSONString("incorrect")) { it.value } - expect("Node not correct type (UInt), was \"incorrect\", at 0") { it.message } + shouldThrow("Node not correct type (UInt), was \"incorrect\", at 0") { + outer.getUInt(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe 0 + it.value shouldBe JSONString("incorrect") } } @@ -319,19 +332,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("unit", 60000) } - expect(60000U) { outer.getUShort("unit") } + outer.getUShort("unit") shouldBe 60000U } @Test fun `should fail on getUShort from object when not a UShort`() { val outer = JSONObject.build { add("unit", -1) } - assertFailsWith { outer.getUShort("unit") }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - expect("unit") { it.key } - expect(JSONInt(-1)) { it.value } - expect("Node not correct type (UShort), was -1, at unit") { it.message } + shouldThrow("Node not correct type (UShort), was -1, at unit") { + outer.getUShort("unit") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe "unit" + it.value shouldBe JSONInt(-1) } } @@ -339,19 +353,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(1000) } - expect(1000U) { outer.getUShort(0) } + outer.getUShort(0) shouldBe 1000U } @Test fun `should fail on getUShort from array when not a UShort`() { val outer = JSONArray.build { add(123456) } - assertFailsWith { outer.getUShort(0) }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - expect(0) { it.key } - expect(JSONInt(123456)) { it.value } - expect("Node not correct type (UShort), was 123456, at 0") { it.message } + shouldThrow("Node not correct type (UShort), was 123456, at 0") { + outer.getUShort(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe 0 + it.value shouldBe JSONInt(123456) } } @@ -359,19 +374,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("unit", 200) } - expect(200U) { outer.getUByte("unit") } + outer.getUByte("unit") shouldBe 200U } @Test fun `should fail on getUByte from object when not a UByte`() { val outer = JSONObject.build { add("unit", -200) } - assertFailsWith { outer.getUByte("unit") }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - expect("unit") { it.key } - expect(JSONInt(-200)) { it.value } - expect("Node not correct type (UByte), was -200, at unit") { it.message } + shouldThrow("Node not correct type (UByte), was -200, at unit") { + outer.getUByte("unit") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe "unit" + it.value shouldBe JSONInt(-200) } } @@ -379,19 +395,20 @@ class JSONStructureTest { val outer = JSONArray.build { add(150) } - expect(150U) { outer.getUByte(0) } + outer.getUByte(0) shouldBe 150U } @Test fun `should fail on getUByte from array when not a UByte`() { val outer = JSONArray.build { add(300) } - assertFailsWith { outer.getUByte(0) }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - expect(0) { it.key } - expect(JSONInt(300)) { it.value } - expect("Node not correct type (UByte), was 300, at 0") { it.message } + shouldThrow("Node not correct type (UByte), was 300, at 0") { + outer.getUByte(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe 0 + it.value shouldBe JSONInt(300) } } @@ -399,19 +416,20 @@ class JSONStructureTest { val outer = JSONObject.build { add("money", "2.50".toBigDecimal()) } - expect("2.50".toBigDecimal()) { outer.getDecimal("money") } + outer.getDecimal("money") shouldBe "2.50".toBigDecimal() } @Test fun `should fail on getDecimal from object when not a decimal`() { val outer = JSONObject.build { add("money", "bad") } - assertFailsWith { outer.getDecimal("money") }.let { - expect("Node") { it.nodeName } - expect("BigDecimal") { it.target } - expect("money") { it.key } - expect(JSONString("bad")) { it.value } - expect("Node not correct type (BigDecimal), was \"bad\", at money") { it.message } + shouldThrow("Node not correct type (BigDecimal), was \"bad\", at money") { + outer.getDecimal("money") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "BigDecimal" + it.key shouldBe "money" + it.value shouldBe JSONString("bad") } } @@ -419,19 +437,20 @@ class JSONStructureTest { val outer = JSONArray.build { add("250.00".toBigDecimal()) } - expect("250.00".toBigDecimal()) { outer.getDecimal(0) } + outer.getDecimal(0) shouldBe "250.00".toBigDecimal() } @Test fun `should fail on getDecimal from array when not a decimal`() { val outer = JSONArray.build { add("sideboard") } - assertFailsWith { outer.getDecimal(0) }.let { - expect("Node") { it.nodeName } - expect("BigDecimal") { it.target } - expect(0) { it.key } - expect(JSONString("sideboard")) { it.value } - expect("Node not correct type (BigDecimal), was \"sideboard\", at 0") { it.message } + shouldThrow("Node not correct type (BigDecimal), was \"sideboard\", at 0") { + outer.getDecimal(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "BigDecimal" + it.key shouldBe 0 + it.value shouldBe JSONString("sideboard") } } @@ -440,20 +459,21 @@ class JSONStructureTest { add("bool1", false) add("bool2", true) } - assertFalse { outer.getBoolean("bool1") } - assertTrue { outer.getBoolean("bool2") } + outer.getBoolean("bool1") shouldBe false + outer.getBoolean("bool2") shouldBe true } @Test fun `should fail on getBoolean from object when not a boolean`() { val outer = JSONObject.build { add("bool1", 27) } - assertFailsWith { outer.getBoolean("bool1") }.let { - expect("Node") { it.nodeName } - expect("Boolean") { it.target } - expect("bool1") { it.key } - expect(JSONInt(27)) { it.value } - expect("Node not correct type (Boolean), was 27, at bool1") { it.message } + shouldThrow("Node not correct type (Boolean), was 27, at bool1") { + outer.getBoolean("bool1") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Boolean" + it.key shouldBe "bool1" + it.value shouldBe JSONInt(27) } } @@ -462,20 +482,21 @@ class JSONStructureTest { add(false) add(true) } - assertFalse { outer.getBoolean(0) } - assertTrue { outer.getBoolean(1) } + outer.getBoolean(0) shouldBe false + outer.getBoolean(1) shouldBe true } @Test fun `should fail on getBoolean from array when not a boolean`() { val outer = JSONArray.build { add("wrong") } - assertFailsWith { outer.getBoolean(0) }.let { - expect("Node") { it.nodeName } - expect("Boolean") { it.target } - expect(0) { it.key } - expect(JSONString("wrong")) { it.value } - expect("Node not correct type (Boolean), was \"wrong\", at 0") { it.message } + shouldThrow("Node not correct type (Boolean), was \"wrong\", at 0") { + outer.getBoolean(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Boolean" + it.key shouldBe 0 + it.value shouldBe JSONString("wrong") } } @@ -486,19 +507,20 @@ class JSONStructureTest { }) } val inner = outer.getArray("inner") - expect("ABC") { inner[0].asString } + inner[0].asString shouldBe "ABC" } @Test fun `should fail on getArray from object when not an array`() { val outer = JSONObject.build { add("inner", 123) } - assertFailsWith { outer.getArray("inner") }.let { - expect("Node") { it.nodeName } - expect("JSONArray") { it.target } - expect("inner") { it.key } - expect(JSONInt(123)) { it.value } - expect("Node not correct type (JSONArray), was 123, at inner") { it.message } + shouldThrow("Node not correct type (JSONArray), was 123, at inner") { + outer.getArray("inner") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONArray" + it.key shouldBe "inner" + it.value shouldBe JSONInt(123) } } @@ -509,19 +531,20 @@ class JSONStructureTest { }) } val inner = outer.getArray(0) - expect("ABC") { inner[0].asString } + inner[0].asString shouldBe "ABC" } @Test fun `should fail on getArray from array when not an array`() { val outer = JSONArray.build { add(false) } - assertFailsWith { outer.getArray(0) }.let { - expect("Node") { it.nodeName } - expect("JSONArray") { it.target } - expect(0) { it.key } - expect(JSONBoolean.FALSE) { it.value } - expect("Node not correct type (JSONArray), was false, at 0") { it.message } + shouldThrow("Node not correct type (JSONArray), was false, at 0") { + outer.getArray(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONArray" + it.key shouldBe 0 + it.value shouldBe JSONBoolean.FALSE } } @@ -532,19 +555,20 @@ class JSONStructureTest { }) } val inner = outer.getObject("inner") - expect("ABC") { inner["item"].asString } + inner["item"].asString shouldBe "ABC" } @Test fun `should fail on getObject from object when not an object`() { val outer = JSONObject.build { add("inner", 42) } - assertFailsWith { outer.getObject("inner") }.let { - expect("Node") { it.nodeName } - expect("JSONObject") { it.target } - expect("inner") { it.key } - expect(JSONInt(42)) { it.value } - expect("Node not correct type (JSONObject), was 42, at inner") { it.message } + shouldThrow("Node not correct type (JSONObject), was 42, at inner") { + outer.getObject("inner") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONObject" + it.key shouldBe "inner" + it.value shouldBe JSONInt(42) } } @@ -555,18 +579,20 @@ class JSONStructureTest { }) } val inner = outer.getObject(0) - expect("ABC") { inner["item"].asString } + inner["item"].asString shouldBe "ABC" } @Test fun `should fail on getObject from array when not an object`() { val outer = JSONArray.build { add("wrong") } - assertFailsWith { outer.getObject(0) }.let { - expect("JSONObject") { it.target } - expect(0) { it.key } - expect(JSONString("wrong")) { it.value } - expect("Node not correct type (JSONObject), was \"wrong\", at 0") { it.message } + shouldThrow("Node not correct type (JSONObject), was \"wrong\", at 0") { + outer.getObject(0) + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONObject" + it.key shouldBe 0 + it.value shouldBe JSONString("wrong") } } diff --git a/src/test/kotlin/io/kjson/JSONTest.kt b/src/test/kotlin/io/kjson/JSONTest.kt index db0a4a9..e28dd21 100644 --- a/src/test/kotlin/io/kjson/JSONTest.kt +++ b/src/test/kotlin/io/kjson/JSONTest.kt @@ -26,16 +26,14 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertFalse -import kotlin.test.assertIs -import kotlin.test.assertNull -import kotlin.test.assertSame -import kotlin.test.assertTrue -import kotlin.test.expect import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow + import io.kjson.JSON.asArray import io.kjson.JSON.asArrayOr import io.kjson.JSON.asArrayOrError @@ -60,6 +58,10 @@ import io.kjson.JSON.asLong import io.kjson.JSON.asLongOr import io.kjson.JSON.asLongOrError import io.kjson.JSON.asLongOrNull +import io.kjson.JSON.asNumber +import io.kjson.JSON.asNumberOr +import io.kjson.JSON.asNumberOrError +import io.kjson.JSON.asNumberOrNull import io.kjson.JSON.asObject import io.kjson.JSON.asObjectOr import io.kjson.JSON.asObjectOrError @@ -101,174 +103,176 @@ class JSONTest { @Test fun `should create values using JSON object`() { val testInt = JSON.of(54321) - expect(JSONInt(54321)) { testInt } + testInt shouldBe JSONInt(54321) val testLong = JSON.of(2233445566778899) - expect(JSONLong(2233445566778899)) { testLong } + testLong shouldBe JSONLong(2233445566778899) val testDecimal = JSON.of("99.999".toBigDecimal()) - expect(JSONDecimal("99.999".toBigDecimal())) { testDecimal } + testDecimal shouldBe JSONDecimal("99.999".toBigDecimal()) val testString = JSON.of("Hello!") - expect(JSONString("Hello!")) { testString } + testString shouldBe JSONString("Hello!") } @Test fun `should parse using JSON object`() { val json = JSON.parse("""{"one":1,"two":2}""") - assertIs(json) - expect(2) { json.size } - expect(JSONInt(1)) { json["one"] } - expect(JSONInt(2)) { json["two"] } + json.shouldBeType() + json.size shouldBe 2 + json["one"] shouldBe JSONInt(1) + json["two"] shouldBe JSONInt(2) } @Test fun `should parse and test for null using JSON object`() { val json = JSON.parseNonNull("""{"one":1,"two":2}""") - assertIs(json) - expect(2) { json.size } - expect(JSONInt(1)) { json["one"] } - expect(JSONInt(2)) { json["two"] } + json.shouldBeType() + json.size shouldBe 2 + json["one"] shouldBe JSONInt(1) + json["two"] shouldBe JSONInt(2) } @Test fun `should fail when parsing non null using JSON object`() { - assertFailsWith { JSON.parseNonNull("null") }.let { - expect("JSON must not be \"null\"") { it.message } + shouldThrow("JSON must not be \"null\"") { + JSON.parseNonNull("null") } } @Test fun `should parse using JSON object parseObject`() { val json = JSON.parseObject("""{"one":1,"two":2}""") - expect(2) { json.size } - expect(JSONInt(1)) { json["one"] } - expect(JSONInt(2)) { json["two"] } - assertFailsWith { JSON.parseObject("[1,2,3]") }.let { - expect("Node") { it.nodeName } - expect("JSONObject") { it.target } - assertNull(it.key) - expect("Node not correct type (JSONObject), was [ ... ]") { it.message } + json.size shouldBe 2 + json["one"] shouldBe JSONInt(1) + json["two"] shouldBe JSONInt(2) + shouldThrow("Node not correct type (JSONObject), was [ ... ]") { + JSON.parseObject("[1,2,3]") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONObject" + it.key shouldBe null } } @Test fun `should parse using JSON object parseArray`() { val json = JSON.parseArray("""["alpha","beta","gamma"]""") - expect(3) { json.size } - expect(JSONString("alpha")) { json[0] } - expect(JSONString("beta")) { json[1] } - expect(JSONString("gamma")) { json[2] } - assertFailsWith { JSON.parseArray("""{"abc":0,"def":-1}""") }.let { - expect("Node") { it.nodeName } - expect("JSONArray") { it.target } - assertNull(it.key) - expect("Node not correct type (JSONArray), was { ... }") { it.message } + json.size shouldBe 3 + json[0] shouldBe JSONString("alpha") + json[1] shouldBe JSONString("beta") + json[2] shouldBe JSONString("gamma") + shouldThrow("Node not correct type (JSONArray), was { ... }") { + JSON.parseArray("""{"abc":0,"def":-1}""") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONArray" + it.key shouldBe null } } @Test fun `should parse JSON Lines`() { val json = JSON.parseLines("{\"aa\":123,\"bb\":321}\n{\"aa\":777,\"bb\":888}") - expect(2) { json.size } + json.size shouldBe 2 with(json[0]) { - assertIs(this) - expect(2) { size } - expect(123) { this["aa"].asInt } - expect(321) { this["bb"].asInt } + shouldBeType() + size shouldBe 2 + this["aa"].asInt shouldBe 123 + this["bb"].asInt shouldBe 321 } with(json[1]) { - assertIs(this) - expect(2) { size } - expect(777) { this["aa"].asInt } - expect(888) { this["bb"].asInt } + shouldBeType() + size shouldBe 2 + this["aa"].asInt shouldBe 777 + this["bb"].asInt shouldBe 888 } } @Test fun `should parse JSONValue using extension function`() { val json = """{"one":1,"two":2}""".parseJSONValue() - assertIs(json) - expect(2) { json.size } - expect(JSONInt(1)) { json["one"] } - expect(JSONInt(2)) { json["two"] } + json.shouldBeType() + json.size shouldBe 2 + json["one"] shouldBe JSONInt(1) + json["two"] shouldBe JSONInt(2) } @Test fun `should parse JSONArray using extension function`() { val json = """["alpha","beta","gamma"]""".parseJSONArray() - expect(3) { json.size } - expect(JSONString("alpha")) { json[0] } - expect(JSONString("beta")) { json[1] } - expect(JSONString("gamma")) { json[2] } + json.size shouldBe 3 + json[0] shouldBe JSONString("alpha") + json[1] shouldBe JSONString("beta") + json[2] shouldBe JSONString("gamma") } @Test fun `should parse JSONObject using extension function`() { val json = """{"one":1,"two":2}""".parseJSONObject() - expect(2) { json.size } - expect(JSONInt(1)) { json["one"] } - expect(JSONInt(2)) { json["two"] } + json.size shouldBe 2 + json["one"] shouldBe JSONInt(1) + json["two"] shouldBe JSONInt(2) } @Test fun `should return displayValue for number types`() { - expect("0") { JSONInt(0).displayValue() } - expect("12345") { JSONInt(12345).displayValue() } - expect("1234567812345678") { JSONLong(1234567812345678).displayValue() } - expect("0.123") { JSONDecimal("0.123".toBigDecimal()).displayValue() } + JSONInt(0).displayValue() shouldBe "0" + JSONInt(12345).displayValue() shouldBe "12345" + JSONLong(1234567812345678).displayValue() shouldBe "1234567812345678" + JSONDecimal("0.123".toBigDecimal()).displayValue() shouldBe "0.123" } @Test fun `should return displayValue for boolean`() { - expect("true") { JSONBoolean.TRUE.displayValue() } - expect("false") { JSONBoolean.FALSE.displayValue() } + JSONBoolean.TRUE.displayValue() shouldBe "true" + JSONBoolean.FALSE.displayValue() shouldBe "false" } @Test fun `should return displayValue for null`() { - expect("null") { JSON.parse("null").displayValue() } + JSON.parse("null").displayValue() shouldBe "null" } @Test fun `should return displayValue for string`() { - expect("\"abc\"") { JSONString("abc").displayValue() } - expect("\"the quic ... lazy dog\"") { JSONString("the quick brown fox jumps over the lazy dog").displayValue() } + JSONString("abc").displayValue() shouldBe "\"abc\"" + JSONString("the quick brown fox jumps over the lazy dog").displayValue() shouldBe "\"the quic ... lazy dog\"" } @Test fun `should return displayValue for string with specified maximum`() { - expect("\"abc\"") { JSONString("abc").displayValue(17) } - expect("\"the qu ... zy dog\"") { JSONString("the quick brown fox jumps over the lazy dog").displayValue(17) } + JSONString("abc").displayValue(17) shouldBe "\"abc\"" + JSONString("the quick brown fox jumps over the lazy dog").displayValue(17) shouldBe "\"the qu ... zy dog\"" } @Test fun `should return displayValue for array`() { - expect("[]") { JSONArray.EMPTY.displayValue() } - expect("[ ... ]") { JSONArray.of(JSONString("Hello")).displayValue() } - expect("[ ... ]") { JSONArray.of(JSONInt(123), JSONInt(456)).displayValue() } + JSONArray.EMPTY.displayValue() shouldBe "[]" + JSONArray.of(JSONString("Hello")).displayValue() shouldBe "[ ... ]" + JSONArray.of(JSONInt(123), JSONInt(456)).displayValue() shouldBe "[ ... ]" } @Test fun `should return displayValue for object`() { - expect("{}") { JSONObject.EMPTY.displayValue() } - expect("{ ... }") { JSONObject.of("abc" to JSONInt(123)).displayValue() } - expect("{ ... }") { JSONObject.of("abc" to JSONInt(123), "def" to JSONInt(456)).displayValue() } + JSONObject.EMPTY.displayValue() shouldBe "{}" + JSONObject.of("abc" to JSONInt(123)).displayValue() shouldBe "{ ... }" + JSONObject.of("abc" to JSONInt(123), "def" to JSONInt(456)).displayValue() shouldBe "{ ... }" } @Test fun `should return elidedValue for number types`() { - expect("0") { JSONInt(0).elidedValue() } - expect("12345") { JSONInt(12345).elidedValue() } - expect("1234567812345678") { JSONLong(1234567812345678).elidedValue() } - expect("0.123") { JSONDecimal("0.123".toBigDecimal()).elidedValue() } + JSONInt(0).elidedValue() shouldBe "0" + JSONInt(12345).elidedValue() shouldBe "12345" + JSONLong(1234567812345678).elidedValue() shouldBe "1234567812345678" + JSONDecimal("0.123".toBigDecimal()).elidedValue() shouldBe "0.123" } @Test fun `should return elidedValue for boolean`() { - expect("true") { JSONBoolean.TRUE.elidedValue() } - expect("false") { JSONBoolean.FALSE.elidedValue() } + JSONBoolean.TRUE.elidedValue() shouldBe "true" + JSONBoolean.FALSE.elidedValue() shouldBe "false" } @Test fun `should return elidedValue for null`() { - expect("null") { JSON.parse("null").elidedValue() } + JSON.parse("null").elidedValue() shouldBe "null" } @Test fun `should return elidedValue for string`() { - expect("\"abc\"") { JSONString("abc").elidedValue() } + JSONString("abc").elidedValue() shouldBe "\"abc\"" } @Test fun `should return elidedValue for array`() { - expect("[]") { JSONArray.EMPTY.elidedValue() } - expect("[123]") { JSONArray.of(JSONInt(123)).elidedValue() } - expect("[\"Hello\"]") { JSONArray.of(JSONString("Hello")).elidedValue() } - expect("[123,456]") { JSONArray.of(JSONInt(123), JSONInt(456)).elidedValue() } + JSONArray.EMPTY.elidedValue() shouldBe "[]" + JSONArray.of(JSONInt(123)).elidedValue() shouldBe "[123]" + JSONArray.of(JSONString("Hello")).elidedValue() shouldBe "[\"Hello\"]" + JSONArray.of(JSONInt(123), JSONInt(456)).elidedValue() shouldBe "[123,456]" } @Test fun `should return elidedValue for object with no exclusions or inclusions`() { - expect("{}") { JSONObject.EMPTY.elidedValue() } - expect("""{"abc":123}""") { JSONObject.of("abc" to JSONInt(123)).elidedValue() } - expect("""{"greeting":"Hello"}""") { JSONObject.of("greeting" to JSONString("Hello")).elidedValue() } - expect("""{"abc":123,"def":4}""") { JSONObject.of("abc" to JSONInt(123), "def" to JSONInt(4)).elidedValue() } + JSONObject.EMPTY.elidedValue() shouldBe "{}" + JSONObject.of("abc" to JSONInt(123)).elidedValue() shouldBe """{"abc":123}""" + JSONObject.of("greeting" to JSONString("Hello")).elidedValue() shouldBe """{"greeting":"Hello"}""" + JSONObject.of("abc" to JSONInt(123), "def" to JSONInt(4)).elidedValue() shouldBe """{"abc":123,"def":4}""" } @Test fun `should return elidedValue for object with exclusions`() { @@ -279,14 +283,11 @@ class JSONTest { add("ddd", 444) add("eee", 555) } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":444,"eee":555}""") { json.elidedValue() } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":444,"eee":555}""") { json.elidedValue(exclude = emptyList()) } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":"****","eee":555}""") { - json.elidedValue(exclude = setOf("ddd")) - } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":"****","eee":"****"}""") { - json.elidedValue(exclude = setOf("ddd", "eee")) - } + json.elidedValue() shouldBe """{"aaa":111,"bbb":222,"ccc":333,"ddd":444,"eee":555}""" + json.elidedValue(exclude = emptyList()) shouldBe """{"aaa":111,"bbb":222,"ccc":333,"ddd":444,"eee":555}""" + json.elidedValue(exclude = setOf("ddd")) shouldBe """{"aaa":111,"bbb":222,"ccc":333,"ddd":"****","eee":555}""" + json.elidedValue(exclude = setOf("ddd", "eee")) shouldBe + """{"aaa":111,"bbb":222,"ccc":333,"ddd":"****","eee":"****"}""" } @Test fun `should return elidedValue for object with inclusions`() { @@ -297,15 +298,12 @@ class JSONTest { add("ddd", 444) add("eee", 555) } - expect("""{"aaa":"****","bbb":"****","ccc":"****","ddd":"****","eee":"****"}""") { - json.elidedValue(include = emptyList()) - } - expect("""{"aaa":"****","bbb":"****","ccc":"****","ddd":444,"eee":"****"}""") { - json.elidedValue(include = setOf("ddd")) - } - expect("""{"aaa":"****","bbb":"****","ccc":"****","ddd":444,"eee":555}""") { - json.elidedValue(include = setOf("ddd", "eee")) - } + json.elidedValue(include = emptyList()) shouldBe + """{"aaa":"****","bbb":"****","ccc":"****","ddd":"****","eee":"****"}""" + json.elidedValue(include = setOf("ddd")) shouldBe + """{"aaa":"****","bbb":"****","ccc":"****","ddd":444,"eee":"****"}""" + json.elidedValue(include = setOf("ddd", "eee")) shouldBe + """{"aaa":"****","bbb":"****","ccc":"****","ddd":444,"eee":555}""" } @Test fun `should return elidedValue for object with custom substitute string`() { @@ -316,12 +314,10 @@ class JSONTest { add("ddd", 444) add("eee", 555) } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":"","eee":555}""") { - json.elidedValue(exclude = setOf("ddd"), substitute = "") - } - expect("""{"aaa":111,"bbb":222,"ccc":333,"ddd":"elided\u2020","eee":"elided\u2020"}""") { - json.elidedValue(exclude = setOf("ddd", "eee"), substitute = "elided\u2020") - } + json.elidedValue(exclude = setOf("ddd"), substitute = "") shouldBe + """{"aaa":111,"bbb":222,"ccc":333,"ddd":"","eee":555}""" + json.elidedValue(exclude = setOf("ddd", "eee"), substitute = "elided\u2020") shouldBe + """{"aaa":111,"bbb":222,"ccc":333,"ddd":"elided\u2020","eee":"elided\u2020"}""" } @Test fun `should return elidedValue for array of object with no exclusions or inclusions`() { @@ -334,7 +330,7 @@ class JSONTest { add("ddd", 444) add("eee", 555) } - expect("""[{"aaa":111,"bbb":222,"ccc":333},{"ddd":444,"eee":555}]""") { JSONArray.of(obj1, obj2).elidedValue() } + JSONArray.of(obj1, obj2).elidedValue() shouldBe """[{"aaa":111,"bbb":222,"ccc":333},{"ddd":444,"eee":555}]""" } @Test fun `should return elidedValue for array of object with exclusions`() { @@ -347,601 +343,621 @@ class JSONTest { add("ddd", 444) add("eee", 555) } - expect("""[{"aaa":111,"bbb":222,"ccc":"****"},{"ddd":444,"eee":555}]""") { - JSONArray.of(obj1, obj2).elidedValue(exclude = setOf("ccc")) - } + JSONArray.of(obj1, obj2).elidedValue(exclude = setOf("ccc")) shouldBe + """[{"aaa":111,"bbb":222,"ccc":"****"},{"ddd":444,"eee":555}]""" } @Test fun `should return asString for JSONString`() { val json = JSONString("boring") - expect("boring") { json.asString } - expect("boring") { json.asStringOrNull } - expect("boring") { json.asStringOrError("string", 333, "Item") } - expect("boring") { json.asStringOr { "wrong" } } + json.asString shouldBe "boring" + json.asStringOrNull shouldBe "boring" + json.asStringOrError("string", 333, "Item") shouldBe "boring" + json.asStringOr { "wrong" } shouldBe "boring" } @Test fun `should fail on attempt to get asString of other types`() { val jsonInt = JSONInt(8) - assertNull(jsonInt.asStringOrNull) - assertFailsWith { jsonInt.asString }.let { - expect("Node") { it.nodeName } - expect("String") { it.target } - assertNull(it.key) - expect(jsonInt) { it.value } - expect("Node not correct type (String), was 8") { it.message } - } - expect("wrong") { jsonInt.asStringOr { "wrong" } } + jsonInt.asStringOrNull shouldBe null + shouldThrow("Node not correct type (String), was 8") { + jsonInt.asString + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "String" + it.key shouldBe null + it.value shouldBe jsonInt + } + jsonInt.asStringOr { "wrong" } shouldBe "wrong" val jsonArray = JSONArray.of(JSONString("Testing")) - assertFailsWith { jsonArray.asString }.let { - expect("Node") { it.nodeName } - expect("String") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (String), was [ ... ]") { it.message } + shouldThrow("Node not correct type (String), was [ ... ]") { + jsonArray.asString + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "String" + it.key shouldBe null + it.value shouldBe jsonArray } - expect("wrong") { jsonArray.asStringOr { "wrong" } } + jsonArray.asStringOr { "wrong" } shouldBe "wrong" } @Test fun `should throw custom error on attempt to get asStringOrError of other types`() { val jsonInt = JSONInt(8) - assertFailsWith { + shouldThrow("Value not correct type (string), was 8, at test1") { jsonInt.asStringOrError("string", "test1", "Value") }.let { - expect("Value") { it.nodeName } - expect("string") { it.target } - expect("test1") { it.key } - expect(jsonInt) { it.value } - expect("Value not correct type (string), was 8, at test1") { it.message } + it.nodeName shouldBe "Value" + it.expected shouldBe "string" + it.key shouldBe "test1" + it.value shouldBe jsonInt } } @Test fun `should return asInt for number types`() { val jsonInt = JSONInt(8) - expect(8) { jsonInt.asInt } - expect(8) { jsonInt.asIntOrNull } - expect(8) { jsonInt.asIntOrError("xxx", "yyy", "zzz") } - expect(8) { jsonInt.asIntOr { 999 } } + jsonInt.asInt shouldBe 8 + jsonInt.asIntOrNull shouldBe 8 + jsonInt.asIntOrError("xxx", "yyy", "zzz") shouldBe 8 + jsonInt.asIntOr { 999 } shouldBe 8 val jsonLong = JSONLong(12345) - expect(12345) { jsonLong.asInt } - expect(12345) { jsonLong.asIntOrNull } - expect(12345) { jsonLong.asIntOrError(key = 27) } - expect(12345) { jsonLong.asIntOr { 999 } } + jsonLong.asInt shouldBe 12345 + jsonLong.asIntOrNull shouldBe 12345 + jsonLong.asIntOrError(key = 27) shouldBe 12345 + jsonLong.asIntOr { 999 } shouldBe 12345 val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123) { jsonDecimal.asInt } - expect(123) { jsonDecimal.asIntOrNull } - expect(123) { jsonDecimal.asIntOrError("aaa") } - expect(123) { jsonDecimal.asIntOr { 999 } } + jsonDecimal.asInt shouldBe 123 + jsonDecimal.asIntOrNull shouldBe 123 + jsonDecimal.asIntOrError("aaa") shouldBe 123 + jsonDecimal.asIntOr { 999 } shouldBe 123 } @Test fun `should fail on attempt to get asInt of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asIntOrNull) - assertFailsWith { jsonString.asInt }.let { - expect("Node") { it.nodeName } - expect("Int") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (Int), was \"not a number\"") { it.message } - } - expect(999) { jsonString.asIntOr { 999 } } + jsonString.asIntOrNull shouldBe null + shouldThrow("Node not correct type (Int), was \"not a number\"") { + jsonString.asInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Int" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asIntOr { 999 } shouldBe 999 val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asIntOrNull) - assertFailsWith { jsonArray.asInt }.let { - expect("Node") { it.nodeName } - expect("Int") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (Int), was [ ... ]") { it.message } - } - expect(999) { jsonArray.asIntOr { 999 } } + jsonArray.asIntOrNull shouldBe null + shouldThrow("Node not correct type (Int), was [ ... ]") { + jsonArray.asInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Int" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asIntOr { 999 } shouldBe 999 val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asIntOrNull) - assertFailsWith { jsonDecimal.asInt }.let { - expect("Node") { it.nodeName } - expect("Int") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (Int), was 123.5000") { it.message } + jsonDecimal.asIntOrNull shouldBe null + shouldThrow("Node not correct type (Int), was 123.5000") { + jsonDecimal.asInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Int" + it.key shouldBe null + it.value shouldBe jsonDecimal } - expect(999) { jsonDecimal.asIntOr { 999 } } + jsonDecimal.asIntOr { 999 } shouldBe 999 } @Test fun `should throw custom error on attempt to get asIntOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Value not correct type (integer), was \"not a number\", at test") { jsonString.asIntOrError("integer", "test", "Value") }.let { - expect("Value") { it.nodeName } - expect("integer") { it.target } - expect("test") { it.key } - expect(jsonString) { it.value } - expect("Value not correct type (integer), was \"not a number\", at test") { it.message } + it.nodeName shouldBe "Value" + it.expected shouldBe "integer" + it.key shouldBe "test" + it.value shouldBe jsonString } } @Test fun `should return asLong for number types`() { val jsonInt = JSONInt(8) - expect(8) { jsonInt.asLong } - expect(8) { jsonInt.asLongOrNull } - expect(8) { jsonInt.asLongOr { -1 } } + jsonInt.asLong shouldBe 8 + jsonInt.asLongOrNull shouldBe 8 + jsonInt.asLongOr { -1 } shouldBe 8 val jsonLong = JSONLong(1234567812345678) - expect(1234567812345678) { jsonLong.asLong } - expect(1234567812345678) { jsonLong.asLongOrNull } - expect(1234567812345678) { jsonLong.asLongOrError("long integer", 999) } - expect(1234567812345678) { jsonLong.asLongOr { -1 } } + jsonLong.asLong shouldBe 1234567812345678 + jsonLong.asLongOrNull shouldBe 1234567812345678 + jsonLong.asLongOrError("long integer", 999) shouldBe 1234567812345678 + jsonLong.asLongOr { -1 } shouldBe 1234567812345678 val jsonDecimal = JSONDecimal("9876543219876543.0000".toBigDecimal()) - expect(9876543219876543) { jsonDecimal.asLong } - expect(9876543219876543) { jsonDecimal.asLongOrNull } - expect(9876543219876543) { jsonDecimal.asLongOrError(key = 6, nodeName = "Property") } - expect(9876543219876543) { jsonDecimal.asLongOr { -1 } } + jsonDecimal.asLong shouldBe 9876543219876543 + jsonDecimal.asLongOrNull shouldBe 9876543219876543 + jsonDecimal.asLongOrError(key = 6, nodeName = "Property") shouldBe 9876543219876543 + jsonDecimal.asLongOr { -1 } shouldBe 9876543219876543 } @Test fun `should fail on attempt to get asLong of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asLongOrNull) - assertFailsWith { jsonString.asLong }.let { - expect("Node") { it.nodeName } - expect("Long") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (Long), was \"not a number\"") { it.message } - } - expect(-1) { jsonString.asLongOr { -1 }} + jsonString.asLongOrNull shouldBe null + shouldThrow("Node not correct type (Long), was \"not a number\"") { + jsonString.asLong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Long" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asLongOr { -1 } shouldBe -1 val jsonObject = JSONObject.of("name" to JSONString("value")) - assertFailsWith { jsonObject.asLong }.let { - expect("Node") { it.nodeName } - expect("Long") { it.target } - assertNull(it.key) - expect(jsonObject) { it.value } - expect("Node not correct type (Long), was { ... }") { it.message } - } - expect(-1) { jsonObject.asLongOr { -1 }} + shouldThrow("Node not correct type (Long), was { ... }") { + jsonObject.asLong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Long" + it.key shouldBe null + it.value shouldBe jsonObject + } + jsonObject.asLongOr { -1 } shouldBe -1 val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asLongOrNull) - assertFailsWith { jsonDecimal.asLong }.let { - expect("Node") { it.nodeName } - expect("Long") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (Long), was 123.5000") { it.message } + jsonDecimal.asLongOrNull shouldBe null + shouldThrow("Node not correct type (Long), was 123.5000") { + jsonDecimal.asLong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Long" + it.key shouldBe null + it.value shouldBe jsonDecimal } - expect(-1) { jsonDecimal.asLongOr { -1 }} + jsonDecimal.asLongOr { -1 } shouldBe -1 } @Test fun `should throw custom error on attempt to get asLongOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Value not correct type (long), was \"not a number\", at test") { jsonString.asLongOrError("long", "test", "Value") }.let { - expect("Value") { it.nodeName } - expect("long") { it.target } - expect("test") { it.key } - expect(jsonString) { it.value } - expect("Value not correct type (long), was \"not a number\", at test") { it.message } + it.nodeName shouldBe "Value" + it.expected shouldBe "long" + it.key shouldBe "test" + it.value shouldBe jsonString } } @Test fun `should return asShort for number types`() { val jsonInt = JSONInt(8) - expect(8) { jsonInt.asShort } - expect(8) { jsonInt.asShortOrNull } - expect(8) { jsonInt.asShortOrError("short") } - expect(8) { jsonInt.asShortOr { 9999 } } + jsonInt.asShort shouldBe 8 + jsonInt.asShortOrNull shouldBe 8 + jsonInt.asShortOrError("short") shouldBe 8 + jsonInt.asShortOr { 9999 } shouldBe 8 val jsonLong = JSONLong(12345) - expect(12345) { jsonLong.asShort } - expect(12345) { jsonLong.asShortOrNull } - expect(12345) { jsonLong.asShortOrError(key = "key") } - expect(12345) { jsonLong.asShortOr { 9999 } } + jsonLong.asShort shouldBe 12345 + jsonLong.asShortOrNull shouldBe 12345 + jsonLong.asShortOrError(key = "key") shouldBe 12345 + jsonLong.asShortOr { 9999 } shouldBe 12345 val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123) { jsonDecimal.asShort } - expect(123) { jsonDecimal.asShortOrNull } - expect(123) { jsonDecimal.asShortOrError("short", 1) } - expect(123) { jsonDecimal.asShortOr { 9999 } } + jsonDecimal.asShort shouldBe 123 + jsonDecimal.asShortOrNull shouldBe 123 + jsonDecimal.asShortOrError("short", 1) shouldBe 123 + jsonDecimal.asShortOr { 9999 } shouldBe 123 } @Test fun `should fail on attempt to get asShort of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asShortOrNull) - assertFailsWith { jsonString.asShort }.let { - expect("Node") { it.nodeName } - expect("Short") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (Short), was \"not a number\"") { it.message } - } - expect(9999) { jsonString.asShortOr { 9999 } } + jsonString.asShortOrNull shouldBe null + shouldThrow("Node not correct type (Short), was \"not a number\"") { + jsonString.asShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Short" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asShortOr { 9999 } shouldBe 9999 val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asShortOrNull) - assertFailsWith { jsonArray.asShort }.let { - expect("Node") { it.nodeName } - expect("Short") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (Short), was [ ... ]") { it.message } - } - expect(9999) { jsonArray.asShortOr { 9999 } } + jsonArray.asShortOrNull shouldBe null + shouldThrow("Node not correct type (Short), was [ ... ]") { + jsonArray.asShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Short" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asShortOr { 9999 } shouldBe 9999 val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asShortOrNull) - assertFailsWith { jsonDecimal.asShort }.let { - expect("Node") { it.nodeName } - expect("Short") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (Short), was 123.5000") { it.message } + jsonDecimal.asShortOrNull shouldBe null + shouldThrow("Node not correct type (Short), was 123.5000") { + jsonDecimal.asShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Short" + it.key shouldBe null + it.value shouldBe jsonDecimal } - expect(9999) { jsonDecimal.asShortOr { 9999 } } + jsonDecimal.asShortOr { 9999 } shouldBe 9999 } @Test fun `should throw custom error on attempt to get asShortOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Value not correct type (short), was \"not a number\", at test99") { jsonString.asShortOrError("short", "test99", "Value") }.let { - expect("Value") { it.nodeName } - expect("short") { it.target } - expect("test99") { it.key } - expect(jsonString) { it.value } - expect("Value not correct type (short), was \"not a number\", at test99") { it.message } + it.nodeName shouldBe "Value" + it.expected shouldBe "short" + it.key shouldBe "test99" + it.value shouldBe jsonString } } @Test fun `should return asByte for number types`() { val jsonInt = JSONInt(8) - expect(8) { jsonInt.asByte } - expect(8) { jsonInt.asByteOrNull } - expect(8) { jsonInt.asByteOrError() } - expect(8) { jsonInt.asByteOr { 99 } } + jsonInt.asByte shouldBe 8 + jsonInt.asByteOrNull shouldBe 8 + jsonInt.asByteOrError() shouldBe 8 + jsonInt.asByteOr { 99 } shouldBe 8 val jsonLong = JSONLong(123) - expect(123) { jsonLong.asByte } - expect(123) { jsonLong.asByteOrNull } - expect(123) { jsonLong.asByteOrError("b", "c", "d") } - expect(123) { jsonLong.asByteOr { 99 } } + jsonLong.asByte shouldBe 123 + jsonLong.asByteOrNull shouldBe 123 + jsonLong.asByteOrError("b", "c", "d") shouldBe 123 + jsonLong.asByteOr { 99 } shouldBe 123 val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123) { jsonDecimal.asByte } - expect(123) { jsonDecimal.asByteOrNull } - expect(123) { jsonDecimal.asByteOrError("byte", "test4") } - expect(123) { jsonDecimal.asByteOr { 99 } } + jsonDecimal.asByte shouldBe 123 + jsonDecimal.asByteOrNull shouldBe 123 + jsonDecimal.asByteOrError("byte", "test4") shouldBe 123 + jsonDecimal.asByteOr { 99 } shouldBe 123 } @Test fun `should fail on attempt to get asByte of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asByteOrNull) - assertFailsWith { jsonString.asByte }.let { - expect("Node") { it.nodeName } - expect("Byte") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (Byte), was \"not a number\"") { it.message } - } - expect(99) { jsonString.asShortOr { 99 } } + jsonString.asByteOrNull shouldBe null + shouldThrow("Node not correct type (Byte), was \"not a number\"") { + jsonString.asByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Byte" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asShortOr { 99 } shouldBe 99 val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asByteOrNull) - assertFailsWith { jsonArray.asByte }.let { - expect("Node") { it.nodeName } - expect("Byte") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (Byte), was [ ... ]") { it.message } - } - expect(99) { jsonArray.asShortOr { 99 } } + jsonArray.asByteOrNull shouldBe null + shouldThrow("Node not correct type (Byte), was [ ... ]") { + jsonArray.asByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Byte" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asShortOr { 99 } shouldBe 99 val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asByteOrNull) - assertFailsWith { jsonDecimal.asByte }.let { - expect("Node") { it.nodeName } - expect("Byte") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (Byte), was 123.5000") { it.message } + jsonDecimal.asByteOrNull shouldBe null + shouldThrow("Node not correct type (Byte), was 123.5000") { + jsonDecimal.asByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Byte" + it.key shouldBe null + it.value shouldBe jsonDecimal } - expect(99) { jsonDecimal.asShortOr { 99 } } + jsonDecimal.asShortOr { 99 } shouldBe 99 } @Test fun `should throw custom error on attempt to get asByteOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Item not correct type (byte), was \"not a number\", at test123") { jsonString.asByteOrError("byte", "test123", "Item") }.let { - expect("Item") { it.nodeName } - expect("byte") { it.target } - expect("test123") { it.key } - expect(jsonString) { it.value } - expect("Item not correct type (byte), was \"not a number\", at test123") { it.message } + it.nodeName shouldBe "Item" + it.expected shouldBe "byte" + it.key shouldBe "test123" + it.value shouldBe jsonString } } @Test fun `should return asULong for number types`() { val jsonInt = JSONInt(8) - expect(8.toULong()) { jsonInt.asULong } - expect(8.toULong()) { jsonInt.asULongOrNull } - expect(8.toULong()) { jsonInt.asULongOrError() } - expect(8.toULong()) { jsonInt.asULongOr { 99999U } } + jsonInt.asULong shouldBe 8.toULong() + jsonInt.asULongOrNull shouldBe 8.toULong() + jsonInt.asULongOrError() shouldBe 8.toULong() + jsonInt.asULongOr { 99999U } shouldBe 8.toULong() val jsonLong = JSONLong(12345) - expect(12345.toULong()) { jsonLong.asULong } - expect(12345.toULong()) { jsonLong.asULongOrNull } - expect(12345.toULong()) { jsonLong.asULongOrError(key = 27) } - expect(12345.toULong()) { jsonLong.asULongOr { 99999U } } + jsonLong.asULong shouldBe 12345.toULong() + jsonLong.asULongOrNull shouldBe 12345.toULong() + jsonLong.asULongOrError(key = 27) shouldBe 12345.toULong() + jsonLong.asULongOr { 99999U } shouldBe 12345.toULong() val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123.toULong()) { jsonDecimal.asULong } - expect(123.toULong()) { jsonDecimal.asULongOrNull } - expect(123.toULong()) { jsonDecimal.asULongOrError("whatever") } - expect(123.toULong()) { jsonDecimal.asULongOr { 99999U } } + jsonDecimal.asULong shouldBe 123.toULong() + jsonDecimal.asULongOrNull shouldBe 123.toULong() + jsonDecimal.asULongOrError("whatever") shouldBe 123.toULong() + jsonDecimal.asULongOr { 99999U } shouldBe 123.toULong() } @Test fun `should fail on attempt to get asULong of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asULongOrNull) - assertFailsWith { jsonString.asULong }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (ULong), was \"not a number\"") { it.message } - } - expect(99999U) { jsonString.asULongOr { 99999U } } + jsonString.asULongOrNull shouldBe null + shouldThrow("Node not correct type (ULong), was \"not a number\"") { + jsonString.asULong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asULongOr { 99999U } shouldBe 99999U val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asULongOrNull) - assertFailsWith { jsonArray.asULong }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (ULong), was [ ... ]") { it.message } - } - expect(99999U) { jsonArray.asULongOr { 99999U } } + jsonArray.asULongOrNull shouldBe null + shouldThrow("Node not correct type (ULong), was [ ... ]") { + jsonArray.asULong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asULongOr { 99999U } shouldBe 99999U val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asULongOrNull) - assertFailsWith { jsonDecimal.asULong }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (ULong), was 123.5000") { it.message } - } - expect(99999U) { jsonDecimal.asULongOr { 99999U } } + jsonDecimal.asULongOrNull shouldBe null + shouldThrow("Node not correct type (ULong), was 123.5000") { + jsonDecimal.asULong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe null + it.value shouldBe jsonDecimal + } + jsonDecimal.asULongOr { 99999U } shouldBe 99999U val jsonInt = JSONInt(-1) - assertNull(jsonInt.asULongOrNull) - assertFailsWith { jsonInt.asULong }.let { - expect("Node") { it.nodeName } - expect("ULong") { it.target } - assertNull(it.key) - expect(jsonInt) { it.value } - expect("Node not correct type (ULong), was -1") { it.message } + jsonInt.asULongOrNull shouldBe null + shouldThrow("Node not correct type (ULong), was -1") { + jsonInt.asULong + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "ULong" + it.key shouldBe null + it.value shouldBe jsonInt } - expect(99999U) { jsonInt.asULongOr { 99999U } } + jsonInt.asULongOr { 99999U } shouldBe 99999U } @Test fun `should throw custom error on attempt to get asULongOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Item not correct type (unsigned long), was \"not a number\", at test2") { jsonString.asULongOrError("unsigned long", "test2", "Item") }.let { - expect("Item") { it.nodeName } - expect("unsigned long") { it.target } - expect("test2") { it.key } - expect(jsonString) { it.value } - expect("Item not correct type (unsigned long), was \"not a number\", at test2") { it.message } + it.nodeName shouldBe "Item" + it.expected shouldBe "unsigned long" + it.key shouldBe "test2" + it.value shouldBe jsonString } } @Test fun `should return asUInt for number types`() { val jsonInt = JSONInt(8) - expect(8U) { jsonInt.asUInt } - expect(8U) { jsonInt.asUIntOrNull } - expect(8U) { jsonInt.asUIntOrError("stuff", 15, "Thing") } - expect(8U) { jsonInt.asUIntOr { 9999U } } + jsonInt.asUInt shouldBe 8U + jsonInt.asUIntOrNull shouldBe 8U + jsonInt.asUIntOrError("stuff", 15, "Thing") shouldBe 8U + jsonInt.asUIntOr { 9999U } shouldBe 8U val jsonLong = JSONLong(12345) - expect(12345U) { jsonLong.asUInt } - expect(12345U) { jsonLong.asUIntOrNull } - expect(12345U) { jsonLong.asUIntOrError() } - expect(12345U) { jsonLong.asUIntOr { 9999U } } + jsonLong.asUInt shouldBe 12345U + jsonLong.asUIntOrNull shouldBe 12345U + jsonLong.asUIntOrError() shouldBe 12345U + jsonLong.asUIntOr { 9999U } shouldBe 12345U val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123U) { jsonDecimal.asUInt } - expect(123U) { jsonDecimal.asUIntOrNull } - expect(123U) { jsonDecimal.asUIntOrError("unsigned int", "property", "Property") } - expect(123U) { jsonDecimal.asUIntOr { 9999U } } + jsonDecimal.asUInt shouldBe 123U + jsonDecimal.asUIntOrNull shouldBe 123U + jsonDecimal.asUIntOrError("unsigned int", "property", "Property") shouldBe 123U + jsonDecimal.asUIntOr { 9999U } shouldBe 123U } @Test fun `should fail on attempt to get asUInt of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asUIntOrNull) - assertFailsWith { jsonString.asUInt }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (UInt), was \"not a number\"") { it.message } - } - expect(9999U) { jsonString.asUIntOr { 9999U } } + jsonString.asUIntOrNull shouldBe null + shouldThrow("Node not correct type (UInt), was \"not a number\"") { + jsonString.asUInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asUIntOr { 9999U } shouldBe 9999U val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asUIntOrNull) - assertFailsWith { jsonArray.asUInt }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (UInt), was [ ... ]") { it.message } - } - expect(9999U) { jsonArray.asUIntOr { 9999U } } + jsonArray.asUIntOrNull shouldBe null + shouldThrow("Node not correct type (UInt), was [ ... ]") { + jsonArray.asUInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asUIntOr { 9999U } shouldBe 9999U val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asUIntOrNull) - assertFailsWith { jsonDecimal.asUInt }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (UInt), was 123.5000") { it.message } - } - expect(9999U) { jsonDecimal.asUIntOr { 9999U } } + jsonDecimal.asUIntOrNull shouldBe null + shouldThrow("Node not correct type (UInt), was 123.5000") { + jsonDecimal.asUInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe null + it.value shouldBe jsonDecimal + } + jsonDecimal.asUIntOr { 9999U } shouldBe 9999U val jsonInt = JSONInt(-1) - assertNull(jsonInt.asUIntOrNull) - assertFailsWith { jsonInt.asUInt }.let { - expect("Node") { it.nodeName } - expect("UInt") { it.target } - assertNull(it.key) - expect(jsonInt) { it.value } - expect("Node not correct type (UInt), was -1") { it.message } + jsonInt.asUIntOrNull shouldBe null + shouldThrow("Node not correct type (UInt), was -1") { + jsonInt.asUInt + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UInt" + it.key shouldBe null + it.value shouldBe jsonInt } - expect(9999U) { jsonInt.asUIntOr { 9999U } } + jsonInt.asUIntOr { 9999U } shouldBe 9999U } @Test fun `should throw custom error on attempt to get asUIntOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Item not correct type (unsigned int), was \"not a number\", at test1234") { jsonString.asUIntOrError("unsigned int", "test1234", "Item") }.let { - expect("Item") { it.nodeName } - expect("unsigned int") { it.target } - expect("test1234") { it.key } - expect(jsonString) { it.value } - expect("Item not correct type (unsigned int), was \"not a number\", at test1234") { it.message } + it.nodeName shouldBe "Item" + it.expected shouldBe "unsigned int" + it.key shouldBe "test1234" + it.value shouldBe jsonString } } @Test fun `should return asUShort for number types`() { val jsonInt = JSONInt(8) - expect(8U) { jsonInt.asUShort } - expect(8U) { jsonInt.asUShortOrNull } - expect(8U) { jsonInt.asUShortOrError("a", "b", "c") } - expect(8U) { jsonInt.asUShortOr { 999U } } + jsonInt.asUShort shouldBe 8U + jsonInt.asUShortOrNull shouldBe 8U + jsonInt.asUShortOrError("a", "b", "c") shouldBe 8U + jsonInt.asUShortOr { 999U } shouldBe 8U val jsonLong = JSONLong(45678) - expect(45678U) { jsonLong.asUShort } - expect(45678U) { jsonLong.asUShortOrNull } - expect(45678U) { jsonLong.asUShortOrError() } - expect(45678U) { jsonLong.asUShortOr { 999U } } + jsonLong.asUShort shouldBe 45678U + jsonLong.asUShortOrNull shouldBe 45678U + jsonLong.asUShortOrError() shouldBe 45678U + jsonLong.asUShortOr { 999U } shouldBe 45678U val jsonDecimal = JSONDecimal("1234.0000".toBigDecimal()) - expect(1234U) { jsonDecimal.asUShort } - expect(1234U) { jsonDecimal.asUShortOrNull } - expect(1234U) { jsonDecimal.asUShortOrError(key = "it") } - expect(1234U) { jsonDecimal.asUShortOr { 999U } } + jsonDecimal.asUShort shouldBe 1234U + jsonDecimal.asUShortOrNull shouldBe 1234U + jsonDecimal.asUShortOrError(key = "it") shouldBe 1234U + jsonDecimal.asUShortOr { 999U } shouldBe 1234U } @Test fun `should fail on attempt to get asUShort of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asUShortOrNull) - assertFailsWith { jsonString.asUShort }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (UShort), was \"not a number\"") { it.message } - } - expect(999U) { jsonString.asUShortOr { 999U } } + jsonString.asUShortOrNull shouldBe null + shouldThrow("Node not correct type (UShort), was \"not a number\"") { + jsonString.asUShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asUShortOr { 999U } shouldBe 999U val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asUShortOrNull) - assertFailsWith { jsonArray.asUShort }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (UShort), was [ ... ]") { it.message } - } - expect(999U) { jsonArray.asUShortOr { 999U } } + jsonArray.asUShortOrNull shouldBe null + shouldThrow("Node not correct type (UShort), was [ ... ]") { + jsonArray.asUShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asUShortOr { 999U } shouldBe 999U val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asUShortOrNull) - assertFailsWith { jsonDecimal.asUShort }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (UShort), was 123.5000") { it.message } - } - expect(999U) { jsonDecimal.asUShortOr { 999U } } + jsonDecimal.asUShortOrNull shouldBe null + shouldThrow("Node not correct type (UShort), was 123.5000") { + jsonDecimal.asUShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe null + it.value shouldBe jsonDecimal + } + jsonDecimal.asUShortOr { 999U } shouldBe 999U val jsonInt = JSONInt(-1) - assertNull(jsonInt.asUShortOrNull) - assertFailsWith { jsonInt.asUShort }.let { - expect("Node") { it.nodeName } - expect("UShort") { it.target } - assertNull(it.key) - expect(jsonInt) { it.value } - expect("Node not correct type (UShort), was -1") { it.message } + jsonInt.asUShortOrNull shouldBe null + shouldThrow("Node not correct type (UShort), was -1") { + jsonInt.asUShort + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UShort" + it.key shouldBe null + it.value shouldBe jsonInt } - expect(999U) { jsonInt.asUShortOr { 999U } } + jsonInt.asUShortOr { 999U } shouldBe 999U } @Test fun `should throw custom error on attempt to get asUShortOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Item not correct type (unsigned short), was \"not a number\", at testtest") { jsonString.asUShortOrError("unsigned short", "testtest", "Item") }.let { - expect("Item") { it.nodeName } - expect("unsigned short") { it.target } - expect("testtest") { it.key } - expect(jsonString) { it.value } - expect("Item not correct type (unsigned short), was \"not a number\", at testtest") { it.message } + it.nodeName shouldBe "Item" + it.expected shouldBe "unsigned short" + it.key shouldBe "testtest" + it.value shouldBe jsonString } } @Test fun `should return asUByte for number types`() { val jsonInt = JSONInt(8) - expect(8U) { jsonInt.asUByte } - expect(8U) { jsonInt.asUByteOrNull } - expect(8U) { jsonInt.asUByteOrError("unsigned byte", nodeName = "Item") } - expect(8U) { jsonInt.asUByteOr { 99U } } + jsonInt.asUByte shouldBe 8U + jsonInt.asUByteOrNull shouldBe 8U + jsonInt.asUByteOrError("unsigned byte", nodeName = "Item") shouldBe 8U + jsonInt.asUByteOr { 99U } shouldBe 8U val jsonLong = JSONLong(234) - expect(234U) { jsonLong.asUByte } - expect(234U) { jsonLong.asUByteOrNull } - expect(234U) { jsonLong.asUByteOrError() } - expect(234U) { jsonLong.asUByteOr { 99U } } + jsonLong.asUByte shouldBe 234U + jsonLong.asUByteOrNull shouldBe 234U + jsonLong.asUByteOrError() shouldBe 234U + jsonLong.asUByteOr { 99U } shouldBe 234U val jsonDecimal = JSONDecimal("123.0000".toBigDecimal()) - expect(123U) { jsonDecimal.asUByte } - expect(123U) { jsonDecimal.asUByteOrNull } - expect(123U) { jsonDecimal.asUByteOrError("type", "key", "name") } - expect(123U) { jsonDecimal.asUByteOr { 99U } } + jsonDecimal.asUByte shouldBe 123U + jsonDecimal.asUByteOrNull shouldBe 123U + jsonDecimal.asUByteOrError("type", "key", "name") shouldBe 123U + jsonDecimal.asUByteOr { 99U } shouldBe 123U } @Test fun `should fail on attempt to get asUByte of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asUByteOrNull) - assertFailsWith { jsonString.asUByte }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (UByte), was \"not a number\"") { it.message } - } - expect(99U) { jsonString.asUByteOr { 99U } } + jsonString.asUByteOrNull shouldBe null + shouldThrow("Node not correct type (UByte), was \"not a number\"") { + jsonString.asUByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asUByteOr { 99U } shouldBe 99U val jsonArray = JSONArray.of(JSONString("Testing")) - assertNull(jsonArray.asUByteOrNull) - assertFailsWith { jsonArray.asUByte }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - assertNull(it.key) - expect(jsonArray) { it.value } - expect("Node not correct type (UByte), was [ ... ]") { it.message } - } - expect(99U) { jsonArray.asUByteOr { 99U } } + jsonArray.asUByteOrNull shouldBe null + shouldThrow("Node not correct type (UByte), was [ ... ]") { + jsonArray.asUByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe null + it.value shouldBe jsonArray + } + jsonArray.asUByteOr { 99U } shouldBe 99U val jsonDecimal = JSONDecimal("123.5000".toBigDecimal()) - assertNull(jsonDecimal.asUByteOrNull) - assertFailsWith { jsonDecimal.asUByte }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - assertNull(it.key) - expect(jsonDecimal) { it.value } - expect("Node not correct type (UByte), was 123.5000") { it.message } - } - expect(99U) { jsonDecimal.asUByteOr { 99U } } + jsonDecimal.asUByteOrNull shouldBe null + shouldThrow("Node not correct type (UByte), was 123.5000") { + jsonDecimal.asUByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe null + it.value shouldBe jsonDecimal + } + jsonDecimal.asUByteOr { 99U } shouldBe 99U val jsonInt = JSONInt(-1) - assertNull(jsonInt.asUByteOrNull) - assertFailsWith { jsonInt.asUByte }.let { - expect("Node") { it.nodeName } - expect("UByte") { it.target } - assertNull(it.key) - expect(jsonInt) { it.value } - expect("Node not correct type (UByte), was -1") { it.message } + jsonInt.asUByteOrNull shouldBe null + shouldThrow("Node not correct type (UByte), was -1") { + jsonInt.asUByte + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "UByte" + it.key shouldBe null + it.value shouldBe jsonInt } - expect(99U) { jsonInt.asUByteOr { 99U } } + jsonInt.asUByteOr { 99U } shouldBe 99U } @Test fun `should throw custom error on attempt to get asUByteOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Node not correct type (unsigned byte), was \"not a number\", at test333") { jsonString.asUByteOrError("unsigned byte", "test333") }.let { - expect("Node") { it.nodeName } - expect("unsigned byte") { it.target } - expect("test333") { it.key } - expect(jsonString) { it.value } - expect("Node not correct type (unsigned byte), was \"not a number\", at test333") { it.message } + it.nodeName shouldBe "Node" + it.expected shouldBe "unsigned byte" + it.key shouldBe "test333" + it.value shouldBe jsonString } } @@ -949,181 +965,242 @@ class JSONTest { 8.let { val result = it.toBigDecimal() val jsonInt = JSONInt(it) - expect(result) { jsonInt.asDecimal } - expect(result) { jsonInt.asDecimalOrNull } - expect(result) { jsonInt.asDecimalOrError("a", "b", "c") } - expect(result) { jsonInt.asDecimalOr { BigDecimal.TEN } } + jsonInt.asDecimal shouldBe result + jsonInt.asDecimalOrNull shouldBe result + jsonInt.asDecimalOrError("a", "b", "c") shouldBe result + jsonInt.asDecimalOr { BigDecimal.TEN } shouldBe result } 1234567812345678.let { val result = it.toBigDecimal() val jsonLong = JSONLong(it) - expect(result) { jsonLong.asDecimal } - expect(result) { jsonLong.asDecimalOrNull } - expect(result) { jsonLong.asDecimalOrError(key = 9) } - expect(result) { jsonLong.asDecimalOr { BigDecimal.TEN } } + jsonLong.asDecimal shouldBe result + jsonLong.asDecimalOrNull shouldBe result + jsonLong.asDecimalOrError(key = 9) shouldBe result + jsonLong.asDecimalOr { BigDecimal.TEN } shouldBe result } "123.45678".let { val result = it.toBigDecimal() val jsonDecimal = JSONDecimal(result) - expect(result) { jsonDecimal.asDecimal } - expect(result) { jsonDecimal.asDecimalOrNull } - expect(result) { jsonDecimal.asDecimalOrError() } - expect(result) { jsonDecimal.asDecimalOr { BigDecimal.TEN } } + jsonDecimal.asDecimal shouldBe result + jsonDecimal.asDecimalOrNull shouldBe result + jsonDecimal.asDecimalOrError() shouldBe result + jsonDecimal.asDecimalOr { BigDecimal.TEN } shouldBe result } } @Test fun `should fail on attempt to get asDecimal of other types`() { val jsonString = JSONString("not a number") - assertNull(jsonString.asDecimalOrNull) - assertFailsWith { jsonString.asDecimal }.let { - expect("Node") { it.nodeName } - expect("BigDecimal") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (BigDecimal), was \"not a number\"") { it.message } - } - expect(BigDecimal.TEN) { jsonString.asDecimalOr { BigDecimal.TEN } } + jsonString.asDecimalOrNull shouldBe null + shouldThrow("Node not correct type (BigDecimal), was \"not a number\"") { + jsonString.asDecimal + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "BigDecimal" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asDecimalOr { BigDecimal.TEN } shouldBe BigDecimal.TEN val jsonObject = JSONObject.of("name" to JSONString("value")) - assertFailsWith { jsonObject.asDecimal }.let { - expect("Node") { it.nodeName } - expect("BigDecimal") { it.target } - assertNull(it.key) - expect(jsonObject) { it.value } - expect("Node not correct type (BigDecimal), was { ... }") { it.message } + shouldThrow("Node not correct type (BigDecimal), was { ... }") { + jsonObject.asDecimal + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "BigDecimal" + it.key shouldBe null + it.value shouldBe jsonObject } - expect(BigDecimal.TEN) { jsonObject.asDecimalOr { BigDecimal.TEN } } + jsonObject.asDecimalOr { BigDecimal.TEN } shouldBe BigDecimal.TEN } @Test fun `should throw custom error on attempt to get asDecimalOrError of other types`() { val jsonString = JSONString("not a number") - assertFailsWith { + shouldThrow("Property not correct type (decimal number), was \"not a number\", at 1000") { jsonString.asDecimalOrError("decimal number", 1000, "Property") }.let { - expect("Property") { it.nodeName } - expect("decimal number") { it.target } - expect(1000) { it.key } - expect(jsonString) { it.value } - expect("Property not correct type (decimal number), was \"not a number\", at 1000") { it.message } + it.nodeName shouldBe "Property" + it.expected shouldBe "decimal number" + it.key shouldBe 1000 + it.value shouldBe jsonString + } + } + + @Test fun `should return asNumber for number types`() { + 8.let { + val jsonInt = JSONInt(it) + jsonInt.asNumber shouldBe jsonInt + jsonInt.asNumberOrNull shouldBe jsonInt + jsonInt.asNumberOrError("a", "b", "c") shouldBe jsonInt + jsonInt.asNumberOr { BigDecimal.TEN } shouldBe jsonInt + } + 1234567812345678.let { + val jsonLong = JSONLong(it) + jsonLong.asNumber shouldBe jsonLong + jsonLong.asNumberOrNull shouldBe jsonLong + jsonLong.asNumberOrError(key = 9) shouldBe jsonLong + jsonLong.asNumberOr { BigDecimal.TEN } shouldBe jsonLong + } + "123.45678".let { + val jsonDecimal = JSONDecimal(it) + jsonDecimal.asNumber shouldBe jsonDecimal + jsonDecimal.asNumberOrNull shouldBe jsonDecimal + jsonDecimal.asNumberOrError() shouldBe jsonDecimal + jsonDecimal.asNumberOr { BigDecimal.TEN } shouldBe jsonDecimal + } + } + + @Test fun `should fail on attempt to get asNumber of other types`() { + val jsonString = JSONString("not a number") + jsonString.asNumberOrNull shouldBe null + shouldThrow("Node not correct type (Number), was \"not a number\"") { + jsonString.asNumber + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Number" + it.key shouldBe null + it.value shouldBe jsonString + } + jsonString.asNumberOr { BigDecimal.TEN } shouldBe BigDecimal.TEN + val jsonObject = JSONObject.of("name" to JSONString("value")) + shouldThrow("Node not correct type (Number), was { ... }") { + jsonObject.asNumber + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Number" + it.key shouldBe null + it.value shouldBe jsonObject + } + jsonObject.asNumberOr { BigDecimal.TEN } shouldBe BigDecimal.TEN + } + + @Test fun `should throw custom error on attempt to get asNumberOrError of other types`() { + val jsonString = JSONString("not a number") + shouldThrow("Property not correct type (number), was \"not a number\", at 1000") { + jsonString.asNumberOrError("number", 1000, "Property") + }.let { + it.nodeName shouldBe "Property" + it.expected shouldBe "number" + it.key shouldBe 1000 + it.value shouldBe jsonString } } @Test fun `should return asBoolean for JSONBoolean`() { - assertTrue(JSONBoolean.TRUE.asBoolean) - assertTrue(JSONBoolean.TRUE.asBooleanOrNull) - assertTrue(JSONBoolean.TRUE.asBooleanOrError("boolean")) - assertTrue(JSONBoolean.TRUE.asBooleanOr { false }) - assertFalse(JSONBoolean.FALSE.asBoolean) - assertFalse(JSONBoolean.FALSE.asBooleanOrNull) - assertFalse(JSONBoolean.FALSE.asBooleanOrError(key = 0)) - assertFalse(JSONBoolean.FALSE.asBooleanOr { true }) + JSONBoolean.TRUE.asBoolean shouldBe true + JSONBoolean.TRUE.asBooleanOrNull shouldBe true + JSONBoolean.TRUE.asBooleanOrError("boolean") shouldBe true + JSONBoolean.TRUE.asBooleanOr { false } shouldBe true + JSONBoolean.FALSE.asBoolean shouldBe false + JSONBoolean.FALSE.asBooleanOrNull shouldBe false + JSONBoolean.FALSE.asBooleanOrError(key = 0) shouldBe false + JSONBoolean.FALSE.asBooleanOr { true } shouldBe false } @Test fun `should fail on attempt to get asBoolean of other types`() { val jsonString = JSONString("not a boolean") - assertNull(jsonString.asBooleanOrNull) - assertFailsWith { jsonString.asBoolean }.let { - expect("Node") { it.nodeName } - expect("Boolean") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (Boolean), was \"not a boolean\"") { it.message } + jsonString.asBooleanOrNull shouldBe null + shouldThrow("Node not correct type (Boolean), was \"not a boolean\"") { + jsonString.asBoolean + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "Boolean" + it.key shouldBe null + it.value shouldBe jsonString } - assertFalse(jsonString.asBooleanOr { false }) + jsonString.asBooleanOr { false } shouldBe false } @Test fun `should throw custom error on attempt to get asBoolean of other types`() { val jsonString = JSONString("not a boolean") - assertFailsWith { + shouldThrow("Flag not correct type (boolean), was \"not a boolean\", at aaa") { jsonString.asBooleanOrError("boolean", "aaa", "Flag") }.let { - expect("Flag") { it.nodeName } - expect("boolean") { it.target } - expect("aaa") { it.key } - expect(jsonString) { it.value } - expect("Flag not correct type (boolean), was \"not a boolean\", at aaa") { it.message } + it.nodeName shouldBe "Flag" + it.expected shouldBe "boolean" + it.key shouldBe "aaa" + it.value shouldBe jsonString } } @Suppress("DEPRECATION") @Test fun `should return asArray for JSONArray`() { val jsonArray = JSONArray.of(JSONInt(123), JSONInt(456)) - assertSame(jsonArray, jsonArray.asArray) - assertSame(jsonArray, jsonArray.asArrayOrNull) - assertSame(jsonArray, jsonArray.asArrayOrError("array")) - assertSame(jsonArray, jsonArray.asArrayOr { JSONArray.of(JSONInt(99)) }) + jsonArray.asArray shouldBeSameInstance jsonArray + jsonArray.asArrayOrNull shouldBeSameInstance jsonArray + jsonArray.asArrayOrError("array") shouldBeSameInstance jsonArray + jsonArray.asArrayOr { JSONArray.of(JSONInt(99)) } shouldBeSameInstance jsonArray } @Test fun `should fail on attempt to get asArray of other types`() { val jsonString = JSONString("not an array") - assertNull(jsonString.asArrayOrNull) - assertFailsWith { jsonString.asArray }.let { - expect("Node") { it.nodeName } - expect("JSONArray") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (JSONArray), was \"not an array\"") { it.message } + jsonString.asArrayOrNull shouldBe null + shouldThrow("Node not correct type (JSONArray), was \"not an array\"") { + jsonString.asArray + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONArray" + it.key shouldBe null + it.value shouldBe jsonString } - expect(JSONArray.of(JSONInt(99))) { jsonString.asArrayOr { JSONArray.of(JSONInt(99)) } } + jsonString.asArrayOr { JSONArray.of(JSONInt(99)) } shouldBe JSONArray.of(JSONInt(99)) } @Test fun `should throw custom error on attempt to get asArray of other types`() { val jsonString = JSONString("not an array") - assertFailsWith { + shouldThrow("Node not correct type (array), was \"not an array\"") { jsonString.asArrayOrError("array") }.let { - expect("Node") { it.nodeName } - expect("array") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (array), was \"not an array\"") { it.message } + it.nodeName shouldBe "Node" + it.expected shouldBe "array" + it.key shouldBe null + it.value shouldBe jsonString } } @Suppress("DEPRECATION") @Test fun `should return asObject for JSONObject`() { val jsonObject = JSONObject.of("name" to JSONString("value")) - assertSame(jsonObject, jsonObject.asObject) - assertSame(jsonObject, jsonObject.asObjectOrNull) - assertSame(jsonObject, jsonObject.asObjectOrError("object")) - assertSame(jsonObject, jsonObject.asObjectOr { JSONObject.of("error" to JSONInt(999)) }) + jsonObject.asObject shouldBeSameInstance jsonObject + jsonObject.asObjectOrNull shouldBeSameInstance jsonObject + jsonObject.asObjectOrError("object") shouldBeSameInstance jsonObject + jsonObject.asObjectOr { JSONObject.of("error" to JSONInt(999)) } shouldBeSameInstance jsonObject } @Test fun `should fail on attempt to get asObject of other types`() { val jsonString = JSONString("not an object") - assertNull(jsonString.asObjectOrNull) - assertFailsWith { jsonString.asObject }.let { - expect("Node") { it.nodeName } - expect("JSONObject") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Node not correct type (JSONObject), was \"not an object\"") { it.message } - } - expect(JSONObject.of("error" to JSONInt(999))) { - jsonString.asObjectOr { JSONObject.of("error" to JSONInt(999)) } + jsonString.asObjectOrNull shouldBe null + shouldThrow("Node not correct type (JSONObject), was \"not an object\"") { + jsonString.asObject + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "JSONObject" + it.key shouldBe null + it.value shouldBe jsonString } + jsonString.asObjectOr { JSONObject.of("error" to JSONInt(999)) } shouldBe JSONObject.of("error" to JSONInt(999)) } @Test fun `should throw custom error on attempt to get asObject of other types`() { val jsonString = JSONString("not an object") - assertFailsWith { + shouldThrow("Array not correct type (object), was \"not an object\"") { jsonString.asObjectOrError("object", nodeName = "Array") }.let { - expect("Array") { it.nodeName } - expect("object") { it.target } - assertNull(it.key) - expect(jsonString) { it.value } - expect("Array not correct type (object), was \"not an object\"") { it.message } + it.nodeName shouldBe "Array" + it.expected shouldBe "object" + it.key shouldBe null + it.value shouldBe jsonString } } @Test fun `should throw general type error`() { val json = JSONString("this is a string") - assertFailsWith { json.typeError("integer") }.let { - expect("Node") { it.nodeName } - expect("integer") { it.target } - assertNull(it.key) - expect(json) { it.value } - expect("Node not correct type (integer), was \"this is a string\"") { it.message } + shouldThrow("Node not correct type (integer), was \"this is a string\"") { + if (json.isNotEmpty()) // dummy check added to stop editor from flagging "unreachable code" + json.typeError("integer") + }.let { + it.nodeName shouldBe "Node" + it.expected shouldBe "integer" + it.key shouldBe null + it.value shouldBe json } } @@ -1133,8 +1210,8 @@ class JSONTest { val formatter = Formatter(unixLineSeparator) val sb = StringBuilder() formatter.formatTo(sb, json) - expect(formatted1) { sb.toString() } - expect(input) { Formatter.output(json) } + sb.toString() shouldBe formatted1 + Formatter.output(json) shouldBe input } @Test fun `should produce structures capable of being formatted by json-simple - 2`() { @@ -1142,10 +1219,11 @@ class JSONTest { val json = JSON.parse(input) val formatter = Formatter(unixLineSeparator) val formatted = buildString { formatter.formatTo(this, json) } - expect(formatted2) { formatted } - expect(input) { Formatter.output(json) } + formatted shouldBe formatted2 + Formatter.output(json) shouldBe input } + @Suppress("ConstPropertyName") companion object { const val formatted1 = """{ diff --git a/src/test/kotlin/io/kjson/JSONValueTest.kt b/src/test/kotlin/io/kjson/JSONValueTest.kt index 4fc55eb..68a06e5 100644 --- a/src/test/kotlin/io/kjson/JSONValueTest.kt +++ b/src/test/kotlin/io/kjson/JSONValueTest.kt @@ -2,7 +2,7 @@ * @(#) JSONValueTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021 Peter Wall + * Copyright (c) 2021, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,11 +26,12 @@ package io.kjson import kotlin.test.Test -import kotlin.test.assertIs -import kotlin.test.expect import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType + import io.kjson.JSON.appendJSONValue import io.kjson.JSON.appendTo import io.kjson.JSON.toJSON @@ -39,42 +40,42 @@ class JSONValueTest { @Test fun `should use nullable functions`() { var testNull: JSONValue? = createJSONValueNull() - expect("null") { testNull.toJSON() } + testNull.toJSON() shouldBe "null" val sb = StringBuilder() testNull.appendTo(sb) - expect("null") { sb.toString() } + sb.toString() shouldBe "null" testNull = createJSONValue() - expect("222") { testNull.toJSON() } + testNull.toJSON() shouldBe "222" sb.setLength(0) testNull.appendTo(sb) - expect("222") { sb.toString() } + sb.toString() shouldBe "222" } @Test fun `should use Appendable appendJSON`() { val test = StringBuilder() test.appendJSONValue(JSONArray.of(JSONInt(123), JSONInt(321))) - expect("[123,321]") { test.toString() } + test.toString() shouldBe "[123,321]" } @Test fun `should create JSONValue of correct type using of function`() { - assertIs(JSON.of(123)) - assertIs(JSON.of(0L)) - assertIs(JSON.of(BigDecimal.ONE)) - assertIs(JSON.of("hello")) - assertIs(JSON.of(true)) - assertIs(JSON.of(JSONValue(0), JSONValue(1))) - assertIs(JSON.of("alpha" to JSONValue(0), "beta" to JSONValue(1))) + JSON.of(123).shouldBeType() + JSON.of(0L).shouldBeType() + JSON.of(BigDecimal.ONE).shouldBeType() + JSON.of("hello").shouldBeType() + JSON.of(true).shouldBeType() + JSON.of(JSONValue(0), JSONValue(1)).shouldBeType() + JSON.of("alpha" to JSONValue(0), "beta" to JSONValue(1)).shouldBeType() } @Test fun `should create JSONValue of correct type using JSONValue function`() { - assertIs(JSONValue(123)) - assertIs(JSONValue(0L)) - assertIs(JSONValue(BigDecimal.ONE)) - assertIs(JSONValue("hello")) - assertIs(JSONValue(true)) - assertIs(JSONValue(JSONValue(0), JSONValue(1))) - assertIs(JSONValue("alpha" to JSONValue(0), "beta" to JSONValue(1))) - assertIs(JSONValue("alpha" refersTo JSONValue(0), "beta" refersTo JSONValue(1))) + JSONValue(123).shouldBeType() + JSONValue(0L).shouldBeType() + JSONValue(BigDecimal.ONE).shouldBeType() + JSONValue("hello").shouldBeType() + JSONValue(true).shouldBeType() + JSONValue(JSONValue(0), JSONValue(1)).shouldBeType() + JSONValue("alpha" to JSONValue(0), "beta" to JSONValue(1)).shouldBeType() + JSONValue("alpha" refersTo JSONValue(0), "beta" refersTo JSONValue(1)).shouldBeType() } companion object { diff --git a/src/test/kotlin/io/kjson/parser/ParseOptionsTest.kt b/src/test/kotlin/io/kjson/parser/ParseOptionsTest.kt index 8585e3d..3de2274 100644 --- a/src/test/kotlin/io/kjson/parser/ParseOptionsTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParseOptionsTest.kt @@ -2,7 +2,7 @@ * @(#) ParseOptionsTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2023 Peter Wall + * Copyright (c) 2023, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,34 +26,35 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldThrow class ParseOptionsTest { @Test fun `should set maximum nesting depth`() { val parseOptions = ParseOptions(maximumNestingDepth = 27) - expect(27) { parseOptions.maximumNestingDepth } + parseOptions.maximumNestingDepth shouldBe 27 } @Test fun `should reject maximum nesting depth too low`() { - assertFailsWith { ParseOptions(maximumNestingDepth = 0) }.let { - expect("Maximum nesting depth must be 1..1200, was 0") { it.message } + shouldThrow("Maximum nesting depth must be 1..1200, was 0") { + ParseOptions(maximumNestingDepth = 0) } - assertFailsWith { ParseOptions(maximumNestingDepth = -1) }.let { - expect("Maximum nesting depth must be 1..1200, was -1") { it.message } + shouldThrow("Maximum nesting depth must be 1..1200, was -1") { + ParseOptions(maximumNestingDepth = -1) } - assertFailsWith { ParseOptions(maximumNestingDepth = -10) }.let { - expect("Maximum nesting depth must be 1..1200, was -10") { it.message } + shouldThrow("Maximum nesting depth must be 1..1200, was -10") { + ParseOptions(maximumNestingDepth = -10) } } @Test fun `should reject maximum nesting depth too high`() { - assertFailsWith { ParseOptions(maximumNestingDepth = 1500) }.let { - expect("Maximum nesting depth must be 1..1200, was 1500") { it.message } + shouldThrow("Maximum nesting depth must be 1..1200, was 1500") { + ParseOptions(maximumNestingDepth = 1500) } - assertFailsWith { ParseOptions(maximumNestingDepth = 9999) }.let { - expect("Maximum nesting depth must be 1..1200, was 9999") { it.message } + shouldThrow("Maximum nesting depth must be 1..1200, was 9999") { + ParseOptions(maximumNestingDepth = 9999) } } diff --git a/src/test/kotlin/io/kjson/parser/ParserArrayTest.kt b/src/test/kotlin/io/kjson/parser/ParserArrayTest.kt index 2be5d16..90e3658 100644 --- a/src/test/kotlin/io/kjson/parser/ParserArrayTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserArrayTest.kt @@ -2,7 +2,7 @@ * @(#) ParserArrayTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021 Peter Wall + * Copyright (c) 2021, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,9 +26,10 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertIs -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow import io.kjson.JSONArray import io.kjson.JSONInt @@ -42,66 +43,63 @@ class ParserArrayTest { @Test fun `should parse empty array`() { val result = Parser.parse("[]") - assertIs(result) - expect(0) { result.size } + result.shouldBeType() + result.size shouldBe 0 } @Test fun `should parse array of string`() { val result = Parser.parse("""["simple"]""") - assertIs(result) - expect(1) { result.size } - expect(JSONString("simple")) { result[0] } + result.shouldBeType() + result.size shouldBe 1 + result[0] shouldBe JSONString("simple") } @Test fun `should parse array of two strings`() { val result = Parser.parse("""["Hello","world"]""") - assertIs(result) - expect(2) { result.size } - expect(JSONString("Hello")) { result[0] } - expect(JSONString("world")) { result[1] } + result.shouldBeType() + result.size shouldBe 2 + result[0] shouldBe JSONString("Hello") + result[1] shouldBe JSONString("world") } @Test fun `should parse array of arrays`() { val result = Parser.parse("""["Hello",["world","universe"]]""") - assertIs(result) - expect(2) { result.size } - expect(JSONString("Hello")) { result[0] } + result.shouldBeType() + result.size shouldBe 2 + result[0] shouldBe JSONString("Hello") val inner = result[1] - assertIs(inner) - expect(2) { inner.size } - expect(JSONString("world")) { inner[0] } - expect(JSONString("universe")) { inner[1] } + inner.shouldBeType() + inner.size shouldBe 2 + inner[0] shouldBe JSONString("world") + inner[1] shouldBe JSONString("universe") } @Test fun `should throw exception on trailing comma`() { - assertFailsWith { Parser.parse("""["simple",]""") }.let { - expect(ILLEGAL_SYNTAX) { it.text } - expect("$ILLEGAL_SYNTAX, at /1") { it.message } - expect("/1") { it.pointer } + shouldThrow("$ILLEGAL_SYNTAX, at /1") { Parser.parse("""["simple",]""") }.let { + it.text shouldBe ILLEGAL_SYNTAX + it.pointer shouldBe "/1" } } @Test fun `should allow trailing comma with option arrayTrailingComma`() { val options = ParseOptions(arrayTrailingComma = true) val result = Parser.parse("""["simple",]""", options) - assertIs(result) - expect(1) { result.size } - expect(JSONString("simple")) { result[0] } + result.shouldBeType() + result.size shouldBe 1 + result[0] shouldBe JSONString("simple") } @Test fun `should throw exception on missing closing bracket`() { - assertFailsWith { Parser.parse("""["simple"""") }.let { - expect(MISSING_CLOSING_BRACKET) { it.text } - expect(MISSING_CLOSING_BRACKET) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(MISSING_CLOSING_BRACKET) { Parser.parse("""["simple"""") }.let { + it.text shouldBe MISSING_CLOSING_BRACKET + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on syntax error`() { - assertFailsWith { Parser.parse("""[&]""") }.let { - expect(ILLEGAL_SYNTAX) { it.text } - expect("$ILLEGAL_SYNTAX, at /0") { it.message } - expect("/0") { it.pointer } + shouldThrow("$ILLEGAL_SYNTAX, at /0") { Parser.parse("""[&]""") }.let { + it.text shouldBe ILLEGAL_SYNTAX + it.pointer shouldBe "/0" } } @@ -110,19 +108,18 @@ class ParserArrayTest { val allowed = "[".repeat(options.maximumNestingDepth) + '1' + "]".repeat(options.maximumNestingDepth) var result = Parser.parse(allowed, options) for (i in 0 until options.maximumNestingDepth) { - assertIs(result) + result.shouldBeType() result = result[0] } - expect(JSONInt(1)) { result } + result shouldBe JSONInt(1) } @Test fun `should throw exception on nesting depth exceeded`() { val options = ParseOptions(maximumNestingDepth = 50) val excessive = "[".repeat(options.maximumNestingDepth + 1) - assertFailsWith { Parser.parse(excessive, options) }.let { - expect(MAX_DEPTH_EXCEEDED) { it.text } - expect(MAX_DEPTH_EXCEEDED) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(MAX_DEPTH_EXCEEDED) { Parser.parse(excessive, options) }.let { + it.text shouldBe MAX_DEPTH_EXCEEDED + it.pointer shouldBe rootPointer } } diff --git a/src/test/kotlin/io/kjson/parser/ParserKeywordTest.kt b/src/test/kotlin/io/kjson/parser/ParserKeywordTest.kt index ae92f36..647cc4a 100644 --- a/src/test/kotlin/io/kjson/parser/ParserKeywordTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserKeywordTest.kt @@ -2,7 +2,7 @@ * @(#) ParserKeywordTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021 Peter Wall + * Copyright (c) 2021, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,44 +26,42 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertNull -import kotlin.test.assertTrue -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType import io.kjson.JSONBoolean import io.kjson.JSONObject -import kotlin.test.assertIs class ParserKeywordTest { @Test fun `should parse null`() { - assertNull(Parser.parse("null")) + Parser.parse("null") shouldBe null } @Test fun `should parse true`() { val result = Parser.parse("true") - assertIs(result) - assertTrue(result.value) + result.shouldBeType() + result.value shouldBe true } @Test fun `should parse false`() { val result = Parser.parse("false") - assertIs(result) - assertFalse(result.value) + result.shouldBeType() + result.value shouldBe false } @Test fun `should parse keywords in object`() { val result = Parser.parse("""{"aaa":true,"bbb":false,"ccc":null}""") - assertIs(result) - expect(3) { result.size } + result.shouldBeType() + result.size shouldBe 3 val aaa = result["aaa"] - assertIs(aaa) - assertTrue(aaa.value) + aaa.shouldBeType() + aaa.value shouldBe true val bbb = result["bbb"] - assertIs(bbb) - assertFalse(bbb.value) - assertNull(result["ccc"]) + bbb.shouldBeType() + bbb.value shouldBe false + result["ccc"] shouldBe null } } diff --git a/src/test/kotlin/io/kjson/parser/ParserLinesTest.kt b/src/test/kotlin/io/kjson/parser/ParserLinesTest.kt index ec119c8..5bc85fd 100644 --- a/src/test/kotlin/io/kjson/parser/ParserLinesTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserLinesTest.kt @@ -2,7 +2,7 @@ * @(#) ParserLinesTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2023 Peter Wall + * Copyright (c) 2023, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,9 +26,10 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.expect -import kotlin.test.assertFailsWith -import kotlin.test.assertIs + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow import io.kjson.JSON.asInt import io.kjson.JSONArray @@ -38,88 +39,89 @@ class ParserLinesTest { @Test fun `should parse single line`() { val result = Parser.parseLines("""{"a":999}""") - expect(1) { result.size } + result.size shouldBe 1 with(result[0]) { - assertIs(this) - expect(1) { size } - expect(999) { this["a"].asInt } + shouldBeType() + size shouldBe 1 + this["a"].asInt shouldBe 999 } } @Test fun `should parse multiple lines`() { val result = Parser.parseLines("{\"a\":999}\n{\"b\":888}") - expect(2) { result.size } + result.size shouldBe 2 with(result[0]) { - assertIs(this) - expect(1) { size } - expect(999) { this["a"].asInt } + shouldBeType() + size shouldBe 1 + this["a"].asInt shouldBe 999 } with(result[1]) { - assertIs(this) - expect(1) { size } - expect(888) { this["b"].asInt } + shouldBeType() + size shouldBe 1 + this["b"].asInt shouldBe 888 } } @Test fun `should parse multiple objects without newline`() { val result = Parser.parseLines("{\"a\":999}{\"b\":888}") - expect(2) { result.size } + result.size shouldBe 2 with(result[0]) { - assertIs(this) - expect(1) { size } - expect(999) { this["a"].asInt } + shouldBeType() + size shouldBe 1 + this["a"].asInt shouldBe 999 } with(result[1]) { - assertIs(this) - expect(1) { size } - expect(888) { this["b"].asInt } + shouldBeType() + size shouldBe 1 + this["b"].asInt shouldBe 888 } } @Test fun `should parse empty file`() { val result = Parser.parseLines("") - expect(0) { result.size } + result.size shouldBe 0 } @Test fun `should parse single line containing array`() { val result = Parser.parseLines("[123,456]") - expect(1) { result.size } + result.size shouldBe 1 with(result[0]) { - assertIs(this) - expect(123) { this[0].asInt } - expect(456) { this[1].asInt } + shouldBeType() + this[0].asInt shouldBe 123 + this[1].asInt shouldBe 456 } } @Test fun `should parse multiple lines containing arrays`() { val result = Parser.parseLines("[123,456]\n[789]") - expect(2) { result.size } + result.size shouldBe 2 with(result[0]) { - assertIs(this) - expect(2) { size } - expect(123) { this[0].asInt } - expect(456) { this[1].asInt } + shouldBeType() + size shouldBe 2 + this[0].asInt shouldBe 123 + this[1].asInt shouldBe 456 } with(result[1]) { - assertIs(this) - expect(1) { size } - expect(789) { this[0].asInt } + shouldBeType() + size shouldBe 1 + this[0].asInt shouldBe 789 } } @Test fun `should ignore BOM if present`() { val result = Parser.parseLines("\uFEFF{}") - expect(1) { result.size } + result.size shouldBe 1 with(result[0]) { - assertIs(this) - expect(0) { size } + shouldBeType() + size shouldBe 0 } } @Test fun `should show correct pointer on errors`() { - assertFailsWith { Parser.parseLines("""{} {"aaa":[xxx]}""") }.let { - expect("Illegal JSON syntax, at /1/aaa/0") { it.message } - expect("/1/aaa/0") { it.pointer } + shouldThrow("Illegal JSON syntax, at /1/aaa/0") { + Parser.parseLines("""{} {"aaa":[xxx]}""") + }.let { + it.pointer shouldBe "/1/aaa/0" } } diff --git a/src/test/kotlin/io/kjson/parser/ParserNumberTest.kt b/src/test/kotlin/io/kjson/parser/ParserNumberTest.kt index e579146..449ab8a 100644 --- a/src/test/kotlin/io/kjson/parser/ParserNumberTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserNumberTest.kt @@ -26,13 +26,14 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertIs -import kotlin.test.assertSame -import kotlin.test.expect import java.math.BigDecimal +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeSameInstance +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow + import io.kjson.JSONDecimal import io.kjson.JSONInt import io.kjson.JSONLong @@ -44,137 +45,132 @@ class ParserNumberTest { @Test fun `should parse zero`() { val result = Parser.parse("0") - assertSame(JSONInt.ZERO, result) + result shouldBeSameInstance JSONInt.ZERO } @Test fun `should reject leading zeros`() { - assertFailsWith { Parser.parse("00") }.let { - expect(ILLEGAL_NUMBER) { it.text } - expect(ILLEGAL_NUMBER) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(ILLEGAL_NUMBER) { Parser.parse("00") }.let { + it.text shouldBe ILLEGAL_NUMBER + it.pointer shouldBe rootPointer } - assertFailsWith { Parser.parse("0123") }.let { - expect(ILLEGAL_NUMBER) { it.text } - expect(ILLEGAL_NUMBER) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(ILLEGAL_NUMBER) { Parser.parse("0123") }.let { + it.text shouldBe ILLEGAL_NUMBER + it.pointer shouldBe rootPointer } } @Test fun `should reject incorrect numbers`() { - assertFailsWith { Parser.parse("123a") }.let { - expect(EXCESS_CHARS) { it.text } - expect(EXCESS_CHARS) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(EXCESS_CHARS) { Parser.parse("123a") }.let { + it.text shouldBe EXCESS_CHARS + it.pointer shouldBe rootPointer } - assertFailsWith { Parser.parse("12:00") }.let { - expect(EXCESS_CHARS) { it.text } - expect(EXCESS_CHARS) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(EXCESS_CHARS) { Parser.parse("12:00") }.let { + it.text shouldBe EXCESS_CHARS + it.pointer shouldBe rootPointer } - assertFailsWith { Parser.parse("1.23/4") }.let { - expect(EXCESS_CHARS) { it.text } - expect(EXCESS_CHARS) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(EXCESS_CHARS) { Parser.parse("1.23/4") }.let { + it.text shouldBe EXCESS_CHARS + it.pointer shouldBe rootPointer } } @Test fun `should parse positive integers`() { val result1 = Parser.parse("123") - assertIs(result1) - expect(123) { result1.value } + result1.shouldBeType() + result1.value shouldBe 123 val result2 = Parser.parse("5678900") - assertIs(result2) - expect(5678900) { result2.value } + result2.shouldBeType() + result2.value shouldBe 5678900 val result3 = Parser.parse("2147483647") - assertIs(result3) - expect(2147483647) { result3.value } + result3.shouldBeType() + result3.value shouldBe 2147483647 } @Test fun `should parse negative integers`() { val result1 = Parser.parse("-1") - assertIs(result1) - expect(-1) { result1.value } + result1.shouldBeType() + result1.value shouldBe -1 val result2 = Parser.parse("-876543") - assertIs(result2) - expect(-876543) { result2.value } + result2.shouldBeType() + result2.value shouldBe -876543 val result3 = Parser.parse("-2147483648") - assertIs(result3) - expect(-2147483648) { result3.value } + result3.shouldBeType() + result3.value shouldBe -2147483648 } @Test fun `should parse positive long integers`() { val result1 = Parser.parse("1234567890000") - assertIs(result1) - expect(1234567890000) { result1.value } + result1.shouldBeType() + result1.value shouldBe 1234567890000 val result2 = Parser.parse("567895678956789") - assertIs(result2) - expect(567895678956789) { result2.value } + result2.shouldBeType() + result2.value shouldBe 567895678956789 val result3 = Parser.parse("2147483648") - assertIs(result3) - expect(2147483648) { result3.value } + result3.shouldBeType() + result3.value shouldBe 2147483648 val result4 = Parser.parse("9223372036854775807") - assertIs(result4) - expect(9223372036854775807) { result4.value } + result4.shouldBeType() + result4.value shouldBe 9223372036854775807 } @Test fun `should parse negative long integers`() { val result1 = Parser.parse("-1234567890000") - assertIs(result1) - expect(-1234567890000) { result1.value } + result1.shouldBeType() + result1.value shouldBe -1234567890000 val result2 = Parser.parse("-567895678956789") - assertIs(result2) - expect(-567895678956789) { result2.value } + result2.shouldBeType() + result2.value shouldBe -567895678956789 val result3 = Parser.parse("-2147483649") - assertIs(result3) - expect(-2147483649) { result3.value } + result3.shouldBeType() + result3.value shouldBe -2147483649 val result4 = Parser.parse("-9223372036854775808") - assertIs(result4) - expect(-9223372036854775807 - 1) { result4.value } + result4.shouldBeType() + result4.value shouldBe -9223372036854775807 - 1 } @Test fun `should parse decimal`() { val result1 = Parser.parse("0.0") - assertIs(result1) - expect(0) { result1.value.compareTo(BigDecimal.ZERO) } + result1.shouldBeType() + result1.value.compareTo(BigDecimal.ZERO) shouldBe 0 val result2 = Parser.parse("0.00") - assertIs(result2) - expect(0) { result2.value.compareTo(BigDecimal.ZERO) } + result2.shouldBeType() + result2.value.compareTo(BigDecimal.ZERO) shouldBe 0 } @Test fun `should parse positive decimal`() { val result1 = Parser.parse("12340.0") - assertIs(result1) - expect(0) { result1.value.compareTo("12340".toBigDecimal()) } + result1.shouldBeType() + result1.value.compareTo("12340".toBigDecimal()) shouldBe 0 val result2 = Parser.parse("1e200") - assertIs(result2) - expect(0) { result2.value.compareTo("1e200".toBigDecimal()) } + result2.shouldBeType() + result2.value.compareTo("1e200".toBigDecimal()) shouldBe 0 val result3 = Parser.parse("27e-60") - assertIs(result3) - expect(0) { result3.value.compareTo("27e-60".toBigDecimal()) } + result3.shouldBeType() + result3.value.compareTo("27e-60".toBigDecimal()) shouldBe 0 val result4 = Parser.parse("0.1e-48") - assertIs(result4) - expect(0) { result4.value.compareTo("0.1e-48".toBigDecimal()) } + result4.shouldBeType() + result4.value.compareTo("0.1e-48".toBigDecimal()) shouldBe 0 val result5 = Parser.parse("9223372036854775808") - assertIs(result5) - expect(0) { result5.value.compareTo("9223372036854775808".toBigDecimal()) } + result5.shouldBeType() + result5.value.compareTo("9223372036854775808".toBigDecimal()) shouldBe 0 } @Test fun `should parse negative decimal`() { val result1 = Parser.parse("-12340.0") - assertIs(result1) - expect(0) { result1.value.compareTo("-12340".toBigDecimal()) } + result1.shouldBeType() + result1.value.compareTo("-12340".toBigDecimal()) shouldBe 0 val result2 = Parser.parse("-1e200") - assertIs(result2) - expect(0) { result2.value.compareTo("-1e200".toBigDecimal()) } + result2.shouldBeType() + result2.value.compareTo("-1e200".toBigDecimal()) shouldBe 0 val result3 = Parser.parse("-27e-60") - assertIs(result3) - expect(0) { result3.value.compareTo("-27e-60".toBigDecimal()) } + result3.shouldBeType() + result3.value.compareTo("-27e-60".toBigDecimal()) shouldBe 0 val result4 = Parser.parse("-0.1e-48") - assertIs(result4) - expect(0) { result4.value.compareTo("-0.1e-48".toBigDecimal()) } + result4.shouldBeType() + result4.value.compareTo("-0.1e-48".toBigDecimal()) shouldBe 0 val result5 = Parser.parse("-9223372036854775809") - assertIs(result5) - expect(0) { result5.value.compareTo("-9223372036854775809".toBigDecimal()) } + result5.shouldBeType() + result5.value.compareTo("-9223372036854775809".toBigDecimal()) shouldBe 0 } } diff --git a/src/test/kotlin/io/kjson/parser/ParserObjectTest.kt b/src/test/kotlin/io/kjson/parser/ParserObjectTest.kt index 0103fbd..a05a446 100644 --- a/src/test/kotlin/io/kjson/parser/ParserObjectTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserObjectTest.kt @@ -2,7 +2,7 @@ * @(#) ParserObjectTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021, 2022, 2023 Peter Wall + * Copyright (c) 2021, 2022, 2023, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,10 +26,10 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertIs -import kotlin.test.assertNull -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow import io.kjson.JSON.asInt import io.kjson.JSON.asObject @@ -48,157 +48,155 @@ class ParserObjectTest { @Test fun `should parse empty object`() { val result = Parser.parse("{}") - assertIs(result) - expect(0) { result.size } + result.shouldBeType() + result.size shouldBe 0 } @Test fun `should ignore BOM if present`() { val result = Parser.parse("\uFEFF{}") - assertIs(result) - expect(0) { result.size } + result.shouldBeType() + result.size shouldBe 0 } @Test fun `should parse simple object`() { val result = Parser.parse("""{"first":123,"second":"Hi there!"}""") - assertIs(result) - expect(2) { result.size } + result.shouldBeType() + result.size shouldBe 2 val first = result["first"] - assertIs(first) - expect(123) { first.value } + first.shouldBeType() + first.value shouldBe 123 val second = result["second"] - assertIs(second) - expect("Hi there!") { second.value } + second.shouldBeType() + second.value shouldBe "Hi there!" } @Test fun `should parse nested object`() { val result = Parser.parse("""{"first":123,"second":{"a":[{"aa":0}]}}""") - assertIs(result) - expect(2) { result.size } + result.shouldBeType() + result.size shouldBe 2 val first = result["first"] - assertIs(first) - expect(123) { first.value } + first.shouldBeType() + first.value shouldBe 123 val second = result["second"] - assertIs(second) - expect(1) { second.size } + second.shouldBeType() + second.size shouldBe 1 val a = second["a"] - assertIs(a) - expect(1) { a.size } + a.shouldBeType() + a.size shouldBe 1 val item = a[0] - assertIs(item) - expect(1) { item.size } + item.shouldBeType() + item.size shouldBe 1 val aa = item["aa"] - assertIs(aa) - expect(0) { aa.value } + aa.shouldBeType() + aa.value shouldBe 0 } @Test fun `should throw exception on missing closing brace`() { - assertFailsWith { Parser.parse("""{"first":123""") }.let { - expect(MISSING_CLOSING_BRACE) { it.text } - expect(MISSING_CLOSING_BRACE) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(MISSING_CLOSING_BRACE) { Parser.parse("""{"first":123""") }.let { + it.text shouldBe MISSING_CLOSING_BRACE + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on missing colon`() { - assertFailsWith { Parser.parse("""{"first"123}""") }.let { - expect(MISSING_COLON) { it.text } - expect(MISSING_COLON) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(MISSING_COLON) { Parser.parse("""{"first"123}""") }.let { + it.text shouldBe MISSING_COLON + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on trailing comma`() { - assertFailsWith { Parser.parse("""{"first":123,}""") }.let { - expect(ILLEGAL_KEY) { it.text } - expect(ILLEGAL_KEY) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(ILLEGAL_KEY) { Parser.parse("""{"first":123,}""") }.let { + it.text shouldBe ILLEGAL_KEY + it.pointer shouldBe rootPointer } } @Test fun `should allow trailing comma with option objectTrailingComma`() { val options = ParseOptions(objectTrailingComma = true) val result = Parser.parse("""{"first":123,}""", options) - assertIs(result) - expect(1) { result.size } + result.shouldBeType() + result.size shouldBe 1 val first = result["first"] - assertIs(first) - expect(123) { first.value } + first.shouldBeType() + first.value shouldBe 123 } @Test fun `should throw exception on missing quotes around key`() { - assertFailsWith { Parser.parse("{first:123}") }.let { - expect(ILLEGAL_KEY) { it.text } - expect(ILLEGAL_KEY) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(ILLEGAL_KEY) { Parser.parse("{first:123}") }.let { + it.text shouldBe ILLEGAL_KEY + it.pointer shouldBe rootPointer } } @Test fun `should allow missing quotes around key with option objectKeyUnquoted`() { val options = ParseOptions(objectKeyUnquoted = true) val result = Parser.parse("{first:123}", options) - assertIs(result) - expect(1) { result.size } + result.shouldBeType() + result.size shouldBe 1 val first = result["first"] - assertIs(first) - expect(123) { first.value } + first.shouldBeType() + first.value shouldBe 123 } @Test fun `should throw exception on duplicate keys`() { - assertFailsWith { Parser.parse("""{"first":123,"first":456}""") }.let { - expect("Duplicate key - first") { it.text } - expect("Duplicate key - first") { it.message } - expect("") { it.key } + shouldThrow("Duplicate key - first") { Parser.parse("""{"first":123,"first":456}""") }.let { + it.text shouldBe "Duplicate key - first" + it.key shouldBe "" } } @Test fun `should throw exception on duplicate keys with option ERROR`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.ERROR) - assertFailsWith { Parser.parse("""{"first":123,"first":456}""", options) }.let { - expect("Duplicate key - first") { it.text } - expect("Duplicate key - first") { it.message } - expect("") { it.key } + shouldThrow("Duplicate key - first") { + Parser.parse("""{"first":123,"first":456}""", options) + }.let { + it.text shouldBe "Duplicate key - first" + it.key shouldBe "" } } @Test fun `should throw exception with pointer on duplicate keys with option ERROR`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.ERROR) - assertFailsWith { Parser.parse("""{"abc":{"first":123,"first":456}}""", options) }.let { - expect("Duplicate key - first") { it.text } - expect("Duplicate key - first, at /abc") { it.message } - expect("/abc") { it.key } + shouldThrow("Duplicate key - first, at /abc") { + Parser.parse("""{"abc":{"first":123,"first":456}}""", options) + }.let { + it.text shouldBe "Duplicate key - first" + it.key shouldBe "/abc" } } @Test fun `should throw exception on duplicate keys with option CHECK_IDENTICAL and different values`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) - assertFailsWith { Parser.parse("""{"first":123,"first":456}""", options) }.let { - expect("Duplicate key - first") { it.text } - expect("Duplicate key - first") { it.message } - expect("") { it.key } + shouldThrow("Duplicate key - first") { + Parser.parse("""{"first":123,"first":456}""", options) + }.let { + it.text shouldBe "Duplicate key - first" + it.key shouldBe "" } } @Test fun `should take first on duplicate keys with option CHECK_IDENTICAL and same values`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.CHECK_IDENTICAL) Parser.parse("""{"first":123,"first":123}""", options).asObject.let { - expect(1) { it.size } - expect(123) { it["first"].asInt } + it.size shouldBe 1 + it["first"].asInt shouldBe 123 } } @Test fun `should take first on duplicate keys with option TAKE_FIRST`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.TAKE_FIRST) Parser.parse("""{"first":123,"first":456}""", options).asObject.let { - expect(1) { it.size } - expect(123) { it["first"].asInt } + it.size shouldBe 1 + it["first"].asInt shouldBe 123 } } @Test fun `should take last on duplicate keys with option TAKE_LAST`() { val options = ParseOptions(JSONObject.DuplicateKeyOption.TAKE_LAST) Parser.parse("""{"first":123,"first":456}""", options).asObject.let { - expect(1) { it.size } - expect(456) { it["first"].asInt } + it.size shouldBe 1 + it["first"].asInt shouldBe 456 } } @@ -207,19 +205,18 @@ class ParserObjectTest { val allowed = """{"a":""".repeat(options.maximumNestingDepth) + '1' + "}".repeat(options.maximumNestingDepth) var result = Parser.parse(allowed, options) for (i in 0 until options.maximumNestingDepth) { - assertIs(result) + result.shouldBeType() result = result["a"] } - expect(JSONInt(1)) { result } + result shouldBe JSONInt(1) } @Test fun `should throw exception on nesting depth exceeded`() { val options = ParseOptions(maximumNestingDepth = 50) val excessive = """{"a":""".repeat(options.maximumNestingDepth + 1) - assertFailsWith { Parser.parse(excessive, options) }.let { - expect(MAX_DEPTH_EXCEEDED) { it.text } - expect(MAX_DEPTH_EXCEEDED) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(MAX_DEPTH_EXCEEDED) { Parser.parse(excessive, options) }.let { + it.text shouldBe MAX_DEPTH_EXCEEDED + it.pointer shouldBe rootPointer } } diff --git a/src/test/kotlin/io/kjson/parser/ParserStringTest.kt b/src/test/kotlin/io/kjson/parser/ParserStringTest.kt index 2480f60..9374772 100644 --- a/src/test/kotlin/io/kjson/parser/ParserStringTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserStringTest.kt @@ -2,7 +2,7 @@ * @(#) ParserStringTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2021 Peter Wall + * Copyright (c) 2021, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,9 +26,10 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFailsWith -import kotlin.test.assertIs -import kotlin.test.expect + +import io.kstuff.test.shouldBe +import io.kstuff.test.shouldBeType +import io.kstuff.test.shouldThrow import io.kjson.JSONString import io.kjson.parser.ParserConstants.rootPointer @@ -38,56 +39,61 @@ class ParserStringTest { @Test fun `should parse simple string`() { val result = Parser.parse("\"simple\"") - assertIs(result) - expect("simple") { result.value } + result.shouldBeType() + result.value shouldBe "simple" } @Test fun `should parse string with escape sequences`() { val result = Parser.parse("\"tab\\tnewline\\nquote\\\" \"") - assertIs(result) - expect("tab\tnewline\nquote\" ") { result.value } + result.shouldBeType() + result.value shouldBe "tab\tnewline\nquote\" " } @Test fun `should parse string with unicode escape sequences`() { val result = Parser.parse("\"mdash \\u2014\"") - assertIs(result) - expect("mdash \u2014") { result.value } + result.shouldBeType() + result.value shouldBe "mdash \u2014" } @Test fun `should throw exception on missing closing quote`() { - assertFailsWith { Parser.parse("\"abc") }.let { - expect(JSONFunctions.UNTERMINATED_STRING) { it.text } - expect(JSONFunctions.UNTERMINATED_STRING) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(JSONFunctions.UNTERMINATED_STRING) { + Parser.parse("\"abc") + }.let { + it.text shouldBe JSONFunctions.UNTERMINATED_STRING + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on bad escape sequence`() { - assertFailsWith { Parser.parse("\"ab\\c\"") }.let { - expect(JSONFunctions.ILLEGAL_ESCAPE_SEQUENCE) { it.text } - expect(JSONFunctions.ILLEGAL_ESCAPE_SEQUENCE) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(JSONFunctions.ILLEGAL_ESCAPE_SEQUENCE) { + Parser.parse("\"ab\\c\"") + }.let { + it.text shouldBe JSONFunctions.ILLEGAL_ESCAPE_SEQUENCE + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on bad unicode sequence`() { - assertFailsWith { Parser.parse("\"ab\\uxxxx\"") }.let { - expect(JSONFunctions.ILLEGAL_UNICODE_SEQUENCE) { it.text } - expect(JSONFunctions.ILLEGAL_UNICODE_SEQUENCE) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(JSONFunctions.ILLEGAL_UNICODE_SEQUENCE) { + Parser.parse("\"ab\\uxxxx\"") + }.let { + it.text shouldBe JSONFunctions.ILLEGAL_UNICODE_SEQUENCE + it.pointer shouldBe rootPointer } } @Test fun `should throw exception on illegal character`() { - assertFailsWith { Parser.parse("\"ab\u0001\"") }.let { - expect(JSONFunctions.ILLEGAL_CHAR) { it.text } - expect(JSONFunctions.ILLEGAL_CHAR) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(JSONFunctions.ILLEGAL_CHAR) { + Parser.parse("\"ab\u0001\"") + }.let { + it.text shouldBe JSONFunctions.ILLEGAL_CHAR + it.pointer shouldBe rootPointer } - assertFailsWith { Parser.parse("\"ab\n\"") }.let { - expect(JSONFunctions.ILLEGAL_CHAR) { it.text } - expect(JSONFunctions.ILLEGAL_CHAR) { it.message } - expect(rootPointer) { it.pointer } + shouldThrow(JSONFunctions.ILLEGAL_CHAR) { + Parser.parse("\"ab\n\"") + }.let { + it.text shouldBe JSONFunctions.ILLEGAL_CHAR + it.pointer shouldBe rootPointer } } diff --git a/src/test/kotlin/io/kjson/parser/ParserUtilTest.kt b/src/test/kotlin/io/kjson/parser/ParserUtilTest.kt index 71c811d..63a97dd 100644 --- a/src/test/kotlin/io/kjson/parser/ParserUtilTest.kt +++ b/src/test/kotlin/io/kjson/parser/ParserUtilTest.kt @@ -2,7 +2,7 @@ * @(#) ParserUtilTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2023 Peter Wall + * Copyright (c) 2023, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,8 +26,8 @@ package io.kjson.parser import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertTrue + +import io.kstuff.test.shouldBe import io.kjson.parser.ParserConstants.identifierContinuationSet import io.kjson.parser.ParserConstants.identifierStartSet @@ -35,33 +35,33 @@ import io.kjson.parser.ParserConstants.identifierStartSet class ParserUtilTest { @Test fun `should recognise leading character of identifier`() { - assertTrue('A' in identifierStartSet) - assertTrue('B' in identifierStartSet) - assertTrue('Z' in identifierStartSet) - assertTrue('a' in identifierStartSet) - assertTrue('z' in identifierStartSet) - assertTrue('_' in identifierStartSet) - assertFalse('0' in identifierStartSet) - assertFalse('9' in identifierStartSet) - assertFalse('.' in identifierStartSet) - assertFalse('"' in identifierStartSet) - assertFalse('{' in identifierStartSet) - assertFalse('[' in identifierStartSet) + ('A' in identifierStartSet) shouldBe true + ('B' in identifierStartSet) shouldBe true + ('Z' in identifierStartSet) shouldBe true + ('a' in identifierStartSet) shouldBe true + ('z' in identifierStartSet) shouldBe true + ('_' in identifierStartSet) shouldBe true + ('0' in identifierStartSet) shouldBe false + ('9' in identifierStartSet) shouldBe false + ('.' in identifierStartSet) shouldBe false + ('"' in identifierStartSet) shouldBe false + ('{' in identifierStartSet) shouldBe false + ('[' in identifierStartSet) shouldBe false } @Test fun `should recognise continuation character of identifier`() { - assertTrue('A' in identifierContinuationSet) - assertTrue('B' in identifierContinuationSet) - assertTrue('Z' in identifierContinuationSet) - assertTrue('a' in identifierContinuationSet) - assertTrue('z' in identifierContinuationSet) - assertTrue('_' in identifierContinuationSet) - assertTrue('0' in identifierContinuationSet) - assertTrue('9' in identifierContinuationSet) - assertFalse('.' in identifierContinuationSet) - assertFalse('"' in identifierContinuationSet) - assertFalse('{' in identifierContinuationSet) - assertFalse('[' in identifierContinuationSet) + ('A' in identifierContinuationSet) shouldBe true + ('B' in identifierContinuationSet) shouldBe true + ('Z' in identifierContinuationSet) shouldBe true + ('a' in identifierContinuationSet) shouldBe true + ('z' in identifierContinuationSet) shouldBe true + ('_' in identifierContinuationSet) shouldBe true + ('0' in identifierContinuationSet) shouldBe true + ('9' in identifierContinuationSet) shouldBe true + ('.' in identifierContinuationSet) shouldBe false + ('"' in identifierContinuationSet) shouldBe false + ('{' in identifierContinuationSet) shouldBe false + ('[' in identifierContinuationSet) shouldBe false } } diff --git a/src/test/kotlin/io/kjson/testutil/ImportTest.kt b/src/test/kotlin/io/kjson/testutil/ImportTest.kt index 992d873..ce140d6 100644 --- a/src/test/kotlin/io/kjson/testutil/ImportTest.kt +++ b/src/test/kotlin/io/kjson/testutil/ImportTest.kt @@ -26,7 +26,8 @@ package io.kjson.testutil import kotlin.test.Test -import kotlin.test.expect + +import io.kstuff.test.shouldBe import io.kjson.JSONInt import io.kjson.JSONValue @@ -36,7 +37,7 @@ class ImportTest { @Test fun `should import JSONValue once for both class and function`() { val intValue1: JSONValue = JSONInt(123) val intValue2 = JSONValue(123) - expect(intValue1) { intValue2 } + intValue2 shouldBe intValue1 } } diff --git a/src/test/kotlin/io/kjson/util/LookupSetTest.kt b/src/test/kotlin/io/kjson/util/LookupSetTest.kt index 7ce5542..6b7f0b7 100644 --- a/src/test/kotlin/io/kjson/util/LookupSetTest.kt +++ b/src/test/kotlin/io/kjson/util/LookupSetTest.kt @@ -2,7 +2,7 @@ * @(#) LookupSetTest.kt * * kjson-core JSON Kotlin core functionality - * Copyright (c) 2023 Peter Wall + * Copyright (c) 2023, 2024 Peter Wall * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,16 +26,16 @@ package io.kjson.util import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertTrue + +import io.kstuff.test.shouldBe class LookupSetTest { @Test fun `should perform simple lookup`() { - assertTrue('a' in LookupSet { it in 'a'..'z' }) - assertFalse('A' in LookupSet { it in 'a'..'z' }) - assertTrue("AUD" in LookupSet { it == "AUD" || it == "NZD" }) - assertFalse("EUR" in LookupSet { it == "AUD" || it == "NZD" }) + ('a' in LookupSet { it in 'a'..'z' }) shouldBe true + ('A' in LookupSet { it in 'a'..'z' }) shouldBe false + ("AUD" in LookupSet { it == "AUD" || it == "NZD" }) shouldBe true + ("EUR" in LookupSet { it == "AUD" || it == "NZD" }) shouldBe false } }