Skip to content

Commit

Permalink
Add code for StringEquivalent and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haacked committed Aug 20, 2012
1 parent 9ab0ca1 commit 465f58a
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/CodeHaacks.sln
Expand Up @@ -47,6 +47,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowPlacementRxDemo", "Wi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControllerInspectorTests", "ControllerInspectorTests\ControllerInspectorTests.csproj", "{B17C881A-F95D-499A-9D0B-592864F28F85}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiscUtils", "MiscUtils\MiscUtils.csproj", "{52D341CE-48CE-4C0C-98E7-9935B300EB2C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{772FA88C-AF50-4919-A4CB-5B8B33E21F7E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiscUtilsTests", "MiscUtilsTests\MiscUtilsTests.csproj", "{802A973B-7614-47D9-B9DD-AD124694DEF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -187,6 +193,26 @@ Global
{B17C881A-F95D-499A-9D0B-592864F28F85}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{B17C881A-F95D-499A-9D0B-592864F28F85}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B17C881A-F95D-499A-9D0B-592864F28F85}.Release|x86.ActiveCfg = Release|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Debug|x86.ActiveCfg = Debug|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Release|Any CPU.Build.0 = Release|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{52D341CE-48CE-4C0C-98E7-9935B300EB2C}.Release|x86.ActiveCfg = Release|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Debug|x86.ActiveCfg = Debug|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Release|Any CPU.Build.0 = Release|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{802A973B-7614-47D9-B9DD-AD124694DEF5}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -205,5 +231,7 @@ Global
{BF86347D-DF92-42A3-8988-A47854BAD9EA} = {F45888CF-254A-423C-9F4A-4E19EB73D548}
{84E1B853-1266-4D22-B7A3-E3F7D6B9165F} = {764297B1-F273-4BAC-A64E-DFAAE8D149E4}
{7DEBE017-3A6B-45AA-B6B7-6F4BDCA91178} = {04697682-8BE7-4DD6-8AED-459682EE6128}
{52D341CE-48CE-4C0C-98E7-9935B300EB2C} = {772FA88C-AF50-4919-A4CB-5B8B33E21F7E}
{802A973B-7614-47D9-B9DD-AD124694DEF5} = {772FA88C-AF50-4919-A4CB-5B8B33E21F7E}
EndGlobalSection
EndGlobal
41 changes: 41 additions & 0 deletions src/MiscUtils/Ensure.cs
@@ -0,0 +1,41 @@
using System;

namespace MiscUtils
{
/// <summary>
/// Ensure input parameters
/// </summary>
public static class Ensure
{
public static void ArgumentNotNull([ValidatedNotNull] object value, string name)
{
if (value != null) return;

throw new ArgumentNullException(name);
}

public static void ArgumentNonNegative(int value, string name)
{
if (value > -1) return;

throw new ArgumentException("The argument must be non-negative", name);
}

public static void ArgumentNotNullOrEmptyString([ValidatedNotNull] string value, string name)
{
ArgumentNotNull(value, name);
if (!string.IsNullOrWhiteSpace(value)) return;

throw new ArgumentException("String cannot be empty", name);
}
}

// Tells CodeAnalysis that this argument is being validated
// to be not null. Great for guard methods.
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class ValidatedNotNullAttribute : Attribute
{
// Internal so it doesn't conflict with the potential billion
// other implementations of this attribute.
}
}
56 changes: 56 additions & 0 deletions src/MiscUtils/MiscUtils.csproj
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{52D341CE-48CE-4C0C-98E7-9935B300EB2C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MiscUtils</RootNamespace>
<AssemblyName>MiscUtils</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Ensure.cs" />
<Compile Include="PathString.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringEquivalent.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
70 changes: 70 additions & 0 deletions src/MiscUtils/PathString.cs
@@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;

namespace MiscUtils
{
[Serializable]
public class PathString : StringEquivalent<PathString>
{
public PathString(string pathString) : base(NormalizePath(pathString))
{
}

protected PathString(SerializationInfo info, StreamingContext context) : base(info)
{
}

// So the XmlSerializer can create this.
protected PathString()
{
}

public override PathString Combine(string path)
{
Ensure.ArgumentNotNull(path, "path");

if (path.StartsWith(@"\", StringComparison.Ordinal))
{
path = path.Substring(1);
}

try
{
return Path.Combine(Value, path);
}
catch (ArgumentException)
{
return AppendPath(Value, path);
}
}

private static string AppendPath(string source, string addition)
{
if (source.Last() != Path.DirectorySeparatorChar) source += "/";
if (addition.First() == '/') addition = addition.Substring(1);
return source + addition;
}

public static implicit operator PathString(string value)
{
return value == null ? null : new PathString(value);
}

public static implicit operator string(PathString path)
{
return path == null ? null : path.Value;
}

private static string NormalizePath(string path)
{
return path == null ? null : path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}

public override bool Equals(string other)
{
return other != null && Value.Equals(other, StringComparison.OrdinalIgnoreCase);
}
}
}
36 changes: 36 additions & 0 deletions src/MiscUtils/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("MiscUtils")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MiscUtils")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[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("18bb8ed0-6334-4eb0-b796-c5c12dae80a8")]

// 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")]
103 changes: 103 additions & 0 deletions src/MiscUtils/StringEquivalent.cs
@@ -0,0 +1,103 @@
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MiscUtils
{
[Serializable]
public abstract class StringEquivalent<T> : ISerializable, IXmlSerializable where T : StringEquivalent<T>
{
protected string Value;

protected StringEquivalent(string value)
{
Ensure.ArgumentNotNull(value, "value");
Value = value;
}

protected StringEquivalent()
{
}

public abstract T Combine(string addition);

public static T operator +(StringEquivalent<T> a, string b)
{
return a.Combine(b);
}

public static bool operator ==(StringEquivalent<T> a, StringEquivalent<T> b)
{
// If both are null, or both are same instance, return true.
if (ReferenceEquals(a, b))
{
return true;
}

// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}

// Return true if the fields match:
return a.Value.Equals(b.Value, StringComparison.OrdinalIgnoreCase);
}

public static bool operator !=(StringEquivalent<T> a, StringEquivalent<T> b)
{
return !(a == b);
}

public override bool Equals(Object obj)
{
return obj != null && Equals(obj as T) || Equals(obj as string);
}

public bool Equals(T stringEquivalent)
{
return this == stringEquivalent;
}

public override int GetHashCode()
{
return (Value ?? "").GetHashCode();
}

public virtual bool Equals(string other)
{
return other != null && Value == other;
}

public override string ToString()
{
return Value;
}

protected StringEquivalent(SerializationInfo info) : this(info.GetValue("Value", typeof (string)) as string)
{
}

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Value", Value);
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
Value = reader.ReadString();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteString(Value);
}
}
}

0 comments on commit 465f58a

Please sign in to comment.