Skip to content

Commit

Permalink
Merge pull request #4 from jeffcutsinger-hb/feature/implicit-goodness
Browse files Browse the repository at this point in the history
Implicit goodness
  • Loading branch information
davidsidlinger committed Apr 15, 2014
2 parents 2e98896 + c939878 commit 4aa0121
Show file tree
Hide file tree
Showing 17 changed files with 485 additions and 23 deletions.
95 changes: 95 additions & 0 deletions src/Options.Fixtures/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Collections.Generic;

using NUnit.Framework;

namespace Options.Fixtures
{
[TestFixture]
public class DictionaryExtensions
{
[Test]
public void FoundElementContainsValue()
{
const string key = "Whatchu tawkin' 'bout";
const string expected = "Willis";

var dictionary = new Dictionary<string, string>
{
{key, expected}
};
var actual = dictionary.OptionGetValue(key);
actual.AssertSomeAnd(Is.EqualTo(expected));
}

[Test]
public void NotFoundElementContainsNoValue()
{
const string key = "Whatchu tawkin' 'bout";
const string value = "Willis";

var dictionary = new Dictionary<string, string>
{
{key, value}
};
var actual = dictionary.OptionGetValue("Arnold");
actual.AssertNone();
}

[Test]
public void FoundNullElementContainsNoValue()
{
const string key = "Whatchu tawkin' 'bout";
const string value = null;

var dictionary = new Dictionary<string, string>
{
{key, value}
};
var actual = dictionary.OptionGetValue(key);
actual.AssertNone();
}

[Test]
public void FoundNullableElementContainsValue()
{
const string key = "Whatchu tawkin' 'bout";
const int expected = 1;

var dictionary = new Dictionary<string, int?>
{
{key, expected}
};
var actual = dictionary.OptionGetValue(key);
actual.AssertSomeAnd(Is.EqualTo(expected));
}

[Test]
public void NotFoundNullableElementContainsNoValue()
{
const string key = "Whatchu tawkin' 'bout";
const int value = 1;

var dictionary = new Dictionary<string, int?>
{
{key, value}
};
var actual = dictionary.OptionGetValue("Arnold");
actual.AssertNone();
}

[Test]
public void FoundNullNullableElementContainsNoValue()
{
const string key = "Whatchu tawkin' 'bout";
int? value = null;

var dictionary = new Dictionary<string, int?>
{
// ReSharper disable once ExpressionIsAlwaysNull
{key, value}
};
var actual = dictionary.OptionGetValue(key);
actual.AssertNone();
}
}
}
31 changes: 29 additions & 2 deletions src/Options.Fixtures/EitherTFirstTSecondFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System;
using NUnit.Framework;

namespace Options.Fixtures
{
Expand Down Expand Up @@ -139,5 +140,31 @@ public void IdenticalEitherValuesShouldHaveTheSameHashCode()
}
Assert.That(target0.GetHashCode(), Is.EqualTo(target1.GetHashCode()));
}
}

[Test]
public void ImplicitSyntax()
{
Func<bool, Either<int, string>> function = returnInt =>
{
if (returnInt)
{
return 5;
}
else
{
return "Five";
}
};

Assert.That(function(true), Is.EqualTo(new Either<int, string>(5)));
Assert.That(function(false), Is.EqualTo(new Either<int, string>("Five")));
}

[Test]
public void EqualityWorksForContainedValues()
{
Assert.That(new Either<int, string>(5).Equals((object)5));
Assert.That(new Either<int, string>("Five").Equals((object)"Five"));
}
}
}
1 change: 1 addition & 0 deletions src/Options.Fixtures/ObjectExtensionsFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using Options.GlobalExtensions;

namespace Options.Fixtures
{
Expand Down
109 changes: 108 additions & 1 deletion src/Options.Fixtures/OptionTOptionFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;

using NUnit.Framework;
Expand Down Expand Up @@ -95,5 +96,111 @@ public void TwoOptionsWithSameValueHaveSameHashCode([Random(int.MinValue, int.Ma
var expected = new Option<int>(random).GetHashCode();
Assert.That(actual, Is.EqualTo(expected));
}
}

[Test]
[Category("Fast")]
public void ImplicitSyntax()
{
Func<decimal, decimal, Option<Decimal>> divide = (divisor, dividend) =>
{
if (dividend != 0)
{
return divisor / dividend;
}
else
{
return Option.None();
}
};

divide(2, 1).AssertSomeAnd(Is.EqualTo(2));
divide(2, 0).AssertNone();
}

[Test]
[Category("Fast")]
public void ContainedObjectEquality([Random(int.MinValue, int.MaxValue, 1)] int random)
{
var option = new Option<int>(random);
var value = random;

Assert.That(option.Equals(value));
Assert.That(option == value);
Assert.That(value == option);
Assert.That(option != value, Is.False);
Assert.That(value != option, Is.False);
}

[Test]
[Category("Fast")]
public void ContainedObjectBoxedEquality([Random(int.MinValue, int.MaxValue, 1)] int random)
{
var option = new Option<int>(random);
var box = (object) random;
Assert.That(option.Equals(box));
}

[Test]
[Category("Fast")]
public void ContainedObjectInequality(
[Random(int.MinValue, int.MaxValue, 1)] int random0,
[Random(int.MinValue, int.MaxValue, 1)] int random1)
{
if (random0 == random1)
{
Assert.Inconclusive();
}

var option = new Option<int>(random0);
var value = random1;

Assert.That(option.Equals(value), Is.False);
Assert.That(option == value, Is.False);
Assert.That(value == option, Is.False);
Assert.That(option != value);
Assert.That(value != option);
}

[Test]
[Category("Fast")]
public void NullReferenceNoneInequality()
{
var option = new Option<string>();
string value = null;

Assert.That(option.Equals(value), Is.False);
Assert.That(option == value, Is.False);
Assert.That(value == option, Is.False);
Assert.That(option != value);
Assert.That(value != option);
}

[Test]
[Category("Fast")]
public void NullReferenceInequality([Random(int.MinValue, int.MaxValue, 1)] int random)
{
var option = new Option<string>(random.ToString());
string value = null;

Assert.That(option.Equals(value), Is.False);
Assert.That(option == value, Is.False);
Assert.That(value == option, Is.False);
Assert.That(option != value);
Assert.That(value != option);
}

[Test]
[Category("Fast")]
public void NoneInequality([Random(int.MinValue, int.MaxValue, 1)] int random)
{
var option = new Option<int>();
var value = random;

Assert.That(option.Equals(value), Is.False);
Assert.That(option == value, Is.False);
Assert.That(value == option, Is.False);
Assert.That(option != value);
Assert.That(value != option);
}
}
}
6 changes: 5 additions & 1 deletion src/Options.Fixtures/Options.Fixtures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Options.Fixtures</RootNamespace>
<AssemblyName>Options.Fixtures</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<BuildPackage>false</BuildPackage>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -24,6 +25,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -32,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="FSharp.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
Expand All @@ -58,6 +61,7 @@
<Compile Include="..\Options\CommonAssemblyInfo.cs">
<Link>CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="DictionaryExtensions.cs" />
<Compile Include="EitherExtensionsFixture.cs" />
<Compile Include="EitherTFirstTSecondFixture.cs" />
<Compile Include="EnumerableExtensionsFixture.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Options.Fixtures/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="NUnit" version="2.5.7.10213" />
<package id="NUnit" version="2.5.7.10213" />
</packages>
14 changes: 10 additions & 4 deletions src/Options.WinRT/Options.WinRT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<Compile Include="..\Options\CommonAssemblyInfo.cs">
<Link>CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\Options\DictionaryExtensions.cs">
<Link>DictionaryExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\Either.cs">
<Link>Either.cs</Link>
</Compile>
Expand All @@ -129,20 +132,23 @@
<Link>FluentEitherFactories.cs</Link>
</Compile>
<Compile Include="..\Options\GlobalExtensions\EitherGlobalExtensions.cs">
<Link>EitherGlobalExtensions.cs</Link>
<Link>GlobalExtensions\EitherGlobalExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\GlobalExtensions\ObjectExtensions.cs">
<Link>GlobalExtensions\ObjectExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\NoneException.cs">
<Link>NoneException.cs</Link>
</Compile>
<Compile Include="..\Options\ObjectExtensions.cs">
<Link>ObjectExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\Option.cs">
<Link>Option.cs</Link>
</Compile>
<Compile Include="..\Options\OptionExtensions.cs">
<Link>OptionExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\OptionNone.cs">
<Link>OptionNone.cs</Link>
</Compile>
<Compile Include="..\Options\Option[TOption].cs">
<Link>Option[TOption].cs</Link>
</Compile>
Expand Down
12 changes: 9 additions & 3 deletions src/Options.WindowsPhone8/Options.WindowsPhone8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
<Compile Include="..\Options\CommonAssemblyInfo.cs">
<Link>CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\Options\DictionaryExtensions.cs">
<Link>DictionaryExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\Either.cs">
<Link>Either.cs</Link>
</Compile>
Expand All @@ -111,18 +114,21 @@
<Compile Include="..\Options\GlobalExtensions\EitherGlobalExtensions.cs">
<Link>GlobalExtensions\EitherGlobalExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\GlobalExtensions\ObjectExtensions.cs">
<Link>GlobalExtensions\ObjectExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\NoneException.cs">
<Link>NoneException.cs</Link>
</Compile>
<Compile Include="..\Options\ObjectExtensions.cs">
<Link>ObjectExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\Option.cs">
<Link>Option.cs</Link>
</Compile>
<Compile Include="..\Options\OptionExtensions.cs">
<Link>OptionExtensions.cs</Link>
</Compile>
<Compile Include="..\Options\OptionNone.cs">
<Link>OptionNone.cs</Link>
</Compile>
<Compile Include="..\Options\Option[TOption].cs">
<Link>Option[TOption].cs</Link>
</Compile>
Expand Down
6 changes: 3 additions & 3 deletions src/Options/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("David Sidlinger, Jeff Cutsinger, Justin Pierce")]
[assembly: AssemblyProduct("Options")]
[assembly: AssemblyCopyright("Copyright © David Sidlinger 2013")]
[assembly: AssemblyCopyright("Copyright © David Sidlinger 2014")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("0.31415.0.0")]
[assembly: AssemblyFileVersion("0.31415.0.0")]
[assembly: AssemblyVersion("0.31415.9.0")]
[assembly: AssemblyFileVersion("0.31415.9.0")]
Loading

0 comments on commit 4aa0121

Please sign in to comment.