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 '
0 commit comments