|
| 1 | +module Main where |
| 2 | + |
| 3 | +import Control.Applicative |
| 4 | +import Control.Monad |
| 5 | +import Data.IORef |
| 6 | + |
| 7 | +import Test.Framework (defaultMain) |
| 8 | +import Test.Framework.Providers.HUnit (hUnitTestToTests) |
| 9 | +import Test.HUnit |
| 10 | +import System.FilePath.Glob (glob) |
| 11 | +import System.FilePath (takeDirectory, replaceDirectory) |
| 12 | + |
| 13 | +import Language.Egison.Types |
| 14 | +import Language.Egison.Core |
| 15 | +import Language.Egison.Primitives |
| 16 | +import Language.Egison |
| 17 | + |
| 18 | +main = do |
| 19 | + testCases <- glob "sample/pi.egi" |
| 20 | + defaultMain $ hUnitTestToTests $ test $ map runTestCase testCases |
| 21 | + |
| 22 | +runTestCase :: FilePath -> Test |
| 23 | +runTestCase file = TestLabel file . TestCase $ do |
| 24 | + env <- initialEnv |
| 25 | + let directory_path = takeDirectory file |
| 26 | + answers <- readFile (replaceDirectory file ("test/answer/" ++ directory_path)) |
| 27 | + assertEgisonM (lines answers) $ do |
| 28 | + exprs <- loadFile file |
| 29 | + let (bindings, tests) = foldr collectDefsAndTests ([], []) exprs |
| 30 | + env' <- recursiveBind env bindings |
| 31 | + forM tests $ evalExprDeep env' |
| 32 | + where |
| 33 | + assertEgisonM :: [String] -> EgisonM [EgisonValue] -> Assertion |
| 34 | + assertEgisonM answers m = fromEgisonM m >>= assertString . either show (f answers) |
| 35 | + |
| 36 | + collectDefsAndTests (Define name expr) (bindings, tests) = |
| 37 | + (((stringToVar $ show name), expr) : bindings, tests) |
| 38 | + collectDefsAndTests (Test expr) (bindings, tests) = |
| 39 | + (bindings, expr : tests) |
| 40 | + collectDefsAndTests _ r = r |
| 41 | + |
| 42 | + f :: [String] -> [EgisonValue] -> String |
| 43 | + f answers ls = g answers ls 0 |
| 44 | + g x y i = if (x !! i) == show (y !! i) |
| 45 | + then (if i < (length y - 1) then g x y (i + 1) |
| 46 | + else "") |
| 47 | + else "failed " ++ show i ++ "\n expected: " ++ (x !! i) ++ "\n but found: " ++ show (y !! i) |
0 commit comments