-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1052 from microsoft/fix1051
Add implicit conversions between `bool` and `VARIANT_BOOL`
- Loading branch information
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
partial struct VARIANT_BOOL | ||
{ | ||
internal VARIANT_BOOL(bool value) => this.Value = value ? VARIANT_TRUE : VARIANT_FALSE; | ||
public static implicit operator bool(VARIANT_BOOL value) => value != VARIANT_FALSE; | ||
public static implicit operator VARIANT_BOOL(bool value) => value ? VARIANT_TRUE : VARIANT_FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Windows.Win32.Foundation; | ||
|
||
public class VARIANT_BOOLTests | ||
{ | ||
[Fact] | ||
public void Ctor_bool() | ||
{ | ||
VARIANT_BOOL b = true; | ||
bool b2 = b; | ||
Assert.True(b); | ||
Assert.True(b2); | ||
|
||
Assert.False(default(VARIANT_BOOL)); | ||
} | ||
|
||
[Fact] | ||
public void Ctor_int() | ||
{ | ||
Assert.Equal(2, new VARIANT_BOOL(2).Value); | ||
} | ||
|
||
[Fact] | ||
public void ExplicitCast() | ||
{ | ||
Assert.Equal(2, ((VARIANT_BOOL)2).Value); | ||
} | ||
|
||
[Theory] | ||
[InlineData(3)] | ||
[InlineData(-1)] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(0xfff)] | ||
public void LossyConversionFromBOOLtoBool(short ordinal) | ||
{ | ||
VARIANT_BOOL nativeBool = new VARIANT_BOOL(ordinal); | ||
bool managedBool = nativeBool; | ||
Assert.Equal(ordinal != 0, managedBool); | ||
BOOLEAN roundtrippedNativeBool = managedBool; | ||
Assert.Equal(managedBool ? 1 : 0, roundtrippedNativeBool); | ||
} | ||
|
||
[Fact] | ||
public void BOOLEqualsComparesExactValue() | ||
{ | ||
VARIANT_BOOL b1 = new VARIANT_BOOL(1); | ||
VARIANT_BOOL b2 = new VARIANT_BOOL(2); | ||
Assert.Equal(b1, b1); | ||
Assert.NotEqual(b1, b2); | ||
} | ||
|
||
[Fact] | ||
public void BOOL_OverridesEqualityOperator() | ||
{ | ||
var @true = new VARIANT_BOOL(true); | ||
var @false = new VARIANT_BOOL(false); | ||
Assert.True(@true == new VARIANT_BOOL(true)); | ||
Assert.False(@true != new VARIANT_BOOL(true)); | ||
Assert.True(@true != @false); | ||
Assert.False(@true == @false); | ||
|
||
var two = new VARIANT_BOOL(2); | ||
Assert.False(two == @true); | ||
Assert.True(two != @true); | ||
} | ||
|
||
[Fact] | ||
public void LogicalOperators_And() | ||
{ | ||
VARIANT_BOOL @true = true, @false = false; | ||
Assert.False(@false && @false); | ||
Assert.False(@true && @false); | ||
Assert.True(@true && @true); | ||
} | ||
|
||
[Fact] | ||
public void LogicalOperators_Or() | ||
{ | ||
VARIANT_BOOL @true = true, @false = false; | ||
Assert.True(@true || @false); | ||
Assert.False(@false || @false); | ||
Assert.True(@true || @true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters