// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections; using System.Collections.Generic; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Xunit; namespace Microsoft.AspNetCore.Mvc.Infrastructure { public class ConfigureCompatibilityOptionsTest { [Fact] public void PostConfigure_NoValueForProperty_DoesNothing() { // Arrange var configure = Create(CompatibilityVersion.Version_2_0, new Dictionary()); var options = new TestOptions(); // Act configure.PostConfigure(Options.DefaultName, options); // Assert Assert.False(options.TestProperty); } [Fact] public void PostConfigure_ValueIsSet_DoesNothing() { // Arrange var configure = Create(CompatibilityVersion.Version_2_0, new Dictionary() { { nameof(TestOptions.TestProperty), true }, }); var options = new TestOptions() { TestProperty = false, }; // Act configure.PostConfigure(Options.DefaultName, options); // Assert Assert.False(options.TestProperty); } [Fact] public void PostConfigure_ValueNotSet_SetsValue() { // Arrange var configure = Create(CompatibilityVersion.Version_2_0, new Dictionary() { { nameof(TestOptions.TestProperty), true }, }); var options = new TestOptions(); // Act configure.PostConfigure(Options.DefaultName, options); // Assert Assert.True(options.TestProperty); } private static ConfigureCompatibilityOptions Create( CompatibilityVersion version, IReadOnlyDictionary defaultValues) { var compatibilityOptions = Options.Create(new MvcCompatibilityOptions() { CompatibilityVersion = version }); return new TestConfigure(NullLoggerFactory.Instance, compatibilityOptions, defaultValues); } private class TestOptions : IEnumerable { private readonly CompatibilitySwitch _testProperty; private readonly ICompatibilitySwitch[] _switches; public TestOptions() { _testProperty = new CompatibilitySwitch(nameof(TestProperty)); _switches = new ICompatibilitySwitch[] { _testProperty }; } public bool TestProperty { get => _testProperty.Value; set => _testProperty.Value = value; } public IEnumerator GetEnumerator() { return ((IEnumerable)_switches).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator(); } private class TestConfigure : ConfigureCompatibilityOptions { public TestConfigure( ILoggerFactory loggerFactory, IOptions compatibilityOptions, IReadOnlyDictionary defaultValues) : base(loggerFactory, compatibilityOptions) { DefaultValues = defaultValues; } protected override IReadOnlyDictionary DefaultValues { get; } } } }