generated from srid/haskell-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an integration test for source generation
- Loading branch information
1 parent
d85860c
commit 3d8b510
Showing
6 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{-# LANGUAGE PackageImports #-} | ||
|
||
module Main where | ||
|
||
import Control.Monad.Catch | ||
import Control.Monad.IO.Unlift | ||
import Control.Monad.Morph (hoist) | ||
import Control.Monad.Trans.Resource | ||
import Data.Text.IO qualified as Text | ||
import Hedgehog (MonadTest, evalEither, evalMaybe, forAll, property, (===)) | ||
import Hedgehog.Gen qualified as Gen | ||
import Hedgehog.Internal.Property (failWith) | ||
import Hedgehog.Range qualified as Range | ||
import Language.Templatespiler.Parser | ||
import Language.Templatespiler.Syntax | ||
import Prettyprinter | ||
import Prettyprinter.Render.Terminal | ||
import System.IO (hClose) | ||
import System.Process | ||
import Templatespiler.Convert | ||
import Templatespiler.Convert.Target | ||
import Templatespiler.Emit.Common | ||
import Templatespiler.Generate (arbitraryInput) | ||
import Templatespiler.Generator | ||
import Test.Syd | ||
import Test.Syd.Hedgehog | ||
import Text.Trifecta | ||
import Util (shouldBeJust, shouldBeRight) | ||
import "temporary" System.IO.Temp qualified as Temp | ||
import "temporary-resourcet" System.IO.Temp qualified as TempResourceT | ||
import System.FilePath ((</>)) | ||
|
||
main :: IO () | ||
main = sydTest spec | ||
|
||
spec :: Spec | ||
spec = describe "Integration Test" $ do | ||
it "Simple Template" $ do | ||
property . hoist runResourceT $ do | ||
let parsed = parseTemplate simpleTemplate3Ints | ||
res <- evalEither parsed | ||
|
||
let allLanguages = [minBound ..] :: [TargetLanguage] | ||
for_ allLanguages $ \lang -> do | ||
genResult <- evalMaybe $ convertTo res lang | ||
code <- case genResult of | ||
ConversionFailed errorDoc -> failWith Nothing $ toString $ renderStrict $ layoutPretty defaultLayoutOptions errorDoc | ||
ConvertResult warnings code -> do | ||
liftIO $ putDoc $ vsep warnings | ||
pure $ show code | ||
|
||
pass | ||
exec <- withCompiled lang code | ||
input <- forAll $ arbitraryInput res | ||
liftIO $ exec input | ||
|
||
|
||
withCompiled :: MonadResource m => TargetLanguage -> Text -> m ([Text] -> IO ()) | ||
withCompiled lang code = do | ||
(_, fp) <- TempResourceT.createTempDirectory Nothing "templatespiler" | ||
|
||
let sourceExt = case lang of | ||
Python -> ".py" | ||
C -> ".c" | ||
(_, sourceFp, sourceHandle) <- TempResourceT.openTempFile (Just fp) ("source" <> sourceExt) | ||
liftIO $ Text.hPutStrLn sourceHandle code | ||
liftIO $ hClose sourceHandle | ||
compiledFile <- case lang of | ||
Python -> pure sourceFp | ||
C -> do | ||
liftIO $ callProcess "gcc" [sourceFp, "-o", fp </> "a.out"] | ||
pure $ fp <> "/a.out" | ||
|
||
let (cmdToRun, argsToRun) = case lang of | ||
Python -> ("python3", [toText compiledFile]) | ||
C -> (toText compiledFile, []) | ||
|
||
pure $ \inputs -> runProcessWithStdin cmdToRun argsToRun inputs | ||
|
||
runProcessWithStdin :: Text -> [Text] -> [Text] -> IO () | ||
runProcessWithStdin processName args input = do | ||
let inputText = unlines input | ||
(Just hin, Just hout, _, _) <- createProcess (proc (toString processName) (map toString args)) {std_in = CreatePipe, std_out = CreatePipe} | ||
Text.hPutStrLn hin inputText | ||
hFlush hin | ||
-- assert that the process exits successfully with no output | ||
output <- Text.hGetContents hout | ||
output `shouldBe` "" | ||
|
||
parseTemplate :: Text -> Either Text BindingList | ||
parseTemplate input = do | ||
let x = parseByteString parseBindingList mempty . encodeUtf8 $ input | ||
case x of | ||
Success a -> Right a | ||
Failure e -> Left $ renderStrict $ layoutPretty defaultLayoutOptions $ _errDoc e | ||
|
||
-- tests :: TestTree | ||
-- tests = | ||
-- testGroup | ||
-- "Test Generation" | ||
-- [ testCaseSteps "Simple Template" $ \step -> do | ||
-- step "Parse Template" | ||
-- let parsed = parseTemplate simpleTemplate3Ints | ||
|
||
-- ] | ||
|
||
simpleTemplate3Ints :: Text | ||
simpleTemplate3Ints = "a : Integer\n b : Integer\n c : Integer\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Util where | ||
|
||
import Test.Syd | ||
|
||
shouldBeRight :: (Show a, HasCallStack) => Either Text a -> IO a | ||
shouldBeRight theEither = do | ||
shouldSatisfyNamed theEither "IsRight" isRight | ||
Right a <- pure theEither | ||
pure a | ||
|
||
shouldBeJust :: (Show a, HasCallStack) => Text -> Maybe a -> IO a | ||
shouldBeJust annotation theMaybe = do | ||
shouldSatisfyNamed theMaybe (toString ("IsJust: " <> annotation)) isJust | ||
Just a <- pure theMaybe | ||
pure a |