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

101 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 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<T>(this JArray array, int index, T expectedElementValue)
{
Assert.NotNull(array);
var element = array[index];
Assert.NotNull(element);
Assert.Equal(expectedElementValue, element.Value<T>());
return array;
}
public static JArray AssertJArrayContains<T>(this JArray array, T value)
{
AssertJArrayContains<T>(array, element => object.Equals(element, value));
return array;
}
public static JArray AssertJArrayContains<T>(this JArray array, Func<T, bool> critiera)
{
bool contains = false;
foreach (var element in array)
{
var value = element.Value<T>();
contains = critiera(value);
if (contains)
{
break;
}
}
Assert.True(contains, "JArray doesn't contains the specified element.");
return array;
}
2016-04-04 02:29:23 +00:00
public static JArray AssertJArrayNotContains<T>(this JArray array, Func<T, bool> critiera)
{
foreach (var element in array)
{
var value = element.Value<T>();
if (critiera(value))
{
Assert.True(false, "JArray contains unexpected element.");
}
}
return array;
}
2015-12-09 17:57:45 +00:00
public static T RetrieveArraryElementAs<T>(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<T>(element, $"Element at {index}");
return (T)element;
}
}
}