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

generate IParams for users in smart way #754

Merged
merged 1 commit into from
May 17, 2018
Merged

generate IParams for users in smart way #754

merged 1 commit into from
May 17, 2018

Conversation

adamsitnik
Copy link
Member

This change generates the right IParam for the users for any complex types returned by [ArgumentsSource] and [ParamsSource]

I was tired of writing another IParam when I realized that I don't need to do that.

Idea:

  1. generate code which is going to call the [Source] getter/method
  2. it always returns IEnumerable so we can call ToArray() on it
  3. use array indexer to get the argument

Examples and auto-generated code:

[ParamsSource(nameof(DictionaryAsParam))]
public Dictionary<int, string> DictionaryParamInstance;

public IEnumerable<object> DictionaryAsParam => new object[] { new Dictionary<int, string>() { { 1234, "it's an instance getter" } } };

generates:
Runnable_0 instance = new Runnable_0(); 
instance.DictionaryParamInstance= (System.Collections.Generic.Dictionary<System.Int32, System.String>)instance.DictionaryAsParam().ToArray()[0];;
[Benchmark]
[ArgumentsSource(nameof(NonPrimitive))]
public void Simple(SomeClass someClass, SomeStruct someStruct)

public IEnumerable<object[]> NonPrimitive()
{
    yield return new object[] { new SomeClass(Enumerable.Range(0, 100).ToArray()), new SomeStruct(100) };
    yield return new object[] { new SomeClass(Enumerable.Range(0, 1000).ToArray()), new SomeStruct(1000) };
}

__argField0 = (BenchmarkDotNet.IntegrationTests.WithNonPrimitiveArgumentsSource.SomeClass)NonPrimitive().ToArray()[0][0];;
__argField1 = (BenchmarkDotNet.IntegrationTests.WithNonPrimitiveArgumentsSource.SomeStruct)NonPrimitive().ToArray()[0][1];;

@adamsitnik
Copy link
Member Author

@AndreyAkinshin please take a look (it's 3:30 am here and I am falling asleep and I need really good review)

@adamsitnik
Copy link
Member Author

It's blocking me, I am merging it.

@adamsitnik adamsitnik merged commit 4665ec5 into master May 17, 2018
@AndreyAkinshin
Copy link
Member

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants