add compile & test worker
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
namespace LiquidCode.Tester.Worker.Services;
|
||||
|
||||
public class OutputCheckerService : IOutputCheckerService
|
||||
{
|
||||
private readonly ILogger<OutputCheckerService> _logger;
|
||||
|
||||
public OutputCheckerService(ILogger<OutputCheckerService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> CheckOutputAsync(string actualOutput, string expectedOutputPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var expectedOutput = await File.ReadAllTextAsync(expectedOutputPath);
|
||||
|
||||
// Normalize outputs for comparison
|
||||
var normalizedActual = NormalizeOutput(actualOutput);
|
||||
var normalizedExpected = NormalizeOutput(expectedOutput);
|
||||
|
||||
var match = normalizedActual == normalizedExpected;
|
||||
|
||||
if (!match)
|
||||
{
|
||||
_logger.LogDebug("Output mismatch. Expected length: {ExpectedLength}, Actual length: {ActualLength}",
|
||||
normalizedExpected.Length, normalizedActual.Length);
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error checking output against {ExpectedFile}", expectedOutputPath);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private string NormalizeOutput(string output)
|
||||
{
|
||||
// Remove trailing whitespace from each line and normalize line endings
|
||||
var lines = output.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)
|
||||
.Select(line => line.TrimEnd())
|
||||
.ToList();
|
||||
|
||||
// Remove trailing empty lines
|
||||
while (lines.Count > 0 && string.IsNullOrWhiteSpace(lines[^1]))
|
||||
{
|
||||
lines.RemoveAt(lines.Count - 1);
|
||||
}
|
||||
|
||||
return string.Join("\n", lines);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user