-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathTestCaseExtensions.cs
More file actions
73 lines (64 loc) · 2.55 KB
/
TestCaseExtensions.cs
File metadata and controls
73 lines (64 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Xunit.Sdk;
#nullable enable
namespace Microsoft.DotNet.XHarness.TestRunners.Xunit;
/// <summary>
/// Useful extensions that make working with the ITestCaseDiscovered interface nicer within the runner.
/// </summary>
public static class TestCaseExtensions
{
/// <summary>
/// Returns boolean indicating whether the test case does have traits.
/// </summary>
/// <param name="testCase">The test case under test.</param>
/// <returns>true if the test case has traits, false otherwise.</returns>
public static bool HasTraits(this ITestCaseDiscovered testCase) =>
testCase.Traits != null && testCase.Traits.Count > 0;
public static bool TryGetTrait(this ITestCaseDiscovered testCase,
string trait,
[NotNullWhen(true)] out List<string>? values,
StringComparison comparer = StringComparison.InvariantCultureIgnoreCase)
{
if (trait == null)
{
values = null;
return false;
}
// there is no guarantee that the dict created by xunit is case insensitive, therefore, trygetvalue might
// not return the value we are interested in. We have to loop, which is not ideal, but will be better
// for our use case.
foreach (var t in testCase.Traits.Keys)
{
if (trait.Equals(t, comparer))
{
values = testCase.Traits[t].ToList();
return true;
}
}
values = null;
return false;
}
/// <summary>
/// Get the name of the test class that owns the test case.
/// </summary>
/// <param name="testCase">TestCase whose class we want to retrieve.</param>
/// <returns>The name of the class that owns the test.</returns>
public static string? GetTestClass(this ITestCaseDiscovered testCase) =>
testCase.TestClassName?.Trim();
public static string? GetNamespace(this ITestCaseDiscovered testCase)
{
var testClassName = testCase.GetTestClass();
if (testClassName == null)
{
return null;
}
int dot = testClassName.LastIndexOf('.');
return dot <= 0 ? null : testClassName.Substring(0, dot);
}
}