Skip to content

Commit 1bdc64e

Browse files
committed
make testfile to execute sample
1 parent df63ac4 commit 1bdc64e

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

egison.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ Library
8585
Test-Suite test
8686
Type: exitcode-stdio-1.0
8787
Hs-Source-Dirs: test
88-
Main-Is: UnitTest.hs
89-
Build-Depends: egison, base >= 4.0 && < 5, transformers, mtl, Glob, HUnit, test-framework, test-framework-hunit
88+
Main-Is: Test.hs
89+
Build-Depends: egison, base >= 4.0 && < 5, transformers, mtl, Glob, HUnit, test-framework, test-framework-hunit, filepath
9090
Other-modules: Paths_egison
9191

9292
Benchmark benchmark

test/Test.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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)

test/answer/sample/pi.egi

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
3
2+
(/ 19 6)
3+
(/ 160 51)
4+
(/ 1744 555)
5+
(/ 644 205)
6+
(/ 2529 805)
7+
(/ 183296 58345)
8+
(/ 3763456 1197945)
9+
(/ 4317632 1374345)
10+
(/ 54743776 17425485)
11+
(/ 188557135970304 60019600489849)
12+
(/ 14971288216320781097518293342791232212277336754690701067495880031840084161028749215081430249543252398598393324081906866270429198591457182295353830443350016706247047366505374418452903096221696 4765509048161794285645064982293258646209455886868326176573345305978152309773422954985053727781525391882745851558234889335081545974370263740232859330606973361218716957428517731023519870721125)
13+
"3.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
14+
"3.1666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666"
15+
"3.1372549019607843137254901960784313725490196078431372549019607843137254901960784313725490196078431372"
16+
"3.1423423423423423423423423423423423423423423423423423423423423423423423423423423423423423423423423423"
17+
"3.1414634146341463414634146341463414634146341463414634146341463414634146341463414634146341463414634146"
18+
"3.1416149068322981366459627329192546583850931677018633540372670807453416149068322981366459627329192546"
19+
"3.1415888250921244322564058616848058959636644099751478275773416745222384094609649498671694232582055017"
20+
"3.1415933118799277095359135853482421980975754312593649958887928911594438809795107454849763553418562621"
21+
"3.1415925404465399881398047797314357020980903630456690277914206403777799606357937781270350603378336589"
22+
"3.1415926730303345932695704022011438992946250850406746211081068905686125809410756716384077688511969681"
23+
"3.1415926535897936712622736560364699508858977905299910896886082997408332108220080192478822547875048379"
24+
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
25+
"3.141592653589793"

0 commit comments

Comments
 (0)