#18 & massive structure overhaul

This commit is contained in:
Tibo De Peuter 2022-12-19 22:54:42 +01:00
parent 83659e69b4
commit 3b0de65de1
16 changed files with 397 additions and 221 deletions

View file

@ -1,7 +1,9 @@
module ParserSpec where
import Test.Hspec
import Parse
import RPGEngine.Internals.Parse
import RPGEngine.Internals.Parse.StructureElement
import RPGEngine.Internals.Data.Internals
import Data.Either
spec :: Spec
@ -9,40 +11,118 @@ spec = do
describe "Basics of entries" $ do
it "can parse integers" $ do
let correct = Right $ Regular $ Integer 1
correct `shouldBe` parseWith regular "1"
parseWith regular "1" `shouldBe` correct
it "can parse string" $ do
let input = "dit is een string"
correct = Right $ Regular $ String input
correct `shouldBe` parseWith regular ("\"" ++ input ++ "\"")
parseWith regular ("\"" ++ input ++ "\"") `shouldBe` correct
it "can parse infinite" $ do
let correct = Right $ Regular Infinite
correct `shouldBe` parseWith regular "infinite"
parseWith regular "infinite" `shouldBe` correct
let wrong = Right $ Regular Infinite
wrong `shouldNotBe` parseWith regular "infinitee"
parseWith regular "infinitee" `shouldNotBe` wrong
it "can parse entries" $ do
let input = "id : \"dagger\""
correct = Right $ Entry "id" $ Regular $ String "dagger"
correct `shouldBe` parseWith entry input
let input = "id: \"dagger\""
correct = Right $ Entry (Tag "id") $ Regular $ String "dagger"
parseWith entry input `shouldBe` correct
let input = "x: 0"
correct = Right $ Entry "x" $ Regular $ Integer 0
correct `shouldBe` parseWith entry input
correct = Right $ Entry (Tag "x") $ Regular $ Integer 0
parseWith entry input `shouldBe` correct
let input = "useTimes: infinite"
correct = Right $ Entry "useTimes" $ Regular Infinite
correct `shouldBe` parseWith entry input
correct = Right $ Entry (Tag "useTimes") $ Regular Infinite
parseWith entry input `shouldBe` correct
describe "Special kinds" $ do
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: {}"
correct = Right $ Entry "actions" $ Regular Infinite -- TODO Change this
correct `shouldBe` parseWith action input
it "can parse conditions" $ do
pending
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