@@ -6,39 +6,99 @@ import dev.appoutlet.some.core.TypeResolver
66import kotlin.random.Random
77import kotlin.reflect.typeOf
88
9+ /* *
10+ * Fixture generator configured with a resolver chain and shared random source.
11+ *
12+ * Instances are created by [someSetup] and can be reused to generate multiple values with the same configuration.
13+ *
14+ * @param resolvers Ordered resolver list used to generate values.
15+ * @param random Random source shared by resolvers created for this instance.
16+ * @param config Configuration used by this generator.
17+ */
918class Some (
1019 val resolvers : List <TypeResolver >,
1120 val random : Random ,
1221 val config : SomeConfig
1322) {
23+ /* *
24+ * Generates a fixture value of type [T] using this instance's configuration.
25+ *
26+ * @param T Type to generate.
27+ * @return Generated value of type [T].
28+ */
1429 @Suppress(" MemberNameEqualsClassName" )
1530 inline fun <reified T > some (): T {
1631 val session = ResolverChain (resolvers, config.nullableStrategy)
1732 return session.resolve(typeOf<T >()) as T
1833 }
1934
35+ /* *
36+ * Generates a fixture value of type [T].
37+ *
38+ * Enables concise usage such as `some<User>()` when `some` is a [Some] instance.
39+ *
40+ * @param T Type to generate.
41+ * @return Generated value of type [T].
42+ */
2043 inline operator fun <reified T > invoke (): T = some()
2144
45+ /* *
46+ * Generates a fixture value of type [T] with per-call configuration overrides.
47+ *
48+ * Overrides are applied to a copy of this instance's configuration and do not mutate the original [Some].
49+ *
50+ * @param T Type to generate.
51+ * @param config Configuration overrides for this call.
52+ * @return Generated value of type [T].
53+ */
2254 inline operator fun <reified T > invoke (noinline config : SomeConfig .() -> Unit = {}): T {
2355 val aggregatedConfig = this .config.copy().apply (config)
2456 return Some (aggregatedConfig.buildResolvers(random), random, aggregatedConfig).some()
2557 }
2658}
2759
60+ /* *
61+ * Creates a reusable [Some] generator.
62+ *
63+ * Use this when multiple fixtures should share the same configuration and random source.
64+ *
65+ * @param config Configuration applied to the created generator.
66+ * @return A configured [Some] instance.
67+ */
2868fun someSetup (config : SomeConfig .() -> Unit = {}): Some {
2969 val someConfig = SomeConfig ().apply (config)
3070 val random = someConfig.buildRandom()
3171 return Some (someConfig.buildResolvers(random), random, someConfig)
3272}
3373
74+ /* *
75+ * Lazily-created default configuration used by top-level [some].
76+ */
3477val defaultConfig: SomeConfig by lazy { SomeConfig () }
78+
79+ /* *
80+ * Lazily-created default resolver chain used by top-level [some].
81+ */
3582val defaultResolvers: List <TypeResolver > by lazy { defaultConfig.buildResolvers() }
3683
84+ /* *
85+ * Generates a fixture value using the default configuration.
86+ *
87+ * @param T Type to generate.
88+ * @return Generated value of type [T].
89+ */
3790inline fun <reified T > some (): T {
3891 val session = ResolverChain (defaultResolvers, defaultConfig.nullableStrategy)
3992 return session.resolve(typeOf<T >()) as T
4093}
4194
95+ /* *
96+ * Generates a fixture value using one-off configuration overrides.
97+ *
98+ * @param T Type to generate.
99+ * @param config Configuration applied only to this generation call.
100+ * @return Generated value of type [T].
101+ */
42102inline fun <reified T > some (noinline config : SomeConfig .() -> Unit = {}): T {
43103 val someSetup = someSetup(config)
44104 return someSetup.some<T >()
0 commit comments