diff --git a/.gitignore b/.gitignore index 174c40b..f3d6ca7 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,3 @@ cabal.project.local~ .HTF/ .ghc.environment.* .idea/ -/test/ diff --git a/test/Parser/EnumSpec.hs b/test/Parser/EnumSpec.hs new file mode 100644 index 0000000..35b5762 --- /dev/null +++ b/test/Parser/EnumSpec.hs @@ -0,0 +1,55 @@ +module Parser.EnumSpec where + +import Test.Hspec +import Model.Enum +import qualified Data.Text as Text +import Text.Megaparsec +import Parser.Enum +import Test.Hspec.Megaparsec + +spec :: Spec +spec = do + describe "Testing enum parsing" $ do + it "[Test 1]" $ do + plainText <- readFile "resources/Enums/testEnum1.rosetta" + parse enumParser "" (Text.pack plainText) `shouldParse` head enums + it "[Test 2]" $ do + plainText <- readFile "resources/Enums/testEnum2.rosetta" + parse enumParser "" (Text.pack plainText) `shouldParse` (enums !! 1) + it "[Test 3]" $ do + plainText <- readFile "resources/Enums/testEnum3.rosetta" + parse enumParser "" (Text.pack plainText) `shouldParse` (enums !! 2) + it "[Test 4]" $ do + plainText <- readFile "resources/Enums/testEnum4.rosetta" + parse enumParser "" (Text.pack plainText) `shouldParse` (enums !! 3) + it "[Test 5]" $ do + plainText <- readFile "resources/Enums/testEnum5.rosetta" + parse enumParser "" `shouldFailOn` Text.pack plainText + it "[Test 6]" $ do + plainText <- readFile "resources/Enums/testEnum6.rosetta" + parse enumParser "" `shouldFailOn` Text.pack 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"}]}, + + MakeEnum {enumName = "Wrong", + enumDescription = Nothing, + enumValues = [MakeEnumValue {enumValueName = "A", enumValueDescription = Just "asd", enumValueDisplayName = Nothing}]}] \ No newline at end of file diff --git a/test/Parser/ExpressionSpec.hs b/test/Parser/ExpressionSpec.hs new file mode 100644 index 0000000..78875b3 --- /dev/null +++ b/test/Parser/ExpressionSpec.hs @@ -0,0 +1,111 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Parser.ExpressionSpec where + +import Test.Hspec +import Test.Hspec.Megaparsec +import Text.Megaparsec +import Model.Function +import Parser.Expression + +spec :: Spec +spec = do + describe "Testing expression parsing" $ do + it "[Test 1]" $ do + parse expressionParser "" "1 + (2 - 3)" `shouldParse` head exps + it "[Test 2]" $ do + parse expressionParser "" "a * b - c * d - e * f = g * h - i * j - k * l" `shouldParse` (exps !! 1) + it "[Test 3]" $ do + parse expressionParser "" "a + b - c * d ^ e" `shouldParse` (exps !! 2) + it "[Test 4]" $ do + parse expressionParser "" "1 - 2 - 3 - 4 - 5 - 6" `shouldParse` (exps !! 3) + it "[Test 5]" $ do + parse expressionParser "" "[1, 2, 3]" `shouldParse` (exps !! 4) + it "[Test 6]" $ do + parse expressionParser "" "[1, 2 + 3, e]" `shouldParse` (exps !! 5) + it "[Test 7]" $ do + parse expressionParser "" "Function()" `shouldParse` (exps !! 6) + it "[Test 8]" $ do + parse expressionParser "" "Function(e)" `shouldParse` (exps !! 7) + it "[Test 9]" $ do + parse expressionParser "" "Function(3, 3+2,e)" `shouldParse` (exps !! 8) + it "[Test 10]" $ do + parse expressionParser "" "if (Function(2 + 3, e)) then a + b - c * d ^ e -> x else not a exists" `shouldParse` (exps !! 9) + it "[Test 11]" $ do + parse expressionParser "" "if [1, Function(3)] then 1 - 2 - 3 * a -> b ^ c" `shouldParse` (exps !! 10) + it "[Test 12]" $ do + parse expressionParser "" "a or b" `shouldParse` (exps !! 11) + it "[Test 13]" $ do + parse expressionParser "" `shouldFailOn` "if[1,as]thenxandoelseaora" + +exps :: [Expression] +exps = [ + InfixExp "+" (Int "1") (Parens (InfixExp "-" (Int "2") (Int "3"))), + InfixExp "=" + (InfixExp "-" + (InfixExp "-" + (InfixExp "*" (Variable "a") (Variable "b")) + (InfixExp "*" (Variable "c") (Variable "d"))) + (InfixExp "*" (Variable "e") (Variable "f"))) + (InfixExp "-" + (InfixExp "-" + (InfixExp "*" (Variable "g") (Variable "h")) + (InfixExp "*" (Variable "i") (Variable "j"))) + (InfixExp "*" (Variable "k") (Variable "l"))), + InfixExp "-" (InfixExp "+" (Variable "a") (Variable "b")) (InfixExp "*" (Variable "c") (InfixExp "^" (Variable "d") (Variable "e"))), + InfixExp "-" (InfixExp "-" (InfixExp "-" (InfixExp "-" (InfixExp "-" (Int "1") (Int "2")) (Int "3")) (Int "4")) (Int "5")) (Int "6"), + List [Int "1", Int "2", Int "3"], + List [Int "1", InfixExp "+" (Int "2") (Int "3"), Variable "e"], + Function "Function" [], + Function "Function" [Variable "e"], + Function "Function" [Int "3", InfixExp "+" (Int "3") (Int "2"), Variable "e"], + IfElse (Function "Function" [InfixExp "+" (Int "2") (Int "3"), Variable "e"]) + (InfixExp "-" (InfixExp "+" (Variable "a") (Variable "b")) (InfixExp "*" (Variable "c") (InfixExp "^" (Variable "d") (Variable "e->x")))) + (PrefixExp "not" (PostfixExp "exists" (Variable "a"))), + IfSimple (List [Int "1", Function "Function" [Int "3"]]) (InfixExp "-" (InfixExp "-" (Int "1") (Int "2")) (InfixExp "*" (Int "3") (InfixExp "^" (Variable "a->b") (Variable "c")))), + InfixExp "or" (Variable "a") (Variable "b") + ] + +--mapExpsToTypes :: [String] -> [(String, String)] +--mapExpsToTypes [] = [] +--mapExpsToTypes (expr: exps) = do +-- case parse expressionParser "" (Text.pack expr) of +-- Left errorBundle -> error (errorBundlePretty errorBundle) +-- Right ex -> (show ex, show $ checkExpression defaultMap ex) :mapExpsToTypes exps +-- +--printOnOneLine :: [(String, String)] -> String +--printOnOneLine [] = "" +--printOnOneLine ((ex, typ): exps) = "(" ++ ex ++ "," ++ typ ++ ")\n" ++ printOnOneLine exps +-- +-- +--expressions :: [String] +--expressions = [ +-- --Or Good +-- "True or False", +-- --Or Bad +-- "1 or False", +-- --And Good +-- "False and False", +-- --And Bad +-- "1 and 2", +-- --Exists Good +-- "a exists", +-- --Plus Good +-- "1.2 + 2.3", +-- "1 + 2.3", +-- "2.3 + 1", +-- "1 + 2", +-- --Plus Bad +-- "True + 2", +-- --If Good +-- "if True then 2 else 3", +-- --If Bad Cond +-- "if 2 then True else False", +-- --If Bad exps +-- "if True then 2 else False", +-- "if True or False then 24 + 15 else 55 + 98 + 35 + 34", +-- --List Good +-- "[2, 3]", +-- --List Bad +-- "[22, False, 5]" +-- ] \ No newline at end of file diff --git a/test/Parser/TypeSpec.hs b/test/Parser/TypeSpec.hs new file mode 100644 index 0000000..990cbe7 --- /dev/null +++ b/test/Parser/TypeSpec.hs @@ -0,0 +1,77 @@ +module Parser.TypeSpec where + +import Test.Hspec +import Text.Megaparsec +import qualified Data.Text as Text +import Model.Type +import Test.Hspec.Megaparsec +import Parser.Type + +spec :: Spec +spec = do + describe "Testing type parsing" $ do + it "[Test 1]" $ do + plainText <- readFile "resources/Types/testType1.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` head types + it "[Test 2]" $ do + plainText <- readFile "resources/Types/testType2.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 1) + it "[Test 3]" $ do + plainText <- readFile "resources/Types/testType3.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 2) + it "[Test 4]" $ do + plainText <- readFile "resources/Types/testType4.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 3) + it "[Test 5]" $ do + plainText <- readFile "resources/Types/testType5.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 4) + it "[Test 6]" $ do + plainText <- readFile "resources/Types/testType6.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 5) + it "[Test 7]" $ do + plainText <- readFile "resources/Types/testType7.rosetta" + parse typeParser "" (Text.pack plainText) `shouldParse` (types !! 6) + +types :: [Type] +types = [ + MakeType {typeName = "Period", + typeDescription = Just "description", + superType = Nothing, + typeAttributes = [MakeTypeAttribute {attributeName = "periodMultiplier", attributeType = MakeType "int" Nothing Nothing [], cardinality = Bounds(1, 1), + attributeDescription = Just "A time period multiplier, e.g. 1, 2 or 3 etc. A negative value can be used when specifying an offset relative to another date, e.g. -2 days."}, + MakeTypeAttribute {attributeName = "testMany", attributeType = MakeType "TestType" Nothing Nothing [], cardinality = OneBound 0, + attributeDescription = Just "Test many"}, + MakeTypeAttribute {attributeName = "testSome", attributeType = MakeType "TestSomeType" Nothing Nothing [], cardinality = OneBound 1, + attributeDescription = Just "Test some"}, + MakeTypeAttribute {attributeName = "testMaybeOne", attributeType = MakeType "TestZeroOneType" Nothing Nothing [], cardinality = Bounds (0, 1), + attributeDescription = Just "Test zero or one"}, + MakeTypeAttribute {attributeName = "testAll", attributeType = MakeType "Test" Nothing Nothing [], cardinality = Bounds (2, 15), + attributeDescription = Just "Test all"}]}, + + MakeType {typeName = "TestType", + typeDescription = Nothing, + superType = Nothing, + typeAttributes = [MakeTypeAttribute {attributeName = "periodMultiplier", attributeType = MakeType "int" Nothing Nothing [], cardinality = Bounds(1, 1), + attributeDescription = Nothing}]}, + + + MakeType {typeName = "TestSomeType", + typeDescription = Just "description", + superType = Nothing, + typeAttributes = [MakeTypeAttribute {attributeName = "periodMultiplier", attributeType = MakeType "int" Nothing Nothing [], cardinality = Bounds(1, 1), + attributeDescription = Just "A time period multiplier, e.g. 1, 2 or 3 etc. A negative value can be used when specifying an offset relative to another date, e.g. -2 days."}]}, + + + MakeType {typeName = "TestZeroOneType", + typeDescription = Nothing, + superType = Just $ MakeType "Period" Nothing Nothing [], + typeAttributes = [MakeTypeAttribute {attributeName = "periodMultiplier", attributeType = MakeType "int" Nothing Nothing [], cardinality = Bounds(1, 1), + attributeDescription = Nothing}]}, + + MakeType {typeName = "WrongCardinality", superType = Nothing, typeDescription = Just "description", typeAttributes = []}, + + MakeType {typeName = "WrongCardinality2", superType = Nothing, typeDescription = Just "description", typeAttributes = []}, + + MakeType {typeName = "MissingType", superType = Nothing, typeDescription = Just "description", typeAttributes = []} + + ] \ No newline at end of file diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..52ef578 --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} \ No newline at end of file