Restructuring, #9
This commit is contained in:
parent
2055ef234e
commit
dab6fadad4
41 changed files with 941 additions and 680 deletions
139
test/Parser/GameSpec.hs
Normal file
139
test/Parser/GameSpec.hs
Normal file
|
@ -0,0 +1,139 @@
|
|||
module Parser.GameSpec where
|
||||
|
||||
import Test.Hspec
|
||||
|
||||
import RPGEngine.Data
|
||||
import RPGEngine.Parse.Core
|
||||
import RPGEngine.Parse.TextToStructure
|
||||
import RPGEngine.Parse.StructureToGame
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "Game" $ do
|
||||
it "TODO: Simple game" $ do
|
||||
pending
|
||||
it "TODO: More complex game" $ do
|
||||
pending
|
||||
it "TODO: Game with multiple levels" $ do
|
||||
pending
|
||||
|
||||
describe "Player" $ do
|
||||
it "cannot die" $ do
|
||||
let input = "player: { hp: infinite, inventory: [] }"
|
||||
correct = Player {
|
||||
playerHp = Prelude.Nothing,
|
||||
inventory = [],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structure input
|
||||
structureToPlayer struct `shouldBe` correct
|
||||
|
||||
it "without inventory" $ do
|
||||
let input = "player: { hp: 50, inventory: [] }"
|
||||
correct = Player {
|
||||
playerHp = Just 50,
|
||||
inventory = [],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structure input
|
||||
structureToPlayer struct `shouldBe` correct
|
||||
|
||||
it "with inventory" $ do
|
||||
let input = "player: { hp: 50, inventory: [ { id: \"dagger\", x: 0, y: 0, name: \"Dolk\", description: \"Basis schade tegen monsters\", useTimes: infinite, value: 10, actions: {} } ] }"
|
||||
correct = Player {
|
||||
playerHp = Just 50,
|
||||
inventory = [
|
||||
Item {
|
||||
itemId = "dagger",
|
||||
itemX = 0,
|
||||
itemY = 0,
|
||||
itemName = "Dolk",
|
||||
itemDescription = "Basis schade tegen monsters",
|
||||
itemActions = [],
|
||||
itemValue = Just 10,
|
||||
useTimes = Prelude.Nothing
|
||||
}
|
||||
],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structure input
|
||||
structureToPlayer struct `shouldBe` correct
|
||||
|
||||
describe "Layout" $ do
|
||||
it "simple" $ do
|
||||
pending
|
||||
|
||||
describe "Items" $ do
|
||||
it "simple" $ do
|
||||
let input = "{ id: \"dagger\", x: 0, y: 0, name: \"Dagger\", description: \"Basic dagger you found somewhere\", useTimes: infinite, value: 10, actions: {} }"
|
||||
correct = Item {
|
||||
itemId = "dagger",
|
||||
itemX = 0,
|
||||
itemY = 0,
|
||||
itemName = "Dagger",
|
||||
itemDescription = "Basic dagger you found somewhere",
|
||||
itemValue = Just 10,
|
||||
itemActions = [],
|
||||
useTimes = Prelude.Nothing
|
||||
}
|
||||
Right struct = parseWith structure input
|
||||
structureToItem struct `shouldBe` correct
|
||||
|
||||
it "with actions" $ do
|
||||
let input = "{ id: \"key\", x: 3, y: 1, name: \"Doorkey\", description: \"Unlocks a secret door\", useTimes: 1, value: 0, actions: { [not(inventoryFull())] retrieveItem(key), [] leave() } }"
|
||||
correct = Item {
|
||||
itemId = "key",
|
||||
itemX = 3,
|
||||
itemY = 1,
|
||||
itemName = "Doorkey",
|
||||
itemDescription = "Unlocks a secret door",
|
||||
itemActions = [
|
||||
([], Leave),
|
||||
([Not InventoryFull], RetrieveItem "key")
|
||||
],
|
||||
itemValue = Just 0,
|
||||
useTimes = Just 1
|
||||
}
|
||||
Right struct = parseWith structure input
|
||||
structureToItem struct `shouldBe` correct
|
||||
|
||||
describe "Actions" $ do
|
||||
it "no conditions" $ do
|
||||
let input = "{[] leave()}"
|
||||
correct = [([], Leave)]
|
||||
Right struct = parseWith structure input
|
||||
structureToActions struct `shouldBe` correct
|
||||
|
||||
it "single condition" $ do
|
||||
let input = "{ [inventoryFull()] useItem(itemId)}"
|
||||
correct = [([InventoryFull], UseItem "itemId")]
|
||||
Right struct = parseWith structure input
|
||||
structureToActions struct `shouldBe` correct
|
||||
|
||||
it "multiple conditions" $ do
|
||||
let input = "{ [not(inventoryFull()), inventoryContains(itemId)] increasePlayerHp(itemId)}"
|
||||
correct = [([Not InventoryFull, InventoryContains "itemId"], IncreasePlayerHp "itemId")]
|
||||
Right struct = parseWith structure input
|
||||
structureToActions struct `shouldBe` correct
|
||||
|
||||
describe "Entities" $ do
|
||||
it "TODO: Simple entity" $ do
|
||||
pending
|
||||
|
||||
describe "Level" $ do
|
||||
it "Simple layout" $ do
|
||||
let input = "{ layout: { | * * * * * * \n| * s . . e *\n| * * * * * * }, items: [], entities: [] }"
|
||||
correct = Level {
|
||||
RPGEngine.Data.layout = [
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
|
||||
[Blocked, Entrance, Walkable, Walkable, Exit, Blocked],
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
|
||||
],
|
||||
items = [],
|
||||
entities = []
|
||||
}
|
||||
Right struct = parseWith structure input
|
||||
structureToLevel struct `shouldBe` correct
|
||||
|
||||
it "TODO: Complex layout" $ do
|
||||
pending
|
291
test/Parser/StructureSpec.hs
Normal file
291
test/Parser/StructureSpec.hs
Normal file
|
@ -0,0 +1,291 @@
|
|||
module Parser.StructureSpec where
|
||||
|
||||
import Test.Hspec
|
||||
|
||||
import RPGEngine.Data
|
||||
import RPGEngine.Parse.Core
|
||||
import RPGEngine.Parse.TextToStructure
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "StructureElement" $ do
|
||||
it "can parse blocks" $ do
|
||||
let input = "{}"
|
||||
correct = Right $ Block []
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{{}}"
|
||||
correct = Right $ Block [Block []]
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{{}, {}}"
|
||||
correct = Right $ Block [Block [], Block []]
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{ id: 1 }"
|
||||
correct = Right (Block [
|
||||
Entry (Tag "id") $ Regular $ Integer 1
|
||||
], "")
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
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 structure input `shouldBe` correct
|
||||
|
||||
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 structure input `shouldBe` correct
|
||||
|
||||
let input = "entities: [ { id: \"door\", x: 4, name:\"Secret door\", description: \"This secret door can only be opened with a key\", direction: left, y: 1}]"
|
||||
correct = Right (Entry (Tag "entities") $ Block [ Block [
|
||||
Entry (Tag "id") $ Regular $ String "door",
|
||||
Entry (Tag "x") $ Regular $ Integer 4,
|
||||
Entry (Tag "name") $ Regular $ String "Secret door",
|
||||
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
|
||||
Entry (Tag "direction") $ Regular $ Direction West,
|
||||
Entry (Tag "y") $ Regular $ Integer 1
|
||||
]], "")
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
let input = "entities: [ { id: \"door\", x: 4, y: 1, name:\"Secret door\", description: \"This secret door can only be opened with a key\", actions: { [inventoryContains(key)] useItem(key), [] leave() } } ]"
|
||||
correct = Right (Entry (Tag "entities") $ Block [ Block [
|
||||
Entry (Tag "id") $ Regular $ String "door",
|
||||
Entry (Tag "x") $ Regular $ Integer 4,
|
||||
Entry (Tag "y") $ Regular $ Integer 1,
|
||||
Entry (Tag "name") $ Regular $ String "Secret door",
|
||||
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
|
||||
Entry (Tag "actions") $ Block [
|
||||
Entry (ConditionList [InventoryContains "key"]) $ Regular $ Action $ UseItem "key",
|
||||
Entry (ConditionList []) $ Regular $ Action Leave
|
||||
]
|
||||
]], "")
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
let input = "entities: [ { id: \"door\", x: 4, y: 1, name:\"Secret door\", description: \"This secret door can only be opened with a key\", direction: left , actions: { [inventoryContains(key)] useItem(key), [] leave() } } ]"
|
||||
correct = Right (Entry (Tag "entities") $ Block [ Block [
|
||||
Entry (Tag "id") $ Regular $ String "door",
|
||||
Entry (Tag "x") $ Regular $ Integer 4,
|
||||
Entry (Tag "y") $ Regular $ Integer 1,
|
||||
Entry (Tag "name") $ Regular $ String "Secret door",
|
||||
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
|
||||
Entry (Tag "direction") $ Regular $ Direction West,
|
||||
Entry (Tag "actions") $ Block [
|
||||
Entry (ConditionList [InventoryContains "key"]) $ Regular $ Action $ UseItem "key",
|
||||
Entry (ConditionList []) $ Regular $ Action Leave
|
||||
]
|
||||
]], "")
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
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
|
||||
|
||||
let input = "direction: up"
|
||||
correct = Right $ Entry (Tag "direction") $ Regular $ Direction North
|
||||
parseWith entry input `shouldBe` correct
|
||||
|
||||
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 structure input `shouldBe` correct
|
||||
|
||||
it "can parse regulars" $ do
|
||||
let input = "this is a string"
|
||||
correct = Right $ Regular $ String input
|
||||
parseWith regular ("\"" ++ input ++ "\"") `shouldBe` correct
|
||||
|
||||
let correct = Right $ Regular $ Integer 1
|
||||
parseWith regular "1" `shouldBe` correct
|
||||
|
||||
let correct = Right $ Regular Infinite
|
||||
parseWith regular "infinite" `shouldBe` correct
|
||||
|
||||
let wrong = Right $ Regular Infinite
|
||||
parseWith regular "infinitee" `shouldNotBe` wrong
|
||||
|
||||
let input = "leave()"
|
||||
correct = Right $ Regular $ Action Leave
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "retrieveItem(firstId)"
|
||||
correct = Right $ Regular $ Action $ RetrieveItem "firstId"
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "useItem(secondId)"
|
||||
correct = Right $ Regular $ Action $ UseItem "secondId"
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "decreaseHp(entityId,objectId)"
|
||||
correct = Right $ Regular $ Action $ DecreaseHp "entityId" "objectId"
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "increasePlayerHp(objectId)"
|
||||
correct = Right $ Regular $ Action $ IncreasePlayerHp "objectId"
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "up"
|
||||
correct = Right $ Regular $ Direction North
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "right"
|
||||
correct = Right $ Regular $ Direction East
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "down"
|
||||
correct = Right $ Regular $ Direction South
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
let input = "left"
|
||||
correct = Right $ Regular $ Direction West
|
||||
parseWith regular input `shouldBe` correct
|
||||
|
||||
describe "Key" $ do
|
||||
it "can parse tags" $ do
|
||||
let input = "simpletag"
|
||||
correct = Right $ Tag "simpletag"
|
||||
parseWith tag 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 conditions" $ do
|
||||
let input = "inventoryFull()"
|
||||
correct = Right (Condition InventoryFull, "")
|
||||
parseWithRest condition input `shouldBe` correct
|
||||
|
||||
let input = "inventoryContains(itemId)"
|
||||
correct = Right (Condition $ InventoryContains "itemId", "")
|
||||
parseWithRest condition input `shouldBe` correct
|
||||
|
||||
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
|
||||
|
||||
describe "Value" $ do
|
||||
it "can parse strings" $ do
|
||||
let input = "dit is een string"
|
||||
correct = Right $ String input
|
||||
parseWith string ("\"" ++ input ++ "\"") `shouldBe` correct
|
||||
|
||||
it "can parse integers" $ do
|
||||
let correct = Right $ Integer 1
|
||||
parseWith integer "1" `shouldBe` correct
|
||||
|
||||
it "can parse infinite" $ do
|
||||
let correct = Right Infinite
|
||||
parseWith infinite "infinite" `shouldBe` correct
|
||||
|
||||
let wrong = Right Infinite
|
||||
parseWith infinite "infinitee" `shouldNotBe` wrong
|
||||
|
||||
it "can parse actions" $ do
|
||||
let input = "leave()"
|
||||
correct = Right $ Action Leave
|
||||
parseWith action input `shouldBe` correct
|
||||
|
||||
let input = "retrieveItem(firstId)"
|
||||
correct = Right $ Action $ RetrieveItem "firstId"
|
||||
parseWith action input `shouldBe` correct
|
||||
|
||||
let input = "useItem(secondId)"
|
||||
correct = Right $ Action $ UseItem "secondId"
|
||||
parseWith action input `shouldBe` correct
|
||||
|
||||
let input = "decreaseHp(entityId,objectId)"
|
||||
correct = Right $ Action $ DecreaseHp "entityId" "objectId"
|
||||
parseWith action input `shouldBe` correct
|
||||
|
||||
let input = "increasePlayerHp(objectId)"
|
||||
correct = Right $ Action $ IncreasePlayerHp "objectId"
|
||||
parseWith action input `shouldBe` correct
|
||||
|
||||
it "can parse directions" $ do
|
||||
let input = "up"
|
||||
correct = Right $ Direction North
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "right"
|
||||
correct = Right $ Direction East
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "down"
|
||||
correct = Right $ Direction South
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "left"
|
||||
correct = Right $ Direction West
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
it "can parse layouts" $ do
|
||||
let input = "| * * * * * * * *\n| * s . . . . e *\n| * * * * * * * *"
|
||||
correct = Right $ Layout [
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
|
||||
[Blocked, Entrance, Walkable, Walkable, Walkable, Walkable, Exit, Blocked],
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
|
||||
]
|
||||
parseWith RPGEngine.Parse.TextToStructure.layout input `shouldBe` correct
|
||||
|
||||
let input = "{ |* * * * * * * *|* s . . . . e *|* * * * * * * * }"
|
||||
-- correct = Right $ Entry (Tag "layout") $ Regular $ Layout [
|
||||
correct = Right $ Layout [
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
|
||||
[Blocked, Entrance, Walkable, Walkable, Walkable, Walkable, Exit, Blocked],
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
|
||||
]
|
||||
parseWith RPGEngine.Parse.TextToStructure.value input `shouldBe` correct
|
||||
|
||||
describe "Brackets" $ do
|
||||
it "matches closing <" $ do
|
||||
let input = '<'
|
||||
correct = '>'
|
||||
getMatchingClosingBracket input `shouldBe` correct
|
||||
|
||||
it "matches closing (" $ do
|
||||
let input = '('
|
||||
correct = ')'
|
||||
getMatchingClosingBracket input `shouldBe` correct
|
||||
|
||||
it "matches closing {" $ do
|
||||
let input = '{'
|
||||
correct = '}'
|
||||
getMatchingClosingBracket input `shouldBe` correct
|
||||
|
||||
it "matches closing [" $ do
|
||||
let input = '['
|
||||
correct = ']'
|
||||
getMatchingClosingBracket input `shouldBe` correct
|
Reference in a new issue