Implemented #634: Arg.AnyType can be used in place of a generic type parameter#715
Implemented #634: Arg.AnyType can be used in place of a generic type parameter#715dtchepak merged 6 commits intonsubstitute:mainfrom
Arg.AnyType can be used in place of a generic type parameter#715Conversation
|
This is really cool, thanks! |
|
I'm not convinced this alternative is a good idea, but one possible option is to store the specific value in public class AnyType {
internal AnyType(object value) => Value = value;
public object Value { get; private set; }
}
// ...
public static ref T Do<T>(Action<T> useArgument)
{
if (typeof(T) == typeof(AnyType))
{
return ref ArgumentMatcher.Enqueue<T>(
new AnyArgumentMatcher(typeof(T)),
x => {
// Need to cast AnyType to object, then to T to be compatible with Action<T>
object arg = new AnyType(x!);
useArgument((T) arg);
}
);
}
return ref ArgumentMatcher.Enqueue<T>(new AnyArgumentMatcher(typeof(T)), x => useArgument((T) x!));
}This let's us use the underlying value in the callback, but the [Test]
public void When_AnyType_Do() {
var something = Substitute.For<ISomethingWithGenerics>();
var output = new List<object>();
// can we remove the `.Value`?
something.Log(1, Arg.Do<Arg.AnyType>(x => output.Add(x.Value)));
something.Log(1, 2);
something.Log(1, "abc");
Assert.AreEqual(new object[] { 2, "abc"}, output.ToArray());
}I'm worried requiring @icalvo do you have any thoughts on this? (@alexandrnikitin , @zvirja , would be good to get your views on this too if you get a chance.) |
|
@dtchepak I found a solution using an overload that seems to solve all the problems, please take a look! |
This addresses the issue of running .NET 6.0 with dotnet 7 SDK. See: * fsprojects/FAKE#2719 * https://fake.build/guide/getting-started.html#Run-FAKE-using-a-dedicated-build-project * https://github.com/TheAngryByrd/MiniScaffold/blob/master/build/build.fs
|
Thanks so much for this @icalvo ! Sorry for bothering you on your holiday. Hope you had a nice break 😊 |
|
@tpodolak Are any analyser changes required for this? |
|
When is this available? I pulled v 5.0.0 via nuget and this change wasn't present. |
v5.0.0 is from February. There is another fix related to AnyType done by icalvo, so probably both need to be merged before releasing it. |
@dtchepak I am not 100% sure, but we might start getting false positives from call info analyzers in cases like |
The latest CI build should have this change: |
|
|

The following is a complete NUnit test with all the implemented features. The only compromise I took is
Arg.DoForAny(action), instead ofArg.Do<Arg.AnyType>(action), but I couldn't make it work.