1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Generate Nitunit tests from commonmark specification.
17 # See the full specification and the test cases at <http://commonmark.org/>.
22 # commonmark_gen <commonmark_tests.json> <output directory>
30 # Generate the test cases from the JSON testfile.
33 # Input file in containing the tests in JSON format
36 # Output directory where the Nitunit files will be generated
37 var output_dir
: String
41 # We ignore some tests for two reasons:
42 # * because `nitmd` does not fully support UTF-8
43 # * because somes tests are wrong
44 var ignored_tests
: Array[Int] do
45 var ignored
= new Array[Int]
46 ignored
.add_all
([171, 304, 305, 306, 311, 312, 313, 477, 514]) # utf-8 tests
47 ignored
.add_all
([275, 276]) # spec is wrong compared to reference implementation
51 # Load the tests files from the JSON input
52 fun load_tests
: Map[String, Array[TestCase]] do
53 var json
= json_file
.to_path
.read_all
.parse_json
54 var tests
= new HashMap[String, Array[TestCase]]
56 for obj
in json
.as(JsonArray) do
57 if not obj
isa JsonObject then continue
59 var number
= obj
["example"].as(Int)
60 if ignored_tests
.has
(number
) then continue
62 var name
= "test{number}"
64 var section
= obj
["section"].as(String)
65 if not tests
.has_key
(section
) then
66 tests
[section
] = new Array[TestCase]
69 var markdown
= obj
["markdown"].as(String)
70 markdown
= markdown
.replace
("\\", "\\\\")
71 markdown
= markdown
.replace
("\n", "\\n")
72 markdown
= markdown
.replace
("\t", "\\t")
74 # fix missing chars in some tests
76 markdown
= markdown
.replace
("my url", "my%20url")
77 else if number
== 467 then
78 markdown
= markdown
.replace
("my uri", "my%20uri")
81 var html
= obj
["html"].as(String)
82 html
= html
.replace
("\\", "\\\\")
83 html
= html
.replace
("\n", "\\n")
84 html
= html
.replace
("\t", "\\t")
86 tests
[section
].add
(new TestCase(name
, markdown
, html
))
92 # Generate the Nitunit test files
94 var tests
= load_tests
96 for section
, test_cases
in tests
do
97 var test_file
= new TestFile("test_commonmark_{strip_module_name(section)}")
98 var test_class
= new TestClass("TestCommonmark{strip_class_name(section)}")
99 test_class
.test_cases
.add_all test_cases
100 test_file
.test_classes
.add test_class
101 test_file
.save
(output_dir
)
107 # Used to create a Nitunit module name from a CommonMark section title.
108 fun strip_module_name
(name
: String): String do
109 var b
= new FlatBuffer
114 if not c
.is_letter
and
116 not allowed_id_chars
.has
(c
) then continue
125 # Used to create a Nitunit test class name from a CommonMark section title.
126 fun strip_class_name
(name
: String): String do
127 var b
= new FlatBuffer
128 var was_space
= false
133 if not c
.is_letter
and
135 not allowed_id_chars
.has
(c
) then continue
147 private var allowed_id_chars
: Array[Char] = ['-', '_', ':', '.']
150 # A Nitunit test file
154 var test_file_name
: String
156 # Test classes in this module
157 var test_classes
= new Array[TestClass]
159 # Copyright header and module declaration
160 fun header
: String do
162 # This file is part of NIT ( http://www.nitlanguage.org ).
164 # Licensed under the Apache License, Version 2.0 (the "License");
165 # you may not use this file except in compliance with the License.
166 # You may obtain a copy of the License at
168 # http://www.apache.org/licenses/LICENSE-2.0
170 # Unless required by applicable law or agreed to in writing, software
171 # distributed under the License is distributed on an "AS IS" BASIS,
172 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
173 # See the License for the specific language governing permissions and
174 # limitations under the License.
176 module {{{test_file_name}}} is test
182 # Render the test module as a Nit string
183 fun render
: String do
184 var tpl
= new Template
186 for test_class
in test_classes
do
187 tpl
.add test_class
.render
189 return tpl
.write_to_string
192 # Save the test module in `directory
193 fun save
(directory
: String) do
194 render
.write_to_file
(directory
/ "{test_file_name}.nit")
198 # A Nitunit test class
202 var test_class_name
: String
204 # Test cases in this test class
205 var test_cases
= new Array[TestCase]
207 # Render the test class as a Nit string
208 fun render
: String do
209 var tpl
= new Template
210 tpl
.addn
"\nclass {test_class_name}"
211 tpl
.addn
"\tsuper TestMarkdownHtml"
213 for test_case
in test_cases
do
214 tpl
.add test_case
.render
217 return tpl
.write_to_string
221 # A Nitunit test case
225 var test_name
: String
230 # Expected html output
233 # Render the test case as a Nit string
234 fun render
: String do
235 var tpl
= new Template
236 tpl
.addn
"\n\tfun {test_name} is test do"
237 tpl
.addn
"\t\tvar md = \"\
"\"{markdown}\
"\"\
""
238 tpl
.addn
"\t\tvar html = \"\
"\"{html}\
"\"\
""
239 tpl
.addn
"\t\tassert md_to_html(md) == html"
241 return tpl
.write_to_string
245 if args
.length
!= 2 then
246 print
"Usage: commonmark_gen <tests.json> <output_dir>"
250 var gen
= new TestGenerator(args
.first
, args
.last
)