Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from laurensb/check
Browse files Browse the repository at this point in the history
Fixed precondition checks
  • Loading branch information
kenkendk committed Apr 14, 2015
2 parents 2659fc1 + 9fb2726 commit 35eae1f
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ HttpServer.Sample/bin
HttpServer.Test/bin
HttpServer/bin
Tutorial/bin
HttpServer_Test/bin
8 changes: 8 additions & 0 deletions HttpServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
52 changes: 31 additions & 21 deletions HttpServer/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,71 @@
namespace HttpServer
{
/// <summary>
/// Small design by contract implementation.
/// Utility class for precondition checks.
/// </summary>
public static class Check
{
/// <summary>
/// Check whether a parameter is empty.
/// Check whether a parameter is non empty.
/// </summary>
/// <param name="value">Parameter value</param>
/// <param name="parameterOrErrorMessage">Parameter name, or error description.</param>
/// <exception cref="ArgumentException">value is empty.</exception>
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);
}

/// <summary>
/// Checks whether a parameter is null.
/// Checks whether a parameter is non null.
/// </summary>
/// <param name="value">Parameter value</param>
/// <param name="parameterOrErrorMessage">Parameter name, or error description.</param>
/// <exception cref="ArgumentNullException">value is null.</exception>
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);

}

/// <summary>
/// Checks whether a parameter is null.
/// Checks whether an integer parameter is at least a certain value.
/// </summary>
/// <param name="minValue"></param>
/// <param name="minValue">Minimum value</param>
/// <param name="value">Parameter value</param>
/// <param name="parameterOrErrorMessage">Parameter name, or error description.</param>
/// <exception cref="ArgumentException">value is null.</exception>
public static void Min(int minValue, object value, string parameterOrErrorMessage)
/// <exception cref="ArgumentException">value is smaller than minValue.</exception>
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);

}
}
Expand Down
56 changes: 56 additions & 0 deletions HttpServer_Test/CheckTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>());

Assert.That(() => Check.NotEmpty("", "error"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.NotEmpty("", "bad parameter"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.NotEmpty("", null), Throws.TypeOf<ArgumentException>());

Assert.That(() => Check.NotEmpty(null, "null"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.NotEmpty(null, "cannot be null"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.NotEmpty(null, null), Throws.TypeOf<ArgumentException>());
}

[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<ArgumentException>());

Assert.That(() => Check.Require(null, "null"), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => Check.Require(null, "cannot be null"), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => Check.Require(null, null), Throws.TypeOf<ArgumentException>());
}

[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<ArgumentException>());

Assert.That(() => Check.Min(100, 0, "value"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.Min(0, -100, "value"), Throws.TypeOf<ArgumentException>());
Assert.That(() => Check.Min(int.MaxValue, int.MinValue, "value"), Throws.TypeOf<ArgumentException>());
}
}
}
57 changes: 57 additions & 0 deletions HttpServer_Test/HttpServer_Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{C3CC0769-9814-4896-8599-9EB0B807E785}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<RootNamespace>HttpServer_Test</RootNamespace>
<AssemblyName>HttpServer_Test</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<AppDesignerFolder>Properties</AppDesignerFolder>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CheckTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HttpServer\HttpServer.csproj">
<Project>{455E7D70-1C85-4D7F-9F01-DC801B8B8C34}</Project>
<Name>HttpServer</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
31 changes: 31 additions & 0 deletions HttpServer_Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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.*")]
4 changes: 4 additions & 0 deletions HttpServer_Test/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.4" targetFramework="net40-Client" />
</packages>

0 comments on commit 35eae1f

Please sign in to comment.