Ejemplo n.º 1
0
        public void Build_WhenArgumentAreAfterInMemory_ShouldUseArgumentBinder()
        {
            var section    = nameof(ICommandLineInterface).TrimStart('I');
            var collection = new InMemoryCollection();

            collection.Add(section, "name", "name");
            collection.Add(section, "Age", "15");

            var sut = SettingsBuilder.CreateBuilder(x =>
                                                    x.AddInMemoryCollection(collection));

            var result = sut.GetSettings <ICommandLineInterface>();

            Assert.Equal("name", result.Name);
            Assert.Equal(15, result.Age);

            sut = SettingsBuilder.CreateBuilder(x =>
                                                x.AddInMemoryCollection(collection)
                                                .AddArguments(Args.Split(' '),
                                                              o => o.SetCaseSensitivity(false)));

            result = sut.GetSettings <ICommandLineInterface>();

            Assert.Equal("value", result.Name);
            Assert.Equal(3, result.Age);
        }
        public void Build_WhenAddGlobalConverter_ShouldReturnConverterValue()
        {
            var sut    = SettingsBuilder.CreateBuilder(x => x.AddTypeConverter(new GuidSettingsConvertor()));
            var result = sut.GetSettings <IGuidInterface>();

            Assert.NotEqual(Guid.Empty, result.Guid);
        }
        public void Build_WhenAddLocalConverter_ShouldReturnConverterValue()
        {
            var sut    = SettingsBuilder.CreateBuilder();
            var result = sut.GetSettings <IGuidInterfaceWithConversionAttribute>();

            Assert.NotEqual(Guid.Empty, result.Guid);
        }
Ejemplo n.º 4
0
        public void GetSettings_BuildingInterfaceNotFromAssembly_ShouldReturnInstanceWithValues()
        {
            var sut = SettingsBuilder.CreateBuilder();

            var result = sut.GetSettings <ISomeInterface>();

            Assert.Equal(result.Name, SomeName);
        }
Ejemplo n.º 5
0
        public void Build_WhenNameMatchKey_ShouldPlaceValue()
        {
            var sut = SettingsBuilder.CreateBuilder(x =>
                                                    x.AddArguments(Args.Split(' ')));

            var result = sut.GetSettings <ICommandLineInterface>();

            Assert.Equal("value", result.Name);
            Assert.NotEqual(3, result.Age);
        }
        private static SettingsBuilder BuildSutWithBinder(params ISectionBinder[] binders)
        {
            var sut = SettingsBuilder.CreateBuilder(x =>
            {
                foreach (var sectionBinder in binders)
                {
                    x.AddSectionBinder(sectionBinder);
                }
            });

            return(sut);
        }
        public void Build_WhereVariableHasValue_ShouldSetProperty()
        {
            var sut = SettingsBuilder.CreateBuilder(x => x.AddAppSettings());

            var result = sut.ScanAssemblies(GetType().Assembly);

            var settings = result.GetSettings <IWithConfigurationValue>();

            Assert.Equal(settings.WithValue, TestsConstants.AppSettingsValue);
            Assert.Equal(string.Empty, settings.WithoutValue);
            Assert.Null(settings.NonExistence);
        }
        public void Build_WhereVariableHasValue_ShouldSetProperty()
        {
            var sut = SettingsBuilder.CreateBuilder();

            var result = sut.GetSettings <IEnumerableInterface>();

            Assert.Equal(4, result.Values.Count());

            Assert.Contains(N12, result.Values);
            Assert.Contains(N3, result.Values);
            Assert.Contains(N4, result.Values);
            Assert.Contains(N103, result.Values);
        }
Ejemplo n.º 9
0
        public void Build_WhereVariableHasValue_ShouldSetProperty()
        {
            var guid = Guid.NewGuid().ToString();

            using (new DisposableEnvironmentVariable(EnvironmentVariable, guid))
            {
                var sut = SettingsBuilder.CreateBuilder(x => x.AddEnvironmentVariable());

                var result = sut.ScanAssemblies(GetType().Assembly);

                var settings = result.GetSettings <IWithEnvironmentVariable>();

                Assert.Equal(guid, settings.EnvironmentVariable);
            }
        }
Ejemplo n.º 10
0
        private static void IntegrateSimpleSettings(this IServiceCollection services,
                                                    Action <ISettingsBuilderOptions> action = null)
        {
            SettingsBuilderOptions options = null;

            var settingsBuilder = SettingsBuilder.CreateBuilder(factory =>
            {
                options = new SettingsBuilderOptions(factory);
                action?.Invoke(options);
            });

            var settingsProvider = settingsBuilder.ScanAssemblies(options.Assemblies);

            // replace this with a replaceable settings provider to support time based Settingsuration

            foreach (var settings in settingsProvider)
            {
                services.AddSingleton(settings.Key, settings.Value);
            }

            services.AddSingleton <ISettingsProvider>(new SettingsProvider(settingsBuilder));
        }
Ejemplo n.º 11
0
        public void TryGetValue_WhenMemoryCollectionIsLast_ShouldSetValueFromMemoryBinder()
        {
            var guid    = Guid.NewGuid().ToString();
            var badGuid = Guid.NewGuid().ToString();

            using (new DisposableEnvironmentVariable(EnvironmentVariable, badGuid))
            {
                var collection = new InMemoryCollection();
                collection.Add(nameof(IWithEnvironmentVariable).TrimStart('I'),
                               EnvironmentVariable, guid);

                var sut = SettingsBuilder.CreateBuilder(x =>
                {
                    x.AddEnvironmentVariable()
                    .AddInMemoryCollection(collection);
                });

                var settings = sut.GetSettings <IWithEnvironmentVariable>();

                Assert.Equal(guid, settings.EnvironmentVariable);
            }
        }
Ejemplo n.º 12
0
        public void Build_WhenAllowEmptyIsFalse_ShouldThrowException()
        {
            var sut = SettingsBuilder.CreateBuilder();

            Assert.Throws <SettingsPropertyValueException>(() => sut.GetSettings <IWithNonNullInterface>());
        }
 public void Run()
 {
     var settingsBuilder    = SettingsBuilder.CreateBuilder();
     var settingsCollection = settingsBuilder.ScanAssemblies(GetType().Assembly);
     var count = settingsCollection.Count();
 }
Ejemplo n.º 14
0
        public void GetSettings_WhenTypeIsNotInterface_ShouldThrowException()
        {
            var sut = SettingsBuilder.CreateBuilder();

            Assert.Throws <InvalidOperationException>(() => sut.GetSettings <SettingsBuilder>());
        }