-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleClassTests.cs
More file actions
126 lines (109 loc) · 3.87 KB
/
Copy pathExampleClassTests.cs
File metadata and controls
126 lines (109 loc) · 3.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using OpenFeature;
using OpenFeature.Providers.Memory;
namespace Example.Tests;
/// <summary>
/// Tests for the <see cref="ExampleClass"/> class.
/// </summary>
/// <remarks>
/// Joins the shared <c>"FeatureFlags"</c> xUnit collection.
/// <see cref="FeatureFlags.CreateClientAsync(FeatureProvider)"/> re-registers the
/// process-wide <c>Api.Instance</c> provider, and xUnit runs different test classes in
/// parallel by default — so give every flag-exercising test class the same
/// <c>[Collection("FeatureFlags")]</c> attribute and xUnit runs them sequentially,
/// keeping their provider registrations from interleaving.
/// </remarks>
[Collection("FeatureFlags")]
public class ExampleClassTests
{
/// <summary>
/// Verifies that <see cref="ExampleClass.Add(int, int)"/> returns the sum of its two operands.
/// </summary>
[Fact]
public void Add_TwoOperands_ReturnsTheirSum()
{
// Arrange
int augend = 2;
int addend = 3;
// Act
int sum = ExampleClass.Add(augend, addend);
// Assert
Assert.Equal(5, sum);
}
/// <summary>
/// Verifies that <see cref="ExampleClass.Add(int, int)"/> returns the correct sum when one operand is negative.
/// </summary>
[Fact]
public void Add_NegativeOperand_ReturnsTheirSum()
{
// Arrange
int augend = 10;
int addend = -4;
// Act
int sum = ExampleClass.Add(augend, addend);
// Assert
Assert.Equal(6, sum);
}
/// <summary>
/// Verifies <see cref="FeatureFlags.DescribeAsync(IFeatureClient)"/> takes the OFF
/// branch when the example flag is registered default-off (the scaffold default).
/// </summary>
[Fact]
public async Task DescribeAsync_FlagDefaultOff_ReturnsOffMessage()
{
// Arrange
var client = await FeatureFlags.CreateClientAsync(FeatureFlags.CreateInMemoryProvider());
// Act
string result = await FeatureFlags.DescribeAsync(client);
// Assert
Assert.Contains("is off", result, StringComparison.Ordinal);
}
/// <summary>
/// Verifies <see cref="FeatureFlags.DescribeAsync(IFeatureClient)"/> takes the ON
/// branch when the example flag resolves on.
/// </summary>
[Fact]
public async Task DescribeAsync_FlagOn_ReturnsOnMessage()
{
// Arrange
var client = await FeatureFlags.CreateClientAsync(BuildExampleProvider(enabled: true));
// Act
string result = await FeatureFlags.DescribeAsync(client);
// Assert
Assert.Contains("is on", result, StringComparison.Ordinal);
}
/// <summary>
/// Verifies an unset flag falls back to the OFF default, so a missing flag definition
/// never silently turns a feature on.
/// </summary>
[Fact]
public async Task DescribeAsync_FlagUnset_DefaultsToOff()
{
// Arrange
var provider = new InMemoryProvider(new Dictionary<string, Flag>(StringComparer.Ordinal));
var client = await FeatureFlags.CreateClientAsync(provider);
// Act
string result = await FeatureFlags.DescribeAsync(client);
// Assert
Assert.Contains("is off", result, StringComparison.Ordinal);
}
/// <summary>
/// Builds an in-memory provider that resolves <see cref="FeatureFlags.ExampleFeature"/>
/// to <paramref name="enabled"/>, so both flag states can be exercised.
/// </summary>
/// <param name="enabled">Whether the example flag should resolve on.</param>
/// <returns>An in-memory provider resolving the example flag to <paramref name="enabled"/>.</returns>
static InMemoryProvider BuildExampleProvider(bool enabled)
{
string variant = enabled ? FeatureFlags.EnabledVariant : FeatureFlags.DisabledVariant;
return new InMemoryProvider(new Dictionary<string, Flag>(StringComparer.Ordinal)
{
[FeatureFlags.ExampleFeature] = new Flag<bool>(
new Dictionary<string, bool>(StringComparer.Ordinal)
{
[FeatureFlags.EnabledVariant] = true,
[FeatureFlags.DisabledVariant] = false,
},
variant),
});
}
}