From 2e83aa30bfcb143f31875481b8551b983427a5df Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Mon, 21 Apr 2025 17:56:07 +0100 Subject: [PATCH] Add type printing for App (#33) --- WoofWare.Whippet.Fantomas.Test/TestSynType.fs | 38 +++++++++++++++++++ .../WoofWare.Whippet.Fantomas.Test.fsproj | 2 + WoofWare.Whippet.Fantomas/SynType.fs | 3 ++ 3 files changed, 43 insertions(+) create mode 100644 WoofWare.Whippet.Fantomas.Test/TestSynType.fs diff --git a/WoofWare.Whippet.Fantomas.Test/TestSynType.fs b/WoofWare.Whippet.Fantomas.Test/TestSynType.fs new file mode 100644 index 0000000..4e93820 --- /dev/null +++ b/WoofWare.Whippet.Fantomas.Test/TestSynType.fs @@ -0,0 +1,38 @@ +namespace WoofWare.Whippet.Fantomas.Test + +open Fantomas.FCS.Syntax +open Fantomas.FCS.Text +open NUnit.Framework +open FsUnitTyped +open WoofWare.Whippet.Fantomas +open Fantomas.FCS + +[] +module TestSynType = + let typeToStringCases = + [ "string", "string" ; "ResizeArray", "ResizeArray" ] + |> List.map TestCaseData + + [] + let ``Snapshot tests for SynType.toHumanReadableString`` (fsharpTypeString : string, expected : string) = + let parsed, diags = + Parse.parseFile false (SourceText.ofString $"let x : %s{fsharpTypeString} = failwith \"\"") [] + + diags |> shouldBeEmpty + + let (SynModuleOrNamespace (decls = parsed)) = + match parsed with + | ParsedInput.ImplFile parsedImplFileInput -> parsedImplFileInput.Contents.[0] + | ParsedInput.SigFile _ -> failwith "logic error" + + let (SynBinding (expr = parsed)) = + match List.exactlyOne parsed with + | SynModuleDecl.Let (bindings = bindings) -> bindings.[0] + | _ -> failwith "logic error" + + let ty = + match parsed with + | SynExpr.Typed (targetType = targetType) -> targetType + | _ -> failwith $"logic error: %O{parsed}" + + SynType.toHumanReadableString ty |> shouldEqual expected diff --git a/WoofWare.Whippet.Fantomas.Test/WoofWare.Whippet.Fantomas.Test.fsproj b/WoofWare.Whippet.Fantomas.Test/WoofWare.Whippet.Fantomas.Test.fsproj index 1c148c4..9743464 100644 --- a/WoofWare.Whippet.Fantomas.Test/WoofWare.Whippet.Fantomas.Test.fsproj +++ b/WoofWare.Whippet.Fantomas.Test/WoofWare.Whippet.Fantomas.Test.fsproj @@ -9,10 +9,12 @@ + + diff --git a/WoofWare.Whippet.Fantomas/SynType.fs b/WoofWare.Whippet.Fantomas/SynType.fs index 80006df..3f6ee53 100644 --- a/WoofWare.Whippet.Fantomas/SynType.fs +++ b/WoofWare.Whippet.Fantomas/SynType.fs @@ -508,6 +508,9 @@ module SynType = | DateOnly -> "DateOnly" | TimeSpan -> "TimeSpan" | SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) -> ident |> List.map _.idText |> String.concat "." + | SynType.App (ty, _, args, _, _, _, _) -> + let args = args |> Seq.map toHumanReadableString |> String.concat ", " + $"%s{toHumanReadableString ty}<%s{args}>" | ty -> failwithf "could not compute human-readable string for type: %O" ty /// Guess whether the types are equal. We err on the side of saying "no, they're different".