diff --git a/src/Tests/ExistForAll.SimpleSettings.UnitTests/SimpleSettings/BindingContextTests.cs b/src/Tests/ExistForAll.SimpleSettings.UnitTests/SimpleSettings/BindingContextTests.cs new file mode 100644 index 0000000..9a3d044 --- /dev/null +++ b/src/Tests/ExistForAll.SimpleSettings.UnitTests/SimpleSettings/BindingContextTests.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; + +namespace ExistForAll.SimpleSettings.UnitTests.SimpleSettings +{ + public class BindingContextTests + { + [Test] + public async Task BindPropertySettings_ContextPropertyType_IsThePropertysOwnType() + { + var binder = new CapturingBinder(); + + SettingsBuilder.CreateBuilder(x => x.AddSectionBinder(binder)) + .GetSettings(); + + // PropertyType must be each property's own type — not the declaring interface + // (regression guard: it was previously set to propertyInfo.DeclaringType). + await Assert.That(binder.Captured[nameof(ICaptureSettings.Name)].PropertyType).IsEqualTo(typeof(string)); + await Assert.That(binder.Captured[nameof(ICaptureSettings.Count)].PropertyType).IsEqualTo(typeof(int)); + } + + [Test] + public async Task BindPropertySettings_ContextStillExposesDeclaringInterface() + { + var binder = new CapturingBinder(); + + SettingsBuilder.CreateBuilder(x => x.AddSectionBinder(binder)) + .GetSettings(); + + var context = binder.Captured[nameof(ICaptureSettings.Name)]; + + // Fixing PropertyType did not remove access to the declaring type: the settings + // interface is on SettingsType, and the property's declaring type on PropertyInfo. + await Assert.That(context.SettingsType).IsEqualTo(typeof(ICaptureSettings)); + await Assert.That(context.PropertyInfo.DeclaringType).IsEqualTo(typeof(ICaptureSettings)); + } + + private sealed class CapturingBinder : ISectionBinder + { + public Dictionary Captured { get; } = new Dictionary(); + + public void BindPropertySettings(BindingContext context) + { + Captured[context.Key] = context; + } + } + + public interface ICaptureSettings + { + string Name { get; set; } + int Count { get; set; } + } + } +}