dotnet-installer/test/dotnet-projectmodel-server.Tests/Helpers/JObjectExtensions.cs

84 lines
2.8 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2015-12-09 17:57:45 +00:00
using System;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public static class JObjectExtensions
{
public static JObject AsJObject(this JToken token)
{
DthMessageExtension.AssertType<JObject>(token, nameof(JToken));
return (JObject)token;
}
public static JObject RetrieveDependencyDiagnosticsErrorAt(this JObject payload, int index)
{
Assert.NotNull(payload);
return payload.RetrievePropertyAs<JArray>("Errors")
.RetrieveArraryElementAs<JObject>(index);
}
public static T RetrieveDependencyDiagnosticsErrorAt<T>(this JObject payload, int index)
where T : JToken
{
Assert.NotNull(payload);
return payload.RetrievePropertyAs<JArray>("Errors")
.RetrieveArraryElementAs<T>(index);
}
public static T RetrievePropertyAs<T>(this JObject json, string propertyName)
where T : JToken
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.NotNull(property);
DthMessageExtension.AssertType<T>(property, $"Property {propertyName}");
return (T)property;
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, T expectedPropertyValue)
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.NotNull(property);
Assert.Equal(expectedPropertyValue, property.Value<T>());
return json;
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, Func<T, bool> assertion)
{
return AssertProperty<T>(json,
propertyName,
assertion,
value => $"Assert failed on {propertyName}.");
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, Func<T, bool> assertion, Func<T, string> errorMessage)
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.False(property == null, $"Property {propertyName} doesn't exist.");
var propertyValue = property.Value<T>();
Assert.False(propertyValue == null, $"Property {propertyName} of type {typeof(T).Name} doesn't exist.");
Assert.True(assertion(propertyValue),
errorMessage(propertyValue));
return json;
}
}
}