Skip to content

InvalidCastException thrown in AssertEqualShouldNotBeUsedForCollectionSizeCheck #1502

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

Closed
dougbu opened this issue Oct 12, 2017 · 0 comments
Closed
Labels
Analyzers https://github.com/xunit/xunit.analyzers Bug A fault in an existing feature help wanted

Comments

@dougbu
Copy link
Contributor

dougbu commented Oct 12, 2017

This relates to #1491 because both issues lead to an AD0001 report. However the underlying Exception is different.

Consider an expression in the analyzed test code such as Assert.Equal('b', reader.Peek()); where reader is a TextReader. This assertion compares char and int expressions. However the semantic model contains (boxed) objects for both. The first object cannot be converted directly to an int, causing an InvalidCastException when (int)size.Value is evaluated here.

Workaround is to convert the char argument to an int up front e.g. Assert.Equal('b' - '\0', reader.Peek());.

Fixes include ignoring all non-int first arguments, converting non-int arguments to int when necessary, or unconditionally doing the conversion e.g. var value = Convert.ToInt32(size.Value);.

@marcind marcind added Analyzers https://github.com/xunit/xunit.analyzers help wanted Bug A fault in an existing feature labels Oct 12, 2017
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Oct 25, 2017
….Web.Razor.Test to end

- part of #65

Few manual changes:
- work around xunit/xunit#1502
- use `Assert.IsAssignableFrom<T>(...)`, `Assert.IsType<T>(...)` and `Assert.Single(...)` return values
- suppress xUnit1013, Public method should be marked as test
  - helper method is called from multiple test classes; none inherit from defining class
- fix xUnit1026, `[Theory]` method doesn't use all parameters
  - add new data set properties
- suppress xUnit2013, Do not use equality check to check for collection size.
  - calls `Count` because the `Mock<T>` does not set up `GetEnumerable()`
- `Assert.Equal<T>(...)` -> `Assert.Equal(...)`
- avoid Linq's `.Where` inside `Assert.Contains(...)` and similar
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Oct 31, 2017
….Web.Razor.Test to end

- part of #65

Few manual changes:
- work around xunit/xunit#1502
- use `Assert.IsAssignableFrom<T>(...)`, `Assert.IsType<T>(...)` and `Assert.Single(...)` return values
- suppress xUnit1013, Public method should be marked as test
  - helper method is called from multiple test classes; none inherit from defining class
- fix xUnit1026, `[Theory]` method doesn't use all parameters
  - add new data set properties
- suppress xUnit2013, Do not use equality check to check for collection size.
  - calls `Count` because the `Mock<T>` does not set up `GetEnumerable()`
- `Assert.Equal<T>(...)` -> `Assert.Equal(...)`
- avoid Linq's `.Where` inside `Assert.Contains(...)` and similar
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Oct 31, 2017
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Nov 1, 2017
….Web.Razor.Test to end

- part of #65

Few manual changes:
- temporarily remove xUnit analyzers from System.Web.Razor.Test
  - work around xunit/xunit#1502
  - should restore analyzers as part of a #96 fix
- use `Assert.IsAssignableFrom<T>(...)`, `Assert.IsType<T>(...)` and `Assert.Single(...)` return values
- avoid xUnit1013, Public method should be marked as test
  - mark a helper method `internal`
- fix xUnit1026, `[Theory]` method doesn't use all parameters
  - add new data set properties
- suppress xUnit2013, Do not use equality check to check for collection size.
  - calls `Count` because the `Mock<T>` does not set up `GetEnumerable()`
- `Assert.Equal<T>(...)` -> `Assert.Equal(...)`
- avoid Linq's `.Where` inside `Assert.Contains(...)` and similar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analyzers https://github.com/xunit/xunit.analyzers Bug A fault in an existing feature help wanted
Projects
None yet
Development

No branches or pull requests

2 participants