Restructuring, #9
This commit is contained in:
parent
2055ef234e
commit
dab6fadad4
41 changed files with 941 additions and 680 deletions
|
@ -1,10 +1,11 @@
|
|||
module ParseGameSpec where
|
||||
module Parser.GameSpec where
|
||||
|
||||
import Test.Hspec
|
||||
import RPGEngine.Parse.StructElement
|
||||
|
||||
import RPGEngine.Data
|
||||
import RPGEngine.Parse.Core
|
||||
import RPGEngine.Parse.Game
|
||||
import RPGEngine.Parse.TextToStructure
|
||||
import RPGEngine.Parse.StructureToGame
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
|
@ -21,19 +22,21 @@ spec = do
|
|||
let input = "player: { hp: infinite, inventory: [] }"
|
||||
correct = Player {
|
||||
playerHp = Prelude.Nothing,
|
||||
inventory = []
|
||||
inventory = [],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structElement input
|
||||
structToPlayer struct `shouldBe` correct
|
||||
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 = []
|
||||
inventory = [],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structElement input
|
||||
structToPlayer struct `shouldBe` correct
|
||||
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: {} } ] }"
|
||||
|
@ -50,10 +53,11 @@ spec = do
|
|||
itemValue = Just 10,
|
||||
useTimes = Prelude.Nothing
|
||||
}
|
||||
]
|
||||
],
|
||||
position = (0, 0)
|
||||
}
|
||||
Right (Entry (Tag "player") struct) = parseWith structElement input
|
||||
structToPlayer struct `shouldBe` correct
|
||||
Right (Entry (Tag "player") struct) = parseWith structure input
|
||||
structureToPlayer struct `shouldBe` correct
|
||||
|
||||
describe "Layout" $ do
|
||||
it "simple" $ do
|
||||
|
@ -72,8 +76,8 @@ spec = do
|
|||
itemActions = [],
|
||||
useTimes = Prelude.Nothing
|
||||
}
|
||||
Right struct = parseWith structElement input
|
||||
structToItem struct `shouldBe` correct
|
||||
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() } }"
|
||||
|
@ -90,27 +94,27 @@ spec = do
|
|||
itemValue = Just 0,
|
||||
useTimes = Just 1
|
||||
}
|
||||
Right struct = parseWith structElement input
|
||||
structToItem struct `shouldBe` correct
|
||||
Right struct = parseWith structure input
|
||||
structureToItem struct `shouldBe` correct
|
||||
|
||||
describe "Actions" $ do
|
||||
it "no conditions" $ do
|
||||
let input = "{[] leave()}"
|
||||
correct = [([], Leave)]
|
||||
Right struct = parseWith structElement input
|
||||
structToActions struct `shouldBe` correct
|
||||
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 structElement input
|
||||
structToActions struct `shouldBe` correct
|
||||
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 structElement input
|
||||
structToActions struct `shouldBe` correct
|
||||
Right struct = parseWith structure input
|
||||
structureToActions struct `shouldBe` correct
|
||||
|
||||
describe "Entities" $ do
|
||||
it "TODO: Simple entity" $ do
|
||||
|
@ -118,7 +122,7 @@ spec = do
|
|||
|
||||
describe "Level" $ do
|
||||
it "Simple layout" $ do
|
||||
let input = "{ layout: { | * * * * * * \n| * s . . e *\n| * * * * * *\n}, items: [], entities: [] }"
|
||||
let input = "{ layout: { | * * * * * * \n| * s . . e *\n| * * * * * * }, items: [], entities: [] }"
|
||||
correct = Level {
|
||||
RPGEngine.Data.layout = [
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
|
||||
|
@ -128,7 +132,8 @@ spec = do
|
|||
items = [],
|
||||
entities = []
|
||||
}
|
||||
Right struct = parseWith structElement input
|
||||
structToLevel struct `shouldBe` correct
|
||||
Right struct = parseWith structure input
|
||||
structureToLevel struct `shouldBe` correct
|
||||
|
||||
it "TODO: Complex layout" $ do
|
||||
pending
|
|
@ -1,10 +1,10 @@
|
|||
module ParseStructElementSpec where
|
||||
module Parser.StructureSpec where
|
||||
|
||||
import Test.Hspec
|
||||
|
||||
import RPGEngine.Data
|
||||
import RPGEngine.Parse.Core
|
||||
import RPGEngine.Parse.StructElement
|
||||
import RPGEngine.Parse.TextToStructure
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
|
@ -12,21 +12,21 @@ spec = do
|
|||
it "can parse blocks" $ do
|
||||
let input = "{}"
|
||||
correct = Right $ Block []
|
||||
parseWith structElement input `shouldBe` correct
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{{}}"
|
||||
correct = Right $ Block [Block []]
|
||||
parseWith structElement input `shouldBe` correct
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{{}, {}}"
|
||||
correct = Right $ Block [Block [], Block []]
|
||||
parseWith structElement input `shouldBe` correct
|
||||
parseWith structure input `shouldBe` correct
|
||||
|
||||
let input = "{ id: 1 }"
|
||||
correct = Right (Block [
|
||||
Entry (Tag "id") $ Regular $ Integer 1
|
||||
], "")
|
||||
parseWithRest structElement input `shouldBe` correct
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
let input = "{ id: \"key\", x: 3, y: 1}"
|
||||
correct = Right $ Block [
|
||||
|
@ -34,14 +34,14 @@ spec = do
|
|||
Entry (Tag "x") $ Regular $ Integer 3,
|
||||
Entry (Tag "y") $ Regular $ Integer 1
|
||||
]
|
||||
parseWith structElement input `shouldBe` correct
|
||||
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 structElement input `shouldBe` correct
|
||||
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 [
|
||||
|
@ -52,7 +52,7 @@ spec = do
|
|||
Entry (Tag "direction") $ Regular $ Direction West,
|
||||
Entry (Tag "y") $ Regular $ Integer 1
|
||||
]], "")
|
||||
parseWithRest structElement input `shouldBe` correct
|
||||
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 [
|
||||
|
@ -66,7 +66,7 @@ spec = do
|
|||
Entry (ConditionList []) $ Regular $ Action Leave
|
||||
]
|
||||
]], "")
|
||||
parseWithRest structElement input `shouldBe` correct
|
||||
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 [
|
||||
|
@ -81,7 +81,7 @@ spec = do
|
|||
Entry (ConditionList []) $ Regular $ Action Leave
|
||||
]
|
||||
]], "")
|
||||
parseWithRest structElement input `shouldBe` correct
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
it "can parse entries" $ do
|
||||
let input = "id: \"dagger\""
|
||||
|
@ -105,7 +105,7 @@ spec = do
|
|||
Entry (ConditionList [Not InventoryFull]) $ Regular $ Action $ RetrieveItem "key",
|
||||
Entry (ConditionList []) $ Regular $ Action Leave
|
||||
], "")
|
||||
parseWithRest structElement input `shouldBe` correct
|
||||
parseWithRest structure input `shouldBe` correct
|
||||
|
||||
it "can parse regulars" $ do
|
||||
let input = "this is a string"
|
||||
|
@ -237,19 +237,19 @@ spec = do
|
|||
it "can parse directions" $ do
|
||||
let input = "up"
|
||||
correct = Right $ Direction North
|
||||
parseWith RPGEngine.Parse.StructElement.direction input `shouldBe` correct
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "right"
|
||||
correct = Right $ Direction East
|
||||
parseWith RPGEngine.Parse.StructElement.direction input `shouldBe` correct
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "down"
|
||||
correct = Right $ Direction South
|
||||
parseWith RPGEngine.Parse.StructElement.direction input `shouldBe` correct
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
let input = "left"
|
||||
correct = Right $ Direction West
|
||||
parseWith RPGEngine.Parse.StructElement.direction input `shouldBe` correct
|
||||
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
|
||||
|
||||
it "can parse layouts" $ do
|
||||
let input = "| * * * * * * * *\n| * s . . . . e *\n| * * * * * * * *"
|
||||
|
@ -258,7 +258,16 @@ spec = do
|
|||
[Blocked, Entrance, Walkable, Walkable, Walkable, Walkable, Exit, Blocked],
|
||||
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
|
||||
]
|
||||
parseWith RPGEngine.Parse.StructElement.layout input `shouldBe` correct
|
||||
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
|
Reference in a new issue