Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect wrong usages of callInfo argument access #30

Closed
tpodolak opened this issue Aug 5, 2018 · 0 comments
Closed

Detect wrong usages of callInfo argument access #30

tpodolak opened this issue Aug 5, 2018 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@tpodolak
Copy link
Member

tpodolak commented Aug 5, 2018

Analyzers should be able to detect common mistakes when accessing invocation arguments via CallInfo.

using System;
using NSubstitute;

namespace MyNamespace
{
    public interface Foo
    {
        int Bar(int x);
    }

    public class Bar
    {
    }

    public class FooTests
    {
        public void Test()
        {
            var substitute = NSubstitute.Substitute.For<Foo>();
            substitute.Bar(Arg.Any<int>()).Returns(callInfo =>
            {
                var x = (Bar)callInfo[0]; // invalid cast
                var xx = callInfo[0] as Bar; // invalid cast
                var xxx = (Bar)callInfo.Args()[0]; // invalid cast
                var xxxx = callInfo.Args()[0] as Bar; // invalid cast
                var xxxxx = callInfo[1]; // out of bounds
                var xxxxxx = callInfo.Args()[1]; // out of bounds
                var xxxxxxx = callInfo.ArgTypes()[1]; // out of bounds
                callInfo[0] = 1; // no ref or out method
                callInfo[0] = new object(); //  no ref or out method, wrong type assigned
                return 1;
            });
        }
    }
}
@tpodolak tpodolak added the enhancement New feature or request label Aug 5, 2018
@tpodolak tpodolak self-assigned this Aug 5, 2018
tpodolak added a commit that referenced this issue Aug 24, 2018
tpodolak added a commit that referenced this issue Aug 24, 2018
tpodolak added a commit that referenced this issue Aug 24, 2018
tpodolak added a commit that referenced this issue Aug 24, 2018
tpodolak added a commit that referenced this issue Aug 24, 2018
tpodolak added a commit that referenced this issue Aug 26, 2018
@tpodolak tpodolak added this to the 0.1.0-beta3 milestone Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant