-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoDataSourceAttributeTests.cs
More file actions
178 lines (150 loc) · 5.68 KB
/
AutoDataSourceAttributeTests.cs
File metadata and controls
178 lines (150 loc) · 5.68 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using AutoFixture.Kernel;
using AutoFixture.TUnit.Tests.TestTypes;
using TestTypeFoundation;
using ConcreteType = TestTypeFoundation.ConcreteType;
namespace AutoFixture.TUnit.Tests;
public class AutoDataSourceAttributeTests
{
[Test]
public async Task SutIsDataAttribute()
{
// Arrange & Act
var sut = new AutoDataSourceAttribute();
// Assert
await Assert.That(sut).IsAssignableTo<BaseDataSourceAttribute>();
}
[Test]
public async Task InitializedWithDefaultConstructorHasCorrectFixture()
{
// Arrange
var sut = new AutoDataSourceAttribute();
// Act
var result = sut.FixtureFactory();
// Assert
await Assert.That(result).IsAssignableTo<Fixture>();
}
[Test]
public async Task InitializedWithFixtureFactoryConstructorHasCorrectFixture()
{
// Arrange
var fixture = new Fixture();
// Act
var sut = new DerivedAutoDataSourceAttribute(() => fixture);
// Assert
await Assert.That(sut.FixtureFactory()).IsSameReferenceAs(fixture);
}
[Test]
public async Task InitializeWithNullFixtureFactoryThrows()
{
// Arrange
// Act & Assert
await Assert.That(() =>
new DerivedAutoDataSourceAttribute(null!)).ThrowsExactly<ArgumentNullException>();
}
[Test]
public async Task DoesntActivateFixtureImmediately()
{
// Arrange
var wasInvoked = false;
// Act
_ = new DerivedAutoDataSourceAttribute(() =>
{
wasInvoked = true;
return null!;
});
// Assert
await Assert.That(wasInvoked).IsFalse();
}
[Test]
public async Task GetDataWithNullMethodThrows()
{
// Arrange
var sut = new AutoDataSourceAttribute();
// Act & assert
await Assert.That(() => sut.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(null!, null!))).ThrowsException();
}
[Test]
public async Task GetDataReturnsCorrectResult()
{
// Arrange
var method = typeof(TypeWithOverloadedMembers)
.GetMethod("DoSomething", [typeof(object)]);
var parameters = method!.GetParameters();
var expectedResult = new object();
object actualParameter = null!;
ISpecimenContext actualContext = null!;
var builder = new DelegatingSpecimenBuilder
{
OnCreate = (r, c) =>
{
actualParameter = r;
actualContext = c;
return expectedResult;
}
};
var composer = new DelegatingFixture { OnCreate = builder.OnCreate };
var sut = new DerivedAutoDataSourceAttribute(() => composer);
// Act
var result = sut.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(method))
.Select(x => x())
.ToArray();
// Assert
await Assert.That(actualContext).IsNotNull();
await Assert.That(parameters).HasSingleItem();
await Assert.That(actualParameter).IsEqualTo(parameters[0]);
await Assert.That(result.Single()).IsEquivalentTo(new[] { expectedResult });
}
[Test]
[Arguments("CreateWithFrozenAndFavorArrays")]
[Arguments("CreateWithFavorArraysAndFrozen")]
[Arguments("CreateWithFrozenAndFavorEnumerables")]
[Arguments("CreateWithFavorEnumerablesAndFrozen")]
[Arguments("CreateWithFrozenAndFavorLists")]
[Arguments("CreateWithFavorListsAndFrozen")]
[Arguments("CreateWithFrozenAndGreedy")]
[Arguments("CreateWithGreedyAndFrozen")]
[Arguments("CreateWithFrozenAndModest")]
[Arguments("CreateWithModestAndFrozen")]
[Arguments("CreateWithFrozenAndNoAutoProperties")]
[Arguments("CreateWithNoAutoPropertiesAndFrozen")]
public async Task GetDataOrdersCustomizationAttributes(string methodName)
{
// Arrange
var method = typeof(TypeWithCustomizationAttributes)
.GetMethod(methodName, [typeof(ConcreteType)]);
var customizationLog = new List<ICustomization>();
var fixture = new DelegatingFixture
{
OnCustomize = c => customizationLog.Add(c)
};
var sut = new DerivedAutoDataSourceAttribute(() => fixture);
// Act
_ = sut.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(method.DeclaringType, method.Name))
.Select(x => x())
.ToArray();
// Assert
await Assert.That(customizationLog[0]).IsAssignableTo<CompositeCustomization>();
var composite = (CompositeCustomization)customizationLog[0];
await Assert.That(composite.Customizations.First()).IsNotTypeOf<FreezeOnMatchCustomization>();
await Assert.That(composite.Customizations.Last()).IsAssignableTo<FreezeOnMatchCustomization>();
}
[Test]
public async Task ShouldRecognizeAttributesImplementingIParameterCustomizationSource()
{
// Arrange
var method = typeof(TypeWithIParameterCustomizationSourceUsage)
.GetMethod(nameof(TypeWithIParameterCustomizationSourceUsage.DecoratedMethod));
var customizationLog = new List<ICustomization>();
var fixture = new DelegatingFixture
{
OnCustomize = c => customizationLog.Add(c)
};
var sut = new DerivedAutoDataSourceAttribute(() => fixture);
// Act
_ = sut.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(method.DeclaringType, method.Name))
.Select(x => x())
.ToArray();
// Assert
await Assert.That(customizationLog[0]).IsAssignableTo<TypeWithIParameterCustomizationSourceUsage.Customization>();
}
}