44module Formatting (tests ) where
55
66import Control.Exception
7- import Data.Aeson (defaultOptions )
7+ import Data.Aeson (SumEncoding ( UntaggedValue ), defaultOptions , sumEncoding , tagSingleConstructors )
88import Data.Aeson.TypeScript.TH
99import Data.Proxy
1010import Data.String.Interpolate
@@ -17,9 +17,14 @@ $(deriveTypeScript defaultOptions ''D)
1717data D2 = S2 | F2 deriving (Eq , Show )
1818$ (deriveTypeScript defaultOptions ''D2 )
1919
20+ -- A.encode U --> "[]"
2021data Unit = U deriving (Eq , Show )
2122$ (deriveTypeScript defaultOptions ''Unit)
2223
24+ -- A.encode UTagSingle --> "\"UTagSingle\""
25+ data UnitTagSingle = UTagSingle deriving (Eq , Show )
26+ $ (deriveTypeScript (defaultOptions { tagSingleConstructors = True , sumEncoding = UntaggedValue }) ''UnitTagSingle)
27+
2328data PrimeInType' = PrimeInType
2429$ (deriveTypeScript defaultOptions ''PrimeInType')
2530
@@ -56,25 +61,33 @@ tests = describe "Formatting" $ do
5661 describe " and the Enum format option is set" $ do
5762 it " should generate a TS Enum" $
5863 formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @ D Proxy ) `shouldBe`
59- [i |enum D { S, F }|]
64+ [i |enum D { S="S" , F="F" }|]
6065
6166 it " should generate a TS Enum with multiple" $
6267 formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @ D Proxy <> getTypeScriptDeclarations @ D2 Proxy ) `shouldBe`
63- [__i |enum D { S, F }
68+ [__i |enum D { S="S" , F="F" }
6469
65- enum D2 { S2, F2 }|]
70+ enum D2 { S2="S2" , F2="F2" }|]
6671
67- it " should generate a TS Enum from unit " $
72+ it " should generate a normal type from Unit, singe tagSingleConstructors=False by default " $
6873 formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @ Unit Proxy ) `shouldBe`
6974 [__i |type Unit = IU;
7075
7176 type IU = void[];|]
7277
73- describe " and the EnumWithType format option is set" $
78+ it " should generate a suitable enum from UnitTagSingle" $
79+ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @ UnitTagSingle Proxy ) `shouldBe`
80+ [__i |enum UnitTagSingle { UTagSingle="UTagSingle" }|]
81+
82+ describe " and the EnumWithType format option is set" $ do
7483 it " should generate a TS Enum with a type declaration" $
7584 formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @ D Proxy ) `shouldBe`
7685 [i |enum DEnum { S="S", F="F" }\n\ntype D = keyof typeof DEnum;|]
7786
87+ it " should also work for UnitTagSingle" $
88+ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @ UnitTagSingle Proxy ) `shouldBe`
89+ [i |enum UnitTagSingleEnum { UTagSingle="UTagSingle" }\n\ntype UnitTagSingle = keyof typeof UnitTagSingleEnum;|]
90+
7891 describe " when the name has an apostrophe" $ do
7992 describe " in the type" $ do
8093 it " throws an error" $ do
0 commit comments