This repository has been archived on 2023-06-24. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2022FuncProg-project3-RPGEn.../test/ParserSpec.hs

132 lines
5.4 KiB
Haskell

module ParserSpec where
import Test.Hspec
import RPGEngine.Internals.Parse
import RPGEngine.Internals.Parse.StructureElement
import RPGEngine.Internals.Data.Internals
import Data.Either
spec :: Spec
spec = do
describe "Basics of entries" $ do
it "can parse integers" $ do
let correct = Right $ Regular $ Integer 1
parseWith regular "1" `shouldBe` correct
it "can parse string" $ do
let input = "dit is een string"
correct = Right $ Regular $ String input
parseWith regular ("\"" ++ input ++ "\"") `shouldBe` correct
it "can parse infinite" $ do
let correct = Right $ Regular Infinite
parseWith regular "infinite" `shouldBe` correct
let wrong = Right $ Regular Infinite
parseWith regular "infinitee" `shouldNotBe` wrong
it "can parse entries" $ do
let input = "id: \"dagger\""
correct = Right $ Entry (Tag "id") $ Regular $ String "dagger"
parseWith entry input `shouldBe` correct
let input = "x: 0"
correct = Right $ Entry (Tag "x") $ Regular $ Integer 0
parseWith entry input `shouldBe` correct
let input = "useTimes: infinite"
correct = Right $ Entry (Tag "useTimes") $ Regular Infinite
parseWith entry input `shouldBe` correct
describe "block: {...}" $ do
it "can parse a block with a single entry" $ do
let input = "{ id: 1}"
correct = Right (Block [
Entry (Tag "id") $ Regular $ Integer 1
], "")
parseWithRest structureElement input `shouldBe` correct
it "can parse a block with entries" $ do
let input = "{ id: \"key\", x: 3, y: 1}"
correct = Right $ Block [
Entry (Tag "id") $ Regular $ String "key",
Entry (Tag "x") $ Regular $ Integer 3,
Entry (Tag "y") $ Regular $ Integer 1
]
parseWith structureElement input `shouldBe` correct
describe "Basics" $ do
it "can parse leave()" $ do
let input = "leave()"
correct = Right $ Action Leave
parseWith action input `shouldBe` correct
it "can parse retrieveItem()" $ do
let input = "retrieveItem(firstId)"
correct = Right $ Action $ RetrieveItem "firstId"
parseWith action input `shouldBe` correct
it "can parse useItem()" $ do
let input = "useItem(secondId)"
correct = Right $ Action $ UseItem "secondId"
parseWith action input `shouldBe` correct
it "can parse decreaseHp()" $ do
let input = "decreaseHp(entityId,objectId)"
correct = Right $ Action $ DecreaseHp "entityId" "objectId"
parseWith action input `shouldBe` correct
it "can parse increasePlayerHp()" $ do
let input = "increasePlayerHp(objectId)"
correct = Right $ Action $ IncreasePlayerHp "objectId"
parseWith action input `shouldBe` correct
it "can parse inventoryFull()" $ do
let input = "inventoryFull()"
correct = Right (Condition InventoryFull, "")
parseWithRest condition input `shouldBe` correct
it "can parse inventoryContains()" $ do
let input = "inventoryContains(itemId)"
correct = Right (Condition $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
it "can parse not()" $ do
let input = "not(inventoryFull())"
correct = Right (Condition $ Not InventoryFull, "")
parseWithRest condition input `shouldBe` correct
let input = "not(inventoryContains(itemId))"
correct = Right (Condition $ Not $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
it "can parse conditionlists" $ do
let input = "[not(inventoryFull())]"
correct = Right (ConditionList [Not InventoryFull], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[inventoryFull(), inventoryContains(itemId)]"
correct = Right (ConditionList [
InventoryFull,
InventoryContains "itemId"
], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[]"
correct = Right $ ConditionList []
parseWith conditionList input `shouldBe` correct
it "can parse actions" $ do
let input = "actions: { [not(inventoryFull())] retrieveItem(key), [] leave()}"
correct = Right (Entry (Tag "actions") $ Block [
Entry (ConditionList [Not InventoryFull]) $ Regular $ Action $ RetrieveItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
], "")
parseWithRest structureElement input `shouldBe` correct
describe "Layouts" $ do
it "can parse layouts" $ do
pending
describe "Lists and blocks" $ do
it "can parse entities" $ do
pending