using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Execution; using FluentAssertions.Primitives; using Newtonsoft.Json.Linq; namespace Microsoft.Extensions.DependencyModel.Tests { public static class JsonAssertionExtensions { public static JsonAssetions Should(this JToken jToken) { return new JsonAssetions(jToken); } } public class JsonAssetions: ReferenceTypeAssertions { public JsonAssetions(JToken token) { Subject = token; } protected override string Context => nameof(JToken); public AndWhichConstraint HaveProperty(string expected) { var token = Subject[expected]; Execute.Assertion .ForCondition(token != null) .FailWith($"Expected {Subject} to have property '" + expected + "'"); return new AndWhichConstraint(this, token); } public AndConstraint NotHaveProperty(string expected) { var token = Subject[expected]; Execute.Assertion .ForCondition(token == null) .FailWith($"Expected {Subject} not to have property '" + expected + "'"); return new AndConstraint(this); } public AndWhichConstraint HavePropertyAsObject(string expected) { return HaveProperty(expected).Subject.Should().BeOfType(); } public AndConstraint HavePropertyValue(string expected, T value) { return HaveProperty(expected).Subject.Value().Should().Be(value); } } }