-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add protected
BaseResult()
method to CallInfo.
Create CallInfo<T> to calls that return results and expose `BaseResult`. This gets messy to push the generic all the way through the code, so am just using a cast in `Returns` extensions to handle this. This should be safe as if we are in `Returns<T>` then the return value should be safe to cast to a `T`. Based off discussion here: #622 (comment)
- Loading branch information
Showing
10 changed files
with
124 additions
and
26 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 |
---|---|---|
|
@@ -304,3 +304,6 @@ docs/_site/* | |
|
||
# Ignore VIM tmp files | ||
*.swp | ||
|
||
# kdiff/merge files | ||
*.orig |
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,16 @@ | ||
namespace NSubstitute.Core | ||
{ | ||
/// <summary> | ||
/// Information for a call that returns a value of type <c>T</c>. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class CallInfo<T> : CallInfo | ||
{ | ||
internal CallInfo(CallInfo info) : base(info) { | ||
} | ||
|
||
public T BaseResult() { | ||
return (T)GetBaseResult(); | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/NSubstitute/Exceptions/NoBaseImplementationException.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,11 @@ | ||
namespace NSubstitute.Exceptions | ||
{ | ||
public class NoBaseImplementationException : SubstituteException | ||
{ | ||
private const string Explanation = | ||
"Cannot call the base method as the base method implementation is missing. " + | ||
"You can call base method only if you create a class substitute and the method is not abstract."; | ||
|
||
public NoBaseImplementationException() : base(Explanation) { } | ||
} | ||
} |
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,52 @@ | ||
using System; | ||
using NSubstitute.Exceptions; | ||
using NUnit.Framework; | ||
|
||
namespace NSubstitute.Acceptance.Specs | ||
{ | ||
public class ReturnFromBase | ||
{ | ||
public class Sample | ||
{ | ||
public virtual string RepeatButLouder(string s) => s + "!"; | ||
public virtual void VoidMethod() { } | ||
} | ||
|
||
public abstract class SampleWithAbstractMethod | ||
{ | ||
public abstract string NoBaseImplementation(); | ||
} | ||
|
||
public interface ISample | ||
{ | ||
string InterfaceMethod(); | ||
} | ||
|
||
[Test] | ||
public void UseBaseInReturn() { | ||
var sub = Substitute.For<Sample>(); | ||
sub.RepeatButLouder(Arg.Any<string>()).Returns(x => x.BaseResult() + "?"); | ||
|
||
Assert.AreEqual("Hi!?", sub.RepeatButLouder("Hi")); | ||
} | ||
|
||
[Test] | ||
public void CallWithNoBaseImplementation() { | ||
var sub = Substitute.For<SampleWithAbstractMethod>(); | ||
sub.NoBaseImplementation().Returns(x => x.BaseResult()); | ||
|
||
Assert.Throws<NoBaseImplementationException>(() => | ||
sub.NoBaseImplementation() | ||
); | ||
} | ||
|
||
[Test] | ||
public void CallBaseForInterface() { | ||
var sub = Substitute.For<ISample>(); | ||
sub.InterfaceMethod().Returns(x => x.BaseResult()); | ||
Assert.Throws<NoBaseImplementationException>(() => | ||
sub.InterfaceMethod() | ||
); | ||
} | ||
} | ||
} |