32 lines
1.3 KiB
Haskell
32 lines
1.3 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE OverloadedRecordDot #-}
|
|
{-# HLINT ignore "Use const" #-}
|
|
{-# HLINT ignore "Unused LANGUAGE pragma" #-}
|
|
{-# HLINT ignore "Avoid lambda" #-}
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
{-# LANGUAGE NoFieldSelectors #-}
|
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
module Test.ArithmeticParserSpec where
|
|
|
|
import Test.Hspec
|
|
|
|
import ArithmeticParser
|
|
import Data.Either
|
|
|
|
spec :: Spec
|
|
spec = do
|
|
describe "evaluate expressions" $ do
|
|
it "with basic arithmetic on literals" $ do
|
|
parseExpr "42" `shouldBe` Right (ENumber 42)
|
|
parseExpr "-7 + 3" `shouldBe` Right ( EAdd (ENumber (-7)) (ENumber 3))
|
|
parseExpr "2 * (3 + 4)" `shouldBe` Right ( EMul (ENumber 2) (EAdd (ENumber 3) (ENumber 4)))
|
|
parseExpr "10 - 2 * 3" `shouldBe` Right (ESub (ENumber 10) (EMul (ENumber 2) (ENumber 3)))
|
|
parseExpr "(8 / 2) + 1" `shouldBe` Right (EAdd (EDiv (ENumber 8) (ENumber 2)) (ENumber 1))
|
|
parseExpr "2 + 3 * (10 - 4)" `shouldBe` Right (EAdd (ENumber 2) (EMul (ENumber 3) (ESub (ENumber 10) (ENumber 4))))
|
|
isLeft (parseExpr "") `shouldBe` True -- missing expression
|
|
isLeft (parseExpr "5 +") `shouldBe` True -- incomplete
|
|
isLeft (parseExpr "2 ** 3") `shouldBe` True -- not supported
|