Skip to content

Commit e8547d8

Browse files
committed
коммутативность сравнения булевых
1 parent aea47c5 commit e8547d8

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/OneScript.Core/Values/BslBooleanValue.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This Source Code Form is subject to the terms of the
99
using OneScript.Exceptions;
1010
using OneScript.Localization;
1111
using OneScript.Types;
12+
using ScriptEngine.Machine;
1213

1314
namespace OneScript.Values
1415
{
@@ -57,12 +58,15 @@ public override string ToString()
5758

5859
public override bool Equals(BslValue other)
5960
{
60-
if (ReferenceEquals(null, other))
61-
return false;
62-
if (ReferenceEquals(this, other))
63-
return true;
61+
if (other is null) return false;
62+
if (ReferenceEquals(this, other)) return true;
6463

65-
return false;
64+
return other switch
65+
{
66+
BslNumericValue num => num == ((decimal)this),
67+
BslBooleanValue boolean => _flag == boolean._flag,
68+
_ => false
69+
};
6670
}
6771

6872
public override int CompareTo(BslValue other)

src/Tests/OneScript.Core.Tests/ValuesTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,37 @@ public void String_To_String_Comparison()
201201
str1.CompareTo(ValueFactory.Create("абв")).Should().BeGreaterThan(0);
202202
}
203203

204+
[Fact]
205+
public void Boolean_Equality()
206+
{
207+
var bool1 = ValueFactory.Create(true);
208+
var bool0 = ValueFactory.Create(false);
209+
var num1 = ValueFactory.Create(1);
210+
var num0 = ValueFactory.Create(0);
211+
var num2 = ValueFactory.Create(2);
212+
213+
Assert.True(bool0.Equals(bool0));
214+
Assert.True(bool1.Equals(bool1));
215+
Assert.False(bool0.Equals(bool1));
216+
Assert.False(bool1.Equals(bool0));
217+
218+
Assert.True(bool0.Equals(num0));
219+
Assert.True(bool1.Equals(num1));
220+
Assert.False(bool0.Equals(num1));
221+
Assert.False(bool1.Equals(num0));
222+
223+
Assert.False(bool0.Equals(num2));
224+
Assert.False(bool1.Equals(num2));
225+
226+
Assert.True(num0.Equals(bool0));
227+
Assert.True(num1.Equals(bool1));
228+
Assert.False(num0.Equals(bool1));
229+
Assert.False(num1.Equals(bool0));
230+
231+
Assert.False(num2.Equals(bool1));
232+
Assert.False(num2.Equals(bool0));
233+
}
234+
204235
[Fact]
205236
public void Boolean_Comparison()
206237
{

0 commit comments

Comments
 (0)