You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RandomClassProvider has special handling for types like primitives and enums that don't have constructors. However, this special handling is only applied when mapping over direct constructor parameters. This means that constructor-less classes don't get built correctly if they are built under other circumstances, such as when they are included in a collection. For example:
packagecom.mr.reproducerimportio.github.serpro69.kfaker.Fakerimportorg.junit.jupiter.api.Testdata classWorks(valcase:AnEnum)
data classDoesNotWork(valcase:List<AnEnum>)
enumclassAnEnum {
FOO,
BAR
}
classTestCases {
@Test
fun`special handling occurs`() {
Faker().randomProvider.randomClassInstance<Works>()
}
@Test
fun`special handling does not occur`() {
Faker().randomProvider.randomClassInstance<DoesNotWork>()
}
}
Although I provide one specific way to reproduce this issue, anything that calls randomClassInstance can hit this bug. I believe this class of error could be handled for all cases by checking for constructorless cases at the top of randomClassInstance, but I haven't studied this question in detail.
If you're open to reviewing a pull request I'd be happy to submit one, but in either case thank you for your work on this project!
The text was updated successfully, but these errors were encountered:
Yes, you're correct, the above case is broken. This is because an enum doesn't have a constructor as such, so when an enum is a type of a collection - it will fail, particularly in this part
?:throwNoSuchElementException("No suitable constructor or predefined instance found for $this")
The fix should be pretty simple I think. For example, if no constructors are found, we can check if the receiver is an enum or not, and then call randomEnumOrNull on the receiver.
I'm always open to PRs that fix issues :) If you want do that - please feel free to open a PR. Otherwise I can take a look at fixing this myself. Just let me know how you want to proceed.
RandomClassProvider
has special handling for types like primitives and enums that don't have constructors. However, this special handling is only applied when mapping over direct constructor parameters. This means that constructor-less classes don't get built correctly if they are built under other circumstances, such as when they are included in a collection. For example:Although I provide one specific way to reproduce this issue, anything that calls
randomClassInstance
can hit this bug. I believe this class of error could be handled for all cases by checking for constructorless cases at the top ofrandomClassInstance
, but I haven't studied this question in detail.If you're open to reviewing a pull request I'd be happy to submit one, but in either case thank you for your work on this project!
The text was updated successfully, but these errors were encountered: