About Monkey 2 › Forums › Monkey 2 Code Library › Simple Unit-Tests for Monkey2
This topic contains 3 replies, has 4 voices, and was last updated by 
 Amon
 1 year, 2 months ago.
		Viewing 4 posts - 1 through 4 (of 4 total)
	
	- 
		AuthorPosts
 - 
		
			
				
January 21, 2018 at 4:23 pm #13150
Hi,
this is my simple Unit-Test solution.
This code also contains test code.Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142#Import ""Using std.collectionsClass MX2UnitProtectedGlobal _classes:Stack = New Stack()Global _testSuccessful:= New Stack()Global _testFailed:= New Stack()Field _tests:Map<Void(), String> = New Map<Void(), String>()Field _name:StringPublicMethod New(className:String)_name = className_classes.Add(Self)End MethodFunction RunAllTests:Void(prepareFunc:Void() = Null)If (prepareFunc <> Null) Then prepareFunc()For Local c:=Eachin _classesc.RunTests()NextPrint " "Print "### RESULTS ###"Print " Successful:"For Local t:=Eachin _testSuccessfulPrint " - " + tNextPrint " "Print " Failed:"For Local t:=Eachin _testFailedPrint " - " + tNextEnd FunctionFunction TestAssert:Void(condition:Bool, failText:String)If (Not condition)Throw New MX2UnitException(failText)EndifEnd FunctionProtectedMethod AddTest:Void(testFuncName:String, testFunc:Void())_tests.Add(testFunc, testFuncName)End MethodPrivateMethod RunTests()For Local t:=Eachin _testsTryt.Key()_testSuccessful.Add("Class "+_name + " Func " + t.Value)Catch e:MX2UnitException_testFailed.Add("Class "+_name + " Func " + t.Value + ": " + e.Text)EndNextEnd MethodClass MX2UnitException Extends ThrowableField Text:StringMethod New(text:String)Text = textEnd MethodEnd ClassEndClass T1 Extends MX2UnitMethod New()Super.New("T1")AddTest("Test1", Test1)AddTest("Test2", Test2)AddTest("Test3", Test3)End MethodMethod Test1()TestAssert(True, "Test1.1")End MethodMethod Test2()TestAssert(True, "Test2.1")TestAssert(False, "Test2.2")End MethodMethod Test3()TestAssert(True, "Test3.1")End MethodEnd ClassClass T2 Extends MX2UnitMethod New()Super.New("T2")AddTest("Test1", Test1)AddTest("Test2", Test2)AddTest("Test3", Test3)End MethodMethod Test1()TestAssert(True, "Test1.1")End MethodMethod Test2()TestAssert(True, "Test2.1")TestAssert(False, "Test2.2")End MethodMethod Test3()TestAssert(True, "Test3.1")End MethodEnd ClassFunction Main()MX2Unit.RunAllTests(Lambda()New T1()New T2()End)EndJanuary 25, 2018 at 4:19 pm #13225January 26, 2018 at 11:01 am #13256Good one.
January 26, 2018 at 2:13 pm #13265 - 
		AuthorPosts
 
		Viewing 4 posts - 1 through 4 (of 4 total)
	
	You must be logged in to reply to this topic.