Skip to content

Commit

Permalink
added tests for enum codec & provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidsonic committed Feb 3, 2019
1 parent 4e5313c commit 578955b
Show file tree
Hide file tree
Showing 3 changed files with 432 additions and 11 deletions.
39 changes: 28 additions & 11 deletions coding/sources/codecs/extended/EnumJSONTransformation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ sealed class EnumJSONTransformation {
enum class Case {

lowerCamelCase,
lowercase,
lower_snake_case,
`lower space case`,
lowercase,
`lowercase words`,
UpperCamelCase,
UPPERCASE,
UPPER_SNAKE_CASE,
`UPPER SPACE CASE`
UPPERCASE,
`UPPERCASE WORDS`
}
}

Expand All @@ -36,13 +36,13 @@ internal fun Case?.convert(string: String) =
when (this) {
null -> string
Case.lowerCamelCase -> string.words().mapIndexed(::wordToLowerCamelCase).joinToString(separator = "")
Case.lowercase -> string.toLowerCase()
Case.lower_snake_case -> string.words().joinToString(separator = "_").toLowerCase()
Case.`lower space case` -> string.words().joinToString(separator = " ").toLowerCase()
Case.lowercase -> string.toLowerCase()
Case.`lowercase words` -> string.words().joinToString(separator = " ").toLowerCase()
Case.UpperCamelCase -> string.words().joinToString(separator = "") { it.camelize() }
Case.UPPERCASE -> string.toUpperCase()
Case.UPPER_SNAKE_CASE -> string.words().joinToString(separator = "_").toUpperCase()
Case.`UPPER SPACE CASE` -> string.words().joinToString(separator = " ").toUpperCase()
Case.UPPERCASE -> string.toUpperCase()
Case.`UPPERCASE WORDS` -> string.words().joinToString(separator = " ").toUpperCase()
}


Expand All @@ -61,21 +61,37 @@ private fun String.words(): List<String> {
var currentWordStart = -1
var currentWordEndsWithDigit = false
var currentWordEndsWithLowerCase = false
var currentWordEndsWithUpperCase = false

for ((index, character) in withIndex()) {
val isLetter = character.isLetter()
val isDigit = character.isDigit()

if (isLetter || isDigit) {
val isLowerCase = character.isLowerCase()
val isUpperCase = character.isUpperCase()

if (isInWord) {
if (currentWordEndsWithLowerCase && character.isUpperCase()) {
if (currentWordStart >= 0) {
when {
!currentWordEndsWithDigit && isDigit -> {
words += substring(startIndex = currentWordStart, endIndex = index)

currentWordStart = index
}

currentWordStart = index
currentWordEndsWithLowerCase && isUpperCase -> {
words += substring(startIndex = currentWordStart, endIndex = index)

currentWordStart = index
}

currentWordEndsWithUpperCase && isLowerCase -> {
if (currentWordStart < index - 1) {
words += substring(startIndex = currentWordStart, endIndex = index - 1)
}

currentWordStart = index - 1
}
}
}
else {
Expand All @@ -92,6 +108,7 @@ private fun String.words(): List<String> {

currentWordEndsWithDigit = isDigit
currentWordEndsWithLowerCase = isLowerCase
currentWordEndsWithUpperCase = isUpperCase
}
else if (isInWord) {
currentWordEnd = index
Expand Down
31 changes: 31 additions & 0 deletions coding/tests/sources/codecs/EnumJSONCodecProviderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tests.coding

import ch.tutteli.atrium.api.cc.en_GB.toBe
import ch.tutteli.atrium.verbs.assert
import com.github.fluidsonic.fluid.json.*
import org.junit.jupiter.api.Test


@Suppress("UNCHECKED_CAST")
internal object EnumJSONCodecProviderTest {

@Test
fun test() {
val provider = EnumJSONCodecProvider(transformation = EnumJSONTransformation.Ordinal)
val serializer = JSONCodingSerializer.builder().encodingWith(provider).build()

for (enumClass in listOf(Example1::class, Example2::class)) {
assert(serializer.serializeValue(enumClass.java.enumConstants.first())).toBe("0")
}
}


private enum class Example1 {
hello1
}


private enum class Example2 {
hello2
}
}
Loading

0 comments on commit 578955b

Please sign in to comment.