Used to generate a nitunit test file skeleton.

Introduced properties

private var _nodot: Array[String]

nitc :: NitUnitGenerator :: _nodot

Method names that do not need a "." in call.
private fun case_name(mmethoddef: MMethodDef): String

nitc :: NitUnitGenerator :: case_name

Generate case name based on MMethodDef.
private fun gen_call(method: MMethodDef, args: Array[String]): Writable

nitc :: NitUnitGenerator :: gen_call

Generate call to method using args.
private fun gen_decl(name: String, mtype: MType, mclassdef: MClassDef): String

nitc :: NitUnitGenerator :: gen_decl

private fun gen_init(mclassdef: MClassDef): Writable

nitc :: NitUnitGenerator :: gen_init

Generate subject init.
fun gen_unit(mmodule: MModule, test_file: String): Template

nitc :: NitUnitGenerator :: gen_unit

Generate the NitUnit test file skeleton for mmodule in test_file.
fun nodot: Array[String]

nitc :: NitUnitGenerator :: nodot

Method names that do not need a "." in call.
protected fun nodot=(nodot: Array[String])

nitc :: NitUnitGenerator :: nodot=

Method names that do not need a "." in call.
protected fun toolcontext=(toolcontext: ToolContext)

nitc :: NitUnitGenerator :: toolcontext=

Redefined properties

redef type SELF: NitUnitGenerator

nitc $ NitUnitGenerator :: 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
private var _nodot: Array[String]

nitc :: NitUnitGenerator :: _nodot

Method names that do not need a "." in call.
private fun case_name(mmethoddef: MMethodDef): String

nitc :: NitUnitGenerator :: case_name

Generate case name based on MMethodDef.
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.
private fun gen_call(method: MMethodDef, args: Array[String]): Writable

nitc :: NitUnitGenerator :: gen_call

Generate call to method using args.
private fun gen_decl(name: String, mtype: MType, mclassdef: MClassDef): String

nitc :: NitUnitGenerator :: gen_decl

private fun gen_init(mclassdef: MClassDef): Writable

nitc :: NitUnitGenerator :: gen_init

Generate subject init.
fun gen_unit(mmodule: MModule, test_file: String): Template

nitc :: NitUnitGenerator :: gen_unit

Generate the NitUnit test file skeleton for mmodule in test_file.
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.
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.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
fun nodot: Array[String]

nitc :: NitUnitGenerator :: nodot

Method names that do not need a "." in call.
protected fun nodot=(nodot: Array[String])

nitc :: NitUnitGenerator :: nodot=

Method names that do not need a "." in call.
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 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.
protected fun toolcontext=(toolcontext: ToolContext)

nitc :: NitUnitGenerator :: toolcontext=

package_diagram nitc::NitUnitGenerator NitUnitGenerator core::Object Object nitc::NitUnitGenerator->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

nitc $ NitUnitGenerator
# Used to generate a nitunit test file skeleton.
class NitUnitGenerator

	var toolcontext: ToolContext

	# Generate the NitUnit test file skeleton for `mmodule` in `test_file`.
	fun gen_unit(mmodule: MModule, test_file: String): Template do
		var with_private = toolcontext.opt_gen_private.value
		var tpl = new Template
		tpl.addn "module test_{mmodule.name} is test"
		tpl.addn ""
		if with_private then
			tpl.addn "intrude import {mmodule.name}"
		else
			tpl.addn "import {mmodule.name}"
		end
		for mclassdef in mmodule.mclassdefs do
			if mclassdef.mclass.kind != concrete_kind then continue
			tpl.addn ""
			tpl.addn "class Test{mclassdef.name}"
			tpl.addn "\ttest"
			for mpropdef in mclassdef.mpropdefs do
				if not mpropdef isa MMethodDef then continue
				var mproperty = mpropdef.mproperty
				if mpropdef.is_abstract then continue
				if mproperty.is_init then continue
				if not with_private and mproperty.visibility <= protected_visibility then continue
				var case_name = case_name(mpropdef)
				tpl.addn ""
				tpl.addn "\tfun {case_name} is test do"
				tpl.addn "\t\tassert not_implemented: false # TODO remove once implemented"
				tpl.addn ""
				tpl.addn gen_init(mclassdef)
				var args = new Array[String]
				for mparameter in mpropdef.msignature.mparameters do
					tpl.addn gen_decl(mparameter.name, mparameter.mtype, mclassdef)
					args.add mparameter.name
				end
				var return_mtype = mpropdef.msignature.return_mtype
				if return_mtype != null then
					tpl.addn gen_decl("exp", return_mtype, mclassdef)
					tpl.add "\t\tvar res = "
				else
					tpl.add "\t\t"
				end
				tpl.addn gen_call(mpropdef, args)
				if return_mtype != null then
					tpl.addn "\t\tassert exp == res"
				end
				tpl.addn "\tend"
			end
			tpl.addn "end"
		end
		return tpl
	end

	# Generate case name based on `MMethodDef`.
	# special method name like "[]", "+"... are filtered
	private fun case_name(mmethoddef: MMethodDef): String do
		var name = mmethoddef.name
		if name == "[]" then return "test_bra"
		if name == "[]=" then return "test_bra_assign"
		if name == "+" then return "test_plus"
		if name == "-" then return "test_minus"
		if name == "*" then return "test_star"
		if name == "/" then return "test_slash"
		if name == "%" then return "test_percent"
		if name == "unary -" then return "test_unary_minus"
		if name == "==" then return "test_equals"
		if name == "!=" then return "test_not_equals"
		if name == "<" then return "test_lt"
		if name == "<=" then return "test_le"
		if name == "<=>" then return "test_compare"
		if name == ">=" then return "test_ge"
		if name == ">" then return "test_gt"
		return "test_{name}"
	end

	# Method names that do not need a "." in call.
	var nodot: Array[String] = ["+", "-", "*", "/", "%", "==", "!=", "<", "<=", "<=>", ">", ">=", ">"]

	# Generate subject init.
	private fun gen_init(mclassdef: MClassDef): Writable do
		if mclassdef.mclass.arity == 0 then
			return "\t\tvar subject: {mclassdef.name}"
		end
		return "\t\tvar subject: {mclassdef.name}[{mclassdef.bound_mtype.arguments.join(", ")}]"
	end

	private fun gen_decl(name: String, mtype: MType, mclassdef: MClassDef): String do
		if mtype.need_anchor then
			mtype = mtype.anchor_to(mclassdef.mmodule, mclassdef.bound_mtype)
		end
		return "\t\tvar {name}: {mtype.to_s}"
	end

	# Generate call to `method` using `args`.
	private fun gen_call(method: MMethodDef, args: Array[String]): Writable do
		# Here we handle the magic of the Nit syntax, have fun :)
		var name = method.name
		if name == "[]" then return "subject[{args.join(", ")}]"
		if name == "[]=" then
			var last = args.pop
			return "subject[{args.join(", ")}] = {last}"
		end
		if name == "unary -" then return "-subject"
		var tpl = new Template
		if nodot.has(name) then
			tpl.add "subject {name}"
			if args.length == 1 then
				tpl.add " {args.first}"
			else if args.length > 1 then
				tpl.add " ({args.join(", ")})"
			end
			return tpl
		end
		if name.has_suffix("=") then
			name = name.substring(0, name.length - 1)
			var last = args.pop
			tpl.add "subject.{name}"
			if not args.is_empty then
				tpl.add "({args.join(", ")})"
			end
			tpl.add " = {last}"
			return tpl
		end
		tpl.add "subject.{name}"
		if args.length == 1 then
			tpl.add " {args.first}"
		else if args.length > 1 then
			tpl.add "({args.join(", ")})"
		end
		return tpl
	end
end
src/testing/testing_gen.nit:21,1--155,3