|
| 1 | +module ComplexGenericTest |
| 2 | + |
| 3 | +open Xunit |
| 4 | +open Swensen.Unquote |
| 5 | +open Hedgehog |
| 6 | + |
| 7 | +// A type with complex generic parameter repetition: <A, A, B, C, A, A, D> |
| 8 | +type ComplexType<'A, 'B, 'C, 'D, 'E, 'F, 'G> = { |
| 9 | + First: 'A |
| 10 | + Second: 'B |
| 11 | + Third: 'C |
| 12 | + Fifth: 'E |
| 13 | + Fourth: 'D |
| 14 | + Sixth: 'F |
| 15 | + Seventh: 'G |
| 16 | +} |
| 17 | + |
| 18 | +type ComplexGenerators = |
| 19 | + // Method with pattern: method has <A, B, C, D> but type uses <A, A, B, C, A, A, D> |
| 20 | + static member Complex<'A, 'B, 'C, 'D>( |
| 21 | + genA: Gen<'A>, |
| 22 | + genC: Gen<'C>, |
| 23 | + genB: Gen<'B>, |
| 24 | + genD: Gen<'D>) : Gen<ComplexType<'A, 'A, 'B, 'C, 'A, 'A, 'D>> = |
| 25 | + gen { |
| 26 | + let! a = genA |
| 27 | + let! b = genB |
| 28 | + let! c = genC |
| 29 | + let! d = genD |
| 30 | + return { |
| 31 | + First = a |
| 32 | + Second = a |
| 33 | + Third = b |
| 34 | + Fourth = c |
| 35 | + Fifth = a |
| 36 | + Sixth = a |
| 37 | + Seventh = d |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | +[<Fact>] |
| 42 | +let ``Should handle complex generic parameter repetition pattern``() = |
| 43 | + let config = |
| 44 | + GenX.defaults |
| 45 | + |> AutoGenConfig.addGenerators<ComplexGenerators> |
| 46 | + |
| 47 | + // Generate ComplexType<int, int, string, bool, int, int, float> |
| 48 | + // Method is Complex<int, string, bool, float> |
| 49 | + let gen = GenX.autoWith<ComplexType<int, int, string, bool, int, int, float>> config |
| 50 | + let sample = Gen.sample 0 1 gen |> Seq.head |
| 51 | + |
| 52 | + // Verify the structure is correct |
| 53 | + test <@ sample.First = sample.Second @> // Both should be the same 'A value |
| 54 | + test <@ sample.First = sample.Fifth @> // All 'A positions should be the same |
| 55 | + test <@ sample.Second = sample.Sixth @> |
| 56 | + test <@ sample.Third.GetType() = typeof<string> @> |
| 57 | + test <@ sample.Fourth.GetType() = typeof<bool> @> |
| 58 | + test <@ sample.Seventh.GetType() = typeof<float> @> |
| 59 | + |
| 60 | +// Better test with specific verifiable values |
| 61 | +type VerifiableGenerators = |
| 62 | + static member VerifiableComplex<'A, 'B, 'C, 'D>( |
| 63 | + genA: Gen<'A>, |
| 64 | + genC: Gen<'C>, |
| 65 | + genB: Gen<'B>, |
| 66 | + genD: Gen<'D>) : Gen<ComplexType<'A, 'A, 'B, 'C, 'A, 'A, 'D>> = |
| 67 | + gen { |
| 68 | + let! a = genA |
| 69 | + let! b = genB |
| 70 | + let! c = genC |
| 71 | + let! d = genD |
| 72 | + return { |
| 73 | + First = a |
| 74 | + Second = a |
| 75 | + Third = b |
| 76 | + Fourth = c |
| 77 | + Fifth = a |
| 78 | + Sixth = a |
| 79 | + Seventh = d |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +// Specific constant generators to verify correct parameter mapping |
| 84 | +type SpecificGenerators = |
| 85 | + static member Int() = Gen.constant 42 |
| 86 | + static member String() = Gen.constant "test" |
| 87 | + static member Bool() = Gen.constant true |
| 88 | + static member Float() = Gen.constant 3.14 |
| 89 | + |
| 90 | +[<Fact>] |
| 91 | +let ``Should map parameters correctly with swapped parameter order``() = |
| 92 | + let config = |
| 93 | + GenX.defaults |
| 94 | + |> AutoGenConfig.addGenerators<SpecificGenerators> |
| 95 | + |> AutoGenConfig.addGenerators<VerifiableGenerators> |
| 96 | + |
| 97 | + let gen = GenX.autoWith<ComplexType<int, int, string, bool, int, int, float>> config |
| 98 | + let sample = Gen.sample 0 1 gen |> Seq.head |
| 99 | + |
| 100 | + // With swapped parameters (genA, genC, genB, genD), the mapping should be: |
| 101 | + // 'A -> int (42) goes to positions: First, Second, Fifth, Sixth |
| 102 | + // 'B -> string ("test") goes to position: Third |
| 103 | + // 'C -> bool (true) goes to position: Fourth |
| 104 | + // 'D -> float (3.14) goes to position: Seventh |
| 105 | + |
| 106 | + test <@ sample.First = 42 @> |
| 107 | + test <@ sample.Second = 42 @> |
| 108 | + test <@ sample.Third = "test" @> |
| 109 | + test <@ sample.Fourth = true @> |
| 110 | + test <@ sample.Fifth = 42 @> |
| 111 | + test <@ sample.Sixth = 42 @> |
| 112 | + test <@ sample.Seventh = 3.14 @> |
0 commit comments