Coder Social home page Coder Social logo

Comments (3)

alexandrnikitin avatar alexandrnikitin commented on June 1, 2024 2

@eValker Thank you for the issue and the great repro. Yes, that's a bug indeed.

It happens because when we check the provided argument specification in

public bool IsSpecificationCompatible(IArgumentSpecification specification, object? argumentValue, Type argumentType)
{
var typeArgSpecIsFor = specification.ForType;
return AreTypesCompatible(argumentType, typeArgSpecIsFor)
&& IsProvidedArgumentTheOneWeWouldGetUsingAnArgSpecForThisType(argumentValue, typeArgSpecIsFor);
}
private bool IsProvidedArgumentTheOneWeWouldGetUsingAnArgSpecForThisType(object? argument, Type typeArgSpecIsFor)
{
return _defaultChecker.IsDefault(argument, typeArgSpecIsFor);
}

we compare the provided argument with its default value
private object DefaultInstanceOfValueType(Type returnType)
{
// Performance optimization for the most popular types.
if (returnType == typeof(bool))
{
return BoxedBoolean;
}
if (returnType == typeof(int))
{
return BoxedInt;
}
if (returnType == typeof(long))
{
return BoxedLong;
}
if (returnType == typeof(double))
{
return BoxedDouble;
}
return Activator.CreateInstance(returnType)!;
}

Where we use Activator.CreateInstance to create an instance of the specified type. It in its turn calls the default constructor of the struct which initializes the field with the default value not the specified one. That's why it doesn't consider the provided argument spec and throws RedundantArgumentMatcherException

It looks like a fix would be to propagate the matchArgs from ArgumentSpecificationsFactory down to the call stack and don't compare default values if it's MatchArgs.Any
https://github.com/nsubstitute/NSubstitute/blob/f470b5de58a0bb1f41157b7ce6210e35dd4f977f/src/NSubstitute/Core/Arguments/ArgumentSpecificationsFactory.cs#L49C16-L51

But I don't remember that part of code and frankly don't understand why we need to compare the provided spec with the default value of its type. @dtchepak @zvirja maybe you remember that? Is there a better fix?

from nsubstitute.

agarbutt avatar agarbutt commented on June 1, 2024

I am specifically encountering this behavior as well while exploring strongly typed ids (defined as structs wrapping the supporting value) in my code base.

It's not obvious if there is a work-around without changing how we would model these values.

from nsubstitute.

dtchepak avatar dtchepak commented on June 1, 2024

Hi @alexandrnikitin 👋 😄

But I don't remember that part of code and frankly don't understand why we need to compare the provided spec with the default value of its type. @dtchepak @zvirja maybe you remember that? Is there a better fix?

IIRC we looked at default values to try to match up enqueued arg matchers with the actual args passed to members. (I'm not sure what the other options are there?)

The instance we return from Arg.* uses DefaultValueContainer. Can we use the same mechanism instead of Activator.CreateInstance?

from nsubstitute.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.