using System; using System.Threading; using Developpez.Dotnet.Diagnostics; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Diagnostics { [TestFixture] public class AutoStopwatchTests { [Test] public void Test_AutoStopWatch_NoAction() { using (var asw = new AutoStopwatch()) { Thread.Sleep(100); asw.Stop(); Assert.GreaterOrEqual(asw.ElapsedMilliseconds, 0); } } [Test] public void Test_AutoStopWatch_ActionIsCalled() { bool wasCalled = false; using (new AutoStopwatch(ts => wasCalled = true)) { } Assert.IsTrue(wasCalled); } [Test] public void Test_AutoStopWatch_ActionIsCalledOnlyOnce() { int nbCalls = 0; using (var asw = new AutoStopwatch(ts => nbCalls++)) { (asw as IDisposable).Dispose(); // Disposed } // Disposed again... Assert.IsTrue(nbCalls == 1); } } }