#10 #18 Fix parsing

This commit is contained in:
Tibo De Peuter 2022-12-22 22:05:25 +01:00
parent 5cc96cbdba
commit f3bce99120
18 changed files with 289 additions and 103 deletions

View file

@ -6,24 +6,50 @@ import RPGEngine.Data
import RPGEngine.Parse.Core
import RPGEngine.Parse.TextToStructure
import RPGEngine.Parse.StructureToGame
import RPGEngine.Parse.TextToStructure (gameFile)
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
-- TODO There is a weird bug that caused this to go in an infinite loop. Fix later.
xit "Simple game" $ do
let input = "player: {\n hp: 50,\n inventory: []\n}\n\nlevels: [\n {\n layout: {\n | * * * * * *\n | * s . . e *\n | * * * * * *\n },\n \n items: [],\n\n entities: []\n\n\n }\n]"
correct = Game {
state = Playing {
levels = [],
count = 0,
level = Level {
RPGEngine.Data.layout = [],
index = [],
items = [],
entities = []
},
player = Player {
playerHp = Just 50,
inventory = [],
position = (0, 0),
showHp = True,
showInventory = False
},
restart = Menu
}
}
(Right struct) = parseWith gameFile input
structureToGame struct `shouldBe` correct
it "More complex game" $ do
pendingWith "fix parsing first"
it "Game with multiple levels" $ do
pendingWith "fix parsing first"
describe "Player" $ do
it "cannot die" $ do
let input = "player: { hp: infinite, inventory: [] }"
correct = Player {
playerHp = Prelude.Nothing,
inventory = [],
position = (0, 0)
playerHp = Prelude.Nothing,
inventory = [],
position = (0, 0),
showHp = True,
showInventory = False
}
Right (Entry (Tag "player") struct) = parseWith structure input
structureToPlayer struct `shouldBe` correct
@ -31,9 +57,11 @@ spec = do
it "without inventory" $ do
let input = "player: { hp: 50, inventory: [] }"
correct = Player {
playerHp = Just 50,
inventory = [],
position = (0, 0)
playerHp = Just 50,
inventory = [],
position = (0, 0),
showHp = True,
showInventory = False
}
Right (Entry (Tag "player") struct) = parseWith structure input
structureToPlayer struct `shouldBe` correct
@ -54,14 +82,12 @@ spec = do
useTimes = Prelude.Nothing
}
],
position = (0, 0)
position = (0, 0),
showHp = True,
showInventory = False
}
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
@ -117,23 +143,40 @@ spec = do
structureToActions struct `shouldBe` correct
describe "Entities" $ do
it "TODO: Simple entity" $ do
pending
it "Simple entity" $ do
pendingWith "fix parsing first"
describe "Level" $ do
it "Simple layout" $ do
let input = "{ layout: { | * * * * * * \n| * s . . e *\n| * * * * * * }, items: [], entities: [] }"
let input = "{ layout: { | * * * * * *\n| * s . . e *\n| * * * * * *\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]
],
index = [
(0, 0, Blocked),
(1, 0, Blocked),
(2, 0, Blocked),
(3, 0, Blocked),
(4, 0, Blocked),
(5, 0, Blocked),
(0, 1, Blocked),
(1, 1, Entrance),
(2, 1, Walkable),
(3, 1, Walkable),
(4, 1, Exit),
(5, 1, Blocked),
(0, 2, Blocked),
(1, 2, Blocked),
(2, 2, Blocked),
(3, 2, Blocked),
(4, 2, Blocked),
(5, 2, Blocked)
],
items = [],
entities = []
}
Right struct = parseWith structure input
structureToLevel struct `shouldBe` correct
it "TODO: Complex layout" $ do
pending
structureToLevel struct `shouldBe` correct

View file

@ -5,6 +5,8 @@ import Test.Hspec
import RPGEngine.Data
import RPGEngine.Parse.Core
import RPGEngine.Parse.TextToStructure
import Text.Parsec.String (parseFromFile)
import GHC.IO (unsafePerformIO)
spec :: Spec
spec = do
@ -68,7 +70,7 @@ spec = do
]], "")
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() } } ]"
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,
@ -83,6 +85,17 @@ spec = do
]], "")
parseWithRest structure input `shouldBe` correct
it "combines actions and direction" $ do
let input = "entities: [ { direction: left, actions: { [inventoryContains(key)] useItem(key), [] leave() } } ]"
correct = Right (Entry (Tag "entities") $ Block [ Block [
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"
@ -252,22 +265,21 @@ spec = do
parseWith RPGEngine.Parse.TextToStructure.direction input `shouldBe` correct
it "can parse layouts" $ do
let input = "| * * * * * * * *\n| * s . . . . e *\n| * * * * * * * *"
let input = "{ | * * * * * * * *\n | * s . . . . e *\n | * * * * * * * *\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
parseWith value input `shouldBe` correct
let input = "{ |* * * * * * * *|* s . . . . e *|* * * * * * * * }"
-- correct = Right $ Entry (Tag "layout") $ Regular $ Layout [
correct = Right $ Layout [
let input = "layout: { | * * * * * * * *\n | * s . . . . e *\n | * * * * * * * *\n }"
correct = Right $ Entry (Tag "layout") $ Regular $ 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
parseWith structure input `shouldBe` correct
describe "Brackets" $ do
it "matches closing <" $ do
@ -289,3 +301,75 @@ spec = do
let input = '['
correct = ']'
getMatchingClosingBracket input `shouldBe` correct
describe "Full game file" $ do
it "single level" $ do
let input = "player: {\n hp: 50,\n inventory: []\n}\n\nlevels: [\n {\n layout: {\n | * * * * * *\n | * s . . e *\n | * * * * * *\n },\n \n items: [],\n\n entities: []\n\n\n }\n]"
correct = Right [
Entry (Tag "player") $ Block [
Entry (Tag "hp") $ Regular $ Integer 50,
Entry (Tag "inventory") $ Block []
],
Entry (Tag "levels") $ Block [ Block [
Entry (Tag "layout") $ Regular $ Layout [
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
[Blocked, Entrance, Walkable, Walkable, Exit, Blocked],
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
],
Entry (Tag "items") $ Block [],
Entry (Tag "entities") $ Block []
]]
]
parseWith gameFile input `shouldBe` correct
it "two levels" $ do
let input = "player: {\n hp: 50,\n inventory: []\n}\n\nlevels: [\n {\n layout: {\n | * * * * * *\n | * s . . e *\n | * * * * * *\n },\n \n items: [],\n\n entities: []\n },\n {\n layout: {\n | * * *\n | * e *\n | * . *\n | * . *\n | * . *\n | * . *\n | * s *\n | * * *\n },\n\n items: [],\n\n entities: []\n }\n]"
correct = Right [
Entry (Tag "player") $ Block [
Entry (Tag "hp") $ Regular $ Integer 50,
Entry (Tag "inventory") $ Block []
],
Entry (Tag "levels") $ Block [
Block [
Entry (Tag "layout") $ Regular $ Layout [
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
[Blocked, Entrance, Walkable, Walkable, Exit, Blocked],
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
],
Entry (Tag "items") $ Block [],
Entry (Tag "entities") $ Block []
], Block [
Entry (Tag "layout") $ Regular $ Layout [
[Blocked,Blocked,Blocked],
[Blocked,Exit,Blocked],
[Blocked,Walkable,Blocked],
[Blocked,Walkable,Blocked],
[Blocked,Walkable,Blocked],
[Blocked,Walkable,Blocked],
[Blocked,Entrance,Blocked],
[Blocked,Blocked,Blocked]
],
Entry (Tag "items") $ Block [],
Entry (Tag "entities") $ Block []
]
]
]
parseWith gameFile input `shouldBe` correct
it "from file" $ do
let correct = Right [
Entry (Tag "player") $ Block [
Entry (Tag "hp") $ Regular $ Integer 50,
Entry (Tag "inventory") $ Block []
],
Entry (Tag "levels") $ Block [ Block [
Entry (Tag "layout") $ Regular $ Layout [
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
[Blocked, Entrance, Walkable, Walkable, Exit, Blocked],
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
],
Entry (Tag "items") $ Block [],
Entry (Tag "entities") $ Block []
]]
]
unsafePerformIO (parseFromFile gameFile "levels/level1.txt") `shouldBe` correct