Skip to content

Commit

Permalink
Allow the null as a value
Browse files Browse the repository at this point in the history
  • Loading branch information
C.A.B committed Nov 19, 2019
1 parent f338936 commit cb73ae9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
}
dependencies {
compile("com.helpchoice.kotlin:koton:1.1.0")
compile("com.helpchoice.kotlin:koton:1.1.6")
}
```
Expand All @@ -24,7 +24,7 @@ dependencies {
<dependency>
<groupId>com.helpchoice.kotlin</groupId>
<artifactId>koton</artifactId>
<version>1.1.0</version>
<version>1.1.6</version>
</dependency>
```

Expand All @@ -37,7 +37,7 @@ Also KotON provides the toJson() method to convert the whole structure into vali
JSON string.

KotON object with parentheses returns the internal value. Depend on the instance that value
may be simple Kotlin instance, array or map (see examples below). The expected type should
may be simple Kotlin instance, array or map (see examples below). The expected type might
be provided as generics before parentheses.

To create the root object call the `kotON(...)` function. Depend on provided parameters
Expand Down Expand Up @@ -69,7 +69,6 @@ kotON(true).toJson() == "true"
kotON("any text")<String>() == "any text"
kotON(true).toJson() == "\"any text\""
```
`

### Array value

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/helpchoice/kotlin/koton/KotON.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private data class KotONEntry(val content: Map<String, KotON<Any>> = emptyMap())
}

data class KotONBuilder(val content: MutableMap<String, KotON<Any>> = mutableMapOf()) {
infix fun String.to(value: Any) {
infix fun String.to(value: Any?) {
content[this] = KotONVal(value)
}

Expand Down
22 changes: 8 additions & 14 deletions src/test/kotlin/com/helpchoice/kotlin/koton/KotONSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,13 @@ class KotONSpec : StringSpec() {
"boolean false" to false
}

doc.size() shouldBe 7 // value "null" is not counted
doc.size() shouldBe 8 // value "null" is not counted
doc["string"]() shouldBe "string value"
doc["integer"]() shouldBe 42
doc["float"]() shouldBe 3.14
doc["boolean true"]() shouldBe true
doc["boolean false"]() shouldBe false
shouldThrow<NullPointerException> { doc["null value"]() }
.apply { message shouldBe "Object contains no value" }
doc["null value"]() shouldBe null
shouldThrow<NullPointerException> { doc["unexisting"]() }
.apply { message shouldBe "Object contains no value" }

Expand All @@ -184,8 +183,8 @@ class KotONSpec : StringSpec() {
doc.contains("float") shouldBe true
doc.contains("boolean true") shouldBe true
doc.contains("boolean false") shouldBe true
doc.contains("null value") shouldBe true

doc.contains("null value") shouldBe false
doc.contains("unexisting") shouldBe false

doc("string") shouldBe "string value"
Expand All @@ -201,14 +200,10 @@ class KotONSpec : StringSpec() {
doc["float"]<Float>() shouldBe 3.14
doc["boolean true"]<Boolean>() shouldBe true
doc["boolean false"]<Boolean>() shouldBe false
shouldThrow<NullPointerException> { doc["null value"]<Int>() }
.apply { message shouldBe "Object contains no value" }
shouldThrow<NullPointerException> { doc["null value"]<String>() }
.apply { message shouldBe "Object contains no value" }
shouldThrow<NullPointerException> { doc["null value"]<Boolean>() }
.apply { message shouldBe "Object contains no value" }
shouldThrow<NullPointerException> { doc["null value"]<Map<String, Any>>() }
.apply { message shouldBe "Object contains no value" }
doc["null value"]<Int>() shouldBe null
doc["null value"]<String>() shouldBe null
doc["null value"]<Boolean>() shouldBe null
doc["null value"]<Map<String, Any>>() shouldBe null

doc<String>("string") shouldBe "string value"
doc<Int>("integer") shouldBe 42
Expand Down Expand Up @@ -258,8 +253,7 @@ class KotONSpec : StringSpec() {
doc("subStruct", "subarray", "1", "intKey") shouldBe 42
doc<Int>("subStruct", "subarray", "1", "intKey") shouldBe 42
doc["subStruct", "subinteger"]<Int>() shouldBe 42
shouldThrow<NullPointerException> { doc["subStruct", "null subvalue"]() }
.apply { message shouldBe "Object contains no value" }
doc["subStruct", "null subvalue"]() shouldBe null

val expected = kotON(
{ "stringElement" to "value of an element" },
Expand Down

0 comments on commit cb73ae9

Please sign in to comment.