forked from exercism/moonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-spec
More file actions
executable file
·166 lines (120 loc) · 4.51 KB
/
generate-spec
File metadata and controls
executable file
·166 lines (120 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env moon
-- ref: https://github.com/exercism/lua/blob/main/bin/generate-spec
require 'moonscript'
json = (require 'dkjson').use_lpeg!
-- import p from require 'moon'
local exercise_name, exercise_directory, spec_generator, included_tests -- forward declarations
file_exists = (path) ->
f = io.open path, 'r'
if f
f\close!
true
else
false
read_file = (path) ->
f = assert io.open path, 'r'
contents = f\read '*a'
f\close!
contents
write_file = (path, contents) ->
f = assert io.open path, 'w'
f\write contents
f\close!
included_tests_from_toml = (path) ->
included = {}
local uuid, last_uuid
line_no = 0
for line in io.lines(path)
line_no += 1
for uuid in line\gmatch('%[([%x%-]+)%]')
last_uuid = uuid
included[uuid] = true
if line\match('^include%s*=%s*false')
included[last_uuid] = nil
included
-- ----------------------------------------------------------
-- functions marked as global so spec_generators can see them
export indent, quote, is_json_null, is_empty, contains
indent = (text, level) ->
error 'Provide a level for `indent`', 2 if not level
string.rep(' ', level) .. text
quote = (str) ->
if str\find "'"
"\"#{str\gsub '"', '\\"'}\""
else
"'#{str}'"
is_empty = (t) -> not next t
-- the dkjson `json.null` value is an empty table
is_json_null = (value) -> type(value) == 'table' and is_empty(value)
-- a table contains a value
contains = (t, v) ->
for elem in *t
return true if elem == v
false
-- ----------------------------------------------------------
test_cmd = 'it'
process = (node, level=0) ->
-- exclude the test cases in this node?
for exclusion in *(spec_generator.exclusions or {})
if node[exclusion.key]
if exclusion.op == 'contains'
return '' if contains node[exclusion.key], exclusion.value
else
return '' if node[exclusion.key] == exclusion.value
if node.cases
output = {}
if node.description
table.insert output, indent("describe #{quote node.description}, ->", level)
else
table.insert output, indent("describe '#{exercise_name}', ->", level)
if spec_generator.test_helpers
table.insert output, spec_generator.test_helpers
cases = {}
for case in *node.cases
if not case.uuid or included_tests[case.uuid]
table.insert cases, process(case, level + 1)
table.insert output, table.concat(cases, '\n')
return table.concat output, '\n'
else -- no "cases" member
test = "#{test_cmd} #{quote node.description}, ->\n#{spec_generator.generate_test(node, level + 1)}\n"
test_cmd = 'pending'
return indent test, level
-- ----------------------------------------------------------
-- "main"
-- ----------------------------------------------------------
exercise_name = arg[1]
snake_name = exercise_name\gsub("-", "_")
-- to differentiate from the lua rock "say" required by busted.
if snake_name == 'say'
snake_name = './say'
exercise_directory = 'exercises/practice/' .. exercise_name
canonical_data_url = "https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/#{exercise_name}/canonical-data.json"
canonical_data_path = "canonical-data/#{exercise_name}.json"
assert os.execute('mkdir -p "$(dirname "' .. canonical_data_path .. '")"')
assert os.execute('curl "' .. canonical_data_url .. '" -s -o "' .. canonical_data_path .. '"')
-- "json.null" ref: https://dkolf.de/dkjson-lua/documentation
canonical_data = json.decode read_file(canonical_data_path), 1, json.null
tests_toml_path = exercise_directory .. '/.meta/tests.toml'
included_tests = included_tests_from_toml tests_toml_path
package.moonpath = "#{exercise_directory}/.meta/?.moon;./lib/?.moon;#{package.moonpath}"
spec_generator = require 'spec_generator'
local spec
if spec_generator.module_name
spec = "#{spec_generator.module_name} = require '#{snake_name}'"
elseif spec_generator.module_imports
spec = "import #{table.concat spec_generator.module_imports, ', '} from require '#{snake_name}'"
else
error 'spec_generator is missing both "module_name" and "module_imports"'
spec ..= "\n\n" .. process(canonical_data)
if spec_generator.bonus
spec ..= [[
-- The next tests are optional.
-- Set the environment variable BONUS_TESTS to run them:
-- For example, in bash run: BONUS_TESTS=true busted
if os.getenv('BONUS_TESTS') == 'true'
describe 'Bonus tests', ->
]]
spec ..= spec_generator.bonus
spec_path = exercise_directory .. '/' .. snake_name .. '_spec.moon'
write_file spec_path, spec
print "Created #{spec_path}"