-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoArgumentsAttributeTests.cs
More file actions
168 lines (143 loc) · 5.32 KB
/
AutoArgumentsAttributeTests.cs
File metadata and controls
168 lines (143 loc) · 5.32 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
using System.Reflection;
using AutoFixture.TUnit.Tests.TestTypes;
using TestTypeFoundation;
namespace AutoFixture.TUnit.Tests;
public class AutoArgumentsAttributeTests
{
[Test]
public async Task SutIsDataAttribute()
{
// Arrange & Act
var sut = new AutoArgumentsAttribute();
// Assert
await Assert.That(sut).IsAssignableTo<BaseDataSourceAttribute>();
}
[Test]
public async Task ValuesWillBeEmptyWhenSutIsCreatedWithDefaultConstructor()
{
// Arrange
var sut = new AutoArgumentsAttribute();
var expected = Enumerable.Empty<object>();
// Act
var result = sut.Values;
// Assert
await Assert.That(result).IsEquivalentTo(expected);
}
[Test]
public async Task ValuesWillNotBeEmptyWhenSutIsCreatedWithConstructorArguments()
{
// Arrange
var expectedValues = new[] { new object(), new object(), new object() };
var sut = new AutoArgumentsAttribute(expectedValues);
// Act
var result = sut.Values;
// Assert
await Assert.That(result).IsEquivalentTo(expectedValues);
}
[Test]
public async Task ValuesAreCorrectWhenConstructedWithExplicitAutoDataAttribute()
{
// Arrange
var expectedValues = new[] { new object(), new object(), new object() };
var sut = new DerivedAutoArgumentsAttribute(() => new DelegatingFixture(), expectedValues);
// Act
var result = sut.Values;
// Assert
await Assert.That(result).IsEqualTo(expectedValues);
}
[Test]
public async Task DoesntActivateFixtureImmediately()
{
// Arrange
var wasInvoked = false;
// Act
_ = new DerivedAutoArgumentsAttribute(() =>
{
wasInvoked = true;
return new DelegatingFixture();
});
// Assert
await Assert.That(wasInvoked).IsFalse();
}
[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 customizationLog = new List<ICustomization>();
var fixture = new DelegatingFixture
{
OnCustomize = c => customizationLog.Add(c)
};
var sut = new DerivedAutoArgumentsAttribute(() => fixture);
// Act
_ = sut.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(typeof(TypeWithCustomizationAttributes), methodName))
.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]
[MethodDataSource(typeof(InlinePrimitiveValuesTestData), nameof(InlinePrimitiveValuesTestData.GetData))]
[MethodDataSource(typeof(InlineFrozenValuesTestData), nameof(InlineFrozenValuesTestData.GetData))]
public async Task ReturnsSingleTestDataWithExpectedValues(BaseDataSourceAttribute attribute, MethodInfo testMethod,
object[] expected)
{
// Act
var actual = attribute.GetDataSources(DataGeneratorMetadataHelper.CreateDataGeneratorMetadata(testMethod.DeclaringType, testMethod.Name)).ToArray();
// Assert
await Assert.That(actual).HasSingleItem();
await Assert.That(actual[0]).IsEquivalentTo(expected);
}
[Test]
[AutoArguments]
public async Task GeneratesRandomData(int a, float b, string c, decimal d)
{
await Assert.That(a).IsNotEqualTo(0);
await Assert.That(b).IsNotEqualTo(0);
await Assert.That(c).IsNotNull();
await Assert.That(d).IsNotEqualTo(0);
}
[Test]
[AutoArguments(12, 32.1f, "hello", 71.231d)]
public async Task InlinesAllData(int a, float b, string c, decimal d)
{
await Assert.That(a).IsEqualTo(12);
await Assert.That(b).IsEqualTo(32.1f);
await Assert.That(c).IsEqualTo("hello");
await Assert.That(d).IsEqualTo(71.231m);
}
[Test]
[AutoArguments(0)]
[AutoArguments(5)]
[AutoArguments(-12)]
[AutoArguments(21.3f)]
[AutoArguments(18.7d)]
[AutoArguments(EnumType.First)]
[AutoArguments("Hello World")]
[AutoArguments("\t\r\n")]
[AutoArguments(" ")]
[AutoArguments("")]
[AutoArguments([null!])]
public async Task InjectsInlineValues([Frozen] object a,
[Frozen] PropertyHolder<object> value,
PropertyHolder<object> frozen)
{
await Assert.That(value.Property).IsEqualTo(a);
await Assert.That(value).IsSameReferenceAs(frozen);
}
}