-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGenericTest.cs
More file actions
118 lines (98 loc) · 3.79 KB
/
GenericTest.cs
File metadata and controls
118 lines (98 loc) · 3.79 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using UnitTestEx.Expectations;
namespace UnitTestEx.NUnit.Test.Other
{
[TestFixture]
public class GenericTest
{
[Test]
public void Run_Success()
{
using var test = GenericTester.Create();
test.Run(() => 1)
.AssertSuccess()
.AssertValue(1);
}
[Test]
public void Run_Success_AssertJSON()
{
using var test = GenericTester.Create();
test.Run(() => 1)
.AssertSuccess()
.AssertJson("1");
}
[Test]
public void Run_Exception()
{
static Func<Task> ThrowBadness() => () => throw new DivideByZeroException("Badness.");
using var test = GenericTester.Create();
test.ExpectException("Badness.")
.Run(ThrowBadness());
}
[Test]
public void Run_Service()
{
using var test = GenericTester.Create<EntryPoint>();
test.Run<Gin, int>(gin => gin.Pour())
.AssertSuccess()
.AssertValue(1);
#if NET9_0_OR_GREATER
// For .NET 9.0 and greater, we can use ValueTask directly.
test.Run<Gin, int>(gin => gin.PourAsync())
.AssertSuccess()
.AssertValue(1);
test.Run<Gin, int>(async gin => await gin.PourAsync())
.AssertSuccess()
.AssertValue(1);
#else
test.Run<Gin, int>(async gin => await gin.PourAsync())
.AssertSuccess()
.AssertValue(1);
#endif
test.Run<Gin>(gin => gin.Shake())
.AssertSuccess();
test.Run<Gin>(gin => gin.ShakeAsync())
.AssertSuccess();
test.Run<Gin>(async gin => await gin.ShakeAsync())
.AssertSuccess();
test.Run<Gin>(gin => gin.Stir())
.AssertException<DivideByZeroException>("As required by Bond; shaken, not stirred.");
test.Run<Gin>(gin => gin.StirAsync())
.AssertException<DivideByZeroException>("As required by Bond; shaken, not stirred.");
}
[Test]
public void Configuration_Overrride_Use()
{
// Demonstrates how to override the configuration settings for a test.
using var test = GenericTester.Create();
test.UseAdditionalConfiguration([new("SpecialKey", "NotSoSpecial")]);
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Just for testing aye bro!")]
public class Gin
{
public void Stir() => throw new DivideByZeroException("As required by Bond; shaken, not stirred.");
public Task StirAsync() => throw new DivideByZeroException("As required by Bond; shaken, not stirred.");
public void Shake() { }
public Task ShakeAsync() => Task.CompletedTask;
public int Pour() => 1;
public ValueTask<int> PourAsync() => ValueTask.FromResult(1);
}
public class EntryPoint
{
//public void ConfigureAppConfiguration(HostBuilderContext context, IConfigurationBuilder config) { }
//public void ConfigureHostConfiguration(IConfigurationBuilder config) { }
//public void ConfigureServices(IServiceCollection services) { }
public void ConfigureApplication(IHostApplicationBuilder builder)
{
builder.Services.AddSingleton<Gin>();
}
}
}