forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureCollectionTests.cs
106 lines (82 loc) · 2.76 KB
/
FeatureCollectionTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Xunit;
namespace Microsoft.AspNetCore.Http.Features;
public class FeatureCollectionTests
{
[Fact]
public void AddedInterfaceIsReturned()
{
var interfaces = new FeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
var thing2 = interfaces[typeof(IThing)];
Assert.Equal(thing2, thing);
}
[Fact]
public void IndexerAlsoAddsItems()
{
var interfaces = new FeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
Assert.Equal(interfaces[typeof(IThing)], thing);
}
[Fact]
public void SetNullValueRemoves()
{
var interfaces = new FeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
Assert.Equal(interfaces[typeof(IThing)], thing);
interfaces[typeof(IThing)] = null;
var thing2 = interfaces[typeof(IThing)];
Assert.Null(thing2);
}
[Fact]
public void GetMissingStructFeatureThrows()
{
var interfaces = new FeatureCollection();
// Regression test: Used to throw NullReferenceException because it tried to unbox a null object to a struct
var ex = Assert.Throws<InvalidOperationException>(() => interfaces.Get<int>());
Assert.Equal("System.Int32 does not exist in the feature collection and because it is a struct the method can't return null. Use 'featureCollection[typeof(System.Int32)] is not null' to check if the feature exists.", ex.Message);
}
[Fact]
public void GetMissingFeatureReturnsNull()
{
var interfaces = new FeatureCollection();
Assert.Null(interfaces.Get<Thing>());
}
[Fact]
public void GetStructFeature()
{
var interfaces = new FeatureCollection();
var value = 20;
interfaces.Set(value);
Assert.Equal(value, interfaces.Get<int>());
}
[Fact]
public void GetNullableStructFeatureWhenSetWithNonNullableStruct()
{
var interfaces = new FeatureCollection();
var value = 20;
interfaces.Set(value);
Assert.Null(interfaces.Get<int?>());
}
[Fact]
public void GetNullableStructFeatureWhenSetWithNullableStruct()
{
var interfaces = new FeatureCollection();
var value = 20;
interfaces.Set<int?>(value);
Assert.Equal(value, interfaces.Get<int?>());
}
[Fact]
public void GetFeature()
{
var interfaces = new FeatureCollection();
var thing = new Thing();
interfaces.Set(thing);
Assert.Equal(thing, interfaces.Get<Thing>());
}
}