Add more test cases (#11)

This commit is contained in:
Patrick Stevens
2025-05-20 22:09:02 +01:00
committed by GitHub
parent bc389f1f23
commit 109b3a70fc
7 changed files with 223 additions and 30 deletions

View File

@@ -9,12 +9,24 @@ open WoofWare.PawPrint
open WoofWare.PawPrint.Test
[<TestFixture>]
module TestNoOp =
module TestCases =
let assy = typeof<RunResult>.Assembly
[<Test>]
let ``Can run a no-op`` () : unit =
let source = Assembly.getEmbeddedResourceAsString "NoOp.cs" assy
let cases : TestCase list =
[
{
FileName = "NoOp.cs"
ExpectedReturnCode = 1
}
{
FileName = "TriangleNumber.cs"
ExpectedReturnCode = 10
}
]
[<TestCaseSource(nameof cases)>]
let ``Can run a no-op`` (case : TestCase) : unit =
let source = Assembly.getEmbeddedResourceAsString case.FileName assy
let image = Roslyn.compile [ source ]
let messages, loggerFactory = LoggerFactory.makeTest ()
@@ -28,10 +40,10 @@ module TestNoOp =
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
| [] -> failwith "expected program to return 1, but it returned void"
| [] -> failwith "expected program to return a value, but it returned void"
| head :: _ ->
match head with
| EvalStackValue.Int32 i -> i
| _ -> failwith "TODO"
| ret -> failwith "expected program to return an int, but it returned %O{ret}"
exitCode |> shouldEqual 1
exitCode |> shouldEqual case.ExpectedReturnCode

View File

@@ -15,3 +15,9 @@ type RunResult =
/// Final interpreter state after we stopped executing.
FinalState : IlMachineState
}
type TestCase =
{
FileName : string
ExpectedReturnCode : int
}

View File

@@ -1,5 +1,6 @@
namespace WoofWare.Pawprint.Test
open System
open System.Collections.Immutable
open System.IO
open FsUnitTyped
@@ -21,17 +22,23 @@ module TestHelloWorld =
let dotnetRuntimes =
DotnetRuntime.SelectForDll assy.Location |> ImmutableArray.CreateRange
use peImage = new MemoryStream (image)
try
use peImage = new MemoryStream (image)
let terminalState, terminatingThread =
Program.run loggerFactory peImage dotnetRuntimes []
let terminalState, terminatingThread =
Program.run loggerFactory peImage dotnetRuntimes []
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
| [] -> failwith "expected program to return 1, but it returned void"
| head :: _ ->
match head with
| EvalStackValue.Int32 i -> i
| _ -> failwith "TODO"
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
| [] -> failwith "expected program to return 1, but it returned void"
| head :: _ ->
match head with
| EvalStackValue.Int32 i -> i
| _ -> failwith "TODO"
exitCode |> shouldEqual 0
exitCode |> shouldEqual 0
with _ ->
for m in messages () do
Console.Error.WriteLine $"{m}"
reraise ()

View File

@@ -14,12 +14,13 @@
<Compile Include="Assembly.fs" />
<Compile Include="Roslyn.fs" />
<Compile Include="TestHarness.fs"/>
<Compile Include="TestNoOp.fs" />
<Compile Include="TestCases.fs" />
<Compile Include="TestHelloWorld.fs" />
<Compile Include="TestBasicLock.fs" />
<EmbeddedResource Include="sources\BasicLock.cs" />
<EmbeddedResource Include="sources\NoOp.cs" />
<EmbeddedResource Include="sources\HelloWorld.cs" />
<EmbeddedResource Include="sources\TriangleNumber.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,17 @@
using System;
namespace TriangleNumber
{
class Program
{
static int Main(string[] args)
{
var answer = 0;
for (int i = 0; i < 5; i++)
{
answer += i;
}
return answer;
}
}
}