@@ -6,12 +6,14 @@ import io.kotest.assertions.throwables.shouldNotThrow
6
6
import io.kotest.assertions.throwables.shouldThrow
7
7
import io.kotest.core.spec.style.DescribeSpec
8
8
import io.kotest.matchers.collections.shouldHaveSize
9
+ import io.kotest.matchers.ints.shouldBeInRange
9
10
import io.kotest.matchers.maps.shouldHaveSize
10
11
import io.kotest.matchers.shouldBe
11
12
import io.kotest.matchers.shouldNotBe
12
13
import io.kotest.matchers.string.shouldHaveLength
13
14
import io.kotest.matchers.types.instanceOf
14
15
import io.kotest.matchers.types.shouldNotBeSameInstanceAs
16
+ import org.junit.jupiter.api.assertThrows
15
17
import java.util.*
16
18
import kotlin.reflect.full.declaredMemberProperties
17
19
@@ -394,14 +396,28 @@ class RandomClassProviderTest : DescribeSpec({
394
396
class TestClass (
395
397
val list: List <Foo >,
396
398
val set: Set <Bar >,
397
- val map: Map <String , Baz >
399
+ val map: Map <String , Baz >,
400
+ // "primitives" (see https://github.com/serpro69/kotlin-faker/issues/204 )
401
+ val charList: List <Char >,
402
+ val intSet: Set <Int >,
403
+ val boolMap: Map <Boolean , Byte >,
404
+ // enums (see https://github.com/serpro69/kotlin-faker/issues/204 )
405
+ val enumList: List <TestEnum >,
406
+ val enumSet: Set <TestEnum >,
407
+ val enumMap: Map <TestEnum , TestEnum >,
398
408
)
399
409
400
410
it("should generate Collections with default size 1") {
401
411
val testClass = randomProvider.randomClassInstance<TestClass >()
402
412
testClass.list shouldHaveSize 1
403
413
testClass.set shouldHaveSize 1
404
414
testClass.map shouldHaveSize 1
415
+ testClass.charList shouldHaveSize 1
416
+ testClass.intSet shouldHaveSize 1
417
+ testClass.boolMap shouldHaveSize 1
418
+ testClass.enumList shouldHaveSize 1
419
+ testClass.enumSet shouldHaveSize 1
420
+ testClass.enumMap shouldHaveSize 1
405
421
}
406
422
407
423
it("should generate Collections with pre-configured size") {
@@ -411,14 +427,31 @@ class RandomClassProviderTest : DescribeSpec({
411
427
testClass.list shouldHaveSize 10
412
428
testClass.set shouldHaveSize 10
413
429
testClass.map shouldHaveSize 10
430
+ testClass.charList shouldHaveSize 10
431
+ testClass.intSet shouldHaveSize 10
432
+ // boolean-key based map can only have 1 or 2 entries, depending on the randomness of the generated key
433
+ testClass.boolMap.size shouldBeInRange 1 ..2
434
+ testClass.enumList shouldHaveSize 10
435
+ // we only have 3 enum classes, so a set can't have more values total than that
436
+ testClass.enumSet.size shouldBeInRange 1 ..3
437
+ // enum-key based map can only have up to 3 entries in this case, depending on the randomness of the generated key
438
+ testClass.enumMap.size shouldBeInRange 1 ..3
414
439
}
415
440
it("should generate Collections with pre-configured type generation") {
416
441
val testClass = randomProvider.randomClassInstance<TestClass > {
417
442
typeGenerator<List <Foo >> { listOf() }
443
+ typeGenerator<List <Char >> { listOf() }
444
+ typeGenerator<List <TestEnum >> { listOf() }
418
445
}
419
446
testClass.list shouldHaveSize 0
420
447
testClass.set shouldHaveSize 1
421
448
testClass.map shouldHaveSize 1
449
+ testClass.charList shouldHaveSize 0
450
+ testClass.intSet shouldHaveSize 1
451
+ testClass.boolMap shouldHaveSize 1
452
+ testClass.enumList shouldHaveSize 0
453
+ testClass.enumSet shouldHaveSize 1
454
+ testClass.enumMap shouldHaveSize 1
422
455
}
423
456
}
424
457
@@ -464,6 +497,16 @@ class RandomClassProviderTest : DescribeSpec({
464
497
}
465
498
}
466
499
500
+ describe("an inner class") {
501
+ it("should throw exception when generated directly") {
502
+ assertThrows<UnsupportedOperationException > { randomProvider.randomClassInstance<Go .BuildNum >() }
503
+ }
504
+ it("should throw exception when included as constructor parameter") {
505
+ class Test (val goBuild: Go .BuildNum )
506
+ assertThrows<UnsupportedOperationException > { randomProvider.randomClassInstance<Test >() }
507
+ }
508
+ }
509
+
467
510
describe("RandomClassProvider configuration") {
468
511
class Foo (val int: Int )
469
512
class Bar (val foo: Foo )
@@ -724,12 +767,19 @@ enum class TestEnum {
724
767
725
768
@Suppress(" CanSealedSubClassBeObject" , " unused" )
726
769
sealed class TestSealedCls {
727
- object Kotlin : TestSealedCls()
770
+ data object Kotlin : TestSealedCls ()
728
771
class Java : TestSealedCls ()
729
772
}
730
773
731
774
@Suppress(" unused" )
732
- class Go (val name : String ) : TestSealedCls()
775
+ class Go (val name : String ) : TestSealedCls() {
776
+
777
+ class Version (val ver : String )
778
+
779
+ inner class BuildNum (v : Version , i : Int ) {
780
+ val innerVersion = " ${v.ver} +$i "
781
+ }
782
+ }
733
783
734
784
object TestObject
735
785
0 commit comments