Skip to content

Commit

Permalink
Fix empty lists as parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Oct 30, 2022
1 parent 5ad8125 commit 0c35379
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.serpro69.kfaker.provider
import io.github.serpro69.kfaker.faker
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldMatch
Expand All @@ -11,6 +12,17 @@ class AddressIT : DescribeSpec({
describe("Address Provider") {
val address: (locale: String) -> Address = { faker { fakerConfig { locale = it } }.address }

context("uk locale") {
context("empty list as a parameter value") {
it("cityPrefix() should return empty string") {
address("uk").cityPrefix() shouldBe ""
}
it("citySuffix() should return empty string") {
address("uk").citySuffix() shouldBe ""
}
}
}

context("nb-NO locale") {
it("city() does NOT throw NoSuchElementException") {
shouldNotThrow<NoSuchElementException> { address("nb-NO").city() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.github.serpro69.kfaker.provider.FakeDataProvider
import io.github.serpro69.kfaker.provider.Name
import io.github.serpro69.kfaker.provider.Tertiary
import io.github.serpro69.kfaker.provider.YamlFakeDataProvider
import org.w3c.dom.ranges.RangeException
import java.io.InputStream
import java.util.*
import java.util.regex.Matcher
Expand Down Expand Up @@ -304,8 +305,10 @@ internal class FakerService {

return when (parameterValue) {
is List<*> -> {
when (val value = randomService.randomValue(parameterValue)) {
is List<*> -> RawExpression(randomService.randomValue(value) as String)
if (parameterValue.isEmpty()) RawExpression("") else when (val value = randomService.randomValue(parameterValue)) {
is List<*> -> {
if (value.isEmpty()) RawExpression("") else RawExpression(randomService.randomValue(value) as String)
}
is String -> RawExpression(value)
is Int -> RawExpression(value.toString())
else -> throw UnsupportedOperationException("Unsupported type of raw value: ${parameterValue::class.simpleName}")
Expand Down

0 comments on commit 0c35379

Please sign in to comment.