// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Identity.Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using Xunit.Sdk;
///
/// Test priority
///
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class TestPriorityAttribute : Attribute
{
///
/// ctor
///
///
public TestPriorityAttribute(int priority)
{
Priority = priority;
}
///
/// Priority
///
public int Priority { get; private set; }
}
///
/// Used to run tests in order.
///
public class PriorityOrderer : ITestCaseOrderer
{
///
/// Orders tests cases
///
///
///
///
public IEnumerable OrderTestCases(IEnumerable testCases) where XunitTestCase : ITestCase
{
var sortedMethods = new SortedDictionary>();
foreach (XunitTestCase testCase in testCases)
{
int priority = 0;
foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute)).AssemblyQualifiedName))
priority = attr.GetNamedArgument("Priority");
GetOrCreate(sortedMethods, priority).Add(testCase);
}
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
{
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
foreach (XunitTestCase testCase in list)
yield return testCase;
}
}
static TValue GetOrCreate(IDictionary dictionary, TKey key) where TValue : new()
{
TValue result;
if (dictionary.TryGetValue(key, out result)) return result;
result = new TValue();
dictionary[key] = result;
return result;
}
}
}