Skip to content

Commit

Permalink
Golden test for evaluation result
Browse files Browse the repository at this point in the history
  • Loading branch information
Unisay committed Apr 25, 2024
1 parent 66d7537 commit 7035386
Show file tree
Hide file tree
Showing 11 changed files with 796 additions and 13 deletions.
45 changes: 39 additions & 6 deletions test/Language/PureScript/Backend/Lua/Golden/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Language.PureScript.Backend.Lua.Optimizer (optimizeChunk)
import Language.PureScript.Backend.Lua.Printer qualified as Printer
import Language.PureScript.Backend.Types (AppOrModule (..))
import Language.PureScript.CoreFn.Reader qualified as CoreFn
import Language.PureScript.Names (ModuleName)
import Language.PureScript.Names qualified as PS
import Path
( Abs
Expand All @@ -38,6 +37,7 @@ import Path
)
import Path.IO
( AnyPath (makeRelativeToCurrentDir)
, doesFileExist
, ensureDir
, makeAbsolute
, walkDirAccum
Expand Down Expand Up @@ -111,14 +111,47 @@ spec = do
}
uberModule
-- lua golden
let evalGolden =
modulePath </> $(mkRelDir "eval") </> $(mkRelFile "golden.txt")
let luaGolden = modulePath </> $(mkRelFile "golden.lua")
let luaActual = modulePath </> $(mkRelFile "actual.lua")
luaTestName runIO do
toFilePath <$> makeRelativeToCurrentDir luaGolden
it luaTestName do
defaultGolden luaGolden (Just luaActual) do
uberModule compileCorefn (Tagged (Rel psOutputPath)) moduleName
compileIr moduleName uberModule
appOrModule
(doesFileExist evalGolden) <&> \case
True AsApplication moduleName (PS.Ident "main")
False AsModule moduleName
cfn compileCorefn (Tagged (Rel psOutputPath)) moduleName
compileIr appOrModule cfn

describe "golden files should evaluate" do
let
collectEvaluatableLuas MonadIO m Path Rel Dir m [Path Abs File]
collectEvaluatableLuas = walkDirAccum Nothing \_dir _subdirs files
pure [file | file files, toFilePath (filename file) == "golden.txt"]

luas runIO do collectEvaluatableLuas psOutputPath
for_ luas \lua do
let evalDir = parent lua
let resActual = evalDir </> $(mkRelFile "actual.txt")
let resGolden = evalDir </> $(mkRelFile "golden.txt")
let luaGolden = parent evalDir </> $(mkRelFile "golden.lua")
luaTestName runIO do makeRelativeToCurrentDir lua
it (toFilePath luaTestName) do
defaultGolden resGolden (Just resActual) do
let process = fromString $ "lua " ++ toFilePath luaGolden
(exitCode, out) readProcessInterleaved process
let niceOut =
decodeUtf8 out
& lines
& fmap Text.stripStart
& filter (not . Text.null)
& unlines
& toString
exitCode `shouldBe` ExitSuccess `annotatingWith` niceOut
pure $ toText niceOut

describe "golden files should typecheck" do
luas runIO do collectLuas psOutputPath
Expand Down Expand Up @@ -185,11 +218,11 @@ compileCorefn outputDir uberModuleName = do
let uberModule = Linker.makeUberModule (LinkAsModule uberModuleName) modules
pure $ optimizedUberModule uberModule

compileIr (MonadIO m, MonadMask m) ModuleName IR.UberModule m Text
compileIr modname uberModule = withCurrentDir [reldir|test/ps|] do
compileIr (MonadIO m, MonadMask m) AppOrModule IR.UberModule m Text
compileIr appOrModule uberModule = withCurrentDir [reldir|test/ps|] do
foreignPath Tagged <$> makeAbsolute [reldir|foreign|]
luaChunk
Lua.fromUberModule foreignPath (Tagged True) (AsModule modname) uberModule
Lua.fromUberModule foreignPath (Tagged True) appOrModule uberModule
& handleLuaError
& Oops.runOops
& liftIO
Expand Down
17 changes: 17 additions & 0 deletions test/ps/golden/Golden/ArrayOfUnits/Test.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Golden.ArrayOfUnits.Test where

import Prelude (Unit, discard, unit)

import Effect (Effect)
import Effect.Console (logShow)
import Data.Traversable (traverse_)
import Data.Foldable (length)

main :: Effect Unit
main = do
let arr :: Array Unit
arr = [unit, unit, unit]
traverse_ logShow arr
let len :: Int
len = length arr
logShow len
1 change: 1 addition & 0 deletions test/ps/output/Golden.ArrayOfUnits.Test/corefn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"builtWith":"0.15.15","comments":[],"decls":[{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[16,19],"start":[16,13]}},"type":"Var","value":{"identifier":"length","moduleName":["Data","Foldable"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[16,23],"start":[16,13]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"foldableArray","moduleName":["Data","Foldable"]}},"type":"App"},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[16,23],"start":[16,13]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"semiringInt","moduleName":["Data","Semiring"]}},"type":"App"},"identifier":"length"},{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[17,10],"start":[17,3]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Effect","Console"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[17,14],"start":[17,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"showInt","moduleName":["Data","Show"]}},"type":"App"},"identifier":"logShow"},{"annotation":{"meta":null,"sourceSpan":{"end":[10,20],"start":[10,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[13,31],"start":[12,3]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[12,24],"start":[12,7]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[13,31],"start":[13,13]}},"type":"Literal","value":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[13,18],"start":[13,14]}},"type":"Var","value":{"identifier":"unit","moduleName":["Data","Unit"]}},{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[13,24],"start":[13,20]}},"type":"Var","value":{"identifier":"unit","moduleName":["Data","Unit"]}},{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[13,30],"start":[13,26]}},"type":"Var","value":{"identifier":"unit","moduleName":["Data","Unit"]}}]}},"identifier":"arr"}],"expression":{"abstraction":{"abstraction":{"abstraction":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[14,24],"start":[14,3]}},"type":"Var","value":{"identifier":"discard","moduleName":["Control","Bind"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"discardUnit","moduleName":["Control","Bind"]}},"type":"App"},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"bindEffect","moduleName":["Effect"]}},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":{"abstraction":{"abstraction":{"abstraction":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[14,12],"start":[14,3]}},"type":"Var","value":{"identifier":"traverse_","moduleName":["Data","Foldable"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[14,20],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"applicativeEffect","moduleName":["Effect"]}},"type":"App"},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[14,20],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"foldableArray","moduleName":["Data","Foldable"]}},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[14,20],"start":[14,3]}},"argument":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[14,20],"start":[14,13]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Effect","Console"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[14,20],"start":[14,13]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"showUnit","moduleName":["Data","Show"]}},"type":"App"},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[14,24],"start":[14,21]}},"type":"Var","value":{"identifier":"arr","sourcePos":[12,7]}},"type":"App"},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[14,24],"start":[14,3]}},"argument":"$__unused","body":{"annotation":{"meta":null,"sourceSpan":{"end":[16,23],"start":[15,3]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[15,17],"start":[15,7]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"length","moduleName":["Golden","ArrayOfUnits","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[16,23],"start":[16,13]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[16,23],"start":[16,20]}},"type":"Var","value":{"identifier":"arr","sourcePos":[12,7]}},"type":"App"},"identifier":"len"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Golden","ArrayOfUnits","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[17,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[17,11]}},"type":"Var","value":{"identifier":"len","sourcePos":[15,7]}},"type":"App"},"type":"Let"},"type":"Abs"},"type":"App"},"type":"Let"},"identifier":"main"}],"exports":["main"],"foreign":[],"imports":[{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Control","Bind"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Data","Foldable"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Data","Semiring"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Data","Show"]},{"annotation":{"meta":null,"sourceSpan":{"end":[7,36],"start":[7,1]}},"moduleName":["Data","Traversable"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Data","Unit"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Effect"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Effect","Console"]},{"annotation":{"meta":null,"sourceSpan":{"end":[3,37],"start":[3,1]}},"moduleName":["Prelude"]},{"annotation":{"meta":null,"sourceSpan":{"end":[17,14],"start":[1,1]}},"moduleName":["Prim"]}],"moduleName":["Golden","ArrayOfUnits","Test"],"modulePath":"golden/Golden/ArrayOfUnits/Test.purs","reExports":{},"sourceSpan":{"end":[17,14],"start":[1,1]}}
1 change: 1 addition & 0 deletions test/ps/output/Golden.ArrayOfUnits.Test/eval/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
actual.txt
4 changes: 4 additions & 0 deletions test/ps/output/Golden.ArrayOfUnits.Test/eval/golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unit
unit
unit
3
Loading

0 comments on commit 7035386

Please sign in to comment.