forked from shouldly/shouldly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Should.Throw() overloads for Func (shouldly#530)
* Delete UpgradeLog.htm Delete UpgradeLog.htm from some previous incarnation that made its way into Git. Update .gitignore so it doesn't happen again. * Add Func-based Should.Throw methods Inspired by xunit's Assert.Throws(), add Func-based Should.Throw() methods to remove the need to create dummy variables for actions and to remove the need to suppress warnings from tools like FxCop for unused objects. * Update examples Update the examples as instructed in the assert failure.
- Loading branch information
1 parent
b3acc89
commit 148a37e
Showing
15 changed files
with
295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ src/*.Tests/report.xml | |
.vscode | ||
src/.idea | ||
src/.idea.* | ||
UpgradeLog*.htm | ||
UpgradeLog*.XML |
9 changes: 2 additions & 7 deletions
9
...onExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFunc.codeSample.approved.txt
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 |
---|---|---|
@@ -1,7 +1,2 @@ | ||
var homer = new Person() { Name = "Homer", Salary = 30000 }; | ||
var denominator = 0; | ||
Should.NotThrow(() => | ||
{ | ||
var task = Task.Factory.StartNew(() => { var y = homer.Salary / denominator; }); | ||
return task; | ||
}); | ||
string name = null; | ||
Should.NotThrow(() => new Person(name)); |
7 changes: 4 additions & 3 deletions
7
...xamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFunc.exceptionText.approved.txt
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
`var task = Task.Factory.StartNew(() => { var y = homer.Salary / denominator; }); return task;` | ||
`new Person(name)` | ||
should not throw but threw | ||
System.DivideByZeroException | ||
System.ArgumentNullException | ||
with message | ||
"Attempted to divide by zero." | ||
"Value cannot be null. | ||
Parameter name: name" |
7 changes: 7 additions & 0 deletions
7
...ples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFuncOfTask.codeSample.approved.txt
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,7 @@ | ||
var homer = new Person() { Name = "Homer", Salary = 30000 }; | ||
var denominator = 0; | ||
Should.NotThrow(() => | ||
{ | ||
var task = Task.Factory.StartNew(() => { var y = homer.Salary / denominator; }); | ||
return task; | ||
}); |
5 changes: 5 additions & 0 deletions
5
...s/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFuncOfTask.exceptionText.approved.txt
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,5 @@ | ||
`var task = Task.Factory.StartNew(() => { var y = homer.Salary / denominator; }); return task;` | ||
should not throw but threw | ||
System.DivideByZeroException | ||
with message | ||
"Attempted to divide by zero." |
1 change: 1 addition & 0 deletions
1
...entationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowFunc.codeSample.approved.txt
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 @@ | ||
Should.Throw<ArgumentNullException>(() => new Person("Homer")); |
4 changes: 4 additions & 0 deletions
4
...ationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowFunc.exceptionText.approved.txt
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,4 @@ | ||
`new Person("Homer")` | ||
should throw | ||
System.ArgumentNullException | ||
but did not |
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,81 @@ | ||
using System; | ||
using Shouldly.Tests.Strings; | ||
using Xunit; | ||
|
||
namespace Shouldly.Tests.ShouldThrow | ||
{ | ||
public class FuncOfObjectScenario | ||
{ | ||
[Fact] | ||
public void FuncOfObjectScenarioShouldFail() | ||
{ | ||
Func<object> action = () => 1; | ||
Verify.ShouldFail(() => | ||
action.ShouldThrow<NotImplementedException>("Some additional context"), | ||
|
||
errorWithSource: | ||
@"`action()` | ||
should throw | ||
System.NotImplementedException | ||
but did not | ||
Additional Info: | ||
Some additional context", | ||
|
||
errorWithoutSource: | ||
@"delegate | ||
should throw | ||
System.NotImplementedException | ||
but did not | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
|
||
[Fact] | ||
public void FuncOfObjectScenarioShouldFail_ExceptionTypePassedIn() | ||
{ | ||
Func<object> action = () => 1; | ||
Verify.ShouldFail(() => | ||
action.ShouldThrow("Some additional context", typeof(NotImplementedException)), | ||
|
||
errorWithSource: | ||
@"`action()` | ||
should throw | ||
System.NotImplementedException | ||
but did not | ||
Additional Info: | ||
Some additional context", | ||
|
||
errorWithoutSource: | ||
@"delegate | ||
should throw | ||
System.NotImplementedException | ||
but did not | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPass() | ||
{ | ||
var action = new Func<object>(() => { throw new NotImplementedException(); }); | ||
|
||
var ex = action.ShouldThrow<NotImplementedException>(); | ||
ex.ShouldBeOfType<NotImplementedException>(); | ||
ex.ShouldNotBe(null); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPass_ExceptionTypePassedIn() | ||
{ | ||
var action = new Func<object>(() => { throw new NotImplementedException(); }); | ||
|
||
var ex = action.ShouldThrow(typeof(NotImplementedException)); | ||
ex.ShouldBeOfType<NotImplementedException>(); | ||
ex.ShouldNotBe(null); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Shouldly.Tests/ShouldThrow/FuncOfObjectThrowsDifferentExceptionScenario.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,65 @@ | ||
using System; | ||
using Shouldly.Tests.Strings; | ||
using Xunit; | ||
|
||
namespace Shouldly.Tests.ShouldThrow | ||
{ | ||
public class FuncOfObjectThrowsDifferentExceptionScenario | ||
{ | ||
[Fact] | ||
public void NestedBlockLambdaWithoutAdditionalInformationsScenarioShouldFail() | ||
{ | ||
Func<object> action = () => { throw new RankException(); }; | ||
Verify.ShouldFail(() => | ||
action.ShouldThrow<InvalidOperationException>("Some additional context"), | ||
|
||
errorWithSource: | ||
@"`action()` | ||
should throw | ||
System.InvalidOperationException | ||
but threw | ||
System.RankException | ||
Additional Info: | ||
Some additional context", | ||
|
||
errorWithoutSource: | ||
@"delegate | ||
should throw | ||
System.InvalidOperationException | ||
but threw | ||
System.RankException | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
|
||
[Fact] | ||
public void NestedBlockLambdaWithoutAdditionalInformationsScenarioShouldFail_ExceptionTypePassedIn() | ||
{ | ||
Func<object> action = () => { throw new RankException(); }; | ||
Verify.ShouldFail(() => | ||
action.ShouldThrow("Some additional context", typeof(InvalidOperationException)), | ||
|
||
errorWithSource: | ||
@"`action()` | ||
should throw | ||
System.InvalidOperationException | ||
but threw | ||
System.RankException | ||
Additional Info: | ||
Some additional context", | ||
|
||
errorWithoutSource: | ||
@"delegate | ||
should throw | ||
System.InvalidOperationException | ||
but threw | ||
System.RankException | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
} | ||
} |
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
Binary file not shown.