Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e test for Purescript #615

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/haskell/natural4/natural4.cabal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions lib/haskell/natural4/test/LS/XPile/PurescriptSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -Wall #-}

module LS.XPile.PurescriptSpec (spec) where

import Control.Monad (unless)
import Data.Either (lefts, rights)
import Data.Foldable qualified as DF
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.IO qualified as TL
import LS qualified as SFL4
import LS.NLP.NLG (NLGEnv, allLangs, langEng, myNLGEnv, printLangs)
import LS.XPile.Logging (XPileLogW, fmapE, mutter, xpLog)
import LS.XPile.Purescript (translate2PS)
import System.FilePath
import Test.Hspec (Spec, describe, it, runIO)
import Test.Hspec.Golden ( Golden(..) )
import Prelude hiding (exp, seq)
import LS.Interpreter (l4interpret)
import PGF (Language)
import LS (Interpreted)
data NLGData
= MkNLGData
{ env :: NLGEnv,
allEnv :: [NLGEnv]
}

loadNLGEnv :: Either [String] Language -> Interpreted -> IO NLGData
loadNLGEnv engE l4i =
case engE of
Left err -> do
error $ unlines $ "natural4: encountered error when obtaining langEng" : err
Right eng -> do
(nlgEnv, _nlgEnvErr) <- xpLog <$> myNLGEnv l4i eng -- Only load the NLG environment if we need it.
(allNLGEnv, _) <- do
nlgLangs <- allLangs
xps <- traverse (myNLGEnv l4i) nlgLangs
return (xpLog $ sequenceA xps)
case nlgEnv of
Left err -> do
error $ unlines $ "natural4: encountered error while obtaining myNLGEnv" : err
Right nlgEnvR -> do
let allNLGEnvErrors = mconcat $ lefts allNLGEnv
unless (null allNLGEnvErrors) do
putStrLn "natural4: encountered error while obtaining allNLGEnv"
DF.traverse_ putStrLn allNLGEnvErrors
fendor marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
putStrLn "natural4: encountered error while obtaining allNLGEnv"
DF.traverse_ putStrLn allNLGEnvErrors
error $ unlines $ "natural4: encountered error while obtaining allNLGEnv" : allNLGEnvErrors


let allNLGEnvR = rights allNLGEnv

let nlgData =
MkNLGData
nlgEnvR
allNLGEnvR

pure nlgData

transpileFile :: String -> IO TL.Text
transpileFile filename = do
let testPath = "test" </> "testdata" </> "golden" </> "PurescriptSpec" </> filename <.> "csv"
opts = SFL4.defaultOptions {SFL4.file = [testPath]}
strLangs <- printLangs allLangs
rules <- SFL4.dumpRules opts
l4i <- l4interpret rules
(engE, _) <- xpLog <$> langEng
nlgData <- loadNLGEnv engE l4i

let justNLGDate = nlgData
nlgEnvs = justNLGDate.allEnv
eng = justNLGDate.env
(psResult, _) = xpLog do
fmapE (<> ("\n\n" <> "allLang = [\"" <> strLangs <> "\"]")) (translate2PS nlgEnvs eng rules)

case psResult of
Left err -> do
error $ unlines $ "natural4: encountered error while obtaining myNLGEnv" : err
Right goodResult -> do
pure $ TL.pack goodResult



goldenGeneric :: String -> TL.Text -> Golden TL.Text
goldenGeneric name myoutput =
Golden
{ output = myoutput,
encodePretty = TL.unpack,
writeToFile = TL.writeFile,
readFromFile = TL.readFile,
goldenFile = testPath <.> "purs.expected",
actualFile = Just (testPath <.> "purs.actual"),
failFirstTime = False
}
where
testPath = "test" </> "testdata" </> "golden" </> "PurescriptSpec" </> name


spec :: Spec
spec = do
describe "toMathLang for arithRule3" do
must_sing_purs <- runIO $ transpileFile "must_sing"
it "convert must sing to Purescript" $ goldenGeneric "must_sing" must_sing_purs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
must_sing_purs <- runIO $ transpileFile "must_sing"
it "convert must sing to Purescript" $ goldenGeneric "must_sing" must_sing_purs
it "convert must sing to Purescript" $ do
must_sing_purs <- transpileFile "must_sing"
goldenGeneric "must_sing" must_sing_purs

Then it is a perfect test case, imo!

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,,EVERY,Person,,,,,,,,,,,,
,,WHO,walks,,,,,,,,,,,,
,,AND,,eats,,,,,,,,,,,
,,,OR,drinks,,,,,,,,,,,
,,MUST,sing,,,,,,,,,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
-- This file was automatically generated by natural4.
-- Do not edit by hand.
-- Instead, revise the toolchain starting at smucclaw/dsl/lib/haskell/natural4/app/Main.hs

module RuleLib.Interview where

import Prelude
import Data.Either
import Data.Maybe
import Data.Tuple
import Data.Map as Map
import Foreign.Object as Object

import AnyAll.Types

interviewRules :: Item String
interviewRules = All
( Pre "all of:" )
[ Leaf "does the person walk?"
, Any
( Pre "any of:" )
[ Leaf "does the person eat?"
, Leaf "does the person drink?"
]
]


interviewRules_nl :: NLDict
interviewRules_nl =
Map.fromFoldable
[ ]

nl4chi :: Object.Object (Item String)
nl4chi = Object.fromFoldable
[ Tuple "Person"
( All
( Pre "all of:" )
[ Leaf "人 走 吗?"
, Any
( Pre "any of:" )
[ Leaf "人 吃 吗?"
, Leaf "人 喝 吗?"
]
]
)
]

nl4chiMarking :: Marking
nl4chiMarking = Marking $ Map.fromFoldable
[]

nl4eng :: Object.Object (Item String)
nl4eng = Object.fromFoldable
[ Tuple "Person"
( All
( Pre "all of:" )
[ Leaf "does the person walk?"
, Any
( Pre "any of:" )
[ Leaf "does the person eat?"
, Leaf "does the person drink?"
]
]
)
]

nl4engMarking :: Marking
nl4engMarking = Marking $ Map.fromFoldable
[]

nl4may :: Object.Object (Item String)
nl4may = Object.fromFoldable
[ Tuple "Person"
( All
( Pre "all of:" )
[ Leaf "adakah seseorang berjalan?"
, Any
( Pre "any of:" )
[ Leaf "adakah seseorang makan?"
, Leaf "adakah seseorang minum?"
]
]
)
]

nl4mayMarking :: Marking
nl4mayMarking = Marking $ Map.fromFoldable
[]



allLang = ["nl4chi", "nl4eng", "nl4may"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
,§,Covered If …,,,,,,,,,,,
FALSE,DECIDE,Loss or Damage 1,IS,,Covered,,,,,,,,
TRUE,IF,NOT,,Loss or Damage,caused by,rodents,FALSE,,,,,,
,,,,,OR,insects,TRUE,,,,,,
,,,,,OR,vermin,FALSE,,,,,,
,,,,,OR,birds,FALSE,,,,,,
FALSE,,,UNLESS,,,Loss or Damage,IS,to Contents,,FALSE,,,
,,,,,AND,"""","""",caused by,birds,FALSE,,,
TRUE,,,,OR,,"""","""",ensuing covered loss,TRUE,,,,
FALSE,,,,,UNLESS,,any other exclusion applies,,FALSE,,,,
FALSE,,,,,,OR,an animal caused water to escape from,,,a household appliance,,,FALSE
,,,,,,,,,OR,a swimming pool,,,FALSE
,,,,,,,,,OR,"a plumbing, heating, or air conditioning system",,,FALSE
;;,,,,,,,,,,,,,
,§,Not Covered If …,,,,,,,,,,,
FALSE,DECIDE,Loss or Damage 2,IS,,Covered,,,,,,,,
FALSE,IF,Loss or Damage,caused by,rodents,FALSE,,,,,,,,
,,,OR,insects,FALSE,,,,,,,,
,,,OR,vermin,FALSE,,,,,,,,
,,,OR,birds,FALSE,,,,,,,,
FALSE,UNLESS,,,Loss or Damage,IS,to Contents,,FALSE,,,,,
,,,AND,"""","""",caused by,birds,FALSE,,,,,
FALSE,,OR,,Loss or Damage,IS,ensuing covered loss,FALSE,,,,,,
FALSE,,,UNLESS,,any other exclusion applies,,FALSE,,,,,,
FALSE,,,,OR,an animal caused water to escape from,,,a household appliance,,,FALSE,,
,,,,,,,OR,a swimming pool,,,FALSE,,
,,,,,,,OR,"a plumbing, heating, or air conditioning system",,,FALSE,,
Loading