Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 6 additions & 43 deletions exercises/practice/sgf-parsing/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -1,49 +1,12 @@
json = require 'dkjson'
json_string = (s) -> json.encode s

is_sequence = (t) ->
return false if type(t) != 'table'
size = 0
size += 1 for k, _ in pairs t when k != 'n'
size == #t

is_empty = (t) -> not next t

-- mostly taken from:
-- https://github.com/leafo/moonscript/blob/7b7899741c6c1e971e436d36c9aabb56f51dc3d5/moonscript/util.moon#L58
to_string = (what, level = 0) ->
seen = {}
_dump = (what, depth = 0) ->
t = type what
if t == 'string' then
json_string what
elseif t != 'table' then
tostring what
else
if seen[what] then
return "<cycle:#{tostring what}>"
seen[what] = true
if is_sequence what then
return "{" .. table.concat([to_string(v, level + depth + 1) for v in *what], ", ") .. "}"

depth += 1
lines = for k,v in pairs what do
key = if type(k) == 'number' then "[#{k}]" else k
(' ')\rep(depth * 2) .. "#{key}: " .. _dump(v, depth)
seen[what] = false
val = if not is_empty lines
table.concat [indent(line, level) .. "\n" for line in *lines]
else
""
class_name = if type(what.__class) == 'table' and type(what.__class.__name) == 'string'
"<#{what.__class.__name}>"
"#{class_name or ""}{\n#{val}#{indent '}', level + depth - 1}"
_dump what

import json_string, table_dump from require 'test_helpers'

{
module_name: 'SGFParser',

test_helpers: [[
assert\set_parameter "TableFormatLevel", 5
]],

generate_test: (case, level) ->
lines = if case.expected.error
{
Expand All @@ -53,7 +16,7 @@ to_string = (what, level = 0) ->
else
{
"result = SGFParser.#{case.property} #{json_string case.input.encoded}",
"expected = #{to_string case.expected, level}",
"expected = #{table_dump case.expected, level}",
"assert.are.same expected, result"
}
table.concat [indent line, level for line in *lines], '\n'
Expand Down
2 changes: 2 additions & 0 deletions exercises/practice/sgf-parsing/sgf_parsing_spec.moon
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SGFParser = require 'sgf_parsing'

describe 'sgf-parsing', ->
assert\set_parameter "TableFormatLevel", 5

it 'empty input', ->
f = -> SGFParser.parse ""
assert.has_error f, 'tree missing'
Expand Down
Loading