1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module Golden (goldenTests) where67import Language.QBE (parse)8import System.Exit (ExitCode (..))9import System.FilePath10import System.IO (IOMode (WriteMode), hClose, hGetContents, openFile)11import System.Process12import Test.Tasty13import Test.Tasty.Golden.Advanced1415type QBEResult = (ExitCode, String)1617runQBE :: FilePath -> IO QBEResult18runQBE filePath = do19 devNull <- openFile "/dev/null" WriteMode2021 (_, _, Just herr, p) <-22 createProcess23 (proc "qbe" [filePath])24 { std_out = UseHandle devNull,25 std_err = CreatePipe26 }2728 ret <- waitForProcess p <* hClose devNull29 out <- hGetContents herr30 return (ret, out)3132runQuebex :: FilePath -> IO QBEResult33runQuebex filePath = do34 content <- readFile filePath35 case parse filePath content of36 Right _ -> pure (ExitSuccess, "")37 Left err -> pure (ExitFailure 1, show err)3839simpleCmp :: QBEResult -> QBEResult -> IO (Maybe String)40simpleCmp (exit, out) (exit', out') =41 return $42 if exit == exit'43 then Nothing44 else Just ("Parsing mismatch: " ++ err)45 where46 err :: String47 err = "qbe=(" ++ show exit ++ "): " ++ out ++ " quebex=(" ++ show exit' ++ "):" ++ out'4849runTest :: TestName -> TestTree50runTest testName =51 goldenTest52 testName53 (runQBE fullPath)54 (runQuebex fullPath)55 simpleCmp56 (\_ -> pure ())57 where58 fullPath :: FilePath59 fullPath = "test" </> "golden" </> (testName ++ ".qbe")6061------------------------------------------------------------------------6263goldenTests :: TestTree64goldenTests =65 testGroup66 "goldenTests"67 [ runTest "data-definition-whitespace",68 runTest "empty-definitions",69 runTest "function-definition",70 runTest "call-instruction",71 runTest "load-instructions",72 runTest "value-global",73 runTest "bubble-sort",74 runTest "phi-instructions",75 runTest "data"76 ]