using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace Luneraria.Core.Tests
{
///
/// Console runner: dotnet run --project tools/dotnet/Luneraria.Core.Tests -- [suite ...]
/// Writes docs/port/logs/u1/core-tests.json and exits non-zero on any failure.
///
public static class Program
{
public static int Main(string[] args)
{
string root = FindRoot();
string golden = Path.Combine(root, "Assets/Luneraria/Tests/Core/Golden");
if (args.Length == 4 && args[0] == "--image")
return ReplayImage.Write(golden, args[1], args[2], Path.GetFullPath(args[3]));
var wanted = new HashSet(args);
var report = new StringBuilder("{\n \"suites\": [\n");
int failures = 0;
bool first = true;
foreach (CheckSuite suite in CheckSuite.All())
{
if (wanted.Count > 0 && !wanted.Contains(suite.Name))
continue;
suite.GoldenDirectory = golden;
var clock = Stopwatch.StartNew();
bool ok = suite.Run();
clock.Stop();
failures += suite.Failures.Count;
Console.WriteLine("LUNE_U1_{0} {1}: {2} checks; {3} failures ({4:0.0} s)", ok ? "PASS" : "FAIL", suite.Name, suite.Checks, suite.Failures.Count, clock.Elapsed.TotalSeconds);
foreach (string failure in suite.Failures)
Console.WriteLine(" FAIL " + failure);
foreach (string note in suite.Notes)
Console.WriteLine(" note " + note);
report.Append(first ? "" : ",\n");
first = false;
report.AppendFormat(" {{\"suite\": \"{0}\", \"checks\": {1}, \"failures\": {2}, \"seconds\": {3:0.00}, \"failed\": [{4}], \"notes\": [{5}]}}",
suite.Name, suite.Checks, suite.Failures.Count, clock.Elapsed.TotalSeconds, Join(suite.Failures), Join(suite.Notes));
}
report.Append("\n ],\n \"runtime\": \"" + System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription + "\",\n");
report.Append(" \"os\": \"" + System.Runtime.InteropServices.RuntimeInformation.OSDescription + " " + System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture + "\"\n}\n");
if (wanted.Count == 0)
{
string logs = Path.Combine(root, "docs/port/logs/u1");
Directory.CreateDirectory(logs);
File.WriteAllText(Path.Combine(logs, "core-tests.json"), report.ToString());
}
Console.WriteLine(failures == 0 ? "LUNE_U1_TESTS_OK" : "LUNE_U1_TESTS_FAILED");
return failures == 0 ? 0 : 1;
}
static string Join(List values)
{
var parts = new List();
foreach (string value in values)
parts.Add("\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\n", "\\n") + "\"");
return string.Join(", ", parts);
}
static string FindRoot()
{
string dir = AppContext.BaseDirectory;
while (dir != null && !Directory.Exists(Path.Combine(dir, "Assets/Luneraria")))
dir = Path.GetDirectoryName(dir);
return dir ?? Directory.GetCurrentDirectory();
}
}
}