Generate the test cases from the JSON testfile.

Introduced properties

init defaultinit(json_file: String, output_dir: String)

markdown2 :: TestGenerator :: defaultinit

fun gen_tests

markdown2 :: TestGenerator :: gen_tests

Generate the Nitunit test files
protected fun ignored_tests=(ignored_tests: Array[Int])

markdown2 :: TestGenerator :: ignored_tests=

Ignored tests
fun json_file: String

markdown2 :: TestGenerator :: json_file

Input file in containing the tests in JSON format
protected fun json_file=(json_file: String)

markdown2 :: TestGenerator :: json_file=

Input file in containing the tests in JSON format
fun load_tests: Map[String, Array[TestCase]]

markdown2 :: TestGenerator :: load_tests

Load the tests files from the JSON input
fun output_dir: String

markdown2 :: TestGenerator :: output_dir

Output directory where the Nitunit files will be generated
protected fun output_dir=(output_dir: String)

markdown2 :: TestGenerator :: output_dir=

Output directory where the Nitunit files will be generated

Redefined properties

redef type SELF: TestGenerator

markdown2 $ TestGenerator :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(json_file: String, output_dir: String)

markdown2 :: TestGenerator :: defaultinit

fun gen_tests

markdown2 :: TestGenerator :: gen_tests

Generate the Nitunit test files
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
protected fun ignored_tests=(ignored_tests: Array[Int])

markdown2 :: TestGenerator :: ignored_tests=

Ignored tests
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun json_file: String

markdown2 :: TestGenerator :: json_file

Input file in containing the tests in JSON format
protected fun json_file=(json_file: String)

markdown2 :: TestGenerator :: json_file=

Input file in containing the tests in JSON format
fun load_tests: Map[String, Array[TestCase]]

markdown2 :: TestGenerator :: load_tests

Load the tests files from the JSON input
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun output_dir: String

markdown2 :: TestGenerator :: output_dir

Output directory where the Nitunit files will be generated
protected fun output_dir=(output_dir: String)

markdown2 :: TestGenerator :: output_dir=

Output directory where the Nitunit files will be generated
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram markdown2::TestGenerator TestGenerator core::Object Object markdown2::TestGenerator->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

markdown2 $ TestGenerator
# 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