128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using NUnit.Framework;
|
|
using NUnit.Mocks;
|
|
using NUnit.Framework.SyntaxHelpers;
|
|
using SimpleLog.Framework;
|
|
|
|
namespace SimpleLog.Tests {
|
|
[TestFixture]
|
|
public class DefaultLoggerTest {
|
|
|
|
private DefaultLogger Logger;
|
|
private DynamicMock Handler;
|
|
private string TempFile;
|
|
|
|
[SetUp]
|
|
public void SetUp() {
|
|
this.Logger = new DefaultLogger();
|
|
this.Logger.MessageHandler = new DefaultMessageHandler();
|
|
this.Handler = new DynamicMock(typeof(ILogHandler));
|
|
this.TempFile = null;
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown() {
|
|
this.Logger.UnregisterAllLogHandlers();
|
|
this.Logger.Enabled = true;
|
|
this.Logger.MessageHandler = new DefaultMessageHandler();
|
|
if (this.TempFile != null && System.IO.File.Exists(this.TempFile)) {
|
|
System.IO.File.Delete(this.TempFile);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestRegisterAndUnregisterLogHandler() {
|
|
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
|
|
|
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
|
|
|
|
this.Logger.RegisterLogHandler(handler);
|
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
|
|
|
|
Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.Framework.ILogHandler"), Is.EqualTo(1));
|
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
|
|
|
this.Logger.RegisterLogHandler(handler);
|
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
|
|
|
|
this.Logger.UnregisterAllLogHandlers();
|
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void TestLogWhenDisabled() {
|
|
this.Logger.Enabled = false;
|
|
Assert.That(this.Logger.Log("yay", LogLevel.Debug));
|
|
}
|
|
|
|
[Test]
|
|
public void TestLogWithHighLogLevel() {
|
|
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
|
msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay");
|
|
msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning);
|
|
|
|
//NUnit mock objects suck, so setting this property on DefaultLogger
|
|
//and assuming handler.MessageHandler returns null is the best I can do
|
|
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
|
|
|
this.Handler.ExpectAndReturn("Log", true, "foobar", LogLevel.Warning);
|
|
//h4x0rz!
|
|
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
|
|
|
|
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
|
|
|
Assert.That(this.Logger.Log("yay", LogLevel.Warning));
|
|
this.Handler.Verify();
|
|
msgHandler.Verify();
|
|
}
|
|
|
|
[Test]
|
|
public void TestLogWithHighLogLevelFails() {
|
|
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
|
msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay");
|
|
msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning);
|
|
|
|
//NUnit mock objects suck, so setting this property on DefaultLogger
|
|
//and assuming handler.MessageHandler returns null is the best I can do
|
|
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
|
|
|
this.Handler.ExpectAndReturn("Log", false, "foobar", LogLevel.Warning);
|
|
//h4x0rz!
|
|
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
|
|
|
|
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
|
|
|
Assert.That(!this.Logger.Log("yay", LogLevel.Warning));
|
|
this.Handler.Verify();
|
|
msgHandler.Verify();
|
|
}
|
|
|
|
[Test]
|
|
public void TestLogWithLowLogLevel() {
|
|
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
|
msgHandler.ExpectNoCall("ConvertMessageToString");
|
|
msgHandler.ExpectNoCall("FormatLogMessage");
|
|
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
|
|
|
this.Handler.ExpectNoCall("Log");
|
|
|
|
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
|
//h4x0rz!
|
|
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
|
|
|
|
Assert.That(this.Logger.Log("yay", LogLevel.Debug));
|
|
this.Handler.Verify();
|
|
msgHandler.Verify();
|
|
}
|
|
|
|
[Test]
|
|
public void TestLoadConfig() {
|
|
this.TempFile = System.IO.Path.GetTempFileName();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|