Skip to content

Commit 8d47297

Browse files
committed
Add additional test case for scenario where an unknown property is provided and a naming strategy is in use.
1 parent 4028606 commit 8d47297

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt

+39-14
Original file line numberDiff line numberDiff line change
@@ -1031,23 +1031,48 @@ class YamlReadingTest : DescribeSpec({
10311031
}
10321032

10331033
context("given some input representing an object with an unknown key, using a naming strategy") {
1034-
val input = "oneTwoThree: something".trimIndent()
1034+
context("given the unknown key does not match any of the known field names") {
1035+
val input = "oneTwoThree: something".trimIndent()
10351036

1036-
context("parsing that input") {
1037-
it("throws an exception with the naming strategy applied") {
1038-
val exception = shouldThrow<UnknownPropertyException> {
1039-
Yaml(
1040-
configuration = YamlConfiguration(yamlNamingStrategy = YamlNamingStrategy.SnakeCase),
1041-
).decodeFromString(ComplexStructure.serializer(), input)
1037+
context("parsing that input") {
1038+
it("throws an exception") {
1039+
val exception = shouldThrow<UnknownPropertyException> {
1040+
Yaml(
1041+
configuration = YamlConfiguration(yamlNamingStrategy = YamlNamingStrategy.SnakeCase),
1042+
).decodeFromString(NestedObjects.serializer(), input)
1043+
}
1044+
1045+
exception.asClue {
1046+
it.message shouldBe "Unknown property 'oneTwoThree'. Known properties are: first_person, second_person"
1047+
it.line shouldBe 1
1048+
it.column shouldBe 1
1049+
it.propertyName shouldBe "oneTwoThree"
1050+
it.validPropertyNames shouldBe setOf("first_person", "second_person")
1051+
it.path shouldBe YamlPath.root.withMapElementKey("oneTwoThree", Location(1, 1))
1052+
}
10421053
}
1054+
}
1055+
}
10431056

1044-
exception.asClue {
1045-
it.message shouldBe "Unknown property 'oneTwoThree'. Known properties are: boolean, byte, char, double, enum, float, int, long, nullable, short, string"
1046-
it.line shouldBe 1
1047-
it.column shouldBe 1
1048-
it.propertyName shouldBe "oneTwoThree"
1049-
it.validPropertyNames shouldBe setOf("boolean", "byte", "char", "double", "enum", "float", "int", "long", "nullable", "short", "string")
1050-
it.path shouldBe YamlPath.root.withMapElementKey("oneTwoThree", Location(1, 1))
1057+
context("given the unknown key uses the Kotlin name of the field, rather than the name from the naming strategy") {
1058+
val input = "firstPerson: something".trimIndent()
1059+
1060+
context("parsing that input") {
1061+
it("throws an exception") {
1062+
val exception = shouldThrow<UnknownPropertyException> {
1063+
Yaml(
1064+
configuration = YamlConfiguration(yamlNamingStrategy = YamlNamingStrategy.SnakeCase),
1065+
).decodeFromString(NestedObjects.serializer(), input)
1066+
}
1067+
1068+
exception.asClue {
1069+
it.message shouldBe "Unknown property 'firstPerson'. Known properties are: first_person, second_person"
1070+
it.line shouldBe 1
1071+
it.column shouldBe 1
1072+
it.propertyName shouldBe "firstPerson"
1073+
it.validPropertyNames shouldBe setOf("first_person", "second_person")
1074+
it.path shouldBe YamlPath.root.withMapElementKey("firstPerson", Location(1, 1))
1075+
}
10511076
}
10521077
}
10531078
}

0 commit comments

Comments
 (0)