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

add unit tests for json #2993

Merged
merged 11 commits into from
Jul 27, 2024
66 changes: 62 additions & 4 deletions tests/Neo.Json.UnitTests/UT_JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,75 @@ public void TestAsString()
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\"}}");
}

[TestMethod]
public void TestCount()
{
var jArray = new JArray { alice, bob };
jArray.Count.Should().Be(2);
}

[TestMethod]
public void TestInvalidIndexAccess()
{
var jArray = new JArray { alice };
Action action = () => { var item = jArray[1]; };
action.Should().Throw<ArgumentOutOfRangeException>();
}

[TestMethod]
public void TestEmptyEnumeration()
{
var jArray = new JArray();
foreach (var item in jArray)
{
Assert.Fail("Enumeration should not occur on an empty JArray");
}
}

[TestMethod]
public void TestImplicitConversionFromJTokenArray()
{
JToken[] jTokens = { alice, bob };
JArray jArray = jTokens;

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

[TestMethod]
public void TestAddNullValues()
{
var jArray = new JArray();
jArray.Add(null);
jArray.Count.Should().Be(1);
jArray[0].Should().BeNull();
}

[TestMethod]
public void TestClone()
{
var jArray = new JArray
var jArray = new JArray { alice, bob };
var clone = (JArray)jArray.Clone();

clone.Should().NotBeSameAs(jArray);
clone.Count.Should().Be(jArray.Count);

for (int i = 0; i < jArray.Count; i++)
{
alice,
bob,
};
clone[i]?.AsString().Should().Be(jArray[i]?.AsString());
}

var a = jArray.AsString();
var b = jArray.Clone().AsString();
a.Should().Be(b);
}

[TestMethod]
public void TestReadOnlyBehavior()
{
var jArray = new JArray();
jArray.IsReadOnly.Should().BeFalse();
}
}
}
45 changes: 45 additions & 0 deletions tests/Neo.Json.UnitTests/UT_JBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Newtonsoft.Json;
Copy link
Member

Choose a reason for hiding this comment

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

When is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TestSerializationAndDeserialization


namespace Neo.Json.UnitTests
{
[TestClass]
Expand All @@ -31,6 +33,49 @@ public void TestAsNumber()
jTrue.AsNumber().Should().Be(1);
}

[TestMethod]
public void TestDefaultConstructor()
{
var defaultJBoolean = new JBoolean();
defaultJBoolean.AsNumber().Should().Be(0);
}

[TestMethod]
public void TestExplicitFalse()
{
var explicitFalse = new JBoolean(false);
explicitFalse.AsNumber().Should().Be(0);
}

[TestMethod]
public void TestNullJBoolean()
{
JBoolean nullJBoolean = null;
Assert.ThrowsException<NullReferenceException>(() => nullJBoolean.AsNumber());
}

[TestMethod]
public void TestConversionToOtherTypes()
{
Assert.AreEqual("true", jTrue.ToString());
Assert.AreEqual("false", jFalse.ToString());
}

[TestMethod]
public void TestComparisonsWithOtherBooleans()
{
Assert.IsTrue(jTrue.Equals(new JBoolean(true)));
Assert.IsTrue(jFalse.Equals(new JBoolean()));
}

[TestMethod]
public void TestSerializationAndDeserialization()
{
string serialized = JsonConvert.SerializeObject(jTrue);
var deserialized = JsonConvert.DeserializeObject<JBoolean>(serialized);
Assert.AreEqual(jTrue, deserialized);
}

[TestMethod]
public void TestEqual()
{
Expand Down
69 changes: 69 additions & 0 deletions tests/Neo.Json.UnitTests/UT_JPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,75 @@ public void TestInvalidFormat()
Assert.ThrowsException<FormatException>(() => json.JsonPath("$..*"));
Assert.ThrowsException<FormatException>(() => json.JsonPath("..book"));
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.."));

// Test with an empty JSON Path
// Assert.ThrowsException<FormatException>(() => json.JsonPath(""));

// Test with only special characters
Assert.ThrowsException<FormatException>(() => json.JsonPath("@#$%^&*()"));

// Test with unmatched brackets
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book["));
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book)]"));

// Test with invalid operators
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book=>2"));

// Test with incorrect field syntax
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.'book'"));
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.[book]"));

// Test with unexpected end of expression
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[?(@.price<"));

// Test with invalid array indexing
// Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book['one']"));
// Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[999]"));

// Test with invalid recursive descent
Assert.ThrowsException<FormatException>(() => json.JsonPath("$..*..author"));

// Test with nonexistent functions
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book.length()"));

// Test with incorrect use of wildcards
// Assert.ThrowsException<FormatException>(() => json.JsonPath("$.*.store"));

// Test with improper use of filters
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[?(@.price)]"));

// Test with mixing of valid and invalid syntax
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[*],$.invalid"));

// Test with invalid escape sequences
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[\\]"));

// Test with incorrect property access
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.'b?ook'"));

// Test with invalid use of wildcard in array index
// Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[*]"));

// Test with missing operators in filter expressions
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[?(@.price)]"));

// Test with incorrect boolean logic in filters
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[?(@.price AND @.title)]"));

// Test with nested filters without proper closure
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[?(@.price[?(@ < 10)])]"));

// Test with misplaced recursive descent operator
// Assert.ThrowsException<FormatException>(() => json.JsonPath("$..store..book"));

// Test with using JSONPath reserved keywords incorrectly
Assert.ThrowsException<FormatException>(() => json.JsonPath("$..@.book"));

// Test with incorrect combinations of valid operators
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book..[0]"));

// Test with invalid script expressions (if supported)
Assert.ThrowsException<FormatException>(() => json.JsonPath("$.store.book[(@.length-1)]"));
}
}
}
Loading