public void GetOptions_ShouldGetArrayOptionForAnyArrayType() { var targets = new TargetContainer(); targets.SetOption <TestOption, Array>("array"); Assert.Equal("array", targets.GetOption <TestOption, int[]>()); Assert.Equal("array", targets.GetOption <TestOption, IServiceProvider[]>()); Assert.Equal("array", targets.GetOption <TestOption, IEnumerable <string>[]>()); }
public void ShouldConfigureServiceSpecicOption() { CombinedTargetContainerConfig config = new CombinedTargetContainerConfig(); config.ConfigureOption <TestOption, string>("for System.String"); var targets = new TargetContainer(config); Assert.Equal("for System.String", targets.GetOption <TestOption, string>()); Assert.Null(targets.GetOption <TestOption>()); }
public void ShouldFallBackToGlobalOptionByDefault() { // setting a global option should apply for all types that don't have // that option explicitly set. var targets = new TargetContainer(); targets.SetOption <TestOption>("global"); Assert.Equal("global", targets.GetOption <TestOption, int>()); Assert.Equal("global", targets.GetOption <TestOption, IEnumerable <int> >()); Assert.Equal("global", targets.GetOption <TestOption>(typeof(IEnumerable <>))); }
public void ShouldUseDefaultInsteadOfGlobalWhenConfigured() { var targets = new TargetContainer(); // you can control how the options functionality works by using options :) targets.SetOption <EnableGlobalOptions>(false); // set a global option which will not be used. targets.SetOption <TestOption>("global"); Assert.Equal("default", targets.GetOption <TestOption, int>("default")); Assert.Equal("default", targets.GetOption <TestOption, IEnumerable <int> >("default")); Assert.Equal("default", targets.GetOption <TestOption>(typeof(IEnumerable <>), "default")); }
public void ShouldGetAndSetOptions() { var targets = new TargetContainer(); targets.SetOption <TestOption>("first") .SetOption <TestOption, int>("second") .SetOption <TestOption>("third", typeof(IEnumerable <>)) .SetOption <TestOption, IEnumerable <int> >("fourth"); Assert.Equal("first", targets.GetOption <TestOption>()); Assert.Equal("second", targets.GetOption <TestOption, int>()); Assert.Equal("third", targets.GetOption <TestOption>(typeof(IEnumerable <>))); Assert.Equal("fourth", targets.GetOption <TestOption, IEnumerable <int> >()); }
public void ShouldConfigureServiceSpecificOptionByCallback() { CombinedTargetContainerConfig config = new CombinedTargetContainerConfig(); config.ConfigureOption(typeof(string), (tc, t) => { Assert.NotNull(tc); Assert.Equal(typeof(string), t); return((TestOption)$"for { t.FullName } (callback)"); }); var targets = new TargetContainer(config); Assert.Equal($"for { typeof(string).FullName } (callback)", targets.GetOption <TestOption, string>()); Assert.Null(targets.GetOption <TestOption>()); }
public void Contravariant_ShouldDisableForOpenGeneric() { // Arrange var targets = new TargetContainer(); // test shows that contravariance still works for Action<> but is disabled for all IContravariant<> targets.SetOption <Options.EnableContravariance>(false, typeof(IContravariant <>)); // Act & Assert Assert.False(targets.GetOption(typeof(IContravariant <>), Options.EnableContravariance.Default).Value); var result1 = new TargetTypeSelector(typeof(IContravariant <BaseClassGrandchild>), targets).ToArray(); var result2 = new TargetTypeSelector(typeof(Action <BaseClassGrandchild>), targets).ToArray(); Assert.Equal( new[] { typeof(IContravariant <BaseClassGrandchild>), typeof(IContravariant <>) }, result1 ); Assert.Equal( new[] { typeof(Action <BaseClassGrandchild>), typeof(Action <BaseClassChild>), typeof(Action <BaseClass>), typeof(Action <object>), typeof(Action <>) }, result2); }
public void ShouldUseOpenGenericServiceOptionForClosed() { var targets = new TargetContainer(); targets.SetOption <TestOption>("open", typeof(IEnumerable <>)); Assert.Equal("open", targets.GetOption <TestOption, IEnumerable <int> >()); }
public void ClosedGenericServiceOptionShouldOverrideOpenGeneric() { var targets = new TargetContainer(); targets.SetOption <TestOption>("open", typeof(IEnumerable <>)); targets.SetOption <TestOption, IEnumerable <int> >("int"); Assert.Equal("int", targets.GetOption <TestOption, IEnumerable <int> >()); }
public void ShouldUseOptionForBaseClass() { var targets = new TargetContainer(); // the underlying options container that's used to store the option value // is contravariant, so should return an option defined at the base class level :) targets.SetOption <TestOption>("for baseclass", typeof(BaseClass)); Assert.Equal("for baseclass", targets.GetOption <TestOption, BaseClassChild>()); }
public void ShouldConfigureGlobalOption() { CombinedTargetContainerConfig config = new CombinedTargetContainerConfig(); config.ConfigureOption((TestOption)"global"); var targets = new TargetContainer(config); Assert.Equal("global", targets.GetOption <TestOption>()); }
public void ShouldConfigureGlobalOptionByCallback() { CombinedTargetContainerConfig config = new CombinedTargetContainerConfig(); config.ConfigureOption(tc => { Assert.NotNull(tc); return((TestOption)"global (callback)"); }); var targets = new TargetContainer(config); Assert.Equal("global (callback)", targets.GetOption <TestOption>()); }