-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathBaseIntegrationTest.cs
More file actions
116 lines (99 loc) · 3.5 KB
/
BaseIntegrationTest.cs
File metadata and controls
116 lines (99 loc) · 3.5 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
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
using kOS.Safe.Compilation;
using kOS.Safe.Compilation.KS;
using kOS.Safe.Encapsulation;
using kOS.Safe.Execution;
using kOS.Safe.Function;
using kOS.Safe.Persistence;
using kOS.Safe.Utilities;
using NUnit.Framework;
using System;
using System.IO;
namespace kOS.Safe.Test.Execution
{
[SetUpFixture]
public class StaticSetup
{
[SetUp]
public void Setup()
{
SafeHouse.Init(new Config(), new VersionInfo(0, 0, 0, 0), "", false, "./");
SafeHouse.Logger = new NoopLogger();
try
{
AssemblyWalkAttribute.Walk();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(e.StackTrace);
throw;
}
}
}
public abstract class BaseIntegrationTest
{
private ICpu cpu;
private SafeSharedObjects shared;
private Screen screen;
private string baseDir;
protected virtual OptimizationLevel OptimizationLevel => OptimizationLevel.None;
private string FindKerboscriptTests()
{
var currentDir = Directory.GetCurrentDirectory();
while (!Directory.Exists(Path.Combine(currentDir, "kerboscript_tests")))
{
currentDir = Directory.GetParent(currentDir).FullName;
}
return Path.Combine(currentDir, "kerboscript_tests");
}
[SetUp]
public void Setup()
{
baseDir = FindKerboscriptTests();
screen = new Screen();
shared = new SafeSharedObjects();
shared.FunctionManager = new FunctionManager(shared);
shared.GameEventDispatchManager = new NoopGameEventDispatchManager();
shared.Processor = new NoopProcessor();
shared.ScriptHandler = new KSScript();
shared.Screen = screen;
shared.UpdateHandler = new UpdateHandler();
shared.VolumeMgr = new VolumeManager();
shared.FunctionManager.Load();
Archive archive = new Archive(baseDir);
shared.VolumeMgr.Add(archive);
shared.VolumeMgr.SwitchTo(archive);
cpu = new CPU(shared);
}
protected void RunScript(string fileName)
{
string contents = File.ReadAllText(Path.Combine(baseDir, fileName));
GlobalPath path = shared.VolumeMgr.GlobalPathFromObject("0:/" + fileName);
var compiled = shared.ScriptHandler.Compile(path, 1, contents, "test", new CompilerOptions()
{
LoadProgramsInSameAddressSpace = false,
IsCalledFromRun = false,
FuncManager = shared.FunctionManager,
BindManager = shared.BindingMgr,
AllowClobberBuiltins = SafeHouse.Config.AllowClobberBuiltIns,
OptimizationLevel = OptimizationLevel
});
cpu.Boot();
screen.ClearOutput();
cpu.PushArgumentStack(new KOSArgMarkerType());
cpu.GetCurrentContext().AddParts(compiled);
}
protected void RunSingleStep()
{
shared.UpdateHandler.UpdateFixedObservers(0.01);
if (cpu.InstructionsThisUpdate == SafeHouse.Config.InstructionsPerUpdate)
{
throw new Exception("Script did not finish");
}
}
protected void AssertOutput(params string[] expected)
{
screen.AssertOutput(expected);
}
}
}