soenneker / soenneker.utils.autobogus

The .NET Autogenerator
https://soenneker.com
MIT License
33 stars 4 forks source link

feat: Support properties that return AsReadOnly instances #247

Closed alexmg closed 1 month ago

alexmg commented 1 month ago

I ran into a situation with a type that returned a read-only instance through a property exposing a collection interface. Unfortunately, this can be done without declaring a return type that accurately indicates the true read-only nature of the collection.

This PR takes into consideration IList or IDictionary instances that are IsReadyOnly when populating collections. I also added unit tests to assert that no exceptions are thrown during the Generate invocation. Because all exceptions are caught and written to the console when populating members, the only option to prove no exception is being thrown in a unit test, was to ensure nothing was written to standard output in addition to checking the collection remained empty.

Props for carrying the torch and creating a viable replacement for AutoBogus. 🙏

soenneker commented 1 month ago

@alexmg Thanks for the PR.

There was an issue with the reflection cache that it wasn't handling IDictionary generics well. I pushed an update so IDictionary<,> doesn't go down the ICollection route like you show here.

Pull latest, and see comments, thanks!

alexmg commented 1 month ago

Tests look good

I added a try-finally block to ensure the standard output is always returned.