Skip to content

Fixes #2970 InvalidCastException @ NUnit.Framework.TestFixtureSourceAttribute.BuildFrom #2971

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

Merged
merged 7 commits into from
Sep 11, 2018

Conversation

BrunoJuchli
Copy link
Contributor

@BrunoJuchli BrunoJuchli commented Aug 21, 2018

Fixes #2970

I've done three things:

  • removed the unnecessary cast from NUnit.Framework.TestFixtureSourceAttribute.BuildFrom
  • removed internal setter from NUnit.Framework.Internal.TestFixtureParameters.TypeArgs - as there's no usages of the setter (apart from being set by ctor)
  • added test cases, including one for type parameter deduction from ctor arguments (=> goal: unit tests show what's supported and what not).

Question: how can / should I run the tests included in the nunit solution locally? I've not found any documentation on this.

@BrunoJuchli BrunoJuchli changed the title Fixes #2970 InvalidCastException @ NUnit.Framework.TestFixtureSourceAttribute.BuildFrom [WIP] Fixes #2970 InvalidCastException @ NUnit.Framework.TestFixtureSourceAttribute.BuildFrom Aug 21, 2018
@jnm2
Copy link
Contributor

jnm2 commented Aug 21, 2018

Thanks for taking the time! Getting @nunit/framework-team's attention since there isn't a discussion issue.

Question: how can / should I run the tests included in the nunit solution locally? I've not found any documentation on this.

Perhaps we should link this from the readme: https://github.com/nunit/nunit/blob/master/BUILDING.md#build-script

jnm2
jnm2 previously approved these changes Aug 21, 2018
Copy link
Contributor

@jnm2 jnm2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides the typo.

}

[TestFixtureSource(typeof(GenericFixtureWithTypeAndConstructorArgsSource), "Source")]
public class GenerixFixtureSourceWithTypeAndConstructorArgs<T>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo Generix

@jnm2
Copy link
Contributor

jnm2 commented Aug 21, 2018

@BrunoJuchli I noticed that this test is failing :

NUnit.Framework.Attributes.TestFixtureSourceTests.CanRunGenericFixtureSourceWithProperTypeAndConstructorArgsProvided
>   Expected: Runnable
>   But was:  NotRunnable
>    at NUnit.Framework.Attributes.TestFixtureSourceTests.CanRunGenericFixtureSourceWithProperTypeAndConstructorArgsProvided() in C:\projects\nunit\src\NUnitFramework\tests\Attributes\TestFixtureSourceTests.cs:line 164

For suggestions on how to run tests locally other than BUILDING.md, see #2973. :D

@mikkelbu
Copy link
Member

Thanks for taking the time! Getting @nunit/framework-team's attention since there isn't a discussion issue.

Isn't the (discussion) issue #2970 (or am I overlooking something)?

@BrunoJuchli I've edited your message to mention #2970, so that the issue and PR will be linked (and also such that the PR will auto close the issue when merged). Mentioning it in the titel will not work as far as I know.

@@ -109,7 +108,7 @@ public IEnumerable<TestSuite> BuildFrom(Type type)
{
Type sourceType = SourceType ?? type;

foreach (TestFixtureParameters parms in GetParametersFor(sourceType))
foreach (ITestFixtureData parms in GetParametersFor(sourceType))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also remove the same unnecessary cast below? (https://github.com/nunit/nunit/pull/2971/files#diff-a7991f3c3308d3394ecebfc7b72ba252R128)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikkelbu
I guess that's why the test is failing. Will have a look.
Thanks!

}
}

[TestFixtureSource(typeof(GenericFixtureWithTypeAndConstructorArgsSource), "Source")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about "Source" => nameof(GenericFixtureWithTypeAndConstructorArgsSource.Source)?

Copy link
Contributor Author

@BrunoJuchli BrunoJuchli Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikkelbu
I implemented it like that at first but then I wasn't sure whether that would be supported by the compiler c# level and what the project's preference is.
All other examples in the same file use constant strings instead of the nameof(...) operator.
If nameof is fine I'll create a separate pull request to modify all string references - across the whole project - to types/members to use nameof(...) - that would make sense, right?

I guess a passing build + passing tests should be conclusive about whether nameof(...) is supported? Or does it need to be compiled on other platforms, too? (I've got windows + net)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The projects are all set to C# 6.0, so if you don't get a error squiggle, you're fine.
In typeof(...), nameof(...) cases like this with so much repetition, I'm not totally happy with any of our options :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnm2 thanks!


I'd be happy to provide a PR to change string-constants to nameof(..), only adjust the newly introduced usages, or open a new issue to discuss the topic.
However, I'm not in a position to decide which is the right way to go so I'll refrain from doing anything regarding this topic until I get instructions ;-)

};
}

[TestFixtureSource(typeof(GenericFixtureWithConstructorArgsSource), "Source")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

@jnm2
Copy link
Contributor

jnm2 commented Aug 21, 2018

Thanks @mikkelbu, the issue had escaped my notice.

@BrunoJuchli BrunoJuchli changed the title [WIP] Fixes #2970 InvalidCastException @ NUnit.Framework.TestFixtureSourceAttribute.BuildFrom Fixes #2970 InvalidCastException @ NUnit.Framework.TestFixtureSourceAttribute.BuildFrom Aug 23, 2018
@BrunoJuchli
Copy link
Contributor Author

BrunoJuchli commented Aug 23, 2018

@jnm2 @mikkelbu
Thanks for all your feedback. I've incorporated it, apart from the nameof(...) operator (see below).
IMO it should be good to merge now.

Regarding nameof(..) I see the following options:

a) leave it as it is
b) apply nameof(...) for the whole solution (=> I'll provide a separate PR)
c) apply nameof(...) just to new code (=> I'll update this PR)

I personally would favor b) for improved refactor-safety. But I'll leave the decision to you guys, of course.
If you choose b) then you might want to create a new issue and assign it to me.

Copy link
Member

@mikkelbu mikkelbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

With respect to nameof(...) then I would also say option b), but to begin with just in the test-project. @nunit/framework-team What do you think?

@ChrisMaddock
Copy link
Member

Yep, I agree

@jnm2
Copy link
Contributor

jnm2 commented Aug 24, 2018

We've already done some work towards a nameof refactor: #2623

I think we're all on board with going further. My viewpoint has been that nameof pays off most in library code and least in test code (since test code is under immediate and intense scrutiny all the time).

@mikkelbu
Copy link
Member

Hi @nunit/framework-team can one of you do a second review, so that we can merge this in, as it is closing an issue which is part of the 3.11 milestone. Then we can always create a subsequent task wrt. nameof(..).

@@ -126,7 +122,7 @@ public IEnumerable<TestSuite> BuildFrom(Type type, IPreFilter filter)
{
Type sourceType = SourceType ?? type;

foreach (TestFixtureParameters parms in GetParametersFor(sourceType))
foreach (ITestFixtureData parms in GetParametersFor(sourceType))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for doing this! This has bugged me for a while. I thought it was due to having to use the type name instead of var which silently introduces a possible invalid cast exception when the method return type is changed.

@jnm2 jnm2 merged commit 49ded54 into nunit:master Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants