-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added helpers for getting coverage on exceptionals
- Loading branch information
Showing
11 changed files
with
242 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/BCLExtensions.Tests/FuncExtensions/AsActionUsingTests - Copy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
src/BCLExtensions.Tests/FuncExtensions/AsActionUsingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |