-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc5b0ca
commit e355878
Showing
14 changed files
with
126 additions
and
67 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 |
---|---|---|
@@ -1,42 +1,94 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import Bonlang.Lang | ||
import Bonlang.Lexer | ||
import Data.Aeson | ||
import qualified Data.ByteString as B | ||
import Data.Maybe (fromJust) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as E | ||
import qualified Data.Yaml as Y | ||
import GHC.Generics | ||
import System.Directory | ||
import System.FilePath | ||
import Test.Tasty.HUnit | ||
import Test.Tasty | ||
import Test.Tasty.HUnit | ||
import Text.ParserCombinators.Parsec | ||
import Debug.Trace as D | ||
|
||
data TestFile = TestFile { meta :: T.Text | ||
, code :: T.Text | ||
} | ||
deriving (Show) | ||
|
||
data TestInputOutput = TestInputOutput { output :: String | ||
} | ||
deriving (Generic, Show) | ||
|
||
instance FromJSON TestInputOutput | ||
instance ToJSON TestInputOutput | ||
|
||
main :: IO () | ||
main = do tests' <- tests | ||
defaultMain tests' | ||
main = tests >>= defaultMain | ||
|
||
tests :: IO TestTree | ||
tests = parsing | ||
>>= \p -> return $ testGroup "Bonlang" [p] | ||
tests = do p <- sequence [parsing, testExpectations] | ||
return $ testGroup "Bonlang" p | ||
|
||
testExpectations :: IO TestTree | ||
testExpectations = getFiles "test/examples/" >>= | ||
\files -> return $ testGroup "Example tests" $ map exampleFiles files | ||
|
||
|
||
exampleFiles :: FilePath -> TestTree | ||
exampleFiles f = testCase ("Example file: " `mappend` f) exampleAssertion | ||
where | ||
exampleAssertion :: Assertion | ||
exampleAssertion = do testFile <- makeTestFile f | ||
case testFile of | ||
Right testF -> runTestFile testF | ||
Left x -> error x | ||
|
||
runTestFile :: TestFile -> Assertion | ||
runTestFile testFile | ||
= do output' <- programOutput | ||
expectedOutput' <- expectedOutput | ||
output' @?= (output $ fromJust expectedOutput') | ||
where | ||
programOutput = return "" :: IO String | ||
expectedOutput | ||
= let tioSource = E.encodeUtf8 $ meta testFile in | ||
return $ Y.decode tioSource :: IO (Maybe TestInputOutput) | ||
|
||
makeTestFile :: FilePath -> IO (Either String TestFile) | ||
makeTestFile f = do fileContents <- T.pack <$> readFile f | ||
case T.splitOn "---" fileContents of | ||
[_, meta', code'] | ||
-> return $ Right TestFile { meta = meta' | ||
, code = code' | ||
} | ||
_ -> return $ Left "Invalid syntax for example file" | ||
|
||
parsing :: IO TestTree | ||
parsing = testFiles | ||
>>= \files -> return $ testGroup "Parsing tests" $ map testCaseFiles files | ||
parsing = getFiles "test/parser_tests/" >>= | ||
\files -> return $ testGroup "Parsing tests" $ map testCaseFiles files | ||
|
||
testCaseFiles :: String -> TestTree | ||
testCaseFiles f = testCase ("Parsing file: " `mappend` f) fileAssertion | ||
where | ||
fileAssertion :: Assertion | ||
fileAssertion = do (_, b) <- parse' f | ||
b @?= True | ||
|
||
testFiles :: IO [String] | ||
testFiles = do let testDir = "test/parser_tests/" | ||
files <- getDirectoryContents testDir | ||
let filtered = filter (\f -> takeExtension f == ".bl") files | ||
return $ map (testDir ++) filtered | ||
where | ||
fileAssertion :: Assertion | ||
fileAssertion = do (_, b) <- parse' f | ||
b @?= True | ||
|
||
parse' :: String -> IO (String, Bool) | ||
parse' f = parseFromFile bonlangParser f | ||
>>= \r -> return (f, parsedCorr r) | ||
where | ||
parsedCorr (Left _) = False | ||
parsedCorr (Right _) = True | ||
|
||
parse' f = parseFromFile bonlangParser f >>= \r -> return (f, parsedCorr r) | ||
where | ||
parsedCorr :: Either ParseError BonlangValue -> Bool | ||
parsedCorr (Left _) = False | ||
parsedCorr (Right _) = True | ||
|
||
getFiles :: FilePath -> IO [String] | ||
getFiles dir = do files <- getDirectoryContents dir | ||
let filtered = filter (\f -> takeExtension f == ".bl") files | ||
return $ map (dir ++) filtered |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "4\n4\n" | ||
--- | ||
|
||
module Main where | ||
|
||
/** | ||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "0" | ||
--- | ||
|
||
module Main where | ||
|
||
def main [] = { | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "Hello Bool! 0" | ||
--- | ||
|
||
module Main where | ||
|
||
def three [] = 3 | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "Testing this...\nIs equal? True\nWhat was equal... 2" | ||
--- | ||
|
||
module Main where | ||
|
||
def main [] = { | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "Gonna say...\nHello World!\n" | ||
--- | ||
|
||
module Main where | ||
|
||
def main [] = { | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
output: "Lazy Hello!\n" | ||
--- | ||
|
||
module Main where | ||
|
||
def main [] = { | ||
|