Add more tests for complex dict element binding by generator (#89210)
authorLayomi Akinrinade <laakinri@microsoft.com>
Thu, 20 Jul 2023 00:26:18 +0000 (17:26 -0700)
committerGitHub <noreply@github.com>
Thu, 20 Jul 2023 00:26:18 +0000 (17:26 -0700)
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs

index df894d6..50d1a48 100644 (file)
@@ -667,8 +667,14 @@ namespace Microsoft.Extensions
             public int MyInt { get; }
         }
 
+        public interface IGeolocation
+        {
+            public double Latitude { get; set; }
+            public double Longitude { get; set; }
+        }
+
         [TypeConverter(typeof(GeolocationTypeConverter))]
-        public struct Geolocation
+        public struct Geolocation : IGeolocation
         {
             public static readonly Geolocation Zero = new(0, 0);
 
@@ -692,6 +698,18 @@ namespace Microsoft.Extensions
             }
         }
 
+        public sealed class GeolocationClass : IGeolocation
+        {
+            public double Latitude { get; set; }
+            public double Longitude { get; set; }
+        }
+
+        public sealed record GeolocationRecord : IGeolocation
+        {
+            public double Latitude { get; set; }
+            public double Longitude { get; set; }
+        }
+
         public class GeolocationWrapper
         {
             public Geolocation Location { get; set; }
index b1af371..098eeb4 100644 (file)
@@ -1939,9 +1939,18 @@ if (!System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Launc
 
             Geolocation obj = configuration.Get<IDictionary<string, Geolocation>>()["First"];
             ValidateGeolocation(obj);
-
             obj = configuration.Get<IReadOnlyDictionary<string, Geolocation>>()["First"];
             ValidateGeolocation(obj);
+
+            GeolocationClass obj1 = configuration.Get<IDictionary<string, GeolocationClass>>()["First"];
+            ValidateGeolocation(obj1);
+            obj1 = configuration.Get<IReadOnlyDictionary<string, GeolocationClass>>()["First"];
+            ValidateGeolocation(obj1);
+
+            GeolocationRecord obj2 = configuration.Get<IDictionary<string, GeolocationRecord>>()["First"];
+            ValidateGeolocation(obj2);
+            obj1 = configuration.Get<IReadOnlyDictionary<string, GeolocationClass>>()["First"];
+            ValidateGeolocation(obj2);
         }
 
         [Fact]
@@ -1960,7 +1969,7 @@ if (!System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Launc
             ValidateGeolocation(obj);
         }
 
-        private void ValidateGeolocation(Geolocation location)
+        private void ValidateGeolocation(IGeolocation location)
         {
             Assert.Equal(3, location.Latitude);
             Assert.Equal(4, location.Longitude);