From 287cb03517ca2c85702b50bce2ec6701b73247a4 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 16 Oct 2011 19:42:11 -0700 Subject: [PATCH] Added ignore exception type to ExceptionVerfication --- .../Framework/AutoMoq/TestBaseTests.cs | 61 +++++++++++++++++++ .../Framework/ExceptionVerification.cs | 19 +++++- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs diff --git a/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs b/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs new file mode 100644 index 000000000..babfd4977 --- /dev/null +++ b/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using NLog; +using NUnit.Framework; + +namespace NzbDrone.Core.Test.Framework.AutoMoq +{ + [TestFixture] + class TestBaseTests : TestBase + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + [Test] + public void Test_should_pass_when_no_exceptions_are_logged() + { + Logger.Info("Everything is fine and dandy!"); + } + + [Test] + public void Test_should_pass_when_errors_are_excpected() + { + Logger.Error("I knew this would happer"); + ExceptionVerification.ExcpectedErrors(1); + } + + [Test] + public void Test_should_pass_when_warns_are_excpected() + { + Logger.Warn("I knew this would happer"); + ExceptionVerification.ExcpectedWarns(1); + } + + [Test] + public void Test_should_pass_when_warns_are_ignored() + { + Logger.Warn("I knew this would happer"); + Logger.Warn("I knew this would happer"); + Logger.Warn("I knew this would happer"); + ExceptionVerification.IgnoreWarns(); + } + + [Test] + public void Test_should_pass_when_errors_are_ignored() + { + Logger.Error("I knew this would happer"); + Logger.Error("I knew this would happer"); + Logger.Error("I knew this would happer"); + ExceptionVerification.IgnoreErrors(); + } + + [Test] + public void Test_should_pass_when_exception_type_is_ignored() + { + Logger.ErrorException("bad exception", new WebException("Test")); + ExceptionVerification.MarkForInconclusive(typeof(WebException)); + } + } +} diff --git a/NzbDrone.Core.Test/Framework/ExceptionVerification.cs b/NzbDrone.Core.Test/Framework/ExceptionVerification.cs index 6cbabaf2d..52b9bf6ec 100644 --- a/NzbDrone.Core.Test/Framework/ExceptionVerification.cs +++ b/NzbDrone.Core.Test/Framework/ExceptionVerification.cs @@ -11,6 +11,7 @@ namespace NzbDrone.Core.Test.Framework public class ExceptionVerification : Target { private static List _logs = new List(); + private static List _inconclusive = new List(); protected override void Write(LogEventInfo logEvent) { @@ -23,6 +24,7 @@ namespace NzbDrone.Core.Test.Framework internal static void Reset() { _logs = new List(); + _inconclusive = new List(); } internal static void AssertNoUnexcpectedLogs() @@ -72,9 +74,16 @@ namespace NzbDrone.Core.Test.Framework Ignore(LogLevel.Error); } + internal static void MarkForInconclusive(Type exception) + { + _inconclusive.Add(exception); + } + private static void Excpected(LogLevel level, int count) { - var levelLogs = _logs.Where(l => l.Level == level).ToList(); + var _inconclusiveLogs = _logs.Where(l => _inconclusive.Any(c => c.IsAssignableFrom(l.Exception.GetType()))).ToList(); + + var levelLogs = _logs.Except(_inconclusiveLogs).Where(l => l.Level == level).ToList(); if (levelLogs.Count != count) { @@ -82,14 +91,18 @@ namespace NzbDrone.Core.Test.Framework var message = String.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}", count, level, levelLogs.Count, GetLogsString(levelLogs)); - message = - "********************************************************************************************************************************\n\r" + message = "********************************************************************************************************************************\n\r" + message + "\n\r********************************************************************************************************************************"; Assert.Fail(message); } + if (_inconclusiveLogs.Count != 0) + { + Assert.Inconclusive(GetLogsString(_inconclusiveLogs)); + } + levelLogs.ForEach(c => _logs.Remove(c)); } diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 86e4fcfdd..878721f38 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -91,6 +91,7 @@ +