Skip to content

Commit 52c170d

Browse files
committed
сравнение поднято в базовый класс; уточнены тесты
1 parent 8d71eee commit 52c170d

File tree

12 files changed

+41
-66
lines changed

12 files changed

+41
-66
lines changed

src/OneScript.Core/Exceptions/ComparisonException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static ComparisonException NotSupported(string type)
3333
public static ComparisonException NotSupported(string type1, string type2)
3434
{
3535
return new ComparisonException(new BilingualString(
36-
$"Сравнение на больше/меньше типов '{type1}' и '{type2}' не поддерживается ",
36+
$"Сравнение на больше/меньше типов '{type1}' и '{type2}' не поддерживается",
3737
$"Greater-than/Less-than comparison operations are not supported for '{type1}' and '{type2}'"));
3838
}
3939
}

src/OneScript.Core/Values/BslBooleanValue.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public override string ToString()
5757

5858
public override bool Equals(BslValue other)
5959
{
60-
if (other is null) return false;
6160
if (ReferenceEquals(this, other)) return true;
6261

6362
return other switch

src/OneScript.Core/Values/BslDateValue.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ private BslDateValue(DateTime value)
2525

2626
public override int CompareTo(BslValue other)
2727
{
28-
if (ReferenceEquals(null, other))
29-
return -1;
30-
3128
if(other is BslDateValue d)
3229
return _value.CompareTo(d._value);
3330

src/OneScript.Core/Values/BslNumericValue.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public bool Equals(BslNumericValue other)
8484

8585
public override bool Equals(BslValue other)
8686
{
87-
if (ReferenceEquals(null, other)) return false;
8887
if (ReferenceEquals(this, other)) return true;
8988

9089
return other switch

src/OneScript.Core/Values/BslObjectValue.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ namespace OneScript.Values
1212
{
1313
public abstract class BslObjectValue : BslValue
1414
{
15-
public override int CompareTo(BslValue other)
16-
{
17-
throw ComparisonException.NotSupported();
18-
}
19-
2015
public override bool Equals(BslValue other)
2116
{
2217
return ReferenceEquals(this, other);

src/OneScript.Core/Values/BslPrimitiveValue.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,10 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8-
using System;
9-
using OneScript.Exceptions;
10-
118
namespace OneScript.Values
129
{
1310
public abstract class BslPrimitiveValue : BslValue
1411
{
15-
public override int CompareTo(BslValue other)
16-
{
17-
if (other == null)
18-
return -1;
19-
20-
string typeOfThis = null;
21-
string typeOfOther = null;
22-
23-
try
24-
{
25-
typeOfThis = this.SystemType.Name;
26-
typeOfOther = other.SystemType.Name;
27-
}
28-
catch (InvalidOperationException) // если тип не зарегистрирован
29-
{
30-
typeOfThis ??= this.GetType().ToString();
31-
typeOfOther ??= other.GetType().ToString();
32-
}
33-
34-
if (typeOfThis == typeOfOther)
35-
throw ComparisonException.NotSupported(typeOfThis);
36-
else
37-
throw ComparisonException.NotSupported(typeOfThis, typeOfOther);
38-
}
39-
4012
public override bool Equals(BslValue other) => false;
4113
}
4214
}

src/OneScript.Core/Values/BslStringValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public override bool Equals(BslValue other)
4949

5050
public override int CompareTo(BslValue other)
5151
{
52-
if (ReferenceEquals(null, other))
53-
return -1;
52+
if (ReferenceEquals(this, other))
53+
return 0;
5454

5555
if (other is BslStringValue s)
5656
return String.Compare(_value, s._value, StringComparison.CurrentCulture);

src/OneScript.Core/Values/BslValue.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,30 @@ public virtual string ToString(IBslProcess process)
2727
return ToString();
2828
}
2929

30-
public abstract int CompareTo(BslValue other);
30+
public virtual int CompareTo(BslValue other)
31+
{
32+
if (other is null)
33+
return -1;
34+
35+
string typeOfThis = null;
36+
string typeOfOther = null;
37+
38+
try
39+
{
40+
typeOfThis = this.SystemType.Name;
41+
typeOfOther = other.SystemType.Name;
42+
}
43+
catch (InvalidOperationException) // если тип не зарегистрирован
44+
{
45+
typeOfThis ??= this.GetType().ToString();
46+
typeOfOther ??= other.GetType().ToString();
47+
}
48+
49+
if (typeOfThis == typeOfOther)
50+
throw ComparisonException.NotSupported(typeOfThis);
51+
else
52+
throw ComparisonException.NotSupported(typeOfThis, typeOfOther);
53+
}
3154

3255
public abstract bool Equals(BslValue other);
3356

@@ -76,7 +99,7 @@ public static explicit operator DateTime(BslValue target) =>
7699

77100
public virtual IValue GetRawValue() => this;
78101

79-
private BslValue UnwrapReference(IValue v)
102+
private static BslValue UnwrapReference(IValue v)
80103
{
81104
if (v is IValueReference r)
82105
return r.BslValue;

src/OneScript.Core/Values/EnumerationValue.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public override string ToString()
4646

4747
public override IValue GetRawValue() => this;
4848

49-
public override int CompareTo(BslValue other)
50-
{
51-
throw ComparisonException.NotSupported();
52-
}
53-
5449
public override bool Equals(BslValue other)
5550
{
5651
return ReferenceEquals(other?.GetRawValue(), this);

src/ScriptEngine/Machine/Contexts/ContextIValueImpl.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ public override TypeDescriptor SystemType
5858

5959
#endregion
6060

61-
#region IComparable<IValue> Members
62-
63-
public override int CompareTo(BslValue other)
64-
{
65-
throw ComparisonException.NotSupported();
66-
}
67-
68-
#endregion
69-
7061
#region IEquatable<IValue> Members
7162

7263
public override bool Equals(BslValue other)

0 commit comments

Comments
 (0)