Skip to content

Commit

Permalink
Add test for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
flbulgarelli committed Mar 21, 2023
1 parent 0b9a362 commit 566b3b9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
12 changes: 11 additions & 1 deletion spec/GenericSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Language.Mulang.Normalizers.Haskell (haskellNormalizationOption
import Language.Mulang.Parsers.Haskell
import Language.Mulang.Parsers.Java (java)
import Language.Mulang.Parsers.JavaScript
import Language.Mulang.Parsers.Python (py2, py3)
import Language.Mulang.Parsers.Python (npy, py2, py3)
import Language.Mulang.Transform.Normalizer

nhs = normalize haskellNormalizationOptions . hs
Expand Down Expand Up @@ -82,6 +82,16 @@ spec = do
it "is False when constant is declared with a variable literal" $ do
declaresFunction (named "f") (hs "f = snd") `shouldBe` False

describe "with function declarations, npy" $ do
it "is True when a function by parts is declared" $ do
declaresFunction (named "f") (npy "def f(x):\n\tif x > 4:\n\t\treturn 1\n\telse:\n\t\treturn 5") `shouldBe` True

it "is True when a function by parts is declared using an imperative style" $ do
declaresFunction (named "f") (npy "def f(x):\n\tif x > 4:\n\t\treturn 1\n\treturn 5") `shouldBe` True

it "is False when a partial function is declared" $ do
declaresFunction (named "f") (npy "def f(x):\n\tif x > 4:\n\t\treturn 1\n") `shouldBe` False

describe "with function declarations, js" $ do
it "is True when functions is declared" $ do
declaresFunction (named "f") (js "function f(x) {return 1}") `shouldBe` True
Expand Down
8 changes: 3 additions & 5 deletions spec/NormalizerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ module NormalizerSpec (spec) where
import Language.Mulang.Parsers.Haskell (hs)
import Language.Mulang.Parsers.Java (java)
import Language.Mulang.Parsers.JavaScript (js)
import Language.Mulang.Parsers.Python (py)
import Language.Mulang.Parsers.Python (npy, py)
import Language.Mulang.Normalizers.Java (javaNormalizationOptions)
import Language.Mulang.Normalizers.Python (pythonNormalizationOptions)
import Language.Mulang.Normalizers.Haskell (haskellNormalizationOptions)
import Language.Mulang.Transform.Normalizer

njava = normalize javaNormalizationOptions . java
npy = normalize pythonNormalizationOptions . py
nhs = normalize haskellNormalizationOptions . hs

spec :: Spec
Expand Down Expand Up @@ -79,10 +77,10 @@ module NormalizerSpec (spec) where
let n = normalize options

it "does not insert return in single literal statement" $ do
n (py "def x(): x = 1") `shouldBe` SimpleProcedure "x" [] (Assignment "x" (MuNumber 1.0))
n (npy "def x(): x = 1") `shouldBe` SimpleProcedure "x" [] (Assignment "x" (MuNumber 1.0))

it "inserts return in single literal expression" $ do
n (py "def x(): 3") `shouldBe` SimpleProcedure "x" [] (Return (MuNumber 3.0))
n (npy "def x(): 3") `shouldBe` SimpleProcedure "x" [] (Return (MuNumber 3.0))

it "does not insert return in empty block" $ do
n (SimpleFunction "x" [] None) `shouldBe` (SimpleFunction "x" [] None)
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Mulang/Normalizers/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import Language.Mulang.Transform.Normalizer (unnormalized, NormalizationOptions(

pythonNormalizationOptions :: NormalizationOptions
pythonNormalizationOptions = unnormalized {
sortSequenceDeclarations = SortAllNonVariables,
sortSequenceDeclarations = SortUniqueNonVariables,
convertProcedureByPartsIntoFunction = True
}
6 changes: 5 additions & 1 deletion src/Language/Mulang/Parsers/Python.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Language.Mulang.Parsers.Python (
npy,
py,
py2,
py3,
Expand All @@ -10,9 +11,11 @@ import qualified Language.Mulang.Ast as M
import qualified Language.Mulang.Ast.Operator as O
import Language.Mulang.Builder (compactMap, compactTuple)
import Language.Mulang.Parsers
import Language.Mulang.Transform.Normalizer (normalize)

import qualified Language.Python.Version3.Parser as Python3
import qualified Language.Python.Version2.Parser as Python2
import Language.Mulang.Normalizers.Python (pythonNormalizationOptions)
import Language.Python.Common.Token (Token)
import Language.Python.Common.AST

Expand All @@ -23,7 +26,8 @@ import Data.Maybe (fromMaybe, listToMaybe)
import Control.Fallible
import Control.Monad (msum)

py, py2, py3 :: Parser
npy, py, py2, py3 :: Parser
npy = normalize pythonNormalizationOptions . py
py = py3
py2 = parsePythonOrFail Python2.parseModule
py3 = parsePythonOrFail Python3.parseModule
Expand Down

0 comments on commit 566b3b9

Please sign in to comment.