From 65937ec510a32329e4c2623654094b816f296196 Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 11 Apr 2009 21:58:00 +0000 Subject: [PATCH] added SimpleLog.Tests project and unit test for DefaultMessageHandler --- SimpleLog.Tests/DefaultMessageHandlerTest.cs | 44 +++++++++++++ SimpleLog.Tests/Properties/AssemblyInfo.cs | 36 +++++++++++ SimpleLog.Tests/SimpleLog.Tests.csproj | 68 ++++++++++++++++++++ SimpleLog.sln | 6 ++ SimpleLog/DefaultLogger.cs | 2 +- SimpleLog/DefaultMessageHandler.cs | 4 +- SimpleLog/IMessageHandler.cs | 2 +- SimpleLog/Properties/AssemblyInfo.cs | 6 +- 8 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 SimpleLog.Tests/DefaultMessageHandlerTest.cs create mode 100644 SimpleLog.Tests/Properties/AssemblyInfo.cs create mode 100644 SimpleLog.Tests/SimpleLog.Tests.csproj diff --git a/SimpleLog.Tests/DefaultMessageHandlerTest.cs b/SimpleLog.Tests/DefaultMessageHandlerTest.cs new file mode 100644 index 0000000..49f6dad --- /dev/null +++ b/SimpleLog.Tests/DefaultMessageHandlerTest.cs @@ -0,0 +1,44 @@ +using System; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace SimpleLog.Tests { + [TestFixture] + public class DefaultMessageHandlerTest { + + private DefaultMessageHandler messageHandler; + + [SetUp] + public void Init() { + this.messageHandler = new DefaultMessageHandler(); + } + + [Test] + public void TestConvertMessageToStringWithException() { + Exception e = new Exception("yay!"); + Assert.That(this.messageHandler.ConvertMessageToString(e), Text.StartsWith("yay!\n")); + } + + [Test] + public void TestConvertMessageToString() { + string message = "yay!"; + Assert.That(this.messageHandler.ConvertMessageToString(message), Text.Matches("yay!")); + object obj = "yay!"; + Assert.That(this.messageHandler.ConvertMessageToString(obj), Text.Matches("yay!")); + } + + [Test] + public void TestConstructLogMessage() { + this.messageHandler.DateFormat = "yyyy-MM-dd HH:mm:ss"; + this.messageHandler.Delimiter = " "; + this.messageHandler.Context = "some kind of context"; + string expected = "\\d{4}-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d "; + expected += "WARNING "; + expected += "\\[some kind of context\\] "; + expected += "log message\n"; + + Assert.That(this.messageHandler.ConstructLogMessage("log message", LogLevel.Warning), Text.Matches(expected)); + } + + } +} diff --git a/SimpleLog.Tests/Properties/AssemblyInfo.cs b/SimpleLog.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..90cf93c --- /dev/null +++ b/SimpleLog.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SimpleLog.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Bridgepoint Education")] +[assembly: AssemblyProduct("SimpleLog.Tests")] +[assembly: AssemblyCopyright("Copyright © Bridgepoint Education 2009")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("36a0db9c-388f-41a7-a278-0aafa72986bb")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SimpleLog.Tests/SimpleLog.Tests.csproj b/SimpleLog.Tests/SimpleLog.Tests.csproj new file mode 100644 index 0000000..7fccfa2 --- /dev/null +++ b/SimpleLog.Tests/SimpleLog.Tests.csproj @@ -0,0 +1,68 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD} + Library + Properties + SimpleLog.Tests + SimpleLog.Tests + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {20AC889F-0D03-453F-8759-4D1887F3BC0C} + SimpleLog + + + + + \ No newline at end of file diff --git a/SimpleLog.sln b/SimpleLog.sln index 4ca6a45..58bab5c 100644 --- a/SimpleLog.sln +++ b/SimpleLog.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00 # Visual C# Express 2008 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog", "SimpleLog\SimpleLog.csproj", "{20AC889F-0D03-453F-8759-4D1887F3BC0C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog.Tests", "SimpleLog.Tests\SimpleLog.Tests.csproj", "{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -13,6 +15,10 @@ Global {20AC889F-0D03-453F-8759-4D1887F3BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU {20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU {20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.Build.0 = Release|Any CPU + {4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SimpleLog/DefaultLogger.cs b/SimpleLog/DefaultLogger.cs index d92265b..84c311a 100644 --- a/SimpleLog/DefaultLogger.cs +++ b/SimpleLog/DefaultLogger.cs @@ -51,7 +51,7 @@ namespace SimpleLog { messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat; convertedMessage = messageHandler.ConvertMessageToString(message); - convertedMessage = messageHandler.ConstructLogMessage(handler, convertedMessage, level); + convertedMessage = messageHandler.ConstructLogMessage(convertedMessage, level); success = (success && handler.Log(convertedMessage, allowedLevel)); } diff --git a/SimpleLog/DefaultMessageHandler.cs b/SimpleLog/DefaultMessageHandler.cs index 2d07e05..5f58fc5 100644 --- a/SimpleLog/DefaultMessageHandler.cs +++ b/SimpleLog/DefaultMessageHandler.cs @@ -22,12 +22,12 @@ namespace SimpleLog { } #region IMessageHandler Members - public string ConstructLogMessage(ILogHandler handler, string message, LogLevel level) { + public string ConstructLogMessage(string message, LogLevel level) { List messageData = new List(); messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now)); messageData.Add(level.ToString().ToUpper()); if (!string.IsNullOrEmpty(this.Context)) { - messageData.Add(this.Context); + messageData.Add("[" + this.Context + "]"); } messageData.Add(message); diff --git a/SimpleLog/IMessageHandler.cs b/SimpleLog/IMessageHandler.cs index dbc1c17..724ad42 100644 --- a/SimpleLog/IMessageHandler.cs +++ b/SimpleLog/IMessageHandler.cs @@ -1,7 +1,7 @@ namespace SimpleLog { public interface IMessageHandler : IMessageFormatter { - string ConstructLogMessage(ILogHandler handler, string message, LogLevel level); + string ConstructLogMessage(string message, LogLevel level); string ConvertMessageToString(object message); string Context { get; set; } diff --git a/SimpleLog/Properties/AssemblyInfo.cs b/SimpleLog/Properties/AssemblyInfo.cs index 08d2c0f..ff8a4ef 100644 --- a/SimpleLog/Properties/AssemblyInfo.cs +++ b/SimpleLog/Properties/AssemblyInfo.cs @@ -6,11 +6,11 @@ using System.Runtime.InteropServices; // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("SimpleLog")] -[assembly: AssemblyDescription("")] +[assembly: AssemblyDescription("Simple .NET 3.5 logging utility")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Bridgepoint Education")] +[assembly: AssemblyCompany("")] [assembly: AssemblyProduct("SimpleLog")] -[assembly: AssemblyCopyright("Copyright © Bridgepoint Education 2009")] +[assembly: AssemblyCopyright("Copyright © Tommy Montgomery 2009")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")]