From 31c61a5bcf2425cb1b55ca85e2e1b0349e116ec8 Mon Sep 17 00:00:00 2001 From: Laurens Blankers Date: Tue, 14 Apr 2015 11:27:03 +0200 Subject: [PATCH 1/3] Create test project --- .gitignore | 1 + HttpServer.sln | 8 +++ HttpServer_Test/HttpServer_Test.csproj | 57 ++++++++++++++++++++++ HttpServer_Test/Properties/AssemblyInfo.cs | 31 ++++++++++++ HttpServer_Test/packages.config | 4 ++ 5 files changed, 101 insertions(+) create mode 100644 HttpServer_Test/HttpServer_Test.csproj create mode 100644 HttpServer_Test/Properties/AssemblyInfo.cs create mode 100644 HttpServer_Test/packages.config diff --git a/.gitignore b/.gitignore index dddc6e2..57bb01e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ HttpServer.Sample/bin HttpServer.Test/bin HttpServer/bin Tutorial/bin +HttpServer_Test/bin diff --git a/HttpServer.sln b/HttpServer.sln index a398e46..9b55e93 100644 --- a/HttpServer.sln +++ b/HttpServer.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 11.00 # Visual C# Express 2010 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpServer", "HttpServer\HttpServer.csproj", "{455E7D70-1C85-4D7F-9F01-DC801B8B8C34}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpServer_Test", "HttpServer_Test\HttpServer_Test.csproj", "{C3CC0769-9814-4896-8599-9EB0B807E785}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -16,6 +18,12 @@ Global {455E7D70-1C85-4D7F-9F01-DC801B8B8C34}.Release|Any CPU.Build.0 = Release|Any CPU {455E7D70-1C85-4D7F-9F01-DC801B8B8C34}.Test|Any CPU.ActiveCfg = Test|Any CPU {455E7D70-1C85-4D7F-9F01-DC801B8B8C34}.Test|Any CPU.Build.0 = Test|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Release|Any CPU.Build.0 = Release|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Test|Any CPU.Build.0 = Debug|Any CPU + {C3CC0769-9814-4896-8599-9EB0B807E785}.Test|Any CPU.ActiveCfg = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HttpServer_Test/HttpServer_Test.csproj b/HttpServer_Test/HttpServer_Test.csproj new file mode 100644 index 0000000..9dd84fd --- /dev/null +++ b/HttpServer_Test/HttpServer_Test.csproj @@ -0,0 +1,57 @@ + + + + {C3CC0769-9814-4896-8599-9EB0B807E785} + Debug + AnyCPU + Library + HttpServer_Test + HttpServer_Test + v4.0 + Client + Properties + + + x86 + + + bin\Debug\ + True + Full + False + True + DEBUG;TRACE + + + bin\Release\ + False + None + True + False + TRACE + + + + False + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + + + + + + + + + {455E7D70-1C85-4D7F-9F01-DC801B8B8C34} + HttpServer + + + + + + + + + + \ No newline at end of file diff --git a/HttpServer_Test/Properties/AssemblyInfo.cs b/HttpServer_Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1b87f7e --- /dev/null +++ b/HttpServer_Test/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +#region Using directives + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +#endregion + +// 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("HttpServer_Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("HttpServer_Test")] +[assembly: AssemblyCopyright("Copyright 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// This sets the default COM visibility of types in the assembly to invisible. +// If you need to expose a type to COM, use [ComVisible(true)] on that type. +[assembly: ComVisible(false)] + +// The assembly version has following format : +// +// Major.Minor.Build.Revision +// +// You can specify all the values or you can use the default the Revision and +// Build Numbers by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.*")] diff --git a/HttpServer_Test/packages.config b/HttpServer_Test/packages.config new file mode 100644 index 0000000..ef8b4ac --- /dev/null +++ b/HttpServer_Test/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file From fe378215922be4bc78d54367a39bf97ae7a204e1 Mon Sep 17 00:00:00 2001 From: Laurens Blankers Date: Tue, 14 Apr 2015 11:27:23 +0200 Subject: [PATCH 2/3] Added unit test for Check --- HttpServer_Test/CheckTests.cs | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 HttpServer_Test/CheckTests.cs diff --git a/HttpServer_Test/CheckTests.cs b/HttpServer_Test/CheckTests.cs new file mode 100644 index 0000000..af54801 --- /dev/null +++ b/HttpServer_Test/CheckTests.cs @@ -0,0 +1,56 @@ +using System; +using HttpServer; +using NUnit.Framework; + +namespace HttpServer_Test +{ + [TestFixture] + public class CheckTests + { + [Test] + public void TestNotEmpty() + { + Check.NotEmpty("string", "error"); + Check.NotEmpty("another string", "another error"); + + // Even though the argument to check is non empty an exception is expected, + // if this was not the case the missing error message would only be discovered when the check fails (possibly in production) + Assert.That(() => Check.NotEmpty("something not empty", null), Throws.TypeOf()); + + Assert.That(() => Check.NotEmpty("", "error"), Throws.TypeOf()); + Assert.That(() => Check.NotEmpty("", "bad parameter"), Throws.TypeOf()); + Assert.That(() => Check.NotEmpty("", null), Throws.TypeOf()); + + Assert.That(() => Check.NotEmpty(null, "null"), Throws.TypeOf()); + Assert.That(() => Check.NotEmpty(null, "cannot be null"), Throws.TypeOf()); + Assert.That(() => Check.NotEmpty(null, null), Throws.TypeOf()); + } + + [Test] + public void testRequire() + { + Object someObject = "some string"; + + Check.Require(someObject, "parameter"); + Check.Require(someObject, "error message"); + Assert.That(() => Check.Require(someObject, null), Throws.TypeOf()); + + Assert.That(() => Check.Require(null, "null"), Throws.TypeOf()); + Assert.That(() => Check.Require(null, "cannot be null"), Throws.TypeOf()); + Assert.That(() => Check.Require(null, null), Throws.TypeOf()); + } + + [Test] + public void TestMin() + { + Check.Min(0, 100, "value"); + Check.Min(-100, 0, "value"); + Check.Min(int.MaxValue, int.MaxValue, "value"); + Assert.That(() => Check.Min(0, 100, null), Throws.TypeOf()); + + Assert.That(() => Check.Min(100, 0, "value"), Throws.TypeOf()); + Assert.That(() => Check.Min(0, -100, "value"), Throws.TypeOf()); + Assert.That(() => Check.Min(int.MaxValue, int.MinValue, "value"), Throws.TypeOf()); + } + } +} From 9fb2726aa5c527fffbd465ab9473933737dd455e Mon Sep 17 00:00:00 2001 From: Laurens Blankers Date: Tue, 14 Apr 2015 11:28:35 +0200 Subject: [PATCH 3/3] Fixed checks in Check --- HttpServer/Check.cs | 52 +++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/HttpServer/Check.cs b/HttpServer/Check.cs index 0875e85..dcc7ded 100755 --- a/HttpServer/Check.cs +++ b/HttpServer/Check.cs @@ -3,61 +3,71 @@ namespace HttpServer { /// - /// Small design by contract implementation. + /// Utility class for precondition checks. /// public static class Check { /// - /// Check whether a parameter is empty. + /// Check whether a parameter is non empty. /// /// Parameter value /// Parameter name, or error description. /// value is empty. - public static void NotEmpty(string value, string parameterOrErrorMessage) + public static void NotEmpty(string value, string parameterOrErrorMessage) { + if (string.IsNullOrEmpty(parameterOrErrorMessage)) + Check.NotEmpty(parameterOrErrorMessage, "parameterOrErrorMessage"); + if (!string.IsNullOrEmpty(value)) return; - if (parameterOrErrorMessage.IndexOf(' ') == -1) - throw new ArgumentException("'" + parameterOrErrorMessage + "' cannot be empty.", parameterOrErrorMessage); - - throw new ArgumentException(parameterOrErrorMessage); + bool isParameterName = parameterOrErrorMessage.IndexOf(' ') == -1; + if (isParameterName) + throw new ArgumentException(String.Format("'{0}' cannot be empty.", parameterOrErrorMessage), parameterOrErrorMessage); + else + throw new ArgumentException(parameterOrErrorMessage); } /// - /// Checks whether a parameter is null. + /// Checks whether a parameter is non null. /// /// Parameter value /// Parameter name, or error description. /// value is null. public static void Require(object value, string parameterOrErrorMessage) { + Check.NotEmpty(parameterOrErrorMessage, "parameterOrErrorMessage"); + if (value != null) return; - if (parameterOrErrorMessage.IndexOf(' ') == -1) - throw new ArgumentNullException("'" + parameterOrErrorMessage + "' cannot be null.", parameterOrErrorMessage); - - throw new ArgumentNullException(parameterOrErrorMessage); + bool isParameterName = parameterOrErrorMessage.IndexOf(' ') == -1; + if (isParameterName) + throw new ArgumentNullException(parameterOrErrorMessage, String.Format("'{0}' cannot be null.", parameterOrErrorMessage)); + else + throw new ArgumentNullException(null, parameterOrErrorMessage); } /// - /// Checks whether a parameter is null. + /// Checks whether an integer parameter is at least a certain value. /// - /// + /// Minimum value /// Parameter value /// Parameter name, or error description. - /// value is null. - public static void Min(int minValue, object value, string parameterOrErrorMessage) + /// value is smaller than minValue. + public static void Min(int minValue, int value, string parameterOrErrorMessage) { - if (value != null) - return; + Check.NotEmpty(parameterOrErrorMessage, "parameterOrErrorMessage"); - if (parameterOrErrorMessage.IndexOf(' ') == -1) - throw new ArgumentException("'" + parameterOrErrorMessage + "' must be at least " + minValue + ".", parameterOrErrorMessage); + if (minValue <= value) + return; - throw new ArgumentException(parameterOrErrorMessage); + bool isParameterName = parameterOrErrorMessage.IndexOf(' ') == -1; + if (isParameterName) + throw new ArgumentException(String.Format("'{0}' must be at least {1}.", parameterOrErrorMessage, minValue)); + else + throw new ArgumentException(parameterOrErrorMessage); } }