From 919987acff39885fb974b0261a66f0e9d38ba957 Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Wed, 13 Sep 2023 22:04:39 +0100 Subject: [PATCH 1/8] unit testing i guess --- Northstar.Custom/mod.json | 13 + .../mod/scripts/vscripts/_testing.nut | 294 ++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 Northstar.Custom/mod/scripts/vscripts/_testing.nut diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json index 22f9c45fd..ae62fe387 100644 --- a/Northstar.Custom/mod.json +++ b/Northstar.Custom/mod.json @@ -434,6 +434,19 @@ { "Path": "sh_northstar_safe_io.gnut", "RunOn": "CLIENT || SERVER || UI" + }, + { + "Path": "_testing.nut", + "RunOn": "CLIENT || SERVER || UI", + "ClientCallback": { + "Before": "Testing_Init" + }, + "ServerCallback": { + "Before": "Testing_Init" + }, + "UICallback": { + "Before": "Testing_Init" + } } ], diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut new file mode 100644 index 000000000..336d571ae --- /dev/null +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -0,0 +1,294 @@ +global function Testing_Init +global function RunAllTests +global function RunAllTests_SaveToFile +global function RunTestsByCategory +global function RunTestByCategoryAndName + +global function AddTest + +struct TestInfo +{ + string testName + var functionref() callback + // whether the test completed successfully + // if this is true, actualResult is valid + bool completed + // var not string because then i can just set it to an exception + // which print can then handle + var error + // whether the test is considered successful + var expectedResult + var actualResult + bool passed +} + +struct { + table< string, array< TestInfo > > tests = {} +} file + +void function Testing_Init() +{ + // string testCategory, string testName, var functionref() testFunc, var expectedResult + AddTest( "Example Tests", "example succeeding test", ExampleTest_ReturnsTrue, true ) + AddTest( "Example Tests", "example failing test", ExampleTest_ReturnsFalse, true ) + AddTest( "Example Tests", "example erroring test", ExampleTest_ThrowsError, true ) +} + +var function ExampleTest_ReturnsFalse() +{ + return false +} + +var function ExampleTest_ReturnsTrue() +{ + return true +} + +var function ExampleTest_ThrowsError() +{ + throw "Example exception" + return null +} + +void function RunAllTests_SaveToFile() +{ + RunAllTests() + + #if UI + string fileName = "ns-unit-tests-UI.json" + #elseif CLIENT + string fileName = "ns-unit-tests-CLIENT.json" + #elseif SERVER + string fileName = "ns-unit-tests-SERVER.json" + #endif + + // cant encode structs so have to reconstruct a table manually from the structs + table out = {} + foreach ( category, tests in file.tests ) + { + array categoryResults = [] + foreach ( test in tests ) + { + table testTable = {} + testTable[ "name" ] <- test.testName + testTable[ "completed" ] <- test.completed + testTable[ "passed" ] <- test.passed + if ( !test.completed ) + testTable[ "error" ] <- test.error + else if ( !test.passed ) + { + testTable[ "expectedResult" ] <- test.expectedResult + testTable[ "actualResult" ] <- test.actualResult + } + + categoryResults.append( testTable ) + } + out[ category ] <- categoryResults + } + + NSSaveJSONFile( fileName, out ) +} + +void function RunAllTests() +{ + printt( "Running all tests!" ) + + foreach ( category, categoryTests in file.tests ) + { + foreach ( test in categoryTests ) + { + RunTest( test ) + } + } + + PrintAllTestResults() +} + +void function RunTestsByCategory( string category ) +{ + if ( !( category in file.tests ) ) + { + printt( format( "Category '%s' has no tests registered", category ) ) + return + } + + foreach ( categoryTest in file.tests[ category ] ) + { + RunTest( categoryTest ) + } +} + +void function RunTestByCategoryAndName( string category, string testName ) +{ + // find test + if ( !( category in file.tests ) ) + { + printt( format( "Category '%s' has no tests registered", category ) ) + return + } + + TestInfo ornull foundTest = null + foreach ( categoryTest in file.tests[ category ] ) + { + if ( categoryTest.testName == testName ) + { + foundTest = categoryTest + break + } + } + + if ( !foundTest ) + { + printt( format( "Category '%s' does not contain a test with name '%s'", category, testName ) ) + return + } + + expect TestInfo( foundTest ) + + printt( "Running test!" ) + // run test + RunTest( foundTest ) + // print result + PrintTestResult( foundTest ) +} + +void function RunTest( TestInfo test ) +{ + test.completed = false + test.passed = false + test.actualResult = null + test.error = "" + + try + { + test.actualResult = test.callback() + test.completed = true + test.passed = test.actualResult == test.expectedResult + } + catch ( exception ) + { + test.completed = false + test.error = exception + } +} + +void function PrintAllTestResults() +{ + int totalSucceeded = 0 + int totalFailed = 0 + int totalErrored = 0 + + foreach ( category, categoryTests in file.tests ) + { + int categorySucceeded = 0 + int categoryFailed = 0 + int categoryErrored = 0 + + printt( format( "Results for category: '%s'", category ) ) + foreach ( test in categoryTests ) + { + if ( test.completed ) + { + if ( test.passed ) + { + printt( "\t", test.testName, "- Passed!" ) + categorySucceeded++ + } + else + { + printt( "\t", test.testName, "- Failed!" ) + printt( "\t\tExpected:", test.expectedResult ) + printt( "\t\tActual: ", test.actualResult ) + categoryFailed++ + } + } + else + { + printt( "\t", test.testName, "- Errored!" ) + printt( "\t\tError:", test.error ) + categoryErrored++ + } + } + + printt( "Succeeded:", categorySucceeded, "Failed:", categoryFailed, "Errored:", categoryErrored ) + + totalSucceeded += categorySucceeded + totalFailed += categoryFailed + totalErrored += categoryErrored + } + + printt( "TOTAL SUCCEEDED:", totalSucceeded, "TOTAL FAILED:", totalFailed, "TOTAL ERRORED:", totalErrored ) +} + +void function PrintCategoryResults( string category ) +{ + int categorySucceeded = 0 + int categoryFailed = 0 + int categoryErrored = 0 + + printt( format( "Results for category: '%s'", category ) ) + foreach ( test in categoryTests ) + { + if ( test.completed ) + { + if ( test.passed ) + { + printt( "\t", test.testName, "- Passed!" ) + categorySucceeded++ + } + else + { + printt( "\t", test.testName, "- Failed!" ) + printt( "\t\tExpected:", test.expectedResult ) + printt( "\t\tActual: ", test.actualResult ) + categoryFailed++ + } + } + else + { + printt( "\t", test.testName, "- Errored!" ) + printt( "\t\tError:", test.error ) + categoryErrored++ + } + } + + printt( "Succeeded:", categorySucceeded, "Failed:", categoryFailed, "Errored:", categoryErrored ) +} + +void function PrintTestResult( TestInfo test ) +{ + string resultString = test.testName + + if ( test.completed ) + { + if ( test.passed ) + resultString += " - Passed!" + else + { + resultString += " - Failed!" + resultString += "\n\tExpected: " + test.expectedResult + resultString += "\n\tActual: " + test.actualResult + } + } + else + { + resultString += " - Not completed!" + resultString += "\n\tError: " + test.error + } + + printt( resultString ) +} + +void function AddTest( string testCategory, string testName, var functionref() testFunc, var expectedResult ) +{ + TestInfo newTest + newTest.testName = testName + newTest.callback = testFunc + newTest.expectedResult = expectedResult + + // create the test category if it doesn't exist + if ( !( testCategory in file.tests ) ) + file.tests[ testCategory ] <- [ newTest ] + else + file.tests[ testCategory ].append( newTest ) +} \ No newline at end of file From 1cb413d94be990def3c31851ea6b555e2b797d73 Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Wed, 13 Sep 2023 22:07:51 +0100 Subject: [PATCH 2/8] im actually stupid though --- Northstar.Custom/mod/scripts/vscripts/_testing.nut | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut index 336d571ae..7e8d0e643 100644 --- a/Northstar.Custom/mod/scripts/vscripts/_testing.nut +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -227,7 +227,7 @@ void function PrintCategoryResults( string category ) int categoryErrored = 0 printt( format( "Results for category: '%s'", category ) ) - foreach ( test in categoryTests ) + foreach ( test in file.tests[ category ] ) { if ( test.completed ) { From eac57ea3dac9c0a9ae70fcd5f2fbb701cb3ececa Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Wed, 13 Sep 2023 22:12:27 +0100 Subject: [PATCH 3/8] comment out the testing functions used for testing the testing logic --- Northstar.Custom/mod/scripts/vscripts/_testing.nut | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut index 7e8d0e643..8c4b4cb4f 100644 --- a/Northstar.Custom/mod/scripts/vscripts/_testing.nut +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -28,10 +28,10 @@ struct { void function Testing_Init() { - // string testCategory, string testName, var functionref() testFunc, var expectedResult - AddTest( "Example Tests", "example succeeding test", ExampleTest_ReturnsTrue, true ) - AddTest( "Example Tests", "example failing test", ExampleTest_ReturnsFalse, true ) - AddTest( "Example Tests", "example erroring test", ExampleTest_ThrowsError, true ) + // tests for the testing functions :) + //AddTest( "Example Tests", "example succeeding test", ExampleTest_ReturnsTrue, true ) + //AddTest( "Example Tests", "example failing test", ExampleTest_ReturnsFalse, true ) + //AddTest( "Example Tests", "example erroring test", ExampleTest_ThrowsError, true ) } var function ExampleTest_ReturnsFalse() From f2233c4b329f10ec3bd7fbd4516a6f555a7253dd Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Wed, 13 Sep 2023 22:17:17 +0100 Subject: [PATCH 4/8] provide example test for args + different return types --- Northstar.Custom/mod/scripts/vscripts/_testing.nut | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut index 8c4b4cb4f..2ce564568 100644 --- a/Northstar.Custom/mod/scripts/vscripts/_testing.nut +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -32,6 +32,14 @@ void function Testing_Init() //AddTest( "Example Tests", "example succeeding test", ExampleTest_ReturnsTrue, true ) //AddTest( "Example Tests", "example failing test", ExampleTest_ReturnsFalse, true ) //AddTest( "Example Tests", "example erroring test", ExampleTest_ThrowsError, true ) + AddTest( "Example Tests", "example test with args", var function() { + return ExampleTest_HasArgs_ReturnsNonVar( 2, 3 ) + }, 6 ) +} + +int function ExampleTest_HasArgs_ReturnsNonVar( int first, int second ) +{ + return first * second } var function ExampleTest_ReturnsFalse() From b9c185625be51635ef97fe8339087303f68b1ba8 Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Wed, 13 Sep 2023 22:19:07 +0100 Subject: [PATCH 5/8] comment out the new test --- Northstar.Custom/mod/scripts/vscripts/_testing.nut | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut index 2ce564568..0232ec5f3 100644 --- a/Northstar.Custom/mod/scripts/vscripts/_testing.nut +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -32,9 +32,9 @@ void function Testing_Init() //AddTest( "Example Tests", "example succeeding test", ExampleTest_ReturnsTrue, true ) //AddTest( "Example Tests", "example failing test", ExampleTest_ReturnsFalse, true ) //AddTest( "Example Tests", "example erroring test", ExampleTest_ThrowsError, true ) - AddTest( "Example Tests", "example test with args", var function() { - return ExampleTest_HasArgs_ReturnsNonVar( 2, 3 ) - }, 6 ) + //AddTest( "Example Tests", "example test with args", var function() { + // return ExampleTest_HasArgs_ReturnsNonVar( 2, 3 ) + //}, 6 ) } int function ExampleTest_HasArgs_ReturnsNonVar( int first, int second ) From 5414c3fdad5ed86e82d0d925a064a7d3263c7b53 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Tue, 10 Oct 2023 13:14:28 +0200 Subject: [PATCH 6/8] Add missing trailing newline --- Northstar.Custom/mod/scripts/vscripts/_testing.nut | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/_testing.nut b/Northstar.Custom/mod/scripts/vscripts/_testing.nut index 0232ec5f3..15bcf18ba 100644 --- a/Northstar.Custom/mod/scripts/vscripts/_testing.nut +++ b/Northstar.Custom/mod/scripts/vscripts/_testing.nut @@ -299,4 +299,4 @@ void function AddTest( string testCategory, string testName, var functionref() t file.tests[ testCategory ] <- [ newTest ] else file.tests[ testCategory ].append( newTest ) -} \ No newline at end of file +} From 6ba22a10f5261e4c8e70511ed3f392c816697ac7 Mon Sep 17 00:00:00 2001 From: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Thu, 14 Dec 2023 21:46:39 +0000 Subject: [PATCH 7/8] Actually indent properly --- Northstar.Custom/mod.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json index d6e36d092..d2d369190 100644 --- a/Northstar.Custom/mod.json +++ b/Northstar.Custom/mod.json @@ -451,9 +451,9 @@ }, "UICallback": { "Before": "Testing_Init" - } - }, - { + } + }, + { "Path": "_event_models.gnut", "RunOn": "SERVER && LOBBY", "ServerCallback": { @@ -472,4 +472,4 @@ "Localisation": [ "resource/northstar_custom_%language%.txt" ] -} \ No newline at end of file +} From e9731bfe82844113b362aff19cf4c565d013cc30 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Fri, 12 Apr 2024 01:42:08 +0200 Subject: [PATCH 8/8] Revert unrelated style change --- Northstar.Custom/mod.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json index d2d369190..69432f498 100644 --- a/Northstar.Custom/mod.json +++ b/Northstar.Custom/mod.json @@ -472,4 +472,4 @@ "Localisation": [ "resource/northstar_custom_%language%.txt" ] -} +} \ No newline at end of file