nitc :: NitUnitTester :: defaultinit
# Used to test nitunit test files.
class NitUnitTester
# `ModelBuilder` used to parse test files.
var mbuilder: ModelBuilder
# Compile and execute `mmodule` as a test suite.
fun test_module_unit(mmodule: MModule): TestSuite do
var toolcontext = mbuilder.toolcontext
var suite = new TestSuite(mmodule, toolcontext)
# method to execute before all tests in the module
for mmethod in mmodule.before_all do
toolcontext.modelbuilder.total_tests += 1
suite.before_all.add new TestCase(suite, mmethod, toolcontext)
end
# generate all test cases
for mclassdef in mmodule.mclassdefs do
if not mclassdef.is_test then continue
if not suite_match_pattern(mclassdef) then continue
toolcontext.modelbuilder.total_classes += 1
var test_class = new TestClass
# method to execute before all tests in the class
for mmethod in mclassdef.before_all do
toolcontext.modelbuilder.total_tests += 1
test_class.before_all.add new TestCase(suite, mmethod, toolcontext)
end
var before = mclassdef.before
var after = mclassdef.after
for mpropdef in mclassdef.mpropdefs do
if not mpropdef isa MMethodDef or not mpropdef.is_test then continue
if not case_match_pattern(mpropdef) then continue
toolcontext.modelbuilder.total_tests += 1
var test = new TestCase(suite, mpropdef, toolcontext)
test.before = before
test.after = after
test_class.test_cases.add test
end
# method to execute after all tests in the class
for mmethod in mclassdef.after_all do
toolcontext.modelbuilder.total_tests += 1
test_class.after_all.add new TestCase(suite, mmethod, toolcontext)
end
suite.test_classes.add test_class
end
# method to execute after all tests in the module
for mmethod in mmodule.after_all do
toolcontext.modelbuilder.total_tests += 1
suite.after_all.add new TestCase(suite, mmethod, toolcontext)
end
suite.run
return suite
end
# Is the test suite name match the pattern option?
private fun suite_match_pattern(suite: MClassDef): Bool do
var pattern = mbuilder.toolcontext.opt_pattern.value
if pattern == null then return true
var ps = pattern.split_with("::")
var p = ps.first
if ps.length == 1 and p.first.is_lower then return true
if ps.length == 2 and p.first.is_lower then return false
if p.has_suffix("*") then
p = p.substring(0, p.length - 1)
if suite.name.has_prefix(p) then return true
else
if suite.name == p then return true
end
return false
end
# Is the test case name match the pattern option?
private fun case_match_pattern(case: MPropDef): Bool do
var pattern = mbuilder.toolcontext.opt_pattern.value
if pattern == null then return true
var ps = pattern.split_with("::")
var p = ps.last
if ps.length == 1 and p.first.is_upper then return true
if ps.length == 2 and p.first.is_upper then return false
if p.has_suffix("*") then
p = p.substring(0, p.length - 1)
if case.name.has_prefix(p) then return true
else
if case.name == p then return true
end
return false
end
end
src/testing/testing_suite.nit:30,1--122,3