packages feed

spade-0.1.0.3: test/Compiler/Lexer/LiteralSpec.hs

module Compiler.Lexer.LiteralSpec where

import Test.Hspec

import Common
import Compiler.Lexer.Literals
import Parser.Lib
import Parser.Parser

spec :: Spec
spec = do
  let
    unwrapParseError :: Either (ParseErrorWithParsed a) a -> Either ParseError a
    unwrapParseError (Right a)  = Right a
    unwrapParseError (Left a)  = Left $ parseError a

  describe "Literal parsing" $ do
    it "can parse strings" $ runParser parser ("\"test\"" :: TextWithOffset) `shouldReturn` (Just $ LitString "test")
    it "can parse strings with escaped quotes" $ runParser parser (toTextWithOffset $ "\"te" <> "\\" <> "\"" <> "st\"") `shouldReturn` (Just $ LitString "te\"st")
    it "can parse numbers" $ runParser parser "90" `shouldReturn` (Just $ LitNumber 90)
    it "reject numbers starting with 0" $ (runParser @Literal) parser "09" `shouldReturn` Nothing
    it "can parse float: 1" $ runParser parser "90.05" `shouldReturn` (Just $ LitFloat 90.05)
    it "can parse float: 2" $ runParser parser "0.05" `shouldReturn` (Just $ LitFloat 0.05)
    it "reject float without leading zero" $ (unwrapParseError <$> runParserEither @TextWithOffset @Literal parser ".05") `shouldReturn` (Left CantHandle)
    it "can parse bool:true" $ runParser parser "true" `shouldReturn` (Just $ LitBool True)
    it "can parse bool:false" $ runParser parser "false" `shouldReturn` (Just $ LitBool False)
  specGenerateAndParse @Literal