diff --git a/src/Data/Aeson/TypeScript/Formatting.hs b/src/Data/Aeson/TypeScript/Formatting.hs index fea6941..9412b53 100644 --- a/src/Data/Aeson/TypeScript/Formatting.hs +++ b/src/Data/Aeson/TypeScript/Formatting.hs @@ -28,7 +28,7 @@ formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVar mainDeclaration = case chooseTypeAlternativesFormat typeAlternativesFormat of Enum -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnum} }|] where - alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names] + alternativesEnum = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names] EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name}Enum { #{alternativesEnumWithType} }#{enumType}|] where alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names] diff --git a/test/Formatting.hs b/test/Formatting.hs index ab3c770..a86f948 100644 --- a/test/Formatting.hs +++ b/test/Formatting.hs @@ -4,7 +4,7 @@ module Formatting (tests) where import Control.Exception -import Data.Aeson (defaultOptions) +import Data.Aeson (SumEncoding(UntaggedValue), defaultOptions, sumEncoding, tagSingleConstructors) import Data.Aeson.TypeScript.TH import Data.Proxy import Data.String.Interpolate @@ -17,9 +17,14 @@ $(deriveTypeScript defaultOptions ''D) data D2 = S2 | F2 deriving (Eq, Show) $(deriveTypeScript defaultOptions ''D2) +-- A.encode U --> "[]" data Unit = U deriving (Eq, Show) $(deriveTypeScript defaultOptions ''Unit) +-- A.encode UTagSingle --> "\"UTagSingle\"" +data UnitTagSingle = UTagSingle deriving (Eq, Show) +$(deriveTypeScript (defaultOptions { tagSingleConstructors = True, sumEncoding = UntaggedValue }) ''UnitTagSingle) + data PrimeInType' = PrimeInType $(deriveTypeScript defaultOptions ''PrimeInType') @@ -56,25 +61,33 @@ tests = describe "Formatting" $ do describe "and the Enum format option is set" $ do it "should generate a TS Enum" $ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @D Proxy) `shouldBe` - [i|enum D { S, F }|] + [i|enum D { S="S", F="F" }|] it "should generate a TS Enum with multiple" $ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @D Proxy <> getTypeScriptDeclarations @D2 Proxy) `shouldBe` - [__i|enum D { S, F } + [__i|enum D { S="S", F="F" } - enum D2 { S2, F2 }|] + enum D2 { S2="S2", F2="F2" }|] - it "should generate a TS Enum from unit" $ + it "should generate a normal type from Unit, singe tagSingleConstructors=False by default" $ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe` [__i|type Unit = IU; type IU = void[];|] - describe "and the EnumWithType format option is set" $ + it "should generate a suitable enum from UnitTagSingle" $ + formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @UnitTagSingle Proxy) `shouldBe` + [__i|enum UnitTagSingle { UTagSingle="UTagSingle" }|] + + describe "and the EnumWithType format option is set" $ do it "should generate a TS Enum with a type declaration" $ formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @D Proxy) `shouldBe` [i|enum DEnum { S="S", F="F" }\n\ntype D = keyof typeof DEnum;|] + it "should also work for UnitTagSingle" $ + formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @UnitTagSingle Proxy) `shouldBe` + [i|enum UnitTagSingleEnum { UTagSingle="UTagSingle" }\n\ntype UnitTagSingle = keyof typeof UnitTagSingleEnum;|] + describe "when the name has an apostrophe" $ do describe "in the type" $ do it "throws an error" $ do