mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
Revert filescoped (#2227)
* Revert "Add git blame entry (#2226)" This reverts commit239286737d
. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a
.
This commit is contained in:
@ -7,222 +7,223 @@ using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace Bit.Test.Common.Helpers;
|
||||
|
||||
public static class AssertHelper
|
||||
namespace Bit.Test.Common.Helpers
|
||||
{
|
||||
public static void AssertPropertyEqual(object expected, object actual, params string[] excludedPropertyStrings)
|
||||
public static class AssertHelper
|
||||
{
|
||||
var relevantExcludedProperties = excludedPropertyStrings.Where(name => !name.Contains('.')).ToList();
|
||||
if (expected == null)
|
||||
public static void AssertPropertyEqual(object expected, object actual, params string[] excludedPropertyStrings)
|
||||
{
|
||||
Assert.Null(actual);
|
||||
return;
|
||||
var relevantExcludedProperties = excludedPropertyStrings.Where(name => !name.Contains('.')).ToList();
|
||||
if (expected == null)
|
||||
{
|
||||
Assert.Null(actual);
|
||||
return;
|
||||
}
|
||||
|
||||
if (actual == null)
|
||||
{
|
||||
throw new Exception("Actual object is null but expected is not");
|
||||
}
|
||||
|
||||
foreach (var expectedPropInfo in expected.GetType().GetProperties().Where(pi => !relevantExcludedProperties.Contains(pi.Name)))
|
||||
{
|
||||
var actualPropInfo = actual.GetType().GetProperty(expectedPropInfo.Name);
|
||||
|
||||
if (actualPropInfo == null)
|
||||
{
|
||||
throw new Exception(string.Concat($"Expected actual object to contain a property named {expectedPropInfo.Name}, but it does not\n",
|
||||
$"Expected:\n{JsonSerializer.Serialize(expected, JsonHelpers.Indented)}\n",
|
||||
$"Actual:\n{JsonSerializer.Serialize(actual, JsonHelpers.Indented)}"));
|
||||
}
|
||||
|
||||
if (expectedPropInfo.PropertyType == typeof(string) || expectedPropInfo.PropertyType.IsValueType)
|
||||
{
|
||||
Assert.Equal(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual));
|
||||
}
|
||||
else if (expectedPropInfo.PropertyType == typeof(JsonDocument) && actualPropInfo.PropertyType == typeof(JsonDocument))
|
||||
{
|
||||
static string JsonDocString(PropertyInfo info, object obj) => JsonSerializer.Serialize(info.GetValue(obj));
|
||||
Assert.Equal(JsonDocString(expectedPropInfo, expected), JsonDocString(actualPropInfo, actual));
|
||||
}
|
||||
else
|
||||
{
|
||||
var prefix = $"{expectedPropInfo.PropertyType.Name}.";
|
||||
var nextExcludedProperties = excludedPropertyStrings.Where(name => name.StartsWith(prefix))
|
||||
.Select(name => name[prefix.Length..]).ToArray();
|
||||
AssertPropertyEqual(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual), nextExcludedProperties);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (actual == null)
|
||||
private static Predicate<T> AssertPropertyEqualPredicate<T>(T expected, params string[] excludedPropertyStrings) => (actual) =>
|
||||
{
|
||||
throw new Exception("Actual object is null but expected is not");
|
||||
AssertPropertyEqual(expected, actual, excludedPropertyStrings);
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<T>> AssertPropertyEqual<T>(T expected, params string[] excludedPropertyStrings) =>
|
||||
(T actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
|
||||
|
||||
private static Predicate<IEnumerable<T>> AssertPropertyEqualPredicate<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) => (actual) =>
|
||||
{
|
||||
// IEnumerable.Zip doesn't account for different lengths, we need to check this ourselves
|
||||
if (actual.Count() != expected.Count())
|
||||
{
|
||||
throw new Exception(string.Concat($"Actual IEnumerable does not have the expected length.\n",
|
||||
$"Expected: {expected.Count()}\n",
|
||||
$"Actual: {actual.Count()}"));
|
||||
}
|
||||
|
||||
var elements = expected.Zip(actual);
|
||||
foreach (var (expectedEl, actualEl) in elements)
|
||||
{
|
||||
AssertPropertyEqual(expectedEl, actualEl, excludedPropertyStrings);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<IEnumerable<T>>> AssertPropertyEqual<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) =>
|
||||
(actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
|
||||
|
||||
private static Predicate<T> AssertEqualExpectedPredicate<T>(T expected) => (actual) =>
|
||||
{
|
||||
Assert.Equal(expected, actual);
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<T>> AssertEqualExpected<T>(T expected) =>
|
||||
(T actual) => AssertEqualExpectedPredicate(expected)(actual);
|
||||
|
||||
public static JsonElement AssertJsonProperty(JsonElement element, string propertyName, JsonValueKind jsonValueKind)
|
||||
{
|
||||
if (!element.TryGetProperty(propertyName, out var subElement))
|
||||
{
|
||||
throw new XunitException($"Could not find property by name '{propertyName}'");
|
||||
}
|
||||
|
||||
Assert.Equal(jsonValueKind, subElement.ValueKind);
|
||||
return subElement;
|
||||
}
|
||||
|
||||
foreach (var expectedPropInfo in expected.GetType().GetProperties().Where(pi => !relevantExcludedProperties.Contains(pi.Name)))
|
||||
public static void AssertEqualJson(JsonElement a, JsonElement b)
|
||||
{
|
||||
var actualPropInfo = actual.GetType().GetProperty(expectedPropInfo.Name);
|
||||
|
||||
if (actualPropInfo == null)
|
||||
switch (a.ValueKind)
|
||||
{
|
||||
throw new Exception(string.Concat($"Expected actual object to contain a property named {expectedPropInfo.Name}, but it does not\n",
|
||||
$"Expected:\n{JsonSerializer.Serialize(expected, JsonHelpers.Indented)}\n",
|
||||
$"Actual:\n{JsonSerializer.Serialize(actual, JsonHelpers.Indented)}"));
|
||||
}
|
||||
|
||||
if (expectedPropInfo.PropertyType == typeof(string) || expectedPropInfo.PropertyType.IsValueType)
|
||||
{
|
||||
Assert.Equal(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual));
|
||||
}
|
||||
else if (expectedPropInfo.PropertyType == typeof(JsonDocument) && actualPropInfo.PropertyType == typeof(JsonDocument))
|
||||
{
|
||||
static string JsonDocString(PropertyInfo info, object obj) => JsonSerializer.Serialize(info.GetValue(obj));
|
||||
Assert.Equal(JsonDocString(expectedPropInfo, expected), JsonDocString(actualPropInfo, actual));
|
||||
}
|
||||
else
|
||||
{
|
||||
var prefix = $"{expectedPropInfo.PropertyType.Name}.";
|
||||
var nextExcludedProperties = excludedPropertyStrings.Where(name => name.StartsWith(prefix))
|
||||
.Select(name => name[prefix.Length..]).ToArray();
|
||||
AssertPropertyEqual(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual), nextExcludedProperties);
|
||||
case JsonValueKind.Array:
|
||||
Assert.Equal(JsonValueKind.Array, b.ValueKind);
|
||||
AssertEqualJsonArray(a, b);
|
||||
break;
|
||||
case JsonValueKind.Object:
|
||||
Assert.Equal(JsonValueKind.Object, b.ValueKind);
|
||||
AssertEqualJsonObject(a, b);
|
||||
break;
|
||||
case JsonValueKind.False:
|
||||
Assert.Equal(JsonValueKind.False, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.True:
|
||||
Assert.Equal(JsonValueKind.True, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.Number:
|
||||
Assert.Equal(JsonValueKind.Number, b.ValueKind);
|
||||
Assert.Equal(a.GetDouble(), b.GetDouble());
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
Assert.Equal(JsonValueKind.String, b.ValueKind);
|
||||
Assert.Equal(a.GetString(), b.GetString());
|
||||
break;
|
||||
case JsonValueKind.Null:
|
||||
Assert.Equal(JsonValueKind.Null, b.ValueKind);
|
||||
break;
|
||||
default:
|
||||
throw new XunitException($"Bad JsonValueKind '{a.ValueKind}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Predicate<T> AssertPropertyEqualPredicate<T>(T expected, params string[] excludedPropertyStrings) => (actual) =>
|
||||
{
|
||||
AssertPropertyEqual(expected, actual, excludedPropertyStrings);
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<T>> AssertPropertyEqual<T>(T expected, params string[] excludedPropertyStrings) =>
|
||||
(T actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
|
||||
|
||||
private static Predicate<IEnumerable<T>> AssertPropertyEqualPredicate<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) => (actual) =>
|
||||
{
|
||||
// IEnumerable.Zip doesn't account for different lengths, we need to check this ourselves
|
||||
if (actual.Count() != expected.Count())
|
||||
private static void AssertEqualJsonObject(JsonElement a, JsonElement b)
|
||||
{
|
||||
throw new Exception(string.Concat($"Actual IEnumerable does not have the expected length.\n",
|
||||
$"Expected: {expected.Count()}\n",
|
||||
$"Actual: {actual.Count()}"));
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Object && b.ValueKind == JsonValueKind.Object);
|
||||
|
||||
var aObjectEnumerator = a.EnumerateObject();
|
||||
var bObjectEnumerator = b.EnumerateObject();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aObjectEnumerator.MoveNext();
|
||||
var bCanMove = bObjectEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over object '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over object '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aProp = aObjectEnumerator.Current;
|
||||
var bProp = bObjectEnumerator.Current;
|
||||
|
||||
Assert.Equal(aProp.Name, bProp.Name);
|
||||
// Recursion!
|
||||
AssertEqualJson(aProp.Value, bProp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
var elements = expected.Zip(actual);
|
||||
foreach (var (expectedEl, actualEl) in elements)
|
||||
private static void AssertEqualJsonArray(JsonElement a, JsonElement b)
|
||||
{
|
||||
AssertPropertyEqual(expectedEl, actualEl, excludedPropertyStrings);
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Array && b.ValueKind == JsonValueKind.Array);
|
||||
|
||||
var aArrayEnumerator = a.EnumerateArray();
|
||||
var bArrayEnumerator = b.EnumerateArray();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aArrayEnumerator.MoveNext();
|
||||
var bCanMove = bArrayEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over array '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over array '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aElement = aArrayEnumerator.Current;
|
||||
var bElement = bArrayEnumerator.Current;
|
||||
|
||||
// Recursion!
|
||||
AssertEqualJson(aElement, bElement);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<IEnumerable<T>>> AssertPropertyEqual<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) =>
|
||||
(actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
|
||||
|
||||
private static Predicate<T> AssertEqualExpectedPredicate<T>(T expected) => (actual) =>
|
||||
{
|
||||
Assert.Equal(expected, actual);
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Expression<Predicate<T>> AssertEqualExpected<T>(T expected) =>
|
||||
(T actual) => AssertEqualExpectedPredicate(expected)(actual);
|
||||
|
||||
public static JsonElement AssertJsonProperty(JsonElement element, string propertyName, JsonValueKind jsonValueKind)
|
||||
{
|
||||
if (!element.TryGetProperty(propertyName, out var subElement))
|
||||
public async static Task<T> AssertResponseTypeIs<T>(HttpContext context)
|
||||
{
|
||||
throw new XunitException($"Could not find property by name '{propertyName}'");
|
||||
return await JsonSerializer.DeserializeAsync<T>(context.Response.Body);
|
||||
}
|
||||
|
||||
Assert.Equal(jsonValueKind, subElement.ValueKind);
|
||||
return subElement;
|
||||
}
|
||||
public static TimeSpan AssertRecent(DateTime dateTime, int skewSeconds = 2)
|
||||
=> AssertRecent(dateTime, TimeSpan.FromSeconds(skewSeconds));
|
||||
|
||||
public static void AssertEqualJson(JsonElement a, JsonElement b)
|
||||
{
|
||||
switch (a.ValueKind)
|
||||
public static TimeSpan AssertRecent(DateTime dateTime, TimeSpan skew)
|
||||
{
|
||||
case JsonValueKind.Array:
|
||||
Assert.Equal(JsonValueKind.Array, b.ValueKind);
|
||||
AssertEqualJsonArray(a, b);
|
||||
break;
|
||||
case JsonValueKind.Object:
|
||||
Assert.Equal(JsonValueKind.Object, b.ValueKind);
|
||||
AssertEqualJsonObject(a, b);
|
||||
break;
|
||||
case JsonValueKind.False:
|
||||
Assert.Equal(JsonValueKind.False, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.True:
|
||||
Assert.Equal(JsonValueKind.True, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.Number:
|
||||
Assert.Equal(JsonValueKind.Number, b.ValueKind);
|
||||
Assert.Equal(a.GetDouble(), b.GetDouble());
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
Assert.Equal(JsonValueKind.String, b.ValueKind);
|
||||
Assert.Equal(a.GetString(), b.GetString());
|
||||
break;
|
||||
case JsonValueKind.Null:
|
||||
Assert.Equal(JsonValueKind.Null, b.ValueKind);
|
||||
break;
|
||||
default:
|
||||
throw new XunitException($"Bad JsonValueKind '{a.ValueKind}'");
|
||||
var difference = DateTime.UtcNow - dateTime;
|
||||
Assert.True(difference < skew);
|
||||
return difference;
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertEqualJsonObject(JsonElement a, JsonElement b)
|
||||
{
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Object && b.ValueKind == JsonValueKind.Object);
|
||||
|
||||
var aObjectEnumerator = a.EnumerateObject();
|
||||
var bObjectEnumerator = b.EnumerateObject();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aObjectEnumerator.MoveNext();
|
||||
var bCanMove = bObjectEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over object '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over object '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aProp = aObjectEnumerator.Current;
|
||||
var bProp = bObjectEnumerator.Current;
|
||||
|
||||
Assert.Equal(aProp.Name, bProp.Name);
|
||||
// Recursion!
|
||||
AssertEqualJson(aProp.Value, bProp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertEqualJsonArray(JsonElement a, JsonElement b)
|
||||
{
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Array && b.ValueKind == JsonValueKind.Array);
|
||||
|
||||
var aArrayEnumerator = a.EnumerateArray();
|
||||
var bArrayEnumerator = b.EnumerateArray();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aArrayEnumerator.MoveNext();
|
||||
var bCanMove = bArrayEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over array '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over array '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aElement = aArrayEnumerator.Current;
|
||||
var bElement = bArrayEnumerator.Current;
|
||||
|
||||
// Recursion!
|
||||
AssertEqualJson(aElement, bElement);
|
||||
}
|
||||
}
|
||||
|
||||
public async static Task<T> AssertResponseTypeIs<T>(HttpContext context)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<T>(context.Response.Body);
|
||||
}
|
||||
|
||||
public static TimeSpan AssertRecent(DateTime dateTime, int skewSeconds = 2)
|
||||
=> AssertRecent(dateTime, TimeSpan.FromSeconds(skewSeconds));
|
||||
|
||||
public static TimeSpan AssertRecent(DateTime dateTime, TimeSpan skew)
|
||||
{
|
||||
var difference = DateTime.UtcNow - dateTime;
|
||||
Assert.True(difference < skew);
|
||||
return difference;
|
||||
}
|
||||
}
|
||||
|
@ -4,48 +4,49 @@ using AutoFixture.Kernel;
|
||||
using AutoFixture.Xunit2;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
|
||||
namespace Bit.Test.Common.Helpers;
|
||||
|
||||
public static class BitAutoDataAttributeHelpers
|
||||
namespace Bit.Test.Common.Helpers
|
||||
{
|
||||
public static IEnumerable<object[]> GetData(MethodInfo testMethod, IFixture fixture, object[] fixedTestParameters)
|
||||
public static class BitAutoDataAttributeHelpers
|
||||
{
|
||||
var methodParameters = testMethod.GetParameters();
|
||||
var classCustomizations = testMethod.DeclaringType.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
var methodCustomizations = testMethod.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
|
||||
fixedTestParameters = fixedTestParameters ?? Array.Empty<object>();
|
||||
|
||||
fixture = ApplyCustomizations(ApplyCustomizations(fixture, classCustomizations), methodCustomizations);
|
||||
var missingParameters = methodParameters.Skip(fixedTestParameters.Length).Select(p => CustomizeAndCreate(p, fixture));
|
||||
|
||||
return new object[1][] { fixedTestParameters.Concat(missingParameters).ToArray() };
|
||||
}
|
||||
|
||||
public static object CustomizeAndCreate(ParameterInfo p, IFixture fixture)
|
||||
{
|
||||
var customizations = p.GetCustomAttributes(typeof(CustomizeAttribute), false)
|
||||
.OfType<CustomizeAttribute>()
|
||||
.Select(attr => attr.GetCustomization(p));
|
||||
|
||||
var context = new SpecimenContext(ApplyCustomizations(fixture, customizations));
|
||||
return context.Resolve(p);
|
||||
}
|
||||
|
||||
public static IFixture ApplyCustomizations(IFixture fixture, IEnumerable<ICustomization> customizations)
|
||||
{
|
||||
var newFixture = new Fixture();
|
||||
|
||||
foreach (var customization in fixture.Customizations.Reverse().Select(b => b.ToCustomization()))
|
||||
public static IEnumerable<object[]> GetData(MethodInfo testMethod, IFixture fixture, object[] fixedTestParameters)
|
||||
{
|
||||
newFixture.Customize(customization);
|
||||
var methodParameters = testMethod.GetParameters();
|
||||
var classCustomizations = testMethod.DeclaringType.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
var methodCustomizations = testMethod.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
|
||||
fixedTestParameters = fixedTestParameters ?? Array.Empty<object>();
|
||||
|
||||
fixture = ApplyCustomizations(ApplyCustomizations(fixture, classCustomizations), methodCustomizations);
|
||||
var missingParameters = methodParameters.Skip(fixedTestParameters.Length).Select(p => CustomizeAndCreate(p, fixture));
|
||||
|
||||
return new object[1][] { fixedTestParameters.Concat(missingParameters).ToArray() };
|
||||
}
|
||||
|
||||
foreach (var customization in customizations)
|
||||
public static object CustomizeAndCreate(ParameterInfo p, IFixture fixture)
|
||||
{
|
||||
newFixture.Customize(customization);
|
||||
var customizations = p.GetCustomAttributes(typeof(CustomizeAttribute), false)
|
||||
.OfType<CustomizeAttribute>()
|
||||
.Select(attr => attr.GetCustomization(p));
|
||||
|
||||
var context = new SpecimenContext(ApplyCustomizations(fixture, customizations));
|
||||
return context.Resolve(p);
|
||||
}
|
||||
|
||||
return newFixture;
|
||||
public static IFixture ApplyCustomizations(IFixture fixture, IEnumerable<ICustomization> customizations)
|
||||
{
|
||||
var newFixture = new Fixture();
|
||||
|
||||
foreach (var customization in fixture.Customizations.Reverse().Select(b => b.ToCustomization()))
|
||||
{
|
||||
newFixture.Customize(customization);
|
||||
}
|
||||
|
||||
foreach (var customization in customizations)
|
||||
{
|
||||
newFixture.Customize(customization);
|
||||
}
|
||||
|
||||
return newFixture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,44 +1,45 @@
|
||||
namespace Bit.Test.Common.Helpers;
|
||||
|
||||
public static class TestCaseHelper
|
||||
namespace Bit.Test.Common.Helpers
|
||||
{
|
||||
public static IEnumerable<IEnumerable<T>> GetCombinations<T>(params T[] items)
|
||||
public static class TestCaseHelper
|
||||
{
|
||||
var count = Math.Pow(2, items.Length);
|
||||
for (var i = 0; i < count; i++)
|
||||
public static IEnumerable<IEnumerable<T>> GetCombinations<T>(params T[] items)
|
||||
{
|
||||
var str = Convert.ToString(i, 2).PadLeft(items.Length, '0');
|
||||
List<T> combination = new();
|
||||
for (var j = 0; j < str.Length; j++)
|
||||
var count = Math.Pow(2, items.Length);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
if (str[j] == '1')
|
||||
var str = Convert.ToString(i, 2).PadLeft(items.Length, '0');
|
||||
List<T> combination = new();
|
||||
for (var j = 0; j < str.Length; j++)
|
||||
{
|
||||
combination.Add(items[j]);
|
||||
if (str[j] == '1')
|
||||
{
|
||||
combination.Add(items[j]);
|
||||
}
|
||||
}
|
||||
yield return combination;
|
||||
}
|
||||
yield return combination;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<IEnumerable<object>> GetCombinationsOfMultipleLists(params IEnumerable<object>[] optionLists)
|
||||
{
|
||||
if (!optionLists.Any())
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var item in optionLists.First())
|
||||
public static IEnumerable<IEnumerable<object>> GetCombinationsOfMultipleLists(params IEnumerable<object>[] optionLists)
|
||||
{
|
||||
var itemArray = new[] { item };
|
||||
|
||||
if (optionLists.Length == 1)
|
||||
if (!optionLists.Any())
|
||||
{
|
||||
yield return itemArray;
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var nextCombination in GetCombinationsOfMultipleLists(optionLists.Skip(1).ToArray()))
|
||||
foreach (var item in optionLists.First())
|
||||
{
|
||||
yield return itemArray.Concat(nextCombination);
|
||||
var itemArray = new[] { item };
|
||||
|
||||
if (optionLists.Length == 1)
|
||||
{
|
||||
yield return itemArray;
|
||||
}
|
||||
|
||||
foreach (var nextCombination in GetCombinationsOfMultipleLists(optionLists.Skip(1).ToArray()))
|
||||
{
|
||||
yield return itemArray.Concat(nextCombination);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user