Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing property with private/protected setter #94

Open
djkeh opened this issue Apr 25, 2023 · 1 comment
Open

Customizing property with private/protected setter #94

djkeh opened this issue Apr 25, 2023 · 1 comment

Comments

@djkeh
Copy link

djkeh commented Apr 25, 2023

Consider we have a class to create a fixture like this:

class MyClass {
    var name: String = "initial value"
        protected set
}

when we do:

kotlinFixture {
    property(MyClass::name) { "test" }
}

will throw an exception like this:

java.lang.UnsupportedOperationException: Unable to handle MyClass
Unable to resolve class MyClass
    Unable to populate class MyClass
    Unable to instantiate class MyClass using factory methods

    at com.appmattus.kotlinfixture.Fixture.create(KotlinFixture.kt:85)

because the proeprty name doesn't provide public setter.
How do I customize that field?

@mattmook
Copy link
Member

This is somewhat linked to issue #90

There are effectively 2 issues happening here.

Firstly, there's some validation that occurs that won't let you define that property override. It does this because MyClass::name is not a mutable property and so it checks to see if it appears in the constructor, as that would then be the only way for that override to work. Of course the property doesn't exist in the constructor so it fails because of the validation exception.

Secondly, if I remove the validation, the object will be created but the value will not be overridden as currently the overrides only work on public setters or constructor parameters.

Thinking about the other open issue (#90) which relates to latent vars I realised that there's no way to easily ignore property setting; and likewise customise properties that would otherwise not be set which is what you want here. This is something I'm currently looking at.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants