I am making a compiler from (a small subset of) Java to CIL (MSIL) in F# and I was thinking about writing some unit tests for the actual compilation part. Is there a way I could run ilasm on the generated IL code and the run the .exe file from a unit test? I am using NUnit.
Concretely, is there a way to do the following?
[<Test>]
member this.TestSimpleMain () =
let fileName = __SOURCE_DIRECTORY__.Replace("code", "examples\SimpleMain.cil")
// run ilasm on fileName -> should produce ...\SimpleMain.exe
let actualResult = // run ...\SimpleMain.exe file
Assert.That(actualResult , Is.EqualTo("Program returned value 5!")))
If you want to use ilasm make sure you create SimpleMain.cil
in IL assembly format (text representation of CIL).
Then you should create PE (exe/dll) file from your il file.
here is an example for the nunit test with output and exit code verification:
[<Test>]
member this.TestSimpleMain() =
// Code that creates SimpleMain.il
let p1 = new System.Diagnostics.Process()
p1.StartInfo.FileName <- "C:/Windows/Microsoft.NET/Framework64/v4.0.30319/ilasm.exe"
p1.StartInfo.Arguments <- "SimpleMain.il /exe /output=SimpleMain.exe"
p1.Start()
p1.WaitForExit()
let p2 = new System.Diagnostics.Process()
p2.StartInfo.FileName <- "SimpleMain.exe"
p2.StartInfo.RedirectStandardOutput <- true // For output verification
p2.Start()
p2.WaitForExit()
let resultA = p2.ExitCode // For exit code verification
let resultB = p2.StandardOutput.ReadToEnd() // For output verification
Assert.That(result, Is.EqualTo(expectedResult))