Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Oct 3, 2024
1 parent 7a71d16 commit cfa5ba4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
29 changes: 12 additions & 17 deletions Content.Tests/DMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DMCompiler.Compiler;
using NUnit.Framework;
using OpenDreamRuntime;
using OpenDreamRuntime.Objects;
Expand Down Expand Up @@ -59,24 +60,19 @@ private static void Cleanup(string? compiledFile) {
}

[Test, TestCaseSource(nameof(GetTests))]
public void TestFiles(string sourceFile, DMTestFlags testFlags, string errorCode) {
public void TestFiles(string sourceFile, DMTestFlags testFlags, int errorCode) {
string initialDirectory = Directory.GetCurrentDirectory();
TestContext.WriteLine($"--- TEST {sourceFile} | Flags: {testFlags}");
try {
if (testFlags.HasFlag(DMTestFlags.CompileError)) {
Assert.That(string.IsNullOrEmpty(errorCode), Is.False, "Expected an error code");
var originalConsoleOut = Console.Out;
StringWriter consoleOutput = new StringWriter();
Console.SetOut(consoleOutput);
Assert.That(errorCode == -1, Is.False, "Expected an error code");
string? compileErrorFile = Compile(Path.Join(initialDirectory, TestsDirectory, sourceFile));
string output = consoleOutput.ToString();
Console.SetOut(originalConsoleOut);
Console.WriteLine(output);
Assert.That(output.Contains(errorCode), Is.True, $"Expected error code \"{errorCode}\" was not found");

Assert.That(DMCompiler.DMCompiler.UniqueEmissions.Contains((WarningCode)errorCode), Is.True, $"Expected error code \"{errorCode}\" was not found");
Assert.That(compileErrorFile, Is.Null, "Expected an error during DM compilation");

Cleanup(compileErrorFile);
TestContext.WriteLine($"--- PASS {sourceFile}");
TestContext.WriteLine(output);
return;
}

Expand Down Expand Up @@ -167,9 +163,9 @@ private static IEnumerable<object[]> GetTests()
}
}

private static DMTestFlags GetDMTestFlags(string sourceFile, out string errorCode) {
private static DMTestFlags GetDMTestFlags(string sourceFile, out int errorCode) {
DMTestFlags testFlags = DMTestFlags.NoError;
errorCode = string.Empty;
errorCode = -1; // If it's null GetTests() fusses about a NRE

using (StreamReader reader = new StreamReader(sourceFile)) {
string? firstLine = reader.ReadLine();
Expand All @@ -179,11 +175,10 @@ private static DMTestFlags GetDMTestFlags(string sourceFile, out string errorCod
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) {
Console.WriteLine($"\"COMPILE ERROR\" test \"{sourceFile}\" does not specify an error code");
} else {
errorCode = match.Value;
if (match.Success) {
errorCode = int.Parse(match.Groups[1].Value);
}
}

Expand All @@ -198,7 +193,7 @@ private static DMTestFlags GetDMTestFlags(string sourceFile, out string errorCod
return testFlags;
}

[GeneratedRegex(@"OD[0-9]{4}")]
[GeneratedRegex(@"OD([0-9]{4})")]
private static partial Regex ErrorCodeRegex();

// TODO Convert the below async tests
Expand Down
2 changes: 2 additions & 0 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace DMCompiler;
public static class DMCompiler {
public static int ErrorCount;
public static int WarningCount;
public static HashSet<WarningCode> UniqueEmissions = new();
public static DMCompilerSettings Settings;
public static IReadOnlyList<string> ResourceDirectories => _resourceDirectories;

Expand Down Expand Up @@ -187,6 +188,7 @@ public static void Emit(CompilerEmission emission) {
break;
}

UniqueEmissions.Add(emission.Code);
Console.WriteLine(emission);
}

Expand Down

0 comments on commit cfa5ba4

Please sign in to comment.