132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
namespace DevDisciples.Json.Parser.Tests;
|
|
|
|
public class JsonParserTests
|
|
{
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_null()
|
|
{
|
|
const string source = "null";
|
|
var node = JsonParser.Parse(nameof(Can_parse_null), source);
|
|
Assert.That(node is JsonNull);
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_booleans()
|
|
{
|
|
const string trueSource = "true";
|
|
const string falseSource = "false";
|
|
var trueNode = JsonParser.Parse(nameof(Can_parse_booleans), trueSource);
|
|
var falseNode = JsonParser.Parse(nameof(Can_parse_booleans), falseSource);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(trueNode is JsonBool { Value: true });
|
|
Assert.That(falseNode is JsonBool { Value: false });
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_integer()
|
|
{
|
|
const string source = "1";
|
|
var node = JsonParser.Parse(nameof(Can_parse_an_integer), source);
|
|
Assert.That(node is JsonNumber { Value: 1 });
|
|
}
|
|
|
|
[Test]
|
|
public void Can_lex_a_decimal()
|
|
{
|
|
const string source = "1.0";
|
|
var node = JsonParser.Parse(nameof(Can_lex_a_decimal), source);
|
|
Assert.That(node is JsonNumber { Value: 1.0 });
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_a_string()
|
|
{
|
|
const string source = "\"Hello world!\"";
|
|
var tokens = JsonParser.Parse(nameof(Can_parse_a_string), source);
|
|
Assert.That(tokens is JsonString { Value: "Hello world!" });
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_empty_array()
|
|
{
|
|
const string source = "[]";
|
|
var tokens = JsonParser.Parse(nameof(Can_parse_an_empty_array), source);
|
|
Assert.That(tokens is JsonArray);
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_a_single_item_array()
|
|
{
|
|
const string source = "[1]";
|
|
var tokens = JsonParser.Parse(nameof(Can_parse_a_single_item_array), source);
|
|
|
|
Assert.That(tokens is JsonArray
|
|
{
|
|
Elements: [JsonNumber { Value: 1 }]
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_array_with_multiple_items()
|
|
{
|
|
const string source = "[1,true,{},[],null]";
|
|
var node = JsonParser.Parse(nameof(Can_parse_an_array_with_multiple_items), source);
|
|
|
|
Assert.That(node is JsonArray
|
|
{
|
|
Elements:
|
|
[
|
|
JsonNumber { Value: 1 },
|
|
JsonBool { Value: true },
|
|
JsonObject { Properties.Length: 0 },
|
|
JsonArray { Elements.Length: 0 },
|
|
JsonNull
|
|
]
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_empty_object()
|
|
{
|
|
const string source = "{}";
|
|
var tokens = JsonParser.Parse(nameof(Can_parse_an_empty_object), source);
|
|
Assert.That(tokens is JsonObject { Properties.Length: 0 });
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_object_with_one_entry()
|
|
{
|
|
const string source = "{\"first_name\":\"John\"}";
|
|
var node = JsonParser.Parse(nameof(Can_parse_an_object_with_one_entry), source);
|
|
|
|
Assert.That(node is JsonObject { Properties.Length: 1 });
|
|
var @object = (JsonObject)node;
|
|
Assert.That(@object.Properties.Any(property => property.Key == "first_name"));
|
|
Assert.That(@object.Properties.First(property => property.Key == "first_name").Value is JsonString { Value: "John" });
|
|
}
|
|
|
|
[Test]
|
|
public void Can_parse_an_object_with_multiple_entries()
|
|
{
|
|
const string source = "{\"first_name\":\"John\", \"last_name\": \"Doe\"}";
|
|
var node = JsonParser.Parse(nameof(Can_parse_an_object_with_one_entry), source);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(node is JsonObject { Properties.Length: 2 });
|
|
var @object = (JsonObject)node;
|
|
Assert.That(@object.Properties.Any(property => property.Key == "first_name"));
|
|
Assert.That(@object.Properties.First(property => property.Key == "first_name").Value is JsonString { Value: "John" });
|
|
Assert.That(@object.Properties.Any(property => property.Key == "last_name"));
|
|
Assert.That(@object.Properties.First(property => property.Key == "last_name").Value is JsonString { Value: "Doe" });
|
|
});
|
|
}
|
|
} |