-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Levenshtein distance calculation for the enhancement of the 'invalid …
…enum' error (#232) ### What's done: - Inspired by diktat: in mostly each and every configuration files we have enums and authors of libraries that work with this configuration file would like to help users to find a misprint and suggest the closest value - we think that this should be done on the side of all decoding libraries
- Loading branch information
Showing
3 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/EnumValidationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import com.akuleshov7.ktoml.exceptions.IllegalTypeException | ||
import com.akuleshov7.ktoml.exceptions.InvalidEnumValueException | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
@Serializable | ||
data class Color(val myEnum: EnumExample) | ||
|
||
enum class EnumExample { | ||
CANAPA, | ||
KANAPA, | ||
KANADA, | ||
USA, | ||
MEXICO, | ||
} | ||
|
||
class EnumValidationTest { | ||
@Test | ||
fun testRegressions() { | ||
var exception = assertFailsWith<InvalidEnumValueException> { | ||
Toml.decodeFromString<Color>("myEnum = \"KANATA\"") | ||
} | ||
|
||
exception.exceptionValidation( | ||
"Line 1: value <KANATA> is not a valid enum option. Did you mean <KANAPA>? " + | ||
"Permitted choices are: CANAPA, KANADA, KANAPA, MEXICO, USA." | ||
) | ||
|
||
exception = assertFailsWith<InvalidEnumValueException> { | ||
Toml.decodeFromString<Color>("myEnum = \"TEST\"") | ||
} | ||
|
||
exception.exceptionValidation( | ||
"Line 1: value <TEST> is not a valid enum option. Did you mean <USA>? " + | ||
"Permitted choices are: CANAPA, KANADA, KANAPA, MEXICO, USA." | ||
) | ||
|
||
exception = assertFailsWith<InvalidEnumValueException> { | ||
Toml.decodeFromString<Color>("myEnum = \"MEKSICA\"") | ||
} | ||
|
||
exception.exceptionValidation( | ||
"Line 1: value <MEKSICA> is not a valid enum option. Did you mean <MEXICO>? " + | ||
"Permitted choices are: CANAPA, KANADA, KANAPA, MEXICO, USA." | ||
) | ||
} | ||
} | ||
|
||
private fun InvalidEnumValueException.exceptionValidation(expected: String) { | ||
assertEquals(expected, this.message) | ||
} |