Property definitions

markdown2 $ TestGenerator :: defaultinit
# Generate the test cases from the JSON testfile.
class TestGenerator

	# Input file in containing the tests in JSON format
	var json_file: String

	# Output directory where the Nitunit files will be generated
	var output_dir: String

	# Ignored tests
	#
	# We ignore some tests for two reasons:
	# * because `nitmd` does not fully support UTF-8
	# * because somes tests are wrong
	var ignored_tests: Array[Int] do
		var ignored = new Array[Int]
		ignored.add_all([171, 304, 305, 306, 311, 312, 313, 477, 514]) # utf-8 tests
		ignored.add_all([275, 276]) # spec is wrong compared to reference implementation
		return ignored
	end

	# Load the tests files from the JSON input
	fun load_tests: Map[String, Array[TestCase]] do
		var json = json_file.to_path.read_all.parse_json
		var tests = new HashMap[String, Array[TestCase]]

		for obj in json.as(JsonArray) do
			if not obj isa JsonObject then continue

			var number = obj["example"].as(Int)
			if ignored_tests.has(number) then continue

			var name = "test{number}"

			var section = obj["section"].as(String)
			if not tests.has_key(section) then
				tests[section] = new Array[TestCase]
			end

			var markdown = obj["markdown"].as(String)
			markdown = markdown.replace("\\", "\\\\")
			markdown = markdown.replace("\n", "\\n")
			markdown = markdown.replace("\t", "\\t")

			# fix missing chars in some tests
			if number == 162 then
				markdown = markdown.replace("my url", "my%20url")
			else if number == 467 then
				markdown = markdown.replace("my uri", "my%20uri")
			end

			var html = obj["html"].as(String)
			html = html.replace("\\", "\\\\")
			html = html.replace("\n", "\\n")
			html = html.replace("\t", "\\t")

			tests[section].add(new TestCase(name, markdown, html))
		end

		return tests
	end

	# Generate the Nitunit test files
	fun gen_tests do
		var tests = load_tests

		for section, test_cases in tests do
			var test_file = new TestFile("test_commonmark_{strip_module_name(section)}")
			var test_class = new TestClass("TestCommonmark{strip_class_name(section)}")
			test_class.test_cases.add_all test_cases
			test_file.test_classes.add test_class
			test_file.save(output_dir)
		end
	end

	# Strip module name
	#
	# Used to create a Nitunit module name from a CommonMark section title.
	fun strip_module_name(name: String): String do
		var b = new FlatBuffer
		for c in name do
			if c == ' ' then
				b.add '_'
			else
				if not c.is_letter and
				   not c.is_digit and
				   not allowed_id_chars.has(c) then continue
				b.add c.to_lower
			end
		end
		return b.to_s
	end

	# Strip class name
	#
	# Used to create a Nitunit test class name from a CommonMark section title.
	fun strip_class_name(name: String): String do
		var b = new FlatBuffer
		var was_space = false
		for c in name do
			if c == ' ' then
				was_space = true
			else
				if not c.is_letter and
				   not c.is_digit and
				   not allowed_id_chars.has(c) then continue
				if was_space then
					b.add c.to_upper
					was_space = false
				else
					b.add c
				end
			end
		end
		return b.to_s
	end

	private var allowed_id_chars: Array[Char] = ['-', '_', ':', '.']
end
lib/markdown2/tests/commonmark_gen.nit:30,1--148,3