Skip to content

Commit efbc0e6

Browse files
committed
Put TestOrderExamples classes in namespace to clarify the need for FQDN
1 parent d574fa9 commit efbc0e6

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

TestOrderExamples/TestCaseOrdering/AlphabeticalOrderExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Xunit;
22

3-
[TestCaseOrderer("AlphabeticalOrderer", "TestOrderExamples")]
3+
[TestCaseOrderer("TestOrderExamples.TestCaseOrdering.AlphabeticalOrderer", "TestOrderExamples")]
44
public class AlphabeticalOrderExample
55
{
66
public static bool Test1Called;

TestOrderExamples/TestCaseOrdering/AlphabeticalOrderer.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
using Xunit.Abstractions;
55
using Xunit.Sdk;
66

7-
public class AlphabeticalOrderer : ITestCaseOrderer
7+
namespace TestOrderExamples.TestCaseOrdering
88
{
9-
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
10-
where TTestCase : ITestCase
9+
public class AlphabeticalOrderer : ITestCaseOrderer
1110
{
12-
var result = testCases.ToList();
13-
result.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
14-
return result;
11+
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
12+
where TTestCase : ITestCase
13+
{
14+
var result = testCases.ToList();
15+
result.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
16+
return result;
17+
}
1518
}
1619
}

TestOrderExamples/TestCaseOrdering/PriorityOrderExamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Xunit;
22

3-
[TestCaseOrderer("PriorityOrderer", "TestOrderExamples")]
4-
public class PriorityOrderExample
3+
[TestCaseOrderer("TestOrderExamples.TestCaseOrdering.PriorityOrderer", "TestOrderExamples")]
4+
public class PriorityOrderExamples
55
{
66
public static bool Test1Called;
77
public static bool Test2ACalled;

TestOrderExamples/TestCaseOrdering/PriorityOrderer.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,42 @@
44
using Xunit.Abstractions;
55
using Xunit.Sdk;
66

7-
public class PriorityOrderer : ITestCaseOrderer
7+
namespace TestOrderExamples.TestCaseOrdering
88
{
9-
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
9+
public class PriorityOrderer : ITestCaseOrderer
1010
{
11-
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
12-
13-
foreach (TTestCase testCase in testCases)
11+
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
1412
{
15-
int priority = 0;
13+
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
14+
15+
foreach (TTestCase testCase in testCases)
16+
{
17+
int priority = 0;
1618

17-
foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof (TestPriorityAttribute).AssemblyQualifiedName)))
18-
priority = attr.GetNamedArgument<int>("Priority");
19+
foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName)))
20+
priority = attr.GetNamedArgument<int>("Priority");
1921

20-
GetOrCreate(sortedMethods, priority).Add(testCase);
22+
GetOrCreate(sortedMethods, priority).Add(testCase);
23+
}
24+
25+
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
26+
{
27+
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
28+
foreach (TTestCase testCase in list)
29+
yield return testCase;
30+
}
2131
}
2232

23-
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
33+
static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
2434
{
25-
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
26-
foreach (TTestCase testCase in list)
27-
yield return testCase;
28-
}
29-
}
35+
TValue result;
3036

31-
static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
32-
{
33-
TValue result;
37+
if (dictionary.TryGetValue(key, out result)) return result;
3438

35-
if (dictionary.TryGetValue(key, out result)) return result;
36-
37-
result = new TValue();
38-
dictionary[key] = result;
39+
result = new TValue();
40+
dictionary[key] = result;
3941

40-
return result;
42+
return result;
43+
}
4144
}
42-
}
45+
}

TestOrderExamples/TestCollectionOrdering/DisplayNameOrderExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Xunit;
22

33
// Set the orderer
4-
[assembly: TestCollectionOrderer("DisplayNameOrderer", "TestOrderExamples")]
4+
[assembly: TestCollectionOrderer("TestOrderExamples.TestCollectionOrdering.DisplayNameOrderer", "TestOrderExamples")]
55

66
// Need to turn off test parallelization so we can validate the run order
77
[assembly: CollectionBehavior(DisableTestParallelization = true)]

TestOrderExamples/TestCollectionOrdering/DisplayNameOrderer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
using Xunit;
44
using Xunit.Abstractions;
55

6-
public class DisplayNameOrderer : ITestCollectionOrderer
6+
namespace TestOrderExamples.TestCollectionOrdering
77
{
8-
public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections)
8+
public class DisplayNameOrderer : ITestCollectionOrderer
99
{
10-
return testCollections.OrderBy(collection => collection.DisplayName);
10+
public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections)
11+
{
12+
return testCollections.OrderBy(collection => collection.DisplayName);
13+
}
1114
}
1215
}

0 commit comments

Comments
 (0)