CollectionOrderer.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #if !(NET20 || NET40)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. namespace Padi.RedmineApi.Tests.Infrastructure
  9. {
  10. /// <summary>
  11. /// Custom xUnit test collection orderer that uses the OrderAttribute
  12. /// </summary>
  13. public class CollectionOrderer : ITestCollectionOrderer
  14. {
  15. public const string TYPE_NAME = "redmine.net.api.Tests.Infrastructure.CollectionOrderer";
  16. public const string ASSEMBY_NAME = "redmine-net-api.Tests";
  17. public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections)
  18. {
  19. return testCollections.OrderBy(GetOrder);
  20. }
  21. /// <summary>
  22. /// Test collections are not bound to a specific class, however they
  23. /// are named by default with the type name as a suffix. We try to
  24. /// get the class name from the DisplayName and then use reflection to
  25. /// find the class and OrderAttribute.
  26. /// </summary>
  27. private static int GetOrder(ITestCollection testCollection)
  28. {
  29. var i = testCollection.DisplayName.LastIndexOf(' ');
  30. if (i <= -1) return 0;
  31. var className = testCollection.DisplayName.Substring(i + 1);
  32. var type = Type.GetType(className);
  33. if (type == null) return 0;
  34. var attr = type.GetCustomAttribute<OrderAttribute>();
  35. return attr != null ? attr.Index : 0;
  36. }
  37. }
  38. }
  39. #endif