jtr/Jtr.Parsing.Tests/JsonParserTests.cs
mdnapo 96c203f842
All checks were successful
Run the JSON parser tests / test (push) Has been skipped
Moved UI to separate project, added Dockerfile & renamed project
2024-10-19 12:04:40 +02:00

136 lines
4.6 KiB
C#

using Jtr.Parsing.Extensions;
using Jtr.Parsing.Json;
using Jtr.Parsing.Json.Syntax;
namespace Jtr.Parsing.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 JsonNullSyntax);
}
[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 JsonBoolSyntax { Value: true });
Assert.That(falseNode is JsonBoolSyntax { 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 JsonNumberSyntax { Value: 1 });
}
[Test]
public void Can_parse_a_decimal()
{
const string source = "1.0";
var node = JsonParser.Parse(nameof(Can_parse_a_decimal), source);
Assert.That(node is JsonNumberSyntax { 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 JsonStringSyntax { 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 JsonArraySyntax);
}
[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 JsonArraySyntax
{
Elements: [JsonNumberSyntax { 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 JsonArraySyntax
{
Elements:
[
JsonNumberSyntax { Value: 1 },
JsonBoolSyntax { Value: true },
JsonObjectSyntax { Properties.Length: 0 },
JsonArraySyntax { Elements.Length: 0 },
JsonNullSyntax
]
});
}
[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 JsonObjectSyntax { 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 JsonObjectSyntax { Properties.Length: 1 });
var @object = node.As<JsonObjectSyntax>();
Assert.That(@object.Properties.Any(property => property.Key.Lexeme == "first_name"));
Assert.That(@object.Properties.First(property => property.Key.Lexeme == "first_name").Value is JsonStringSyntax { 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 JsonObjectSyntax { Properties.Length: 2 });
var @object = node.As<JsonObjectSyntax>();
Assert.That(@object.Properties.Any(property => property.Key.Lexeme == "first_name"));
Assert.That(@object.Properties.First(property => property.Key.Lexeme == "first_name").Value is JsonStringSyntax { Value: "John" });
Assert.That(@object.Properties.Any(property => property.Key.Lexeme == "last_name"));
Assert.That(@object.Properties.First(property => property.Key.Lexeme == "last_name").Value is JsonStringSyntax { Value: "Doe" });
});
}
}