diff --git a/MutableOptions.Tests/MutableOptionsMonitorTests.cs b/MutableOptions.Tests/MutableOptionsMonitorTests.cs index e2f2fcd..e3066c2 100644 --- a/MutableOptions.Tests/MutableOptionsMonitorTests.cs +++ b/MutableOptions.Tests/MutableOptionsMonitorTests.cs @@ -1,8 +1,11 @@ namespace MutableOptions.Tests { + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Microsoft.Extensions.Options.Mutable; + using MutableOptions.Tests.TestOptions; using NUnit.Framework; [TestFixture] @@ -17,5 +20,37 @@ protected override IOptionsMonitor CreateOptionsMonitor(IServiceProvider s { return serviceProvider.GetRequiredService>(); } + + [Test] + public void NotifyPropertyChangedWrapper_ReadingCurrentValueFromChangedCallback_ShouldReturnNewValue() + { + var configData = new[] + { + new KeyValuePair("StringValue", "foo"), + }; + + using var host = Host.CreateDefaultBuilder(Array.Empty()) + .ConfigureAppConfiguration(builder => builder.AddInMemoryCollection(configData)) + .ConfigureServices((context, services) => + { + services.ConfigureMutable(context.Configuration); + services.AddSingleton(); + }) + .Build(); + + var simpleSettingsService = host.Services.GetRequiredService(); + Assert.That(simpleSettingsService.StringValue, Is.Not.EqualTo("bar")); + + var onChangeCalledTimes = 0; + simpleSettingsService.PropertyChanged += (_, _) => + { + Assert.That(simpleSettingsService.StringValue, Is.EqualTo("bar")); + onChangeCalledTimes++; + }; + + simpleSettingsService.StringValue = "bar"; + + Assert.That(onChangeCalledTimes, Is.EqualTo(1)); + } } } \ No newline at end of file diff --git a/MutableOptions.Tests/OptionsMutatorTests.cs b/MutableOptions.Tests/OptionsMutatorTests.cs index 325e9a8..6639568 100644 --- a/MutableOptions.Tests/OptionsMutatorTests.cs +++ b/MutableOptions.Tests/OptionsMutatorTests.cs @@ -23,6 +23,35 @@ public void Setup() .Returns(info => EqualityComparer.Default.Equals(info.ArgAt(0), info.ArgAt(1))); } + [Test] + public void OptionsMonitor_ReadingCurrentValueFromChangedCallback_ShouldReturnNewValue() + { + var configData = new[] + { + new KeyValuePair("StringValue", "foo"), + }; + + using var host = Host.CreateDefaultBuilder(Array.Empty()) + .ConfigureAppConfiguration(builder => builder.AddInMemoryCollection(configData)) + .ConfigureServices((context, services) => services.ConfigureMutable(context.Configuration)) + .Build(); + + var optionsMonitor = CreateOptionsMonitor(host.Services); + Assert.That(optionsMonitor.CurrentValue.StringValue, Is.Not.EqualTo("bar")); + + var onChangeCalledTimes = 0; + optionsMonitor.OnChange(options => + { + Assert.That(optionsMonitor.CurrentValue.StringValue, Is.EqualTo("bar").And.EqualTo(options.StringValue)); + onChangeCalledTimes++; + }); + + var optionsMutator = CreateSut(host.Services); + optionsMutator.Mutate(options => options with { StringValue = "bar" }); + + Assert.That(onChangeCalledTimes, Is.EqualTo(1)); + } + [Test] public void ConfigurationEmpty_WhenMutate_ShouldWriteToConfiguration() { diff --git a/MutableOptions.Tests/TestOptions/SimpleSettingsService.cs b/MutableOptions.Tests/TestOptions/SimpleSettingsService.cs new file mode 100644 index 0000000..01f3063 --- /dev/null +++ b/MutableOptions.Tests/TestOptions/SimpleSettingsService.cs @@ -0,0 +1,35 @@ +namespace MutableOptions.Tests.TestOptions +{ + using Microsoft.Extensions.Options.Mutable; + using System.ComponentModel; + + internal sealed class SimpleSettingsService : INotifyPropertyChanged, IDisposable + { + private readonly IMutableOptionsMonitor _mutableOptionsMonitor; + private readonly IDisposable _changeSubscription; + + public SimpleSettingsService(IMutableOptionsMonitor mutableOptionsMonitor) + { + _mutableOptionsMonitor = mutableOptionsMonitor; + _changeSubscription = _mutableOptionsMonitor.OnChange(HandleSimpleOptionsChanged); + } + + public event PropertyChangedEventHandler? PropertyChanged; + + public string StringValue + { + get => _mutableOptionsMonitor.CurrentValue.StringValue; + set => _mutableOptionsMonitor.Mutate(options => options with { StringValue = value }); + } + + public void Dispose() + { + _changeSubscription.Dispose(); + } + + private void HandleSimpleOptionsChanged(SimpleOptions simpleOptions, string name) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null)); + } + } +} \ No newline at end of file diff --git a/MutableOptions/MutableOptions.csproj b/MutableOptions/MutableOptions.csproj index 41536d9..99816c8 100644 --- a/MutableOptions/MutableOptions.csproj +++ b/MutableOptions/MutableOptions.csproj @@ -1,7 +1,7 @@ 10 - netstandard2.0;netstandard2.1;net6.0 + netstandard2.0;net6.0 enable True true