forked from lucasmeijer/jamconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSharpRunner.cs
56 lines (49 loc) · 1.78 KB
/
CSharpRunner.cs
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
using System;
using System.Linq;
using NiceIO;
using NUnit.Framework;
using Unity.IL2CPP;
using System.Collections.Generic;
namespace jamconverter.Tests
{
class CSharpRunner
{
public string[] Run(SourceFileDescription[] program, NPath[] additionalLibs = null)
{
var tmpDir = NPath.CreateTempDirectory("Csharp");
var absoluteCSFiles = new List<NPath>();
foreach (var fileEntry in program)
{
var absolutePath = tmpDir.Combine(fileEntry.FileName);
absoluteCSFiles.Add(absolutePath);
var file = absolutePath.WriteAllText(fileEntry.Contents);
Console.WriteLine(".cs: " + file);
}
var csc = new NPath(Environment.OSVersion.Platform == PlatformID.Win32NT ? "C:/il2cpp-dependencies/Roslyn/Binaries/csc.exe" : "/usr/local/bin/mcs");
var executable = tmpDir.Combine("program.exe");
if (additionalLibs == null) additionalLibs = new NPath[0];
Shell.Execute(csc, absoluteCSFiles.InQuotes().SeperateWithSpace() + " "+additionalLibs.InQuotes().Select(l=>"-r:"+l).SeperateWithSpace()+ " -debug -out:" + executable);
foreach (var lib in additionalLibs)
lib.Copy(tmpDir);
return Shell.Execute(executable, "").Split(new[] {Environment.NewLine}, StringSplitOptions.None);
}
}
[TestFixture]
class CSharpRunnerTests
{
[Test]
public void CanRunSimpleProgram()
{
var program = @"
class Dummy {
static void Main()
{
System.Console.WriteLine(""Hello!"");
}
}
";
var output = new CSharpRunner().Run(new[] { new SourceFileDescription() { Contents = program, FileName = "Main.cs" }});
CollectionAssert.AreEqual(new[] {"Hello!"}, output);
}
}
}