Skip to content

Commit

Permalink
fix: parser adding empty levels b/c of blank lines
Browse files Browse the repository at this point in the history
I'd solved this in dino's parser, finally hit it here.
  • Loading branch information
russmatney committed Jan 29, 2024
1 parent fa5738f commit a45b174
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/parse/ParsedGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func parse(contents):
var parser = section_parsers.get(header.to_lower())
if parser:
chunks = Array(chunks).map(func(c): return c.split("\n"))
chunks = chunks.map(func(chunk):
return Array(chunk).filter(func(c):
return c != "" and c != "\t" and c != "\t\t" and c != "\t\t\t")
).filter(func(chunk): return len(chunk) > 0)
parsed[header.to_lower()] = parser.call(chunks)
return parsed

Expand Down
15 changes: 11 additions & 4 deletions src/parse/Puzz.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
extends Node
class_name Puzz

static func parse_game_def(path):
# could make sure file exists
var file = FileAccess.open(path, FileAccess.READ)
var contents = file.get_as_text()
static func parse_game_def(path, opts={}):
var contents
if path != null:
# could make sure file exists
var file = FileAccess.open(path, FileAccess.READ)
# TODO check if file exists!!
contents = file.get_as_text()
else:
contents = opts.get("contents")

# TODO check if contents exists!!
var game = ParsedGame.new()
var parsed = game.parse(contents)
return parsed
Expand Down
57 changes: 57 additions & 0 deletions test/dotHop/gamedef_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extends GdUnitTestSuite
class_name GameDefTest

func test_level_counts():
# TODO these are unnecessarily hard-coded - should move to a more reasonable pattern,
# especially so these numbers can move around

var one = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-one.txt"})
assert_int(len(one.game_def.levels)).is_equal(4)

var two = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-two.txt"})
assert_int(len(two.game_def.levels)).is_equal(12)

var three = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-three.txt"})
assert_int(len(three.game_def.levels)).is_equal(10)

var four = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-four.txt"})
assert_int(len(four.game_def.levels)).is_equal(3)

var five = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-five.txt"})
assert_int(len(five.game_def.levels)).is_equal(3)

var six = DotHopPuzzle.build_puzzle_node({game_def_path="res://src/puzzles/dothop-six.txt"})
assert_int(len(six.game_def.levels)).is_equal(3)

func test_expected_level_count():
var parsed = Puzz.parse_game_def(null, {contents="
title DotHop
author Russell Matney
=======
LEGEND
=======
. = Background
a = Player
o = Dot
t = Goal
x = Player and Dotted
=======
LEVELS
=======
.......
.xooot.
.......
o..o.o.
ox.o.ot
...o.o.
.......
"}) # note the extra empty line! ^

assert_int(len(parsed.levels)).is_equal(2)

0 comments on commit a45b174

Please sign in to comment.