Skip to content

Commit 7f2fa3e

Browse files
authored
sgf-parsing uses test_helpers library (#148)
Also, set the TableFormatLevel to 5 so failed tests show complete expected value
1 parent 18051a1 commit 7f2fa3e

2 files changed

Lines changed: 8 additions & 43 deletions

File tree

exercises/practice/sgf-parsing/.meta/spec_generator.moon

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
1-
json = require 'dkjson'
2-
json_string = (s) -> json.encode s
3-
4-
is_sequence = (t) ->
5-
return false if type(t) != 'table'
6-
size = 0
7-
size += 1 for k, _ in pairs t when k != 'n'
8-
size == #t
9-
10-
is_empty = (t) -> not next t
11-
12-
-- mostly taken from:
13-
-- https://github.com/leafo/moonscript/blob/7b7899741c6c1e971e436d36c9aabb56f51dc3d5/moonscript/util.moon#L58
14-
to_string = (what, level = 0) ->
15-
seen = {}
16-
_dump = (what, depth = 0) ->
17-
t = type what
18-
if t == 'string' then
19-
json_string what
20-
elseif t != 'table' then
21-
tostring what
22-
else
23-
if seen[what] then
24-
return "<cycle:#{tostring what}>"
25-
seen[what] = true
26-
if is_sequence what then
27-
return "{" .. table.concat([to_string(v, level + depth + 1) for v in *what], ", ") .. "}"
28-
29-
depth += 1
30-
lines = for k,v in pairs what do
31-
key = if type(k) == 'number' then "[#{k}]" else k
32-
(' ')\rep(depth * 2) .. "#{key}: " .. _dump(v, depth)
33-
seen[what] = false
34-
val = if not is_empty lines
35-
table.concat [indent(line, level) .. "\n" for line in *lines]
36-
else
37-
""
38-
class_name = if type(what.__class) == 'table' and type(what.__class.__name) == 'string'
39-
"<#{what.__class.__name}>"
40-
"#{class_name or ""}{\n#{val}#{indent '}', level + depth - 1}"
41-
_dump what
42-
1+
import json_string, table_dump from require 'test_helpers'
432

443
{
454
module_name: 'SGFParser',
465

6+
test_helpers: [[
7+
assert\set_parameter "TableFormatLevel", 5
8+
]],
9+
4710
generate_test: (case, level) ->
4811
lines = if case.expected.error
4912
{
@@ -53,7 +16,7 @@ to_string = (what, level = 0) ->
5316
else
5417
{
5518
"result = SGFParser.#{case.property} #{json_string case.input.encoded}",
56-
"expected = #{to_string case.expected, level}",
19+
"expected = #{table_dump case.expected, level}",
5720
"assert.are.same expected, result"
5821
}
5922
table.concat [indent line, level for line in *lines], '\n'

exercises/practice/sgf-parsing/sgf_parsing_spec.moon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
SGFParser = require 'sgf_parsing'
22

33
describe 'sgf-parsing', ->
4+
assert\set_parameter "TableFormatLevel", 5
5+
46
it 'empty input', ->
57
f = -> SGFParser.parse ""
68
assert.has_error f, 'tree missing'

0 commit comments

Comments
 (0)