Barebone test/lib cabal config

This commit is contained in:
Enrico Lumetti 2024-10-04 23:41:45 +02:00
parent a8ec2f9f21
commit 2d3e48ed69
5 changed files with 96 additions and 44 deletions

43
Main.hs
View File

@ -1,46 +1,7 @@
module Main where module Main where
import Data.Aeson (FromJSON, parseJSON, withObject, withText, (.:), decode) import Bril
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"
main :: IO () main :: IO ()
main = do main = do
putStrLn $ show (decode "int" :: Maybe Type) putStrLn $ "Hello World!"

View File

@ -1,4 +1,4 @@
cabal-version: >=1.10 cabal-version: 2.0
-- Initial package description 'eoc.cabal' generated by 'cabal init'. For -- Initial package description 'eoc.cabal' generated by 'cabal init'. For
-- further documentation, see http://haskell.org/cabal/users-guide/ -- further documentation, see http://haskell.org/cabal/users-guide/
@ -16,13 +16,35 @@ maintainer: enrico.lumetti@gmail.com
build-type: Simple build-type: Simple
extra-source-files: README.md 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 executable eoc
main-is: Main.hs main-is: Main.hs
-- other-modules: -- other-modules:
-- other-extensions: -- other-extensions:
build-depends: build-depends:
base >=4.13 && <4.14, base >=4.13 && <4.18,
aeson >= 2.2 eoc-lib
-- hs-source-dirs: -- hs-source-dirs:
default-language: Haskell2010 default-language: Haskell2010
default-extensions: DeriveGeneric, OverloadedStrings 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

47
lib/Bril.hs Normal file
View File

@ -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)

2
lib/Eoc.hs Normal file
View File

@ -0,0 +1,2 @@
module Eoc where

20
tests/Main.hs Normal file
View File

@ -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