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

[Neo Json Fix] Json null tests #3450

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Neo.Json/JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Add(JToken? item)

public override string AsString()
{
return string.Join(",", items.Select(p => p?.AsString()));
return ToString();
}

public override void Clear()
Expand Down
94 changes: 93 additions & 1 deletion tests/Neo.Json.UnitTests/UT_JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void TestAsString()
bob,
};
var s = jArray.AsString();
Assert.AreEqual(s, "{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}");
Assert.AreEqual(s, "[{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]");
Copy link
Member

@shargon shargon Aug 5, 2024

Choose a reason for hiding this comment

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

it change the behavior, and it could require a hardfork

Copy link
Contributor

Choose a reason for hiding this comment

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

And JSON is available from contracts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it must be a hardfork, then make it a hardfork, this one also need to be reviewed:

#3037

Copy link
Contributor Author

Choose a reason for hiding this comment

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

change the contact behavior is bad,,,,,,,i know, but what we gonna do with tons of problems in the core....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And JSON is available from contracts.

BTW, how does contract use this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And without [] is only part of the problem, it also lacks null object.

Copy link
Member

Choose a reason for hiding this comment

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

Than what is the point of having AsString vs ToString? If they both do the samething.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Than what is the point of having AsString vs ToString? If they both do the samething.

To be honest i dont know,,,, and there is no document for this

Copy link
Member

Choose a reason for hiding this comment

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

And without [] is only part of the problem, it also lacks null object.

could you fix both at same time, but we need to ensure that is not possible to call it by a smart contract

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And without [] is only part of the problem, it also lacks null object.

could you fix both at same time, but we need to ensure that is not possible to call it by a smart contract

Already fixed.

}

[TestMethod]
Expand Down Expand Up @@ -325,5 +325,97 @@ public void TestReadOnlyBehavior()
var jArray = new JArray();
jArray.IsReadOnly.Should().BeFalse();
}

[TestMethod]
public void TestAddNull()
{
var jArray = new JArray { null };

jArray.Count.Should().Be(1);
jArray[0].Should().BeNull();
}

[TestMethod]
public void TestSetNull()
{
var jArray = new JArray { alice };
jArray[0] = null;

jArray.Count.Should().Be(1);
jArray[0].Should().BeNull();
}

[TestMethod]
public void TestInsertNull()
{
var jArray = new JArray { alice };
jArray.Insert(0, null);

jArray.Count.Should().Be(2);
jArray[0].Should().BeNull();
jArray[1].Should().Be(alice);
}

[TestMethod]
public void TestRemoveNull()
{
var jArray = new JArray { null, alice };
jArray.Remove(null);

jArray.Count.Should().Be(1);
jArray[0].Should().Be(alice);
}

[TestMethod]
public void TestContainsNull()
{
var jArray = new JArray { null, alice };
jArray.Contains(null).Should().BeTrue();
jArray.Contains(bob).Should().BeFalse();
}

[TestMethod]
public void TestIndexOfNull()
{
var jArray = new JArray { null, alice };
jArray.IndexOf(null).Should().Be(0);
jArray.IndexOf(alice).Should().Be(1);
}

[TestMethod]
public void TestCopyToWithNull()
{
var jArray = new JArray { null, alice };
JObject[] jObjects = new JObject[2];
jArray.CopyTo(jObjects, 0);

jObjects[0].Should().BeNull();
jObjects[1].Should().Be(alice);
}

[TestMethod]
public void TestToStringWithNull()
{
var jArray = new JArray { null, alice, bob };
var jsonString = jArray.ToString();
var asString = jArray.AsString();
// JSON string should properly represent the null value
jsonString.Should().Be("[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]");
asString.Should().Be("[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]");
}

[TestMethod]
public void TestFromStringWithNull()
{
var jsonString = "[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]";
var jArray = (JArray)JArray.Parse(jsonString);

jArray.Count.Should().Be(3);
jArray[0].Should().BeNull();

// Checking the second and third elements
jArray[1]["name"].AsString().Should().Be("alice");
jArray[2]["name"].AsString().Should().Be("bob");
}
}
}