Split off original test modification into new test, add other value types to the...
authorBrandon Dahler <brandon.dahler@gmail.com>
Tue, 15 Oct 2019 23:48:21 +0000 (19:48 -0400)
committerBrandon Dahler <brandon.dahler@gmail.com>
Tue, 15 Oct 2019 23:48:21 +0000 (19:48 -0400)
Commit migrated from https://github.com/dotnet/Extensions/commit/5650b50a77f799c73e7aba8b64c51c04826e158c

src/libraries/Microsoft.Extensions.DependencyInjection/tests/Fakes/ClassWithOptionalArgsCtorWithStructs.cs
src/libraries/Microsoft.Extensions.DependencyInjection/tests/ServiceProviderContainerTests.cs

index 3a3130e..3ac6761 100644 (file)
@@ -8,7 +8,16 @@ namespace Microsoft.Extensions.DependencyInjection.Tests.Fakes
 {
     public class ClassWithServiceAndOptionalArgsCtorWithStructs
     {
+        public DateTime DateTime { get; }
         public DateTime DateTimeDefault { get; }
+        public TimeSpan TimeSpan { get; }
+        public TimeSpan TimeSpanDefault { get; }
+        public DateTimeOffset DateTimeOffset { get; }
+        public DateTimeOffset DateTimeOffsetDefault { get; }
+        public Guid Guid { get; }
+        public Guid GuidDefault { get; }
+        public CustomStruct CustomStructValue { get; }
+        public CustomStruct CustomStructDefault { get; }
 
         public ClassWithServiceAndOptionalArgsCtorWithStructs(IFakeService fake,
             DateTime dateTime = new DateTime(),
@@ -23,7 +32,16 @@ namespace Microsoft.Extensions.DependencyInjection.Tests.Fakes
             CustomStruct customStructDefault = default(CustomStruct)
         )
         {
+            DateTime = dateTime;
             DateTimeDefault = dateTimeDefault;
+            TimeSpan = timeSpan;
+            TimeSpanDefault = timeSpanDefault;
+            DateTimeOffset = dateTimeOffset;
+            DateTimeOffsetDefault = dateTimeOffsetDefault;
+            Guid = guid;
+            GuidDefault = guidDefault;
+            CustomStructValue = customStruct;
+            CustomStructDefault = customStructDefault;
         }
 
         public struct CustomStruct { }
index c5acbfd..6e68393 100644 (file)
@@ -121,6 +121,18 @@ namespace Microsoft.Extensions.DependencyInjection.Tests
         {
             var serviceCollection = new ServiceCollection();
             serviceCollection.AddSingleton<IFakeService, FakeService>();
+            serviceCollection.AddSingleton<ClassWithServiceAndOptionalArgsCtorWithStructs>();
+
+            var provider = CreateServiceProvider(serviceCollection);
+            var service = provider.GetService<ClassWithServiceAndOptionalArgsCtorWithStructs>();
+            Assert.NotNull(service);
+        }
+
+        [Fact]
+        public void ResolvesServiceMixedServiceAndOptionalStructConstructorArgumentsReliably()
+        {
+            var serviceCollection = new ServiceCollection();
+            serviceCollection.AddSingleton<IFakeService, FakeService>();
             serviceCollection.AddTransient<ClassWithServiceAndOptionalArgsCtorWithStructs>();
 
             var provider = CreateServiceProvider(serviceCollection);
@@ -130,7 +142,16 @@ namespace Microsoft.Extensions.DependencyInjection.Tests
                 var service = provider.GetService<ClassWithServiceAndOptionalArgsCtorWithStructs>();
 
                 Assert.NotNull(service);
-                Assert.Equal(default, service.DateTimeDefault);
+                Assert.Equal(new DateTime(), service.DateTime);
+                Assert.Equal(default(DateTime), service.DateTimeDefault);
+                Assert.Equal(new TimeSpan(), service.TimeSpan);
+                Assert.Equal(default(TimeSpan), service.TimeSpanDefault);
+                Assert.Equal(new DateTimeOffset(), service.DateTimeOffset);
+                Assert.Equal(default(DateTimeOffset), service.DateTimeOffsetDefault);
+                Assert.Equal(new Guid(), service.Guid);
+                Assert.Equal(default(Guid), service.GuidDefault);
+                Assert.Equal(new ClassWithServiceAndOptionalArgsCtorWithStructs.CustomStruct(), service.CustomStructValue);
+                Assert.Equal(default(ClassWithServiceAndOptionalArgsCtorWithStructs.CustomStruct), service.CustomStructDefault);
             }
         }