From 07d4cc73e056577ed4613fd5c861f676731879af Mon Sep 17 00:00:00 2001 From: macocianradu Date: Wed, 12 Jan 2022 14:41:28 +0200 Subject: [PATCH] added enum pretty printing tests --- RosettaParser.cabal | 1 + resources/Enums/haskellEnum1.hs | 13 +++++++++ resources/Enums/haskellEnum2.hs | 13 +++++++++ resources/Enums/haskellEnum3.hs | 7 +++++ resources/testAll.rosetta | 8 +++--- resources/testFunction.rosetta | 47 ++++++++++++++++++++++++++----- src/PrettyPrinter/Function.hs | 3 ++ src/PrettyPrinter/Type.hs | 2 +- src/Semantic/ExpressionChecker.hs | 2 +- test/PrettyPrinter/EnumSpec.hs | 39 +++++++++++++++++++++++++ 10 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 resources/Enums/haskellEnum1.hs create mode 100644 resources/Enums/haskellEnum2.hs create mode 100644 resources/Enums/haskellEnum3.hs create mode 100644 test/PrettyPrinter/EnumSpec.hs diff --git a/RosettaParser.cabal b/RosettaParser.cabal index 34cf789..dd53c80 100644 --- a/RosettaParser.cabal +++ b/RosettaParser.cabal @@ -79,6 +79,7 @@ test-suite RosettaParser-test Parser.EnumSpec Parser.ExpressionSpec Parser.TypeSpec + PrettyPrinter.EnumSpec Paths_RosettaParser hs-source-dirs: test diff --git a/resources/Enums/haskellEnum1.hs b/resources/Enums/haskellEnum1.hs new file mode 100644 index 0000000..f8aa4b3 --- /dev/null +++ b/resources/Enums/haskellEnum1.hs @@ -0,0 +1,13 @@ +{-The enumerated values to specified the period, e.g. day, week.-} +data PeriodEnum = + {-Day-} + D + {-Month-} + | M + {-Year-} + | Y + +instance Show PeriodEnum where + show D = "day" + show M = "month" + show Y = "year" diff --git a/resources/Enums/haskellEnum2.hs b/resources/Enums/haskellEnum2.hs new file mode 100644 index 0000000..dca845a --- /dev/null +++ b/resources/Enums/haskellEnum2.hs @@ -0,0 +1,13 @@ +{-The enumerated values to specified the period, e.g. day, week.-} +data EnumWithoutDisplay = + {-Day-} + D + {-Month-} + | M + {-Year-} + | Y + +instance Show EnumWithoutDisplay where + show D = "D" + show M = "M" + show Y = "Y" diff --git a/resources/Enums/haskellEnum3.hs b/resources/Enums/haskellEnum3.hs new file mode 100644 index 0000000..8a5d33a --- /dev/null +++ b/resources/Enums/haskellEnum3.hs @@ -0,0 +1,7 @@ +data EnumWithoutDescription = + X + | Y + +instance Show EnumWithoutDescription where + show X = "xs" + show Y = "ys" diff --git a/resources/testAll.rosetta b/resources/testAll.rosetta index 78c563a..8a63a41 100644 --- a/resources/testAll.rosetta +++ b/resources/testAll.rosetta @@ -35,9 +35,9 @@ func EquityPriceObservation: <"Function specification for the observation of an func Something: <"asd"> inputs: - equity1 boolean (1..1) - determinationMethod ObservationPrimitive (1..1) + equity1 boolean (1..1) + valuationTime ObservationPrimitive (1..1) output: - valuation ObservationPrimitive (0..1) + valuation ObservationPrimitive (0..*) - assign-output: if True and False then determinationMethod \ No newline at end of file + assign-output: if True and False then valuationTime \ No newline at end of file diff --git a/resources/testFunction.rosetta b/resources/testFunction.rosetta index 6d5ab45..3392049 100644 --- a/resources/testFunction.rosetta +++ b/resources/testFunction.rosetta @@ -1,11 +1,44 @@ func EquityPriceObservation: <"Function specification for the observation of an equity price, based on the attributes of the 'EquityValuation' class."> inputs: - equity Equity (1..1) - valuationDate AdjustableOrRelativeDate (1..1) - valuationTime BusinessCenterTime (0..1) - timeType TimeTypeEnum (0..1) - determinationMethod DeterminationMethodEnum (1..*) + equity int (1..1) + determinationMethod ObservationPrimitive (1..1) output: - observation ObservationPrimitive (1..1) + observation ObservationPrimitive (0..1) - assign-output: if asd exists then var2 \ No newline at end of file + assign-output: if True and False then valuationDate + +func EquityPriceObservation: <"Function specification for the observation of an equity price, based on the attributes of the 'EquityValuation' class."> + inputs: + equity int (1..1) + determinationMethod ObservationPrimitive (1..1) + output: + observation ObservationPrimitive (0..*) + + assign-output: if True and False then valuationDate + +func EquityPriceObservation: <"Function specification for the observation of an equity price, based on the attributes of the 'EquityValuation' class."> + inputs: + equity int (1..1) + determinationMethod ObservationPrimitive (1..1) + output: + observation ObservationPrimitive (1..1) + + assign-output: if True and False then valuationDate + +func EquityPriceObservation: <"Function specification for the observation of an equity price, based on the attributes of the 'EquityValuation' class."> +inputs: + equity int (1..1) + determinationMethod ObservationPrimitive (1..1) +output: + observation ObservationPrimitive (1..*) + +assign-output: if True and False then valuationDate + +func EquityPriceObservation: <"Function specification for the observation of an equity price, based on the attributes of the 'EquityValuation' class."> +inputs: + equity int (1..1) + determinationMethod ObservationPrimitive (1..1) +output: + observation ObservationPrimitive (*..*) + +assign-output: if True and False then valuationDate \ No newline at end of file diff --git a/src/PrettyPrinter/Function.hs b/src/PrettyPrinter/Function.hs index ca0f469..2e90b3d 100644 --- a/src/PrettyPrinter/Function.hs +++ b/src/PrettyPrinter/Function.hs @@ -34,10 +34,13 @@ printExpression (PostfixExp "multiple exists" ex) = "length" <+> printExpression printExpression (PostfixExp "count" ex) = "length" <+> printExpression ex printExpression (PostfixExp name ex) = pretty name <+> printExpression ex -- Equality expressions +-- [a] a all = +-- any <> printExpression (InfixExp "=" ex1 ex2) = printExpression ex1 <+> "==" <+> printExpression ex2 printExpression (InfixExp "<>" ex1 ex2) = printExpression ex1 <+> "/=" <+> printExpression ex2 printExpression (InfixExp "any =" ex1 ex2) = printExpression ex2 <+> "`elem`" <+> printExpression ex1 printExpression (InfixExp "all <>" ex1 ex2) = printExpression ex2 <+> "`notElem`" <+> printExpression ex1 +--printExpression (InfixExp "all =" ex1 ex2) = "all (Eq)" <+> printExpression ex2 <+> printExpression ex1 printExpression (InfixExp "and" ex1 ex2) = printExpression ex1 <+> "&&" <+> printExpression ex2 printExpression (InfixExp "or" ex1 ex2) = printExpression ex1 <+> "||" <+> printExpression ex2 printExpression (InfixExp name ex1 ex2) = printExpression ex1 <+> pretty name <+> printExpression ex2 diff --git a/src/PrettyPrinter/Type.hs b/src/PrettyPrinter/Type.hs index 545a182..5146c59 100644 --- a/src/PrettyPrinter/Type.hs +++ b/src/PrettyPrinter/Type.hs @@ -28,7 +28,7 @@ printAttributes (at : ats) = (printAttribute at <> ",") : printAttributes ats printAttribute :: TypeAttribute -> Doc a printAttribute (MakeTypeAttribute name typ crd description) = printDescription description - (pretty name <+> "::" <+> printCardinality (MakeTypeAttribute name typ crd description)) + (pretty name <+> "::" <+> printCardinality (MakeTypeAttribute name typ crd description)) -- |Converts a Cardinality into a haskell valid Doc printCardinality :: TypeAttribute -> Doc a diff --git a/src/Semantic/ExpressionChecker.hs b/src/Semantic/ExpressionChecker.hs index f2f65fd..6f78c72 100644 --- a/src/Semantic/ExpressionChecker.hs +++ b/src/Semantic/ExpressionChecker.hs @@ -175,7 +175,7 @@ cardinalityIncluded (Bounds (x1, x2)) (Bounds (y1, y2)) -- |Looks in the symbol map for the type of a variable findVarType :: String -> [Symbol] -> Either TypeCheckError (Type, Cardinality) findVarType var [] = Left $ UndefinedVariable var -findVarType x ((Var name typ crd):symbols) +findVarType x ((Var name typ crd):symbols) | x == name = Right (typ, crd) | otherwise = findVarType x symbols findVarType x (_:symbols) = findVarType x symbols \ No newline at end of file diff --git a/test/PrettyPrinter/EnumSpec.hs b/test/PrettyPrinter/EnumSpec.hs new file mode 100644 index 0000000..45052e4 --- /dev/null +++ b/test/PrettyPrinter/EnumSpec.hs @@ -0,0 +1,39 @@ +module PrettyPrinter.EnumSpec where + +import Test.Hspec +import Model.Enum +import PrettyPrinter.Enum + +spec :: Spec +spec = do + describe "Testing enum parsing" $ do + it "[Test 1]" $ do + plainText <- readFile "resources/Enums/haskellEnum1.hs" + printEnum (head enums) `shouldBe` plainText + it "[Test 2]" $ do + plainText <- readFile "resources/Enums/haskellEnum2.hs" + printEnum (enums !! 1) `shouldBe` plainText + it "[Test 3]" $ do + plainText <- readFile "resources/Enums/haskellEnum3.hs" + printEnum (enums !! 2) `shouldBe` plainText + + +enums :: [EnumType] +enums = [ + MakeEnum {enumName = "PeriodEnum", + enumDescription = Just "The enumerated values to specified the period, e.g. day, week.", + enumValues = [MakeEnumValue {enumValueName = "D", enumValueDescription = Just "Day", enumValueDisplayName = Just "day"}, + MakeEnumValue {enumValueName = "M", enumValueDescription = Just "Month", enumValueDisplayName = Just "month"}, + MakeEnumValue {enumValueName = "Y", enumValueDescription = Just "Year", enumValueDisplayName = Just "year"}]}, + + MakeEnum {enumName = "EnumWithoutDisplay", + enumDescription = Just "The enumerated values to specified the period, e.g. day, week.", + enumValues = [MakeEnumValue {enumValueName = "D", enumValueDescription = Just "Day", enumValueDisplayName = Nothing}, + MakeEnumValue {enumValueName = "M", enumValueDescription = Just "Month", enumValueDisplayName = Nothing}, + MakeEnumValue {enumValueName = "Y", enumValueDescription = Just "Year", enumValueDisplayName = Nothing}]}, + + + MakeEnum {enumName = "EnumWithoutDescription", + enumDescription = Nothing, + enumValues = [MakeEnumValue {enumValueName = "X", enumValueDescription = Nothing, enumValueDisplayName = Just "xs"}, + MakeEnumValue {enumValueName = "Y", enumValueDescription = Nothing, enumValueDisplayName = Just "ys"}]}] \ No newline at end of file