|
| 1 | +namespace Hedgehog.Stateful |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Reflection |
| 5 | +open System.Collections.Generic |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Utilities for formatting state with resolved variable values for display in test failures. |
| 9 | +/// </summary> |
| 10 | +[<RequireQualifiedAccess>] |
| 11 | +module internal StateFormatter = |
| 12 | + |
| 13 | + /// Check if a type is Var<T> |
| 14 | + let private isVarType (t: Type) = |
| 15 | + t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<Var<_>> |
| 16 | + |
| 17 | + /// Mutate a Var<T> object to set its ResolvedValue field for counterexample formatting |
| 18 | + let private mutateVarForDisplay (env: Env) (varObj: obj) : unit = |
| 19 | + if varObj = null then () |
| 20 | + else |
| 21 | + try |
| 22 | + // Call SetResolvedValue method using reflection |
| 23 | + let setMethod = varObj.GetType().GetMethod("SetResolvedValue", BindingFlags.NonPublic ||| BindingFlags.Instance) |
| 24 | + setMethod.Invoke(varObj, [| env |]) |> ignore |
| 25 | + with |
| 26 | + | _ -> () // If resolution fails, leave the var as-is |
| 27 | + |
| 28 | + /// Recursively walk an object and mutate all Var<T> fields/properties for display |
| 29 | + let rec private mutateVarsInObject (env: Env) (visited: HashSet<obj>) (obj: obj) : unit = |
| 30 | + if obj = null then () |
| 31 | + else |
| 32 | + let objType = obj.GetType() |
| 33 | + |
| 34 | + // Avoid infinite loops on circular references |
| 35 | + if visited.Contains(obj) then () |
| 36 | + else |
| 37 | + visited.Add(obj) |> ignore |
| 38 | + |
| 39 | + // Check if this is a Var<T> itself |
| 40 | + if isVarType objType then |
| 41 | + mutateVarForDisplay env obj |
| 42 | + |
| 43 | + // Handle primitive types and strings - no traversal needed |
| 44 | + elif objType.IsPrimitive || objType = typeof<string> || objType.IsEnum then |
| 45 | + () |
| 46 | + |
| 47 | + // Handle collections (including arrays) and other types |
| 48 | + elif obj :? System.Collections.IEnumerable then |
| 49 | + let enumerable = obj :?> System.Collections.IEnumerable |
| 50 | + for element in enumerable do |
| 51 | + if element <> null then |
| 52 | + mutateVarsInObject env visited element |
| 53 | + |
| 54 | + // Also traverse fields/properties in case it's a complex collection |
| 55 | + traverseFieldsAndProperties env visited obj objType |
| 56 | + |
| 57 | + // Handle types with fields/properties |
| 58 | + else |
| 59 | + traverseFieldsAndProperties env visited obj objType |
| 60 | + |
| 61 | + and private traverseFieldsAndProperties (env: Env) (visited: HashSet<obj>) (obj: obj) (objType: Type) : unit = |
| 62 | + try |
| 63 | + // Traverse all readable properties |
| 64 | + let properties = |
| 65 | + objType.GetProperties(BindingFlags.Public ||| BindingFlags.Instance) |
| 66 | + |> Array.filter _.CanRead |
| 67 | + |
| 68 | + for prop in properties do |
| 69 | + try |
| 70 | + let value = prop.GetValue(obj) |
| 71 | + if value <> null then |
| 72 | + mutateVarsInObject env visited value |
| 73 | + with |
| 74 | + | _ -> () // Skip properties that can't be read |
| 75 | + |
| 76 | + // Traverse all fields |
| 77 | + let fields = |
| 78 | + objType.GetFields(BindingFlags.Public ||| BindingFlags.Instance) |
| 79 | + |
| 80 | + for field in fields do |
| 81 | + try |
| 82 | + let value = field.GetValue(obj) |
| 83 | + if value <> null then |
| 84 | + mutateVarsInObject env visited value |
| 85 | + with |
| 86 | + | _ -> () // Skip fields that can't be read |
| 87 | + with |
| 88 | + | _ -> () // If anything fails, continue silently |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Format a state object for display by resolving all Var<T> fields to their concrete values. |
| 92 | + /// Mutates Var instances in-place by setting their ResolvedValue field. |
| 93 | + /// This should only be called during counterexample formatting on test failure. |
| 94 | + /// </summary> |
| 95 | + /// <param name="env">The environment containing variable bindings.</param> |
| 96 | + /// <param name="state">The state object to format.</param> |
| 97 | + /// <returns>The same state object with vars mutated for display.</returns> |
| 98 | + let formatForDisplay (env: Env) (state: 'TState) : 'TState = |
| 99 | + let visited = HashSet<obj>(ReferenceEqualityComparer.Instance) |
| 100 | + mutateVarsInObject env visited (box state) |
| 101 | + state |
0 commit comments