Skip to content

Commit

Permalink
Prevent DMCompiler from being used multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Nov 15, 2024
1 parent 7396b75 commit 6281fbc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
53 changes: 26 additions & 27 deletions Content.Tests/DMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public sealed partial class DMTests : ContentUnitTest {
private const string TestProject = "DMProject";
private const string InitializeEnvironment = "./environment.dme";
private const string TestsDirectory = "Tests";
private DMCompiler.DMCompiler _dmCompiler;

[Dependency] private readonly DreamManager _dreamMan = default!;
[Dependency] private readonly DreamObjectTree _objectTree = default!;
Expand All @@ -38,16 +37,16 @@ public enum DMTestFlags {

[OneTimeSetUp]
public void OneTimeSetup() {
_dmCompiler = new DMCompiler.DMCompiler();
var dmCompiler = new DMCompiler.DMCompiler();
IoCManager.InjectDependencies(this);
_taskManager.Initialize();
Compile(InitializeEnvironment);
Compile(dmCompiler, InitializeEnvironment);
_dreamMan.PreInitialize(Path.ChangeExtension(InitializeEnvironment, "json"));
_dreamMan.OnException += OnException;
}

private string? Compile(string sourceFile) {
bool successfulCompile = _dmCompiler.Compile(new() {
private string? Compile(DMCompiler.DMCompiler compiler, string sourceFile) {
bool successfulCompile = compiler.Compile(new() {
Files = [sourceFile]
});

Expand All @@ -66,10 +65,11 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags, int errorCode) {
string initialDirectory = Directory.GetCurrentDirectory();
TestContext.WriteLine($"--- TEST {sourceFile} | Flags: {testFlags}");
try {
string? compiledFile = Compile(Path.Join(initialDirectory, TestsDirectory, sourceFile));
var dmCompiler = new DMCompiler.DMCompiler();
var compiledFile = Compile(dmCompiler, Path.Join(initialDirectory, TestsDirectory, sourceFile));
if (testFlags.HasFlag(DMTestFlags.CompileError)) {
Assert.That(errorCode == -1, Is.False, "Expected an error code");
Assert.That(_dmCompiler.UniqueEmissions.Contains((WarningCode)errorCode), Is.True, $"Expected error code \"{errorCode}\" was not found");
Assert.That(dmCompiler.UniqueEmissions.Contains((WarningCode)errorCode), Is.True, $"Expected error code \"{errorCode}\" was not found");
Assert.That(compiledFile, Is.Null, "Expected an error during DM compilation");

Cleanup(compiledFile);
Expand Down Expand Up @@ -169,29 +169,28 @@ private static DMTestFlags GetDMTestFlags(string sourceFile, out int errorCode)
DMTestFlags testFlags = DMTestFlags.NoError;
errorCode = -1; // If it's null GetTests() fusses about a NRE

using (StreamReader reader = new StreamReader(sourceFile)) {
string? firstLine = reader.ReadLine();
if (firstLine == null)
return testFlags;
if (firstLine.Contains("IGNORE", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.Ignore;
if (firstLine.Contains("COMPILE ERROR", StringComparison.InvariantCulture)) {
testFlags |= DMTestFlags.CompileError;

Match match = ErrorCodeRegex().Match(firstLine); // "OD" followed by exactly 4 numbers
if (match.Success) {
errorCode = int.Parse(match.Groups[1].Value);
}
using StreamReader reader = new StreamReader(sourceFile);
string? firstLine = reader.ReadLine();
if (firstLine == null)
return testFlags;
if (firstLine.Contains("IGNORE", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.Ignore;
if (firstLine.Contains("COMPILE ERROR", StringComparison.InvariantCulture)) {
testFlags |= DMTestFlags.CompileError;

Match match = ErrorCodeRegex().Match(firstLine); // "OD" followed by exactly 4 numbers
if (match.Success) {
errorCode = int.Parse(match.Groups[1].Value);
}

if (firstLine.Contains("RUNTIME ERROR", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.RuntimeError;
if (firstLine.Contains("RETURN TRUE", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.ReturnTrue;
if (firstLine.Contains("NO RETURN", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.NoReturn;
}

if (firstLine.Contains("RUNTIME ERROR", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.RuntimeError;
if (firstLine.Contains("RETURN TRUE", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.ReturnTrue;
if (firstLine.Contains("NO RETURN", StringComparison.InvariantCulture))
testFlags |= DMTestFlags.NoReturn;

return testFlags;
}

Expand Down
3 changes: 3 additions & 0 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public DMCompiler() {
}

public bool Compile(DMCompilerSettings settings) {
if (_compileStartTime != default)
throw new Exception("Create a new DMCompiler to compile again");

ErrorCount = 0;
WarningCount = 0;
UniqueEmissions.Clear();
Expand Down

0 comments on commit 6281fbc

Please sign in to comment.