MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.Linq;7using PSTests.Internal;8using Xunit.Abstractions;9using Xunit.Sdk;10 11namespace TestOrder.TestCaseOrdering12{13 public class PriorityOrderer : ITestCaseOrderer14 {15 public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase16 {17 var sortedMethods = new SortedDictionary<int, List<TTestCase>>();18 19 foreach (TTestCase testCase in testCases)20 {21 int priority = 0;22 23 foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes(typeof(PriorityAttribute).AssemblyQualifiedName))24 {25 priority = attr.GetNamedArgument<int>("Priority");26 }27 28 GetOrCreate(sortedMethods, priority).Add(testCase);29 }30 31 foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))32 {33 list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));34 foreach (TTestCase testCase in list)35 {36 yield return testCase;37 }38 }39 }40 41 private static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()42 {43 TValue result;44 45 if (dictionary.TryGetValue(key, out result))46 {47 return result;48 }49 50 result = new TValue();51 dictionary[key] = result;52 53 return result;54 }55 }56}57 