-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTests.cs
More file actions
66 lines (55 loc) · 2.21 KB
/
UnitTests.cs
File metadata and controls
66 lines (55 loc) · 2.21 KB
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
using System;
using System.Collections.Generic;
namespace PurityEngine.UnitTests {
[Serializable]
public class UnitTestFailedException : System.Exception
{
public UnitTestFailedException() { }
public UnitTestFailedException(string message) : base(message) { }
public UnitTestFailedException(string message, System.Exception inner) : base(message, inner) { }
protected UnitTestFailedException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
public class UnitTest {
public static List<UnitTest> unitTests = new List<UnitTest>();
public delegate dynamic UnitTestBody();
public UnitTestBody task;
public string expectedOutput;
public string id;
public UnitTest(string id, UnitTestBody body, dynamic output) {
task = body;
expectedOutput = output;
this.id = id;
}
public static void InitializeUnitTests() {
Register(new UnitTest("ALCompilerTest",CompilerTest1,"if (noop) {noop}"));
}
public static dynamic CompilerTest1() {
AL.Compiler cmp = new AL.Compiler();
return cmp.Compile("if (noop) { noop }").ToString();
}
public static void Register(UnitTest t) {
if (!unitTests.Contains(t)) {
unitTests.Add(t);
}
}
public void _Run() {
dynamic output = task();
if (output != expectedOutput) {
throw new UnitTestFailedException("The task \"" + task.ToString() + "\" outputted " + output.ToString() + " but expected " + expectedOutput.ToString());
}
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("PASS");
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(" Test " + id + " completed.");
}
public static void Run() {
InitializeUnitTests();
foreach (UnitTest test in unitTests) {
test._Run();
}
Console.WriteLine("Unit tests ran successfully.");
}
}
}