using System; using System.Linq; using UnitTestSharp; namespace Glass.UnitTests { public class ExceptionEntryTests : TestFixture { public static readonly ExceptionSite SiteA = ExceptionLogTests.SiteA; public static readonly ExceptionSite SiteB = ExceptionLogTests.SiteB; public class CtorTests : TestFixture { public void StampsTheFrameAndTimeAsBothFirstAndLast() { var entry = new ExceptionEntry(new InvalidOperationException(), frame: 2, time: TimeSpan.FromMilliseconds(20)); CheckEqual(2L, entry.FirstFrame); CheckEqual(2L, entry.LastFrame); CheckEqual(TimeSpan.FromMilliseconds(20), entry.FirstTime); CheckEqual(TimeSpan.FromMilliseconds(20), entry.LastTime); } public void KeepsTheException() { var thrown = new InvalidOperationException(); CheckEqual(thrown, new ExceptionEntry(thrown, frame: 0, time: TimeSpan.Zero).Exception); } public void StartsWithNoHits() { var entry = new ExceptionEntry(new InvalidOperationException(), frame: 0, time: TimeSpan.Zero); CheckEqual(0, entry.Count); CheckEqual(0, entry.Sites.Count); } public void StartsNeitherIgnoredNorSnoozed() { var entry = new ExceptionEntry(new InvalidOperationException(), frame: 0, time: TimeSpan.Zero); CheckFalse(entry.Ignored); CheckNull(entry.SnoozedUntil); } } public class RecordHitTests : TestFixture { private const string Pedigree = "window > okButton"; private ExceptionEntry entry; public override void TestSetup() { entry = new ExceptionEntry(new InvalidOperationException(), frame: 0, time: TimeSpan.Zero); } public void Always_CountsTheHit() { entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero); entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero); CheckEqual(2, entry.Count); } public void Always_StampsTheFrameAndTimeAsLastOnly() { entry.RecordHit(SiteA, Pedigree, frame: 1, time: TimeSpan.FromMilliseconds(16)); CheckEqual(0L, entry.FirstFrame); CheckEqual(TimeSpan.Zero, entry.FirstTime); CheckEqual(1L, entry.LastFrame); CheckEqual(TimeSpan.FromMilliseconds(16), entry.LastTime); } public void NewSite_IsAddedWithItsPedigreeAndReportedAsNew() { CheckTrue(entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero)); CheckEqual(new[] { SiteA }, entry.Sites.ToArray()); CheckEqual(new[] { Pedigree }, entry.PedigreesOf(SiteA).ToArray()); } public void RepeatedSiteAndPedigree_AreNeitherAddedAgainNorReportedAsNew() { entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero); CheckFalse(entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero)); CheckEqual(new[] { SiteA }, entry.Sites.ToArray()); CheckEqual(new[] { Pedigree }, entry.PedigreesOf(SiteA).ToArray()); } public void RepeatedSiteByAnotherPath_KeepsBothPedigreesButIsNotReportedAsNew() { entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero); CheckFalse(entry.RecordHit(SiteA, "dialog > okButton", frame: 0, time: TimeSpan.Zero)); CheckEqualCommutative(new[] { Pedigree, "dialog > okButton" }, entry.PedigreesOf(SiteA)); } public void SeveralSites_AreEachKeptOnce() { entry.RecordHit(SiteB, Pedigree, frame: 0, time: TimeSpan.Zero); entry.RecordHit(SiteA, Pedigree, frame: 0, time: TimeSpan.Zero); entry.RecordHit(SiteB, Pedigree, frame: 0, time: TimeSpan.Zero); CheckEqualCommutative(new[] { SiteA, SiteB }, entry.Sites); } } public class PedigreesOfTests : TestFixture { public void SiteThatNeverHit_HasNone() { var entry = new ExceptionEntry(new InvalidOperationException(), frame: 0, time: TimeSpan.Zero); entry.RecordHit(SiteA, "window > okButton", frame: 0, time: TimeSpan.Zero); CheckEqual(0, entry.PedigreesOf(SiteB).Count); } } public class IsSkippingAtTests : TestFixture { private static readonly TimeSpan Now = TimeSpan.FromMilliseconds(16); private ExceptionEntry entry; public override void TestSetup() { entry = new ExceptionEntry(new InvalidOperationException(), frame: 0, time: TimeSpan.Zero); } public void NeitherIgnoredNorSnoozed_IsNotSkipping() { CheckFalse(entry.IsSkippingAt(Now)); } public void Ignored_IsSkipping() { entry.Ignored = true; CheckTrue(entry.IsSkippingAt(Now)); } public void SnoozedUntilLater_IsSkipping() { entry.SnoozedUntil = Now + TimeSpan.FromTicks(1); CheckTrue(entry.IsSkippingAt(Now)); } public void SnoozedUntilNow_IsNotSkipping() { entry.SnoozedUntil = Now; CheckFalse(entry.IsSkippingAt(Now)); } public void IgnoredWithAnExpiredSnooze_IsSkipping() { entry.Ignored = true; entry.SnoozedUntil = TimeSpan.Zero; CheckTrue(entry.IsSkippingAt(Now)); } } } }