|
1 | | -namespace Objectivity.AutoFixture.XUnit2.Core.Tests.Attributes |
2 | | -{ |
3 | | - using System; |
4 | | - using System.Diagnostics.CodeAnalysis; |
5 | | - using System.Linq; |
6 | | - using System.Reflection; |
7 | | - |
8 | | - using global::AutoFixture; |
9 | | - using global::AutoFixture.Kernel; |
10 | | - using global::AutoFixture.Xunit2; |
11 | | - using Objectivity.AutoFixture.XUnit2.Core.Attributes; |
12 | | - |
13 | | - using Xunit; |
14 | | - |
15 | | - [Collection("AutoDataAdapterAttribute")] |
16 | | - [Trait("Category", "DataAttribute")] |
17 | | - [SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "Test objects")] |
18 | | - public class AutoDataAdapterAttributeTests |
19 | | - { |
20 | | - [AutoData] |
21 | | - [Theory(DisplayName = "GIVEN fixture WHEN constructor is invoked THEN passed fixture is being adapted and inline values collection is empty")] |
22 | | - public void GivenFixture_WhenConstructorIsInvoked_ThenPassedFixtureIsBeingAdaptedAndInlineValuesCollectionIsEmpty(Fixture fixture) |
23 | | - { |
24 | | - // Arrange |
25 | | - // Act |
26 | | - var attribute = new AutoDataAdapterAttribute(fixture, null); |
27 | | - |
28 | | - // Assert |
29 | | - Assert.Equal(fixture, attribute.AdaptedFixture); |
30 | | - Assert.Empty(attribute.InlineValues); |
31 | | - } |
32 | | - |
33 | | - [Fact(DisplayName = "GIVEN uninitialized fixture WHEN constructor is invoked THEN exception is thrown")] |
34 | | - public void GivenUninitializedFixture_WhenConstructorIsInvoked_ThenExceptionIsThrown() |
35 | | - { |
36 | | - // Arrange |
37 | | - // Act |
38 | | - static object Act() => new AutoDataAdapterAttribute(null); |
39 | | - |
40 | | - // Assert |
41 | | - var exception = Assert.Throws<ArgumentNullException>(Act); |
42 | | - Assert.Equal("fixture", exception.ParamName); |
43 | | - } |
44 | | - |
45 | | - [AutoData] |
46 | | - [Theory(DisplayName = "GIVEN uninitialized method info WHEN GetData is invoked THEN exception is thrown")] |
47 | | - public void GivenUninitializedMethodInfo_WhenConstructorIsInvoked_ThenExceptionIsThrown(Fixture fixture) |
48 | | - { |
49 | | - // Arrange |
50 | | - var attribute = new AutoDataAdapterAttribute(fixture); |
51 | | - |
52 | | - // Act |
53 | | - object Act() => attribute.GetData(null); |
54 | | - |
55 | | - // Assert |
56 | | - var exception = Assert.Throws<ArgumentNullException>(Act); |
57 | | - Assert.Equal("testMethod", exception.ParamName); |
58 | | - } |
59 | | - |
60 | | - [Fact(DisplayName = "GIVEN test data with instance WHEN GetData called THEN auto data generation skipped")] |
61 | | - public void GivenTestDataWithInstance_WhenGetDataCalled_ThenAutoDataGenerationSkipped() |
62 | | - { |
63 | | - // Arrange |
64 | | - IFixture fixture = new Fixture(); |
65 | | - var attribute = new AutoDataAdapterAttribute(fixture, SpecificTestClass.Create()); |
66 | | - var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithAbstractTestClass), BindingFlags.Instance | BindingFlags.NonPublic); |
67 | | - var expectedNumberOfParameters = methodInfo.GetParameters().Length; |
68 | | - |
69 | | - // Act |
70 | | - var data = attribute.GetData(methodInfo).ToArray(); |
71 | | - |
72 | | - // Assert |
73 | | - Assert.Single(data); |
74 | | - Assert.Equal(expectedNumberOfParameters, data.First().Length); |
75 | | - Assert.All(data.First(), Assert.NotNull); |
76 | | - Assert.All(data.First().Skip(1), item => Assert.Equal(data.First().Last(), item)); |
77 | | - } |
78 | | - |
79 | | - [Fact(DisplayName = "GIVEN empty test data WHEN GetData called THEN auto data generation throws exception")] |
80 | | - public void GivenEmptyTestData_WhenGetDataCalled_ThenAutoDataGenerationSkipped() |
81 | | - { |
82 | | - // Arrange |
83 | | - IFixture fixture = new Fixture(); |
84 | | - var attribute = new AutoDataAdapterAttribute(fixture); |
85 | | - var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithAbstractTestClass), BindingFlags.Instance | BindingFlags.NonPublic); |
86 | | - |
87 | | - // Act |
88 | | - void Act() => attribute.GetData(methodInfo); |
89 | | - |
90 | | - // Assert |
91 | | - Assert.ThrowsAny<Exception>(Act); |
92 | | - } |
93 | | - |
94 | | - [Fact(DisplayName = "GIVEN test method with Frozen customization after others WHEN GetData called THEN ensure parameter is frozen on the end")] |
95 | | - public void GivenTestMethodWithFrozenCustomizationAfterOthers_WhenGetDataCalled_ThenEnsureParameterIsFrozenOnTheEnd() |
96 | | - { |
97 | | - // Arrange |
98 | | - IFixture fixture = new Fixture(); |
99 | | - var attribute = new AutoDataAdapterAttribute(fixture, null); |
100 | | - var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithFrozenCustomizationBeforeOthers), BindingFlags.Instance | BindingFlags.NonPublic); |
101 | | - var expectedNumberOfParameters = methodInfo.GetParameters().Length; |
102 | | - |
103 | | - // Act |
104 | | - var data = attribute.GetData(methodInfo).ToArray(); |
105 | | - |
106 | | - // Assert |
107 | | - Assert.Single(data); |
108 | | - Assert.Equal(expectedNumberOfParameters, data.First().Length); |
109 | | - Assert.All(data.First(), Assert.NotNull); |
110 | | - Assert.All( |
111 | | - data.First().Skip(1), |
112 | | - x => |
113 | | - { |
114 | | - var parameters = data.First(); |
115 | | - Assert.Equal(parameters.Last(), x); |
116 | | - Assert.NotEqual(parameters.First(), x); |
117 | | - Assert.Contains(StopStringCustomization.Value, x as string); |
118 | | - }); |
119 | | - } |
120 | | - |
121 | | - protected string TestMethodWithAbstractTestClass( |
122 | | - SpecificTestClass instance, |
123 | | - [Frozen] string text, |
124 | | - string message) |
125 | | - { |
126 | | - return $"{instance}: {text}, {message}"; |
127 | | - } |
128 | | - |
129 | | - protected string TestMethodWithFrozenCustomizationBeforeOthers( |
130 | | - string first, |
131 | | - [Frozen][StopString] string second, |
132 | | - string third) |
133 | | - { |
134 | | - return $"{first}: {second}, {third}"; |
135 | | - } |
136 | | - |
137 | | - [SuppressMessage("Minor Code Smell", "S2094:Classes should not be empty", Justification = "Test class")] |
138 | | - protected abstract class AbstractTestClass |
139 | | - { |
140 | | - } |
141 | | - |
142 | | - protected sealed class SpecificTestClass : AbstractTestClass |
143 | | - { |
144 | | - private SpecificTestClass() |
145 | | - { |
146 | | - } |
147 | | - |
148 | | - public static AbstractTestClass Create() |
149 | | - { |
150 | | - return new SpecificTestClass(); |
151 | | - } |
152 | | - } |
153 | | - |
154 | | - protected sealed class StopStringAttribute : CustomizeWithAttribute<StopStringCustomization> |
155 | | - { |
156 | | - } |
157 | | - |
158 | | - [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "This class is instantiated indirectly.")] |
159 | | - protected class StopStringCustomization : ICustomization |
160 | | - { |
161 | | - public const string Value = "STOP"; |
162 | | - |
163 | | - public void Customize(IFixture fixture) |
164 | | - { |
165 | | - fixture.Customizations.Add( |
166 | | - new FilteringSpecimenBuilder( |
167 | | - new FixedBuilder(Value), |
168 | | - new ExactTypeSpecification(Value.GetType()))); |
169 | | - } |
170 | | - } |
171 | | - } |
172 | | -} |
| 1 | +namespace Objectivity.AutoFixture.XUnit2.Core.Tests.Attributes |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Diagnostics.CodeAnalysis; |
| 5 | + using System.Linq; |
| 6 | + using System.Reflection; |
| 7 | + |
| 8 | + using global::AutoFixture; |
| 9 | + using global::AutoFixture.Kernel; |
| 10 | + using global::AutoFixture.Xunit2; |
| 11 | + using Objectivity.AutoFixture.XUnit2.Core.Attributes; |
| 12 | + |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + [Collection("AutoDataAdapterAttribute")] |
| 16 | + [Trait("Category", "DataAttribute")] |
| 17 | + [SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "Test objects")] |
| 18 | + public class AutoDataAdapterAttributeTests |
| 19 | + { |
| 20 | + [AutoData] |
| 21 | + [Theory(DisplayName = "GIVEN fixture WHEN constructor is invoked THEN passed fixture is being adapted and inline values collection is empty")] |
| 22 | + public void GivenFixture_WhenConstructorIsInvoked_ThenPassedFixtureIsBeingAdaptedAndInlineValuesCollectionIsEmpty(Fixture fixture) |
| 23 | + { |
| 24 | + // Arrange |
| 25 | + // Act |
| 26 | + var attribute = new AutoDataAdapterAttribute(fixture, null); |
| 27 | + |
| 28 | + // Assert |
| 29 | + Assert.Equal(fixture, attribute.AdaptedFixture); |
| 30 | + Assert.Empty(attribute.InlineValues); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact(DisplayName = "GIVEN uninitialized fixture WHEN constructor is invoked THEN exception is thrown")] |
| 34 | + public void GivenUninitializedFixture_WhenConstructorIsInvoked_ThenExceptionIsThrown() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + // Act |
| 38 | + static object Act() => new AutoDataAdapterAttribute(null); |
| 39 | + |
| 40 | + // Assert |
| 41 | + var exception = Assert.Throws<ArgumentNullException>(Act); |
| 42 | + Assert.Equal("fixture", exception.ParamName); |
| 43 | + } |
| 44 | + |
| 45 | + [AutoData] |
| 46 | + [Theory(DisplayName = "GIVEN uninitialized method info WHEN GetData is invoked THEN exception is thrown")] |
| 47 | + public void GivenUninitializedMethodInfo_WhenConstructorIsInvoked_ThenExceptionIsThrown(Fixture fixture) |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var attribute = new AutoDataAdapterAttribute(fixture); |
| 51 | + |
| 52 | + // Act |
| 53 | + object Act() => attribute.GetData(null); |
| 54 | + |
| 55 | + // Assert |
| 56 | + var exception = Assert.Throws<ArgumentNullException>(Act); |
| 57 | + Assert.Equal("testMethod", exception.ParamName); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact(DisplayName = "GIVEN test data with instance WHEN GetData called THEN auto data generation skipped")] |
| 61 | + public void GivenTestDataWithInstance_WhenGetDataCalled_ThenAutoDataGenerationSkipped() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + IFixture fixture = new Fixture(); |
| 65 | + var attribute = new AutoDataAdapterAttribute(fixture, SpecificTestClass.Create()); |
| 66 | + var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithAbstractTestClass), BindingFlags.Instance | BindingFlags.NonPublic); |
| 67 | + var expectedNumberOfParameters = methodInfo.GetParameters().Length; |
| 68 | + |
| 69 | + // Act |
| 70 | + var data = attribute.GetData(methodInfo).ToArray(); |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.Single(data); |
| 74 | + Assert.Equal(expectedNumberOfParameters, data.First().Length); |
| 75 | + Assert.All(data.First(), Assert.NotNull); |
| 76 | + Assert.All(data.First().Skip(1), item => Assert.Equal(data.First().Last(), item)); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact(DisplayName = "GIVEN empty test data WHEN GetData called THEN auto data generation throws exception")] |
| 80 | + public void GivenEmptyTestData_WhenGetDataCalled_ThenAutoDataGenerationSkipped() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + IFixture fixture = new Fixture(); |
| 84 | + var attribute = new AutoDataAdapterAttribute(fixture); |
| 85 | + var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithAbstractTestClass), BindingFlags.Instance | BindingFlags.NonPublic); |
| 86 | + |
| 87 | + // Act |
| 88 | + void Act() => attribute.GetData(methodInfo); |
| 89 | + |
| 90 | + // Assert |
| 91 | + Assert.ThrowsAny<Exception>(Act); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact(DisplayName = "GIVEN test method with Frozen customization after others WHEN GetData called THEN ensure parameter is frozen on the end")] |
| 95 | + public void GivenTestMethodWithFrozenCustomizationAfterOthers_WhenGetDataCalled_ThenEnsureParameterIsFrozenOnTheEnd() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + IFixture fixture = new Fixture(); |
| 99 | + var attribute = new AutoDataAdapterAttribute(fixture, null); |
| 100 | + var methodInfo = typeof(AutoDataAdapterAttributeTests).GetMethod(nameof(this.TestMethodWithFrozenCustomizationBeforeOthers), BindingFlags.Instance | BindingFlags.NonPublic); |
| 101 | + var expectedNumberOfParameters = methodInfo.GetParameters().Length; |
| 102 | + |
| 103 | + // Act |
| 104 | + var data = attribute.GetData(methodInfo).ToArray(); |
| 105 | + |
| 106 | + // Assert |
| 107 | + Assert.Single(data); |
| 108 | + Assert.Equal(expectedNumberOfParameters, data.First().Length); |
| 109 | + Assert.All(data.First(), Assert.NotNull); |
| 110 | + Assert.All( |
| 111 | + data.First().Skip(1), |
| 112 | + x => |
| 113 | + { |
| 114 | + var parameters = data.First(); |
| 115 | + Assert.Equal(parameters.Last(), x); |
| 116 | + Assert.NotEqual(parameters.First(), x); |
| 117 | + Assert.Contains(StopStringCustomization.Value, x as string); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + protected string TestMethodWithAbstractTestClass( |
| 122 | + SpecificTestClass instance, |
| 123 | + [Frozen] string text, |
| 124 | + string message) |
| 125 | + { |
| 126 | + return $"{instance}: {text}, {message}"; |
| 127 | + } |
| 128 | + |
| 129 | + protected string TestMethodWithFrozenCustomizationBeforeOthers( |
| 130 | + string first, |
| 131 | + [Frozen][StopString] string second, |
| 132 | + string third) |
| 133 | + { |
| 134 | + return $"{first}: {second}, {third}"; |
| 135 | + } |
| 136 | + |
| 137 | + protected abstract class AbstractTestClass |
| 138 | + { |
| 139 | + public override string ToString() => this.GetType().Name; |
| 140 | + } |
| 141 | + |
| 142 | + protected sealed class SpecificTestClass : AbstractTestClass |
| 143 | + { |
| 144 | + private SpecificTestClass() |
| 145 | + { |
| 146 | + } |
| 147 | + |
| 148 | + public static AbstractTestClass Create() |
| 149 | + { |
| 150 | + return new SpecificTestClass(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + protected sealed class StopStringAttribute : CustomizeWithAttribute<StopStringCustomization> |
| 155 | + { |
| 156 | + } |
| 157 | + |
| 158 | + [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "This class is instantiated indirectly.")] |
| 159 | + protected class StopStringCustomization : ICustomization |
| 160 | + { |
| 161 | + public const string Value = "STOP"; |
| 162 | + |
| 163 | + public void Customize(IFixture fixture) |
| 164 | + { |
| 165 | + fixture.Customizations.Add( |
| 166 | + new FilteringSpecimenBuilder( |
| 167 | + new FixedBuilder(Value), |
| 168 | + new ExactTypeSpecification(Value.GetType()))); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments