Skip to content

Commit

Permalink
Added helpers for getting coverage on exceptionals
Browse files Browse the repository at this point in the history
  • Loading branch information
csMACnz committed Feb 13, 2015
1 parent 881b5c0 commit 629560e
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/BCLExtensions.Tests/BCLExtensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Compile Include="ArrayExtensions\ClearTests.cs" />
<Compile Include="ArrayExtensions\OrEmptyIfNullTests.cs" />
<Compile Include="ArrayExtensions\OrNullIfEmptyTests.cs" />
<Compile Include="FuncExtensions\AsActionUsingTests - Copy.cs" />
<Compile Include="FuncExtensions\AsActionUsingTests.cs" />
<Compile Include="FuncExtensions\AsActionTests.cs" />
<Compile Include="GenericExtensions\ValueOrDefaultTests.cs" />
<Compile Include="TestHelpers\ArrayProvider.cs" />
<Compile Include="TestHelpers\CollectionProvider.cs" />
Expand Down Expand Up @@ -91,6 +94,7 @@
<Compile Include="StringExtensions\ValueOrIfNullOrWhitespaceTests.cs" />
<Compile Include="StringExtensions\ValueorIfNullTests.cs" />
<Compile Include="StringExtensions\ValueOrNullIfWhitespaceTests.cs" />
<Compile Include="TestHelpers\ThrowsHelpers.cs" />
<Compile Include="TestHelperTests\CustomICollectionTests.cs" />
<Compile Include="TypeExtensions\IsOfTypeTests.cs" />
</ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions src/BCLExtensions.Tests/FuncExtensions/AsActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Xunit;

namespace BCLExtensions.Tests.FuncExtensions
{

public class AsActionTests
{
[Fact]
public void SampleFunctionIsValid()
{
Assert.DoesNotThrow(() => SampleFunction());
}

[Fact]
public void ResultNotNull()
{
Func<int> function = SampleFunction;

var action = function.AsAction();

Assert.NotNull(action);
}

[Fact]
public void InternalFunctionExecutes()
{
bool internalFunctionWasCalled = false;
Func<int> function = () =>
{
internalFunctionWasCalled = true;
return 42;
};
var action = function.AsAction();
action();

Assert.True(internalFunctionWasCalled);
}


private int SampleFunction()
{
return 42;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using Xunit;

namespace BCLExtensions.Tests.FuncExtensions
{

public class AsActionUsingWithTwoParametersTests
{
[Fact]
public void SampleFunctionIsValid()
{
Assert.DoesNotThrow(() => SampleFunction(42, "Test"));
}

[Fact]
public void ResultNotNull()
{
Func<int, string, decimal> function = SampleFunction;

var action = function.AsActionUsing(12, "12");

Assert.NotNull(action);
}

[Fact]
public void InternalFunctionExecutes()
{
bool internalFunctionWasCalled = false;
Func<int, string, decimal> function = (p1,p2) =>
{
internalFunctionWasCalled = true;
return 42m;
};
var action = function.AsActionUsing(12,"24");
action();

Assert.True(internalFunctionWasCalled);
}


[Fact]
public void InternalFunctionCapturesCorrectParameters()
{
const int expectedParameter1 = 12;
const string expectedParameter2 = "24";
int passedParameter1 = 0;
string passedParameter2 = null;
Func<int, string, decimal> function = (p1,p2) =>
{
passedParameter1 = p1;
passedParameter2 = p2;
return 42;
};

var action = function.AsActionUsing(expectedParameter1, expectedParameter2);
action();

Assert.Equal(expectedParameter1, passedParameter1);
Assert.Equal(expectedParameter2, passedParameter2);
}

private decimal SampleFunction(int parameter1, string parameter2)
{
return 42m;
}

}
}
63 changes: 63 additions & 0 deletions src/BCLExtensions.Tests/FuncExtensions/AsActionUsingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using Xunit;

namespace BCLExtensions.Tests.FuncExtensions
{

public class AsActionUsingTests
{
[Fact]
public void SampleFunctionIsValid()
{
Assert.DoesNotThrow(() => SampleFunction(42));
}

[Fact]
public void ResultNotNull()
{
Func<int, int> function = SampleFunction;

var action = function.AsActionUsing(12);

Assert.NotNull(action);
}

private int SampleFunction(int parameter)
{
return parameter;
}

[Fact]
public void InternalFunctionExecutes()
{
bool internalFunctionWasCalled = false;
Func<int, int> function = parameter =>
{
internalFunctionWasCalled = true;
return 42;
};
var action = function.AsActionUsing(12);
action();

Assert.True(internalFunctionWasCalled);
}


[Fact]
public void InternalFunctionCapturesCorrectParameter()
{
const int expectedParameter = 12;
int passedParameter = 0;
Func<int, int> function = parameter =>
{
passedParameter = parameter;
return 42;
};

var action = function.AsActionUsing(expectedParameter);
action();

Assert.Equal(expectedParameter, passedParameter);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using BCLExtensions.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

Expand All @@ -14,7 +15,8 @@ public class ValueOrDefaultTests
[ObjectData(inputIsNull:true)]
public void WhenInputNullAndDefaultValueIsNullThrowsException<T>(T input, T defaultValue) where T : class
{
Assert.Throws<ArgumentNullException>(() => input.GetValueOrDefault(null));
Func<T,T,T> valueOrDefault = BCLExtensions.GenericExtensions.GetValueOrDefault<T>;
Assert.Throws<ArgumentNullException>(valueOrDefault.AsActionUsing(input, null).AsThrowsDelegate());
}

[Theory]
Expand All @@ -23,7 +25,8 @@ public void WhenInputNullAndDefaultValueIsNullThrowsException<T>(T input, T defa
[ObjectData]
public void WhenValidInputAndDefaultValueIsNullThrowsException<T>(T input, T defaultValue) where T : class
{
Assert.Throws<ArgumentNullException>(() => input.GetValueOrDefault(null));
Func<T, T, T> valueOrDefault = BCLExtensions.GenericExtensions.GetValueOrDefault<T>;
Assert.Throws<ArgumentNullException>(valueOrDefault.AsActionUsing(input, null).AsThrowsDelegate());
}

[Theory]
Expand Down
7 changes: 5 additions & 2 deletions src/BCLExtensions.Tests/StringExtensions/FormatWithTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BCLExtensions.Tests.TestHelpers;
using Xunit;

namespace BCLExtensions.Tests.StringExtensions
Expand All @@ -12,7 +13,8 @@ public class WithNullInputString
[Fact]
public void NoParametersProvidedThowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _myString.FormatWith());
Func<string, object[], string> formatWith = BCLExtensions.StringExtensions.FormatWith;
Assert.Throws<ArgumentNullException>(formatWith.AsActionUsing(_myString, new object[0]).AsThrowsDelegate());
}
}

Expand Down Expand Up @@ -49,7 +51,8 @@ public class WithInputStringContainingOneParameter
[Fact]
public void NoParametersProvidedThrowsFormatException()
{
Assert.Throws<FormatException>(() => _myString.FormatWith());
Func<string, object[], string> formatWith = BCLExtensions.StringExtensions.FormatWith;
Assert.Throws<FormatException>(formatWith.AsActionUsing(_myString, new object[0]).AsThrowsDelegate());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BCLExtensions.Tests.TestHelpers;
using Xunit;

namespace BCLExtensions.Tests.StringExtensions
Expand All @@ -20,7 +21,8 @@ public void DefaultStringReturnsDefaultString()
[Fact]
public void NullDefaultStringThrowsException()
{
Assert.Throws<ArgumentNullException>(() => input.ValueOrIfNullOrWhitespace(null));
Func<string, string, string> valueOrIfNullOrWhitespace = BCLExtensions.StringExtensions.ValueOrIfNullOrWhitespace;
Assert.Throws<ArgumentNullException>(valueOrIfNullOrWhitespace.AsActionUsing(input, null).AsThrowsDelegate());
}

[Fact]
Expand Down Expand Up @@ -70,7 +72,8 @@ public class WithNonNullNonWhitespaceInputString
[Fact]
public void NullReplacementThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => input.ValueOrIfNullOrWhitespace(null));
Func<string, string, string> valueOrIfNullOrWhitespace = BCLExtensions.StringExtensions.ValueOrIfNullOrWhitespace;
Assert.Throws<ArgumentNullException>(valueOrIfNullOrWhitespace.AsActionUsing(input, null).AsThrowsDelegate());
}

[Fact]
Expand Down
13 changes: 8 additions & 5 deletions src/BCLExtensions.Tests/StringExtensions/ValueorIfNullTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BCLExtensions.Tests.TestHelpers;
using Xunit;

namespace BCLExtensions.Tests.StringExtensions
Expand All @@ -7,26 +8,27 @@ public class ValueorIfNullTests
{
public class WithNullInputString
{
private readonly string input = null;
private readonly string _input = null;

[Fact]
public void DefaultStringReturnsDefaultString()
{
string expected = "(Default)";
var result = input.ValueOrIfNull(expected);
var result = _input.ValueOrIfNull(expected);
Assert.Equal(expected, result);
}

[Fact]
public void NullDefaultStringThrowsException()
{
Assert.Throws<ArgumentNullException>(() => input.ValueOrIfNull(null));
Func<string, string, string> valueOrIfNull = BCLExtensions.StringExtensions.ValueOrIfNull;
Assert.Throws<ArgumentNullException>(valueOrIfNull.AsActionUsing(_input, null).AsThrowsDelegate());
}

[Fact]
public void EmptyDefaultStringReturnsEmptyString()
{
var result = input.ValueOrIfNull(string.Empty);
var result = _input.ValueOrIfNull(string.Empty);
Assert.Equal(string.Empty, result);
}
}
Expand All @@ -35,7 +37,8 @@ public void EmptyDefaultStringReturnsEmptyString()
public void WithNonNullInputAndNullDefaultStringThrowsException()
{
var input = "Test";
Assert.Throws<ArgumentNullException>(() => input.ValueOrIfNull(null));
Func<string, string, string> valueOrIfNull = BCLExtensions.StringExtensions.ValueOrIfNull;
Assert.Throws<ArgumentNullException>(valueOrIfNull.AsActionUsing(input, null).AsThrowsDelegate());
}

[Fact]
Expand Down
13 changes: 13 additions & 0 deletions src/BCLExtensions.Tests/TestHelpers/ThrowsHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Xunit;

namespace BCLExtensions.Tests.TestHelpers
{
public static class ThrowsHelpers
{
public static Assert.ThrowsDelegate AsThrowsDelegate(this Action action)
{
return new Assert.ThrowsDelegate(action);
}
}
}
1 change: 1 addition & 0 deletions src/BCLExtensions/BCLExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="ArrayExtensions.cs" />
<Compile Include="FuncExtensions.cs" />
<Compile Include="GenericExtensions.cs" />
<Compile Include="IEnumerableExtensions.cs" />
<Compile Include="ObjectExtensions.cs" />
Expand Down
23 changes: 23 additions & 0 deletions src/BCLExtensions/FuncExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace BCLExtensions
{
public static class FuncExtensions
{
public static Action AsAction<T>(this Func<T> function)
{
return () => function();
}

public static Action AsActionUsing<TOutput, TParameter>(this Func<TParameter, TOutput> function, TParameter parameter)
{
return () => function(parameter);
}


public static Action AsActionUsing<TOutput, TParameter1, TParameter2>(this Func<TParameter1, TParameter2, TOutput> function, TParameter1 parameter1, TParameter2 parameter2)
{
return () => function(parameter1, parameter2);
}
}
}

0 comments on commit 629560e

Please sign in to comment.