Skip to content

Commit 7035386

Browse files
committed
Golden test for evaluation result
1 parent 66d7537 commit 7035386

File tree

11 files changed

+796
-13
lines changed

11 files changed

+796
-13
lines changed

test/Language/PureScript/Backend/Lua/Golden/Spec.hs

+39-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import Language.PureScript.Backend.Lua.Optimizer (optimizeChunk)
1919
import Language.PureScript.Backend.Lua.Printer qualified as Printer
2020
import Language.PureScript.Backend.Types (AppOrModule (..))
2121
import Language.PureScript.CoreFn.Reader qualified as CoreFn
22-
import Language.PureScript.Names (ModuleName)
2322
import Language.PureScript.Names qualified as PS
2423
import Path
2524
( Abs
@@ -38,6 +37,7 @@ import Path
3837
)
3938
import Path.IO
4039
( AnyPath (makeRelativeToCurrentDir)
40+
, doesFileExist
4141
, ensureDir
4242
, makeAbsolute
4343
, walkDirAccum
@@ -111,14 +111,47 @@ spec = do
111111
}
112112
uberModule
113113
-- lua golden
114+
let evalGolden =
115+
modulePath </> $(mkRelDir "eval") </> $(mkRelFile "golden.txt")
114116
let luaGolden = modulePath </> $(mkRelFile "golden.lua")
115117
let luaActual = modulePath </> $(mkRelFile "actual.lua")
116118
luaTestName runIO do
117119
toFilePath <$> makeRelativeToCurrentDir luaGolden
118120
it luaTestName do
119121
defaultGolden luaGolden (Just luaActual) do
120-
uberModule compileCorefn (Tagged (Rel psOutputPath)) moduleName
121-
compileIr moduleName uberModule
122+
appOrModule
123+
(doesFileExist evalGolden) <&> \case
124+
True AsApplication moduleName (PS.Ident "main")
125+
False AsModule moduleName
126+
cfn compileCorefn (Tagged (Rel psOutputPath)) moduleName
127+
compileIr appOrModule cfn
128+
129+
describe "golden files should evaluate" do
130+
let
131+
collectEvaluatableLuas MonadIO m Path Rel Dir m [Path Abs File]
132+
collectEvaluatableLuas = walkDirAccum Nothing \_dir _subdirs files
133+
pure [file | file files, toFilePath (filename file) == "golden.txt"]
134+
135+
luas runIO do collectEvaluatableLuas psOutputPath
136+
for_ luas \lua do
137+
let evalDir = parent lua
138+
let resActual = evalDir </> $(mkRelFile "actual.txt")
139+
let resGolden = evalDir </> $(mkRelFile "golden.txt")
140+
let luaGolden = parent evalDir </> $(mkRelFile "golden.lua")
141+
luaTestName runIO do makeRelativeToCurrentDir lua
142+
it (toFilePath luaTestName) do
143+
defaultGolden resGolden (Just resActual) do
144+
let process = fromString $ "lua " ++ toFilePath luaGolden
145+
(exitCode, out) readProcessInterleaved process
146+
let niceOut =
147+
decodeUtf8 out
148+
& lines
149+
& fmap Text.stripStart
150+
& filter (not . Text.null)
151+
& unlines
152+
& toString
153+
exitCode `shouldBe` ExitSuccess `annotatingWith` niceOut
154+
pure $ toText niceOut
122155

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

188-
compileIr (MonadIO m, MonadMask m) ModuleName IR.UberModule m Text
189-
compileIr modname uberModule = withCurrentDir [reldir|test/ps|] do
221+
compileIr (MonadIO m, MonadMask m) AppOrModule IR.UberModule m Text
222+
compileIr appOrModule uberModule = withCurrentDir [reldir|test/ps|] do
190223
foreignPath Tagged <$> makeAbsolute [reldir|foreign|]
191224
luaChunk
192-
Lua.fromUberModule foreignPath (Tagged True) (AsModule modname) uberModule
225+
Lua.fromUberModule foreignPath (Tagged True) appOrModule uberModule
193226
& handleLuaError
194227
& Oops.runOops
195228
& liftIO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Golden.ArrayOfUnits.Test where
2+
3+
import Prelude (Unit, discard, unit)
4+
5+
import Effect (Effect)
6+
import Effect.Console (logShow)
7+
import Data.Traversable (traverse_)
8+
import Data.Foldable (length)
9+
10+
main :: Effect Unit
11+
main = do
12+
let arr :: Array Unit
13+
arr = [unit, unit, unit]
14+
traverse_ logShow arr
15+
let len :: Int
16+
len = length arr
17+
logShow len
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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]}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
actual.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
unit
2+
unit
3+
unit
4+
3

0 commit comments

Comments
 (0)