1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Test.Common/ExceptionVerification.cs

125 lines
3.6 KiB
C#
Raw Normal View History

2011-05-22 19:53:21 +03:00
// ReSharper disable RedundantUsingDirective
2011-10-24 08:54:09 +03:00
2011-05-22 19:53:21 +03:00
using System;
using System.Collections.Generic;
2011-06-18 05:00:44 +03:00
using System.Linq;
using NLog;
using NLog.Targets;
2011-06-03 00:06:46 +03:00
using NUnit.Framework;
2011-10-24 08:54:09 +03:00
namespace NzbDrone.Test.Common
{
public class ExceptionVerification : Target
{
private static List<LogEventInfo> _logs = new List<LogEventInfo>();
protected override void Write(LogEventInfo logEvent)
{
if (logEvent.Level >= LogLevel.Warn)
{
_logs.Add(logEvent);
}
}
2011-10-24 08:54:09 +03:00
public static void Reset()
{
_logs = new List<LogEventInfo>();
}
2011-10-24 08:54:09 +03:00
public static void AssertNoUnexcpectedLogs()
{
ExpectedFatals(0);
ExpectedErrors(0);
ExpectedWarns(0);
}
private static string GetLogsString(IEnumerable<LogEventInfo> logs)
{
string errors = "";
foreach (var log in logs)
{
string exception = "";
if (log.Exception != null)
{
2011-11-21 06:58:32 +03:00
exception = String.Format("[{0}: {1}]", log.Exception.GetType(), log.Exception.Message);
}
errors += Environment.NewLine + String.Format("[{0}] {1}: {2} {3}", log.Level, log.LoggerName, log.FormattedMessage, exception);
}
return errors;
}
public static void ExpectedErrors(int count)
{
Expected(LogLevel.Error, count);
}
public static void ExpectedFatals(int count)
{
Expected(LogLevel.Fatal, count);
}
public static void ExpectedWarns(int count)
{
Expected(LogLevel.Warn, count);
}
2011-10-24 08:54:09 +03:00
public static void IgnoreWarns()
2011-05-20 07:18:51 +03:00
{
Ignore(LogLevel.Warn);
}
2011-10-24 08:54:09 +03:00
public static void IgnoreErrors()
2011-05-20 07:18:51 +03:00
{
Ignore(LogLevel.Error);
}
2011-10-24 08:54:09 +03:00
public static void MarkInconclusive(Type exception)
{
2011-11-21 06:58:32 +03:00
var inconclusiveLogs = _logs.Where(l => l.Exception != null && l.Exception.GetType() == exception).ToList();
if (inconclusiveLogs.Any())
{
inconclusiveLogs.ForEach(c => _logs.Remove(c));
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
}
}
public static void MarkInconclusive(string text)
{
var inconclusiveLogs = _logs.Where(l => l.FormattedMessage.ToLower().Contains(text.ToLower())).ToList();
if (inconclusiveLogs.Any())
{
inconclusiveLogs.ForEach(c => _logs.Remove(c));
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
}
}
private static void Expected(LogLevel level, int count)
{
var levelLogs = _logs.Where(l => l.Level == level).ToList();
if (levelLogs.Count != count)
{
2011-05-27 06:04:36 +03:00
var message = String.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}",
2011-05-27 06:04:36 +03:00
count, level, levelLogs.Count, GetLogsString(levelLogs));
message = "\n\r****************************************************************************************\n\r"
2011-05-27 06:04:36 +03:00
+ message +
"\n\r****************************************************************************************";
Assert.Fail(message);
}
levelLogs.ForEach(c => _logs.Remove(c));
}
2011-05-20 07:18:51 +03:00
private static void Ignore(LogLevel level)
{
var levelLogs = _logs.Where(l => l.Level == level).ToList();
levelLogs.ForEach(c => _logs.Remove(c));
}
}
}