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

Make It.IsAny, It.IsNotNull work for COM types #361

Merged
merged 1 commit into from
Jun 11, 2017

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Jun 5, 2017

This fixes #269:

Testing a COM object's type using IsAssignableFrom can report false negatives because .NET might only see the generic RCW type __ComObject instead of the actual COM interface type. The is operator on the other hand appears to be performing QueryInterface for COM objects to get the right result.

This commit introduces an additional branch in It.IsAny<> that runs an is-based check for COM objects, and the as-is reflection-based check for everything else. The added logic takes effect only in builds targeting the full .NET Framework, since COM is not available on all .NET Core platforms.

I have successfully verified the new behaviour against a COM object, but am reluctant to add any permanent test code involving COM to Moq. That would tie the unit test project to a Windows platform-specific facility. (It's possible to work around this by running a test conditionally, depending on the value of Environment.OSVersion.Platform, but that seems a little hacky.)

Source/It.cs Outdated
() => It.IsAny<TValue>());
}

/// <include file='It.xdoc' path='docs/doc[@for="It.IsNotNull"]/*'/>
public static TValue IsNotNull<TValue>()
{
return Match<TValue>.Create(
value => value != null && typeof(TValue).IsAssignableFrom(value.GetType()),
value => value is TValue,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The null check can be incorporated into the type check, since null is … is always false.

@stakx stakx force-pushed the com-objects branch 3 times, most recently from fe4fbb5 to 5ea32a3 Compare June 11, 2017 17:09
Testing a COM object's type using `IsAssignableFrom` can report false
negatives. .NET might only see the generic RCW type `__ComObject` in-
stead of the COM interface type. The `is` operator on the other hand
appears to be performing `QueryInterface` for COM objects to get the
right result.

Since `is` and `Type.IsAssignableFrom` work somewhat differently (see
https://stackoverflow.com/q/44368313/240733), the previous type check
is left in place for all objects *except* COM objects; for those, the
`is`-based check is used instead. This distinction should reduce the
likelihood of introducing a breaking change.
@stakx stakx merged commit 30d238f into devlooped:develop Jun 11, 2017
@stakx stakx deleted the com-objects branch June 11, 2017 17:22
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.

None yet

1 participant