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

Fix: fix equal #3028

Merged
merged 10 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/Neo.Json/JBoolean.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2015-2022 The Neo Project.
//
// The Neo.Json is free software distributed under the MIT software license,
//
// The Neo.Json is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -71,5 +71,33 @@ public static implicit operator JBoolean(bool value)
{
return new JBoolean(value);
}

public static bool operator ==(JBoolean left, JBoolean right)
{
return left.Value.Equals(right.Value);
}

public static bool operator !=(JBoolean left, JBoolean right)
{
return !left.Value.Equals(right.Value);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj is JBoolean other)
{
return this.Value.Equals(other.Value);
}
return false;
}

public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}
53 changes: 49 additions & 4 deletions src/Neo.Json/JNumber.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2015-2022 The Neo Project.
//
// The Neo.Json is free software distributed under the MIT software license,
//
// The Neo.Json is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -117,5 +117,50 @@ public static implicit operator JNumber(double value)
{
return new JNumber(value);
}

public static implicit operator JNumber(long value)
{
return new JNumber(value);
}

public static bool operator ==(JNumber left, JNumber? right)
{
if (right is null) return false;
return ReferenceEquals(left, right) || left.Value.Equals(right.Value);
}

public static bool operator !=(JNumber left, JNumber right)
{
return !(left == right);
}

public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;

var other = obj switch
{
JNumber jNumber => jNumber,
uint u => new JNumber(u),
int i => new JNumber(i),
ulong ul => new JNumber(ul),
long l => new JNumber(l),
byte b => new JNumber(b),
sbyte sb => new JNumber(sb),
short s => new JNumber(s),
ushort us => new JNumber(us),
decimal d => new JNumber((double)d),
float f => new JNumber(f),
double d => new JNumber(d),
_ => throw new ArgumentOutOfRangeException(nameof(obj), obj, null)
};
return other == this;
}

public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}
39 changes: 35 additions & 4 deletions src/Neo.Json/JString.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2015-2022 The Neo Project.
//
// The Neo.Json is free software distributed under the MIT software license,
//
// The Neo.Json is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -92,5 +92,36 @@ public static implicit operator JString(Enum value)
{
return value == null ? null : new JString(value);
}

public static bool operator ==(JString left, JString? right)
{
if (right is null) return false;
return ReferenceEquals(left, right) || left.Value.Equals(right.Value);
}

public static bool operator !=(JString left, JString right)
{
return !(left == right);
}

public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj is JString other)
{
return this == other;
}
if (obj is string str)
{
return this.Value == str;
}
return false;
}

public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}
9 changes: 9 additions & 0 deletions tests/Neo.Json.UnitTests/UT_JBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public void TestAsNumber()
jFalse.AsNumber().Should().Be(0);
jTrue.AsNumber().Should().Be(1);
}

[TestMethod]
public void TestEqual()
{
Assert.IsTrue(jTrue.Equals(new JBoolean(true)));
Assert.IsTrue(jTrue == new JBoolean(true));
Assert.IsTrue(jFalse.Equals(new JBoolean()));
Assert.IsTrue(jFalse == new JBoolean());
}
}
}
11 changes: 10 additions & 1 deletion tests/Neo.Json.UnitTests/UT_JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,18 @@ public void TestGetEnum()
new JNumber(1).GetEnum<Woo>().Should().Be(Woo.Jerry);
new JNumber(2).GetEnum<Woo>().Should().Be(Woo.James);
new JNumber(3).AsEnum<Woo>().Should().Be(Woo.Tom);

Action action = () => new JNumber(3).GetEnum<Woo>();
action.Should().Throw<InvalidCastException>();
}

[TestMethod]
public void TestEqual()
{
Assert.IsTrue(maxInt.Equals(JNumber.MAX_SAFE_INTEGER));
Assert.IsTrue(maxInt == JNumber.MAX_SAFE_INTEGER);
Assert.IsTrue(minInt.Equals(JNumber.MIN_SAFE_INTEGER));
Assert.IsTrue(minInt == JNumber.MIN_SAFE_INTEGER);
Assert.IsTrue(zero == new JNumber());
}
}
}
9 changes: 9 additions & 0 deletions tests/Neo.Json.UnitTests/UT_JString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ public void TestGetEnum()
woo = s.AsEnum(Woo.Jerry, false);
Assert.AreEqual(Woo.Jerry, woo);
}
[TestMethod]
public void TestEqual()
{
var str = "hello world";
var jString = new JString(str);
Assert.IsTrue(jString.Equals(str));
Assert.IsTrue(jString == str);
Assert.IsTrue(jString != "hello world2");
}
}
}