Barebone test/lib cabal config
This commit is contained in:
parent
a8ec2f9f21
commit
2d3e48ed69
43
Main.hs
43
Main.hs
|
|
@ -1,46 +1,7 @@
|
|||
module Main where
|
||||
|
||||
import Data.Aeson (FromJSON, parseJSON, withObject, withText, (.:), decode)
|
||||
|
||||
data Program = Program { programFns :: [Function] }
|
||||
deriving (Show)
|
||||
|
||||
data Function = Function {
|
||||
functionName :: String,
|
||||
returnType :: Maybe Type,
|
||||
functionArgs :: [FunctionArg]
|
||||
} deriving (Show)
|
||||
|
||||
data Type = Int | Bool
|
||||
deriving (Show)
|
||||
|
||||
data FunctionArg = FunctionArg {
|
||||
argName :: String,
|
||||
argType :: Type
|
||||
} deriving (Show)
|
||||
|
||||
instance FromJSON Program where
|
||||
parseJSON = withObject "Program" $ \v -> Program
|
||||
<$> v .: "functions"
|
||||
|
||||
instance FromJSON Function where
|
||||
parseJSON = withObject "Function" $ \v -> Function
|
||||
<$> v .: "name"
|
||||
<*> v .: "type"
|
||||
<*> v .: "args"
|
||||
|
||||
instance FromJSON FunctionArg where
|
||||
parseJSON = withObject "FunctionArg" $ \v -> FunctionArg
|
||||
<$> v .: "name"
|
||||
<*> v .: "type"
|
||||
|
||||
instance FromJSON Type where
|
||||
parseJSON = withText "Type" $ \v ->
|
||||
case v of
|
||||
"int" -> return Int
|
||||
"bool" -> return Bool
|
||||
_ -> fail "wrong type"
|
||||
import Bril
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn $ show (decode "int" :: Maybe Type)
|
||||
putStrLn $ "Hello World!"
|
||||
|
|
|
|||
28
eoc.cabal
28
eoc.cabal
|
|
@ -1,4 +1,4 @@
|
|||
cabal-version: >=1.10
|
||||
cabal-version: 2.0
|
||||
-- Initial package description 'eoc.cabal' generated by 'cabal init'. For
|
||||
-- further documentation, see http://haskell.org/cabal/users-guide/
|
||||
|
||||
|
|
@ -16,13 +16,35 @@ maintainer: enrico.lumetti@gmail.com
|
|||
build-type: Simple
|
||||
extra-source-files: README.md
|
||||
|
||||
library eoc-lib
|
||||
exposed-modules: Eoc, Bril
|
||||
hs-source-dirs: lib
|
||||
build-depends:
|
||||
base >=4.13 && <4.18,
|
||||
aeson >= 2.2
|
||||
default-language: Haskell2010
|
||||
default-extensions: DeriveGeneric, OverloadedStrings
|
||||
|
||||
executable eoc
|
||||
main-is: Main.hs
|
||||
-- other-modules:
|
||||
-- other-extensions:
|
||||
build-depends:
|
||||
base >=4.13 && <4.14,
|
||||
aeson >= 2.2
|
||||
base >=4.13 && <4.18,
|
||||
eoc-lib
|
||||
-- hs-source-dirs:
|
||||
default-language: Haskell2010
|
||||
default-extensions: DeriveGeneric, OverloadedStrings
|
||||
|
||||
test-suite tests
|
||||
type: exitcode-stdio-1.0
|
||||
main-is: Main.hs
|
||||
build-depends:
|
||||
base >=4.13 && <4.18,
|
||||
HUnit >=1.6,
|
||||
eoc-lib,
|
||||
aeson >= 2.2
|
||||
hs-source-dirs: tests
|
||||
default-language: Haskell2010
|
||||
default-extensions: DeriveGeneric, OverloadedStrings
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
module Bril (
|
||||
Function (..), Type (..), FunctionArg (..), Program (..), Int, Bool,
|
||||
)where
|
||||
|
||||
import Data.Aeson (FromJSON, parseJSON, withObject, withText, (.:), decode, Value)
|
||||
|
||||
|
||||
data Program = Program { programFns :: [Function] }
|
||||
deriving (Show, Eq)
|
||||
|
||||
data Function = Function {
|
||||
functionName :: String,
|
||||
returnType :: Maybe Type,
|
||||
functionArgs :: [FunctionArg]
|
||||
} deriving (Show, Eq)
|
||||
|
||||
data Type = Int | Bool
|
||||
deriving (Show, Eq)
|
||||
|
||||
data FunctionArg = FunctionArg {
|
||||
argName :: String,
|
||||
argType :: Type
|
||||
} deriving (Show, Eq)
|
||||
|
||||
instance FromJSON Program where
|
||||
parseJSON = withObject "Program" $ \v -> Program
|
||||
<$> v .: "functions"
|
||||
|
||||
instance FromJSON Function where
|
||||
parseJSON = withObject "Function" $ \v -> Function
|
||||
<$> v .: "name"
|
||||
<*> v .: "type"
|
||||
<*> v .: "args"
|
||||
|
||||
instance FromJSON FunctionArg where
|
||||
parseJSON = withObject "FunctionArg" $ \v -> FunctionArg
|
||||
<$> v .: "name"
|
||||
<*> v .: "type"
|
||||
|
||||
instance FromJSON Type where
|
||||
parseJSON = withText "Type" $ \v ->
|
||||
case v of
|
||||
"int" -> return Int
|
||||
"bool" -> return Bool
|
||||
_ -> fail "wrong type"
|
||||
|
||||
parseBrilJSON text = (decode text :: Maybe Program)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
module Eoc where
|
||||
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
module Main where
|
||||
|
||||
import qualified Bril as B
|
||||
import Data.Aeson (decode)
|
||||
import Data.Maybe
|
||||
|
||||
import Test.HUnit
|
||||
import qualified System.Exit as Exit
|
||||
|
||||
test1 :: Test
|
||||
test1 = TestCase
|
||||
(assertEqual "?" (decode "\"int\"" :: Maybe B.Type) (Just B.Int))
|
||||
|
||||
tests :: Test
|
||||
tests = TestList [TestLabel "test1" test1]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
result <- runTestTT tests
|
||||
if failures result > 0 then Exit.exitFailure else Exit.exitSuccess
|
||||
Loading…
Reference in New Issue