aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Custom/mod/scripts/vscripts/_testing.nut
blob: 15bcf18baa17258c39ecc9940a83e422f4b7d49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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()
{
	// 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 )
	//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()
{
	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 file.tests[ category ] )
	{
		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 )
}