Refactor error/exception/message handling
This adds: - LuaCsSetup.ExceptionHandler so we can decide how we want to handle exceptions for unit tests - LuaCsSetup.MessageHandler so we can redirect logs to the XUnit output helper
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
using Barotrauma;
|
||||
using Microsoft.Xna.Framework;
|
||||
using MoonSharp.Interpreter;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
@@ -14,9 +13,6 @@ namespace TestProject.LuaCs
|
||||
|
||||
public HookPatchTests(ITestOutputHelper output)
|
||||
{
|
||||
Console.SetOut(new TestOutputTextWriterAdapter(output));
|
||||
Trace.Listeners.Add(new TestOutputTraceListenerAdapter(output));
|
||||
|
||||
UserData.RegisterType<TestValueType>();
|
||||
UserData.RegisterType<IBogusInterface>();
|
||||
UserData.RegisterType<InterfaceImplementingType>();
|
||||
@@ -27,6 +23,18 @@ namespace TestProject.LuaCs
|
||||
UserData.RegisterType<PatchTarget5>();
|
||||
UserData.RegisterType<PatchTarget6>();
|
||||
|
||||
luaCs.MessageLogger = (prefix, o) =>
|
||||
{
|
||||
o ??= "null";
|
||||
output.WriteLine(prefix + o);
|
||||
};
|
||||
luaCs.ExceptionHandler = (ex, _) =>
|
||||
{
|
||||
// Pretend we never caught the exception in the first place
|
||||
// (this allows us to preserve the stack trace)
|
||||
var di = ExceptionDispatchInfo.Capture(ex);
|
||||
di.Throw();
|
||||
};
|
||||
luaCs.Initialize();
|
||||
luaCs.Lua.Globals["TestValueType"] = UserData.CreateStatic<TestValueType>();
|
||||
luaCs.Lua.Globals["InterfaceImplementingType"] = UserData.CreateStatic<InterfaceImplementingType>();
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject.LuaCs
|
||||
{
|
||||
internal class TestOutputTextWriterAdapter : TextWriter
|
||||
{
|
||||
private readonly ITestOutputHelper output;
|
||||
|
||||
public TestOutputTextWriterAdapter(ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public override Encoding Encoding => Encoding.UTF8;
|
||||
|
||||
public override void WriteLine(string? message) => output.WriteLine(message);
|
||||
|
||||
public override void WriteLine(string? format, params object?[] args) => output.WriteLine(format, args);
|
||||
|
||||
public override void Write(char value) => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject.LuaCs
|
||||
{
|
||||
internal class TestOutputTraceListenerAdapter : TraceListener
|
||||
{
|
||||
private readonly ITestOutputHelper output;
|
||||
|
||||
public TestOutputTraceListenerAdapter(ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public override void Write(string? message) => throw new NotImplementedException();
|
||||
|
||||
public override void WriteLine(string? message) => output.WriteLine(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user