|
11 | 11 | #if BUILDING_SOURCE_GENERATOR_TESTS |
12 | 12 | using Microsoft.Extensions.Configuration; |
13 | 13 | #endif |
| 14 | +using Microsoft.Extensions.Configuration.Memory; |
14 | 15 | using Microsoft.Extensions.Configuration.Test; |
15 | 16 | using Xunit; |
16 | 17 |
|
@@ -1767,7 +1768,7 @@ public void EnsureCallingThePropertySetter() |
1767 | 1768 | Assert.Equal(0, options.OtherCodeNullable); |
1768 | 1769 | Assert.Equal("default", options.OtherCodeString); |
1769 | 1770 | Assert.Null(options.OtherCodeNull); |
1770 | | - Assert.Null(options.OtherCodeUri); |
| 1771 | + Assert.Null(options.OtherCodeUri); |
1771 | 1772 | } |
1772 | 1773 |
|
1773 | 1774 | [Fact] |
@@ -2238,7 +2239,7 @@ void TestUntypedOverloads(IConfiguration? configuration, string? key) |
2238 | 2239 | Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(GeolocationClass), key, new GeolocationClass())); |
2239 | 2240 | Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key)); |
2240 | 2241 | Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, defaultValue: null)); |
2241 | | - Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, default(Geolocation))); |
| 2242 | + Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, default(Geolocation))); |
2242 | 2243 | } |
2243 | 2244 | } |
2244 | 2245 |
|
@@ -2404,5 +2405,38 @@ public void SharedChildInstance() |
2404 | 2405 | config.GetSection("A").Bind(instance); |
2405 | 2406 | Assert.Equal("localhost", instance.ConnectionString); |
2406 | 2407 | } |
| 2408 | + |
| 2409 | + [Fact] |
| 2410 | + public void CanBindToMockConfigurationSection() |
| 2411 | + { |
| 2412 | + const string expectedA = "hello"; |
| 2413 | + |
| 2414 | + var configSource = new MemoryConfigurationSource() |
| 2415 | + { |
| 2416 | + InitialData = new Dictionary<string, string?>() |
| 2417 | + { |
| 2418 | + [$":{nameof(SimplePoco.A)}"] = expectedA, |
| 2419 | + } |
| 2420 | + }; |
| 2421 | + var configRoot = new MockConfigurationRoot(new[] { configSource.Build(null) }); |
| 2422 | + var configSection = new ConfigurationSection(configRoot, string.Empty); |
| 2423 | + |
| 2424 | + SimplePoco result = new(); |
| 2425 | + configSection.Bind(result); |
| 2426 | + |
| 2427 | + Assert.Equal(expectedA, result.A); |
| 2428 | + Assert.Equal(default(string), result.B); |
| 2429 | + } |
| 2430 | + |
| 2431 | + // a mock configuration root that will return null for undefined Sections, |
| 2432 | + // as is common when Configuration interfaces are mocked |
| 2433 | + class MockConfigurationRoot : ConfigurationRoot, IConfigurationRoot |
| 2434 | + { |
| 2435 | + public MockConfigurationRoot(IList<IConfigurationProvider> providers) : base(providers) |
| 2436 | + { } |
| 2437 | + |
| 2438 | + IConfigurationSection IConfiguration.GetSection(string key) => |
| 2439 | + this[key] is null ? null : new ConfigurationSection(this, key); |
| 2440 | + } |
2407 | 2441 | } |
2408 | 2442 | } |
0 commit comments