Skip to content

Commit baa41bb

Browse files
authored
Add collection element and map k/v type gen for random class instance (#243)
1 parent 20c85c5 commit baa41bb

File tree

7 files changed

+321
-35
lines changed

7 files changed

+321
-35
lines changed

.github/workflows/codeql.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ jobs:
5555

5656
# Initializes the CodeQL tools for scanning.
5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v3
58+
uses: github/codeql-action/init@v3.25.6
5959
with:
60+
tools: https://github.com/github/codeql-action/releases/download/codeql-bundle-v2.17.3/codeql-bundle-linux64.tar.gz
6061
languages: ${{ matrix.language }}
6162
build-mode: ${{ matrix.build-mode }}
6263
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -82,6 +83,6 @@ jobs:
8283
exit 1
8384
8485
- name: Perform CodeQL Analysis
85-
uses: github/codeql-action/analyze@v3
86+
uses: github/codeql-action/analyze@v3.25.6
8687
with:
8788
category: "/language:${{matrix.language}}"

core/api/core.api

+11-5
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,20 @@ public final class io/github/serpro69/kfaker/provider/misc/FallbackStrategy : ja
659659
}
660660

661661
public final class io/github/serpro69/kfaker/provider/misc/ParameterInfo {
662-
public fun <init> (ILjava/lang/String;ZZ)V
662+
public fun <init> (ILjava/lang/String;ZZLkotlin/reflect/KType;Lkotlin/reflect/KParameter$Kind;)V
663663
public final fun component1 ()I
664664
public final fun component2 ()Ljava/lang/String;
665665
public final fun component3 ()Z
666666
public final fun component4 ()Z
667-
public final fun copy (ILjava/lang/String;ZZ)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
668-
public static synthetic fun copy$default (Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;ILjava/lang/String;ZZILjava/lang/Object;)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
667+
public final fun component5 ()Lkotlin/reflect/KType;
668+
public final fun component6 ()Lkotlin/reflect/KParameter$Kind;
669+
public final fun copy (ILjava/lang/String;ZZLkotlin/reflect/KType;Lkotlin/reflect/KParameter$Kind;)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
670+
public static synthetic fun copy$default (Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;ILjava/lang/String;ZZLkotlin/reflect/KType;Lkotlin/reflect/KParameter$Kind;ILjava/lang/Object;)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
669671
public fun equals (Ljava/lang/Object;)Z
670672
public final fun getIndex ()I
673+
public final fun getKind ()Lkotlin/reflect/KParameter$Kind;
671674
public final fun getName ()Ljava/lang/String;
675+
public final fun getType ()Lkotlin/reflect/KType;
672676
public fun hashCode ()I
673677
public final fun isOptional ()Z
674678
public final fun isVararg ()Z
@@ -744,13 +748,15 @@ public final class io/github/serpro69/kfaker/provider/misc/RandomProvider$Key :
744748

745749
public final class io/github/serpro69/kfaker/provider/misc/RandomProviderConfig {
746750
public fun <init> ()V
751+
public final fun getCollectionElementTypeGenerators ()Ljava/util/HashMap;
747752
public final fun getCollectionsSize ()I
748753
public final fun getConstructorFilterStrategy ()Lio/github/serpro69/kfaker/provider/misc/ConstructorFilterStrategy;
749754
public final fun getConstructorParamSize ()I
750755
public final fun getFallbackStrategy ()Lio/github/serpro69/kfaker/provider/misc/FallbackStrategy;
756+
public final fun getMapEntriesTypeGenerators ()Lkotlin/Pair;
751757
public final fun getNamedParameterGenerators ()Ljava/util/Map;
752-
public final fun getNullableGenerators ()Ljava/util/Map;
753-
public final fun getPredefinedGenerators ()Ljava/util/Map;
758+
public final fun getNullableGenerators ()Ljava/util/HashMap;
759+
public final fun getPredefinedGenerators ()Ljava/util/HashMap;
754760
public final fun setCollectionsSize (I)V
755761
public final fun setConstructorFilterStrategy (Lio/github/serpro69/kfaker/provider/misc/ConstructorFilterStrategy;)V
756762
public final fun setConstructorParamSize (I)V

core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt

+45
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.kotest.matchers.shouldBe
1010
import org.junit.jupiter.api.Assertions.assertEquals
1111
import org.junit.jupiter.api.Assertions.assertNotEquals
1212
import java.util.*
13+
import kotlin.reflect.KClass
1314

1415
class Extras : DescribeSpec({
1516
val faker = Faker()
@@ -44,6 +45,49 @@ class Extras : DescribeSpec({
4445
assertEquals(baz.uuid, UUID.fromString("00000000-0000-0000-0000-000000000000"))
4546
assertEquals(baz.relatedUuid, UUID.fromString("11111111-1111-1111-1111-111111111111"))
4647
}
48+
49+
context("collection element generation") {
50+
it("should generate pre-configured collection elements for constructor params") {
51+
// START extras_random_instance_sixteen
52+
fun randomListString() = "list"
53+
fun randomString() = "string"
54+
55+
class Baz(val list: List<String>, val set: Set<String>)
56+
57+
val baz: Baz = faker.randomClass.randomClassInstance {
58+
collectionElementTypeGenerator<String> {
59+
// customize generators for different collection types
60+
if ((it.type.classifier as KClass<*>) == List::class) {
61+
// generate random string elements for parameters of List<String> type
62+
randomListString()
63+
} else {
64+
// generate random string elements for parameters of Set type
65+
randomString()
66+
}
67+
}
68+
}
69+
// END extras_random_instance_sixteen
70+
71+
assertEquals(baz.list.all { it == "list" }, true)
72+
assertEquals(baz.set.all { it == "string" }, true)
73+
}
74+
75+
it("should generate pre-configured map key/value pairs for constructor params") {
76+
fun randomKey() = "key"
77+
fun randomValue() = "value"
78+
// START extras_random_instance_seventeen
79+
class Baz(val map: Map<String, String>)
80+
81+
val baz: Baz = faker.randomClass.randomClassInstance {
82+
mapEntryKeyTypeGenerator { randomKey() }
83+
mapEntryValueTypeGenerator { randomValue() }
84+
}
85+
// END extras_random_instance_seventeen
86+
87+
assertEquals(baz.map.keys.all { it == "key" }, true)
88+
assertEquals(baz.map.values.all { it == "value" }, true)
89+
}
90+
}
4791
}
4892

4993
context("configurable number of constructor args") {
@@ -274,6 +318,7 @@ class Extras : DescribeSpec({
274318
// END extras_random_instance_fifteen
275319
}
276320
}
321+
277322
}
278323

279324
describe("Random Everything") {
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
11
package io.github.serpro69.kfaker.provider.misc
22

3+
import kotlin.reflect.KCallable
34
import kotlin.reflect.KParameter
5+
import kotlin.reflect.KType
46

57
/**
68
* Provides additional information about Class parameter to custom defined generators.
79
* The reason why KParameter is not used is that you will want to provide
810
* additional information about parameter that is not available in KParameter class.
11+
*
12+
* @property index 0-based index of this parameter in the parameter list of its containing callable.
13+
*
14+
* @property name Name of this parameter as it was declared in the source code,
15+
* or `null` if the parameter has no name or its name is not available at runtime.
16+
* Examples of nameless parameters include `this` instance for member functions,
17+
* extension receiver for extension functions or properties, parameters of Java methods
18+
* compiled without the debug information, and others.
19+
*
20+
* @property type Type of this parameter. For a `vararg` parameter, this is the type of the corresponding array,
21+
* not the individual element.
22+
*
23+
* @property kind Kind of this parameter.
24+
* Kind represents a particular position of the parameter declaration in the source code,
25+
* such as an instance, an extension receiver parameter or a value parameter.
26+
*
27+
* @property isOptional
28+
* `true` if this parameter is optional and can be omitted when making a call via [KCallable.callBy], or `false` otherwise.
29+
*
30+
* A parameter is optional in any of the two cases:
31+
* 1. The default value is provided at the declaration of this parameter.
32+
* 2. The parameter is declared in a member function and one of the corresponding parameters in the super functions is optional.
33+
*
34+
* @property isVararg
35+
* `true` if this parameter is `vararg`.
36+
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs)
37+
* for more information.
938
*/
1039
data class ParameterInfo(
1140
val index: Int,
1241
val name: String,
1342
val isOptional: Boolean,
14-
val isVararg: Boolean
43+
val isVararg: Boolean,
44+
val type: KType,
45+
val kind: KParameter.Kind,
1546
)
1647

1748
/**
1849
* Extension function that maps KParameter to ParameterInfo dataclass.
1950
*/
20-
internal fun KParameter.toParameterInfo() = ParameterInfo(
21-
index = index,
22-
name = name.toString(),
23-
isOptional = isOptional,
24-
isVararg = isVararg
25-
)
51+
internal fun KParameter.toParameterInfo() =
52+
ParameterInfo(
53+
index = index,
54+
name = name.toString(),
55+
isOptional = isOptional,
56+
isVararg = isVararg,
57+
type = type,
58+
kind = kind,
59+
)

0 commit comments

Comments
 (0)