Skip to content

Apply Kt ArbitraryBuilder changes into ArbitrayBuilderCandidateListExtensions #1258

@chanhyeong

Description

@chanhyeong

Describe the feature you request

I've used ArbitraryBuilderGroup like below.

class UserBuilderGroup : ArbitraryBuilderGroup {
    override fun generateCandidateList(): ArbitraryBuilderCandidateList {
        return ArbitraryBuilderCandidateList.create()
            .add(::user)
    }

    private fun user(builder: ArbitraryBuilder<User>) =
        builder
            .setExp(User::gender, Arbitraries.of("f", "m"))

    ...
}

There are ide warnings for using deprecated method setExp. (related #850)
Because the method ArbitraryBuilderCandidateList.add in ArbitraryBuilderCandidateListExtensions receive ArbitraryBuilder type.

Image

(Optional): Suggest A Solution

Deprecate the existing one and add another method.

@Deprecated(
    message = "Extension function is deprecated. Use the method below.",
    replaceWith = ReplaceWith("addKt(builder)")
)
inline fun <reified T : Any?> ArbitraryBuilderCandidateList.add(
    noinline builder: (ArbitraryBuilder<T>) -> ArbitraryBuilder<T>
): ArbitraryBuilderCandidateList =
    this.add(
        ArbitraryBuilderCandidateFactory.of(object : TypeReference<T>() {})
            .builder(builder)
    )

inline fun <reified T : Any?> ArbitraryBuilderCandidateList.addKt(
    noinline builder: (KotlinTypeDefaultArbitraryBuilder<T>) -> KotlinTypeDefaultArbitraryBuilder<T>
): ArbitraryBuilderCandidateList =
    this.add(
        // add new factory method in ArbitraryBuilderCandidateFactory as well.
        ArbitraryBuilderCandidateFactory.of(object : TypeReference<T>() {}) { fixtureMonkey, _ ->
            fixtureMonkey.giveMeKotlinBuilder<T>()
        }.builder {
            return@builder builder.invoke(it as KotlinTypeDefaultArbitraryBuilder<T>)
        }
    )

If the feature request is approved, would you be willing to submit a PR?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions