From c1938e3725bbe963df6a964b8dc28f3e8eb5f604 Mon Sep 17 00:00:00 2001 From: Markus Date: Fri, 14 Apr 2017 13:20:03 +0200 Subject: [PATCH] Allow the expected return value to be specified in c file tests --- scripts/corrode-cc | 1 + tests/regression-tests/clone-impl.c | 2 ++ tests/test.hs | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/scripts/corrode-cc b/scripts/corrode-cc index 16865cf..370c788 100755 --- a/scripts/corrode-cc +++ b/scripts/corrode-cc @@ -46,6 +46,7 @@ if '-c' in cflags: if outfile is None: outfile = str(PurePath(str(rsfile)).with_suffix(".o")) + print("-------", rsfile) rustwarn = subprocess.run( ['rustc', '--crate-type=dylib', '--emit', 'obj', '-o', outfile, rsfile], diff --git a/tests/regression-tests/clone-impl.c b/tests/regression-tests/clone-impl.c index d2c1b85..5550f18 100644 --- a/tests/regression-tests/clone-impl.c +++ b/tests/regression-tests/clone-impl.c @@ -1,3 +1,5 @@ +// returns 123 + struct Test { int x[100]; diff --git a/tests/test.hs b/tests/test.hs index 0eb92ed..16142ea 100644 --- a/tests/test.hs +++ b/tests/test.hs @@ -10,6 +10,7 @@ import Data.List import Language.Rust.Corrode.CFG import System.Directory import System.FilePath (()) +import System.Exit (ExitCode(..)) import System.Process import Test.Tasty import Test.Tasty.HUnit @@ -33,17 +34,30 @@ regressionDir = "tests" "regression-tests" regressionTest :: String -> TestTree regressionTest name = testCase name $ do - callProcess "gcc" ["-o", "dist/build" name, regressionDir (name ++ ".c")] + let cName = regressionDir (name ++ ".c") + + cFileContents <- readFile cName + let firstLine = head (lines cFileContents) + expectedExitInt = + if isPrefixOf "// returns " firstLine then + read (drop (length "// returns ") firstLine) + else + 0 + expectedExitcode = if expectedExitInt == 0 then ExitSuccess else ExitFailure expectedExitInt + + + callProcess "gcc" ["-o", "dist/build" name, cName] cProgram <- spawnProcess ("dist/build" name) [] callProcess "python" [ "./scripts/corrode-cc" , "-o", "dist/build" (name ++ "-rust") - , regressionDir (name ++ ".c") + , cName ] rustProgram <- spawnProcess ("dist/build" (name ++ "-rust")) [] expected <- waitForProcess cProgram actual <- waitForProcess rustProgram + expectedExitcode @=? expected expected @=? actual regressionTests :: TestTree