// 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. using System; using Newtonsoft.Json.Linq; using Xunit; namespace Microsoft.DotNet.ProjectModel.Server.Tests { public static class JArrayExtensions { public static JArray AssertJArrayEmpty(this JArray array) { Assert.NotNull(array); Assert.Empty(array); return array; } public static JArray AssertJArrayNotEmpty(this JArray array) { Assert.NotNull(array); Assert.NotEmpty(array); return array; } public static JArray AssertJArrayCount(this JArray array, int expectedCount) { Assert.NotNull(array); Assert.Equal(expectedCount, array.Count); return array; } public static JArray AssertJArrayElement(this JArray array, int index, T expectedElementValue) { Assert.NotNull(array); var element = array[index]; Assert.NotNull(element); Assert.Equal(expectedElementValue, element.Value()); return array; } public static JArray AssertJArrayContains(this JArray array, T value) { AssertJArrayContains(array, element => object.Equals(element, value)); return array; } public static JArray AssertJArrayContains(this JArray array, Func critiera) { bool contains = false; foreach (var element in array) { var value = element.Value(); contains = critiera(value); if (contains) { break; } } Assert.True(contains, "JArray doesn't contains the specified element."); return array; } public static JArray AssertJArrayNotContains(this JArray array, Func critiera) { foreach (var element in array) { var value = element.Value(); if (critiera(value)) { Assert.True(false, "JArray contains unexpected element."); } } return array; } public static T RetrieveArraryElementAs(this JArray json, int index) where T : JToken { Assert.NotNull(json); Assert.True(index >= 0 && index < json.Count, "Index out of range"); var element = json[index]; DthMessageExtension.AssertType(element, $"Element at {index}"); return (T)element; } } }