using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Amphora;
using Blacklight.Core;
using Glass;
using UnitTestSharp;
using Action = System.Action;
namespace Foundations.UnitTests
{
public class ExceptionReporterTests : TestFixture
{
public const ReportChoices Muting = ReportChoices.Snooze | ReportChoices.Ignore;
public static readonly ExceptionSite Control = new ExceptionSite(new object(), phase: 0);
public static readonly Duration Second = Duration.FromMilliseconds(1000);
public static ExceptionReporter Player(Action quit = null)
{
return new ExceptionReporter(developerMode: false, quit ?? (() => { }));
}
public static ExceptionReporter Developer(Action quit = null)
{
return new ExceptionReporter(developerMode: true, quit ?? (() => { }));
}
///
/// Records one bug of the given severity. Calling it again with the same type counts another hit.
///
public static ExceptionEntry Throw(ExceptionReporter reporter,
ExceptionSeverity severity = ExceptionSeverity.Contained, Exception exception = null)
{
return reporter.Sandbox.Record(exception ?? new InvalidOperationException(), Control, notes: null,
severity);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowFromGameCode()
{
throw new InvalidOperationException("Thrown by game code.");
}
///
/// Has game code throw at a boundary that quarantines it.
///
public static ExceptionEntry Quarantine(ExceptionReporter reporter)
{
try
{
ThrowFromGameCode();
}
catch (Exception exception)
when (reporter.Sandbox.Claims(exception, Control, notes: null, quarantine: true))
{
}
return reporter.Sandbox.Entries.Single();
}
///
/// Ends with an exception.
///
public static ExceptionEntry Stop(ExceptionReporter reporter, HostWork work)
{
reporter.Catches(new InvalidOperationException(), work);
return reporter.Sandbox.Entries.Single();
}
public class CanCarryOnFromTests : TestFixture
{
public void AnOrdinaryException_Can()
{
CheckTrue(ExceptionReporter.CanCarryOnFrom(new InvalidOperationException()));
}
public void RunningOutOfMemory_Cannot()
{
CheckFalse(ExceptionReporter.CanCarryOnFrom(new OutOfMemoryException()));
}
public void ADeviceFailure_Cannot()
{
CheckFalse(ExceptionReporter.CanCarryOnFrom(
new GraphicsDeviceFailedException(GraphicsDeviceFailure.Removed, innerException: null)));
}
}
public class CtorTests : TestFixture
{
public void NoQuit_Throws()
{
CheckThrow(typeof(ArgumentNullException));
new ExceptionReporter(developerMode: false, quit: null);
}
public void KeepsWhetherADeveloperAnswers()
{
CheckTrue(Developer().DeveloperMode);
CheckFalse(Player().DeveloperMode);
}
public void NoAnchors_SandboxesWhateverIsBuiltOnGlass()
{
CheckEqual(new[] { typeof(Screen).Assembly }, Player().Sandbox.Anchors.ToArray());
}
public void Anchors_AreAddedToGlass()
{
Assembly other = typeof(TestFixture).Assembly;
var reporter = new ExceptionReporter(developerMode: false, quit: () => { },
anchors: new[] { other });
CheckEqual(new[] { typeof(Screen).Assembly, other }, reporter.Sandbox.Anchors.ToArray());
}
public void GlassAsAnAnchor_IsListedOnce()
{
var reporter = new ExceptionReporter(developerMode: false, quit: () => { },
anchors: new[] { typeof(Screen).Assembly });
CheckEqual(1, reporter.Sandbox.Anchors.Count);
}
}
public class CurrentTests : TestFixture
{
public void NothingThrown_IsNull()
{
CheckNull(Player().Current);
}
public void ANewException_IsShown()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Throw(reporter);
CheckEqual(entry, reporter.Current);
}
public void AMoreSevereOne_IsShownFirst()
{
ExceptionReporter reporter = Player();
Throw(reporter, ExceptionSeverity.Contained);
ExceptionEntry fatal = Throw(reporter, ExceptionSeverity.Fatal);
Throw(reporter, ExceptionSeverity.StateSuspect);
CheckEqual(fatal, reporter.Current);
}
public void OfTwoAsSevere_TheEarlierIsShown()
{
ExceptionReporter reporter = Player();
ExceptionEntry first = Throw(reporter, exception: new InvalidOperationException());
Throw(reporter, exception: new ArgumentException());
CheckEqual(first, reporter.Current);
}
public void AnAnsweredOne_GivesWayToTheNext()
{
ExceptionReporter reporter = Player();
ExceptionEntry first = Throw(reporter, exception: new InvalidOperationException());
ExceptionEntry second = Throw(reporter, exception: new ArgumentException());
reporter.Continue(first);
CheckEqual(second, reporter.Current);
}
public void OneSnoozedElsewhere_IsNotShown()
{
ExceptionReporter reporter = Player();
reporter.Sandbox.Snooze(Throw(reporter), Second);
CheckNull(reporter.Current);
}
public void OneIgnoredElsewhere_IsNotShown()
{
ExceptionReporter reporter = Player();
Throw(reporter).Ignored = true;
CheckNull(reporter.Current);
}
}
public class IsWaitingOnTests : TestFixture
{
public void ANewException_IsTrue()
{
ExceptionReporter reporter = Player();
CheckTrue(reporter.IsWaitingOn(Throw(reporter)));
}
public void AnAnsweredException_IsFalse()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Throw(reporter);
reporter.Continue(entry);
CheckFalse(reporter.IsWaitingOn(entry));
}
}
public class IsReportingTests : TestFixture
{
public void NothingThrown_IsFalse()
{
CheckFalse(Player().IsReporting);
}
public void AnExceptionWaiting_IsTrue()
{
ExceptionReporter reporter = Player();
Throw(reporter);
CheckTrue(reporter.IsReporting);
}
}
public class ChoicesForTests : TestFixture
{
public void NoEntry_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Player().ChoicesFor(null);
}
public void Player_Contained_CanOnlyContinue()
{
ExceptionReporter reporter = Player();
CheckEqual(ReportChoices.Continue,
reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.Contained)));
}
public void Player_StateSuspect_CanContinueOrQuit()
{
ExceptionReporter reporter = Player();
CheckEqual(ReportChoices.Continue | ReportChoices.Quit,
reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.StateSuspect)));
}
public void Player_Fatal_CanOnlyQuit()
{
ExceptionReporter reporter = Player();
CheckEqual(ReportChoices.Quit, reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.Fatal)));
}
public void Player_Quarantined_CanOnlyContinue()
{
ExceptionReporter reporter = Player();
CheckEqual(ReportChoices.Continue, reporter.ChoicesFor(Quarantine(reporter)));
}
public void Developer_Contained_CanAlsoMute()
{
ExceptionReporter reporter = Developer();
CheckEqual(ReportChoices.Continue | Muting,
reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.Contained)));
}
public void Developer_StateSuspect_CanAlsoMuteOrQuit()
{
ExceptionReporter reporter = Developer();
CheckEqual(ReportChoices.Continue | Muting | ReportChoices.Quit,
reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.StateSuspect)));
}
public void Developer_Fatal_CanOnlyQuit()
{
ExceptionReporter reporter = Developer();
CheckEqual(ReportChoices.Quit, reporter.ChoicesFor(Throw(reporter, ExceptionSeverity.Fatal)));
}
public void Developer_Quarantined_CanAlsoRelease()
{
ExceptionReporter reporter = Developer();
CheckEqual(ReportChoices.Continue | Muting | ReportChoices.Release,
reporter.ChoicesFor(Quarantine(reporter)));
}
public void Developer_StoppedWork_CanContinueOrQuit()
{
ExceptionReporter reporter = Developer();
CheckEqual(ReportChoices.Continue | ReportChoices.Quit,
reporter.ChoicesFor(Stop(reporter, new HostWork("simulation", () => { }))));
}
public void Player_StoppedWork_CanOnlyQuit()
{
ExceptionReporter reporter = Player();
CheckEqual(ReportChoices.Quit,
reporter.ChoicesFor(Stop(reporter, new HostWork("simulation", () => { }))));
}
}
public class ContinueTests : TestFixture
{
private int resumed;
private HostWork simulation;
public override void TestSetup()
{
resumed = 0;
simulation = new HostWork("simulation", () => resumed++);
}
public void AFatalOne_Throws()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Throw(reporter, ExceptionSeverity.Fatal);
CheckThrow(typeof(InvalidOperationException));
reporter.Continue(entry);
}
public void ClosesTheReport()
{
ExceptionReporter reporter = Developer();
reporter.Continue(Throw(reporter));
CheckFalse(reporter.IsReporting);
}
public void Developer_ThrownAgain_IsShownAgain()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Throw(reporter);
reporter.Continue(entry);
Throw(reporter);
CheckEqual(entry, reporter.Current);
}
public void Player_ThrownAgain_StaysClosed()
{
ExceptionReporter reporter = Player();
reporter.Continue(Throw(reporter));
Throw(reporter);
CheckNull(reporter.Current);
}
public void Player_LeavesTheCodeRunning()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Throw(reporter);
reporter.Continue(entry);
CheckFalse(entry.Ignored);
CheckFalse(reporter.Sandbox.IsSkipped(Control));
}
public void StoppedWork_IsResumed()
{
ExceptionReporter reporter = Developer();
reporter.Continue(Stop(reporter, simulation));
CheckEqual(1, resumed);
}
public void StoppedWorkThatFailsAtOnce_IsShownAgain()
{
ExceptionReporter reporter = Developer();
HostWork failing = null;
failing = new HostWork("simulation",
() => reporter.Catches(new InvalidOperationException(), failing));
ExceptionEntry entry = Stop(reporter, failing);
reporter.Continue(entry);
CheckTrue(reporter.IsReporting);
}
public void ResumingThatThrows_ShowsWhatItThrew()
{
ExceptionReporter reporter = Developer();
var thrown = new NotSupportedException("still broken");
var failing = new HostWork("simulation", () => throw thrown);
ExceptionEntry entry = Stop(reporter, failing);
reporter.Continue(entry);
CheckEqual(thrown, reporter.Current.Exception);
CheckEqual(failing, reporter.Current.Sites.Single().Target);
}
public void ResumingThatRunsOutOfMemory_IsNotCaught()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Stop(reporter,
new HostWork("simulation", () => throw new OutOfMemoryException()));
CheckThrow(typeof(OutOfMemoryException));
reporter.Continue(entry);
}
public void ResumingThatAddsAnotherSite_ResumesTheFirstOnce()
{
ExceptionReporter reporter = Developer();
var other = new HostWork("render");
var failing = new HostWork("simulation", () =>
{
resumed++;
reporter.Catches(new InvalidOperationException(), other);
});
ExceptionEntry entry = Stop(reporter, failing);
reporter.Continue(entry);
CheckEqual(1, resumed);
CheckEqual(2, entry.Sites.Count);
}
public void Refused_DoesNotResume()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Stop(reporter, simulation);
bool refused = false;
try
{
reporter.Continue(entry);
}
catch (InvalidOperationException)
{
refused = true;
}
CheckTrue(refused);
CheckEqual(0, resumed);
}
}
public class SnoozeTests : TestFixture
{
private ExceptionReporter reporter;
private ExceptionEntry entry;
public override void TestSetup()
{
reporter = Developer();
entry = Throw(reporter);
}
public void Player_Throws()
{
ExceptionReporter player = Player();
ExceptionEntry thrown = Throw(player);
CheckThrow(typeof(InvalidOperationException));
player.Snooze(thrown, Second);
}
public void SkipsTheCodeForTheDuration()
{
reporter.Snooze(entry, Second);
CheckEqual(Second, entry.SnoozedUntil);
CheckTrue(reporter.Sandbox.IsSkipped(Control));
}
public void ClosesTheReport()
{
reporter.Snooze(entry, Second);
CheckFalse(reporter.IsReporting);
}
public void Expired_StaysClosedUntilThrownAgain()
{
reporter.Snooze(entry, Second);
reporter.Sandbox.Advance(Second);
CheckFalse(reporter.IsReporting);
Throw(reporter);
CheckEqual(entry, reporter.Current);
}
}
public class IgnoreTests : TestFixture
{
public void Player_Throws()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Throw(reporter);
CheckThrow(typeof(InvalidOperationException));
reporter.Ignore(entry);
}
public void SkipsTheCodeAndClosesTheReport()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Throw(reporter);
reporter.Ignore(entry);
CheckTrue(entry.Ignored);
CheckFalse(reporter.IsReporting);
}
public void NoLongerIgnored_StaysClosedUntilThrownAgain()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Throw(reporter);
reporter.Ignore(entry);
entry.Ignored = false;
CheckFalse(reporter.IsReporting);
}
}
public class ReleaseTests : TestFixture
{
public void Player_Throws()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Quarantine(reporter);
CheckThrow(typeof(InvalidOperationException));
reporter.Release(entry);
}
public void NothingQuarantined_Throws()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Throw(reporter);
CheckThrow(typeof(InvalidOperationException));
reporter.Release(entry);
}
public void LetsTheCodeRunAndClosesTheReport()
{
ExceptionReporter reporter = Developer();
ExceptionEntry entry = Quarantine(reporter);
reporter.Release(entry);
CheckFalse(reporter.Sandbox.IsQuarantined(Control));
CheckFalse(reporter.IsReporting);
}
}
public class QuitTests : TestFixture
{
private int quits;
public override void TestSetup()
{
quits = 0;
}
public void EndsTheProgram()
{
ExceptionReporter reporter = Player(() => quits++);
reporter.Quit(Throw(reporter, ExceptionSeverity.Fatal));
CheckEqual(1, quits);
}
public void LeavesTheReportOpen()
{
ExceptionReporter reporter = Player(() => quits++);
ExceptionEntry entry = Throw(reporter, ExceptionSeverity.Fatal);
reporter.Quit(entry);
CheckEqual(entry, reporter.Current);
}
public void QuittingThatThrows_IsReported()
{
var thrown = new NotSupportedException("won't close");
ExceptionReporter reporter = Player(() => throw thrown);
ExceptionEntry entry = Throw(reporter, ExceptionSeverity.Fatal);
reporter.Quit(entry);
CheckEqual(thrown, reporter.Sandbox.Entries[1].Exception);
CheckEqual("quit", reporter.Sandbox.Entries[1].Sites.Single().Target.ToString());
}
public void QuittingThatRunsOutOfMemory_IsNotCaught()
{
ExceptionReporter reporter = Player(() => throw new OutOfMemoryException());
ExceptionEntry entry = Throw(reporter, ExceptionSeverity.Fatal);
CheckThrow(typeof(OutOfMemoryException));
reporter.Quit(entry);
}
public void WhereNotAChoice_Throws()
{
ExceptionReporter reporter = Player(() => quits++);
ExceptionEntry entry = Throw(reporter, ExceptionSeverity.Contained);
CheckThrow(typeof(InvalidOperationException));
reporter.Quit(entry);
}
}
public class ReportOfTests : TestFixture
{
public void IsTheReportOfTheEntry()
{
ExceptionReporter reporter = Player();
ExceptionEntry entry = Throw(reporter);
CheckEqual(ExceptionReport.Write(entry, environment: null), reporter.ReportOf(entry));
}
public void EndsWithTheEnvironmentAsGiven()
{
var environment = new List>
{
new KeyValuePair("System", "TestOS"),
};
var reporter = new ExceptionReporter(developerMode: false, quit: () => { }, environment);
ExceptionEntry entry = Throw(reporter);
environment.Clear();
CheckTrue(reporter.ReportOf(entry).Contains("System: TestOS"));
}
}
public class CatchesTests : TestFixture
{
private ExceptionReporter reporter;
private HostWork update;
public override void TestSetup()
{
reporter = Player();
update = new HostWork("update");
}
public void NoWork_Throws()
{
CheckThrow(typeof(ArgumentNullException));
reporter.Catches(new InvalidOperationException(), null);
}
public void AsAFilter_RecordsThatStateMayBeWrong()
{
var thrown = new InvalidOperationException();
bool caught = false;
try
{
throw thrown;
}
catch (Exception exception) when (reporter.Catches(exception, update))
{
caught = true;
}
CheckTrue(caught);
ExceptionEntry entry = reporter.Sandbox.Entries.Single();
CheckEqual(thrown, entry.Exception);
CheckEqual(ExceptionSeverity.StateSuspect, entry.Severity);
CheckEqual(update, entry.Sites.Single().Target);
}
public void RunningOutOfMemory_IsLeftAlone()
{
CheckFalse(reporter.Catches(new OutOfMemoryException(), update));
CheckEqual(0, reporter.Sandbox.Entries.Count);
}
public void AGraphicsDeviceFailure_IsLeftAlone()
{
var failure = new GraphicsDeviceFailedException(default, new InvalidOperationException());
CheckFalse(reporter.Catches(failure, update));
CheckEqual(0, reporter.Sandbox.Entries.Count);
}
}
public class PostTests : TestFixture
{
public void NoWork_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Player().Post(new InvalidOperationException(), null);
}
public void IsShownAfterTheNextAdvance()
{
ExceptionReporter reporter = Player();
var simulation = new HostWork("simulation", () => { });
reporter.Post(new InvalidOperationException(), simulation);
CheckFalse(reporter.IsReporting);
reporter.Sandbox.Advance(Duration.Zero);
CheckEqual(ExceptionSeverity.StateSuspect, reporter.Current.Severity);
CheckEqual(simulation, reporter.Current.Sites.Single().Target);
}
}
}
}