Skip to content

Commit

Permalink
Add collection element and map k/v type gen for random class instance (
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 authored Jun 29, 2024
1 parent 20c85c5 commit baa41bb
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 35 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
uses: github/codeql-action/init@v3.25.6
with:
tools: https://github.com/github/codeql-action/releases/download/codeql-bundle-v2.17.3/codeql-bundle-linux64.tar.gz
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -82,6 +83,6 @@ jobs:
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
uses: github/codeql-action/analyze@v3.25.6
with:
category: "/language:${{matrix.language}}"
16 changes: 11 additions & 5 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,20 @@ public final class io/github/serpro69/kfaker/provider/misc/FallbackStrategy : ja
}

public final class io/github/serpro69/kfaker/provider/misc/ParameterInfo {
public fun <init> (ILjava/lang/String;ZZ)V
public fun <init> (ILjava/lang/String;ZZLkotlin/reflect/KType;Lkotlin/reflect/KParameter$Kind;)V
public final fun component1 ()I
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Z
public final fun component4 ()Z
public final fun copy (ILjava/lang/String;ZZ)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
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;
public final fun component5 ()Lkotlin/reflect/KType;
public final fun component6 ()Lkotlin/reflect/KParameter$Kind;
public final fun copy (ILjava/lang/String;ZZLkotlin/reflect/KType;Lkotlin/reflect/KParameter$Kind;)Lio/github/serpro69/kfaker/provider/misc/ParameterInfo;
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;
public fun equals (Ljava/lang/Object;)Z
public final fun getIndex ()I
public final fun getKind ()Lkotlin/reflect/KParameter$Kind;
public final fun getName ()Ljava/lang/String;
public final fun getType ()Lkotlin/reflect/KType;
public fun hashCode ()I
public final fun isOptional ()Z
public final fun isVararg ()Z
Expand Down Expand Up @@ -744,13 +748,15 @@ public final class io/github/serpro69/kfaker/provider/misc/RandomProvider$Key :

public final class io/github/serpro69/kfaker/provider/misc/RandomProviderConfig {
public fun <init> ()V
public final fun getCollectionElementTypeGenerators ()Ljava/util/HashMap;
public final fun getCollectionsSize ()I
public final fun getConstructorFilterStrategy ()Lio/github/serpro69/kfaker/provider/misc/ConstructorFilterStrategy;
public final fun getConstructorParamSize ()I
public final fun getFallbackStrategy ()Lio/github/serpro69/kfaker/provider/misc/FallbackStrategy;
public final fun getMapEntriesTypeGenerators ()Lkotlin/Pair;
public final fun getNamedParameterGenerators ()Ljava/util/Map;
public final fun getNullableGenerators ()Ljava/util/Map;
public final fun getPredefinedGenerators ()Ljava/util/Map;
public final fun getNullableGenerators ()Ljava/util/HashMap;
public final fun getPredefinedGenerators ()Ljava/util/HashMap;
public final fun setCollectionsSize (I)V
public final fun setConstructorFilterStrategy (Lio/github/serpro69/kfaker/provider/misc/ConstructorFilterStrategy;)V
public final fun setConstructorParamSize (I)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import java.util.*
import kotlin.reflect.KClass

class Extras : DescribeSpec({
val faker = Faker()
Expand Down Expand Up @@ -44,6 +45,49 @@ class Extras : DescribeSpec({
assertEquals(baz.uuid, UUID.fromString("00000000-0000-0000-0000-000000000000"))
assertEquals(baz.relatedUuid, UUID.fromString("11111111-1111-1111-1111-111111111111"))
}

context("collection element generation") {
it("should generate pre-configured collection elements for constructor params") {
// START extras_random_instance_sixteen
fun randomListString() = "list"
fun randomString() = "string"

class Baz(val list: List<String>, val set: Set<String>)

val baz: Baz = faker.randomClass.randomClassInstance {
collectionElementTypeGenerator<String> {
// customize generators for different collection types
if ((it.type.classifier as KClass<*>) == List::class) {
// generate random string elements for parameters of List<String> type
randomListString()
} else {
// generate random string elements for parameters of Set type
randomString()
}
}
}
// END extras_random_instance_sixteen

assertEquals(baz.list.all { it == "list" }, true)
assertEquals(baz.set.all { it == "string" }, true)
}

it("should generate pre-configured map key/value pairs for constructor params") {
fun randomKey() = "key"
fun randomValue() = "value"
// START extras_random_instance_seventeen
class Baz(val map: Map<String, String>)

val baz: Baz = faker.randomClass.randomClassInstance {
mapEntryKeyTypeGenerator { randomKey() }
mapEntryValueTypeGenerator { randomValue() }
}
// END extras_random_instance_seventeen

assertEquals(baz.map.keys.all { it == "key" }, true)
assertEquals(baz.map.values.all { it == "value" }, true)
}
}
}

context("configurable number of constructor args") {
Expand Down Expand Up @@ -274,6 +318,7 @@ class Extras : DescribeSpec({
// END extras_random_instance_fifteen
}
}

}

describe("Random Everything") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
package io.github.serpro69.kfaker.provider.misc

import kotlin.reflect.KCallable
import kotlin.reflect.KParameter
import kotlin.reflect.KType

/**
* Provides additional information about Class parameter to custom defined generators.
* The reason why KParameter is not used is that you will want to provide
* additional information about parameter that is not available in KParameter class.
*
* @property index 0-based index of this parameter in the parameter list of its containing callable.
*
* @property name Name of this parameter as it was declared in the source code,
* or `null` if the parameter has no name or its name is not available at runtime.
* Examples of nameless parameters include `this` instance for member functions,
* extension receiver for extension functions or properties, parameters of Java methods
* compiled without the debug information, and others.
*
* @property type Type of this parameter. For a `vararg` parameter, this is the type of the corresponding array,
* not the individual element.
*
* @property kind Kind of this parameter.
* Kind represents a particular position of the parameter declaration in the source code,
* such as an instance, an extension receiver parameter or a value parameter.
*
* @property isOptional
* `true` if this parameter is optional and can be omitted when making a call via [KCallable.callBy], or `false` otherwise.
*
* A parameter is optional in any of the two cases:
* 1. The default value is provided at the declaration of this parameter.
* 2. The parameter is declared in a member function and one of the corresponding parameters in the super functions is optional.
*
* @property isVararg
* `true` if this parameter is `vararg`.
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs)
* for more information.
*/
data class ParameterInfo(
val index: Int,
val name: String,
val isOptional: Boolean,
val isVararg: Boolean
val isVararg: Boolean,
val type: KType,
val kind: KParameter.Kind,
)

/**
* Extension function that maps KParameter to ParameterInfo dataclass.
*/
internal fun KParameter.toParameterInfo() = ParameterInfo(
index = index,
name = name.toString(),
isOptional = isOptional,
isVararg = isVararg
)
internal fun KParameter.toParameterInfo() =
ParameterInfo(
index = index,
name = name.toString(),
isOptional = isOptional,
isVararg = isVararg,
type = type,
kind = kind,
)
Loading

0 comments on commit baa41bb

Please sign in to comment.