Exemplo n.º 1
0
        public void JsonConfiguration_Does_Not_Throw_On_Optional_Configuration()
        {
            var configSource = new JsonConfigurationProvider("NotExistingConfig.json", optional: true);

            configSource.Load();
            Assert.Throws <InvalidOperationException>(() => configSource.Get("key"));
        }
Exemplo n.º 2
0
        public void JsonConfiguration_Throws_On_Missing_Configuration_File()
        {
            var configSource = new JsonConfigurationProvider("NotExistingConfig.json", optional: false);
            var exception    = Assert.Throws <FileNotFoundException>(() => configSource.Load());

            // Assert
            Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message);
        }
        private JsonConfigurationProvider LoadProvider(string json)
        {
            var p = new JsonConfigurationProvider(new JsonConfigurationSource {
                Optional = true
            });

            p.Load(TestStreamHelpers.StringToStream(json));
            return(p);
        }
Exemplo n.º 4
0
        public void NonObjectRootIsInvalid()
        {
            var json             = @"'test'";
            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            var exception = Assert.Throws <FormatException>(
                () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)));

            Assert.NotNull(exception.Message);
        }
Exemplo n.º 5
0
        public void LoadMethodCanHandleEmptyValue()
        {
            var json          = @"
{
    'name': ''
}";
            var jsonConfigSrc = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal(string.Empty, jsonConfigSrc.Get("name"));
        }
Exemplo n.º 6
0
        public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing()
        {
            var json             = @"{
                'name': 'test',
                'address': {
                    'street': 'Something street',
                    'zipcode': '12345'
                }
            /* Missing a right brace here*/";
            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            var exception = Assert.Throws <FormatException>(
                () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)));

            Assert.NotNull(exception.Message);
        }
Exemplo n.º 7
0
        protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(TestSection testConfig)
        {
            var jsonBuilder = new StringBuilder();

            SectionToJson(jsonBuilder, testConfig);

            var provider = new JsonConfigurationProvider(
                new JsonConfigurationSource
            {
                Optional = true
            });

            var json = jsonBuilder.ToString();

            return(provider, () => provider.Load(TestStreamHelpers.StringToStream(json)));
        }
        protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(TestSection testConfig)
        {
            var jsonBuilder = new StringBuilder();

            SectionToJson(jsonBuilder, testConfig, includeComma: false);

            var provider = new JsonConfigurationProvider(
                new JsonConfigurationSource
            {
                Optional = true
            });

            var json = jsonBuilder.ToString();

            json = JObject.Parse(json).ToString(); // standardize the json (removing trailing commas)
            return(provider, () => provider.Load(TestStreamHelpers.StringToStream(json)));
        }
Exemplo n.º 9
0
        public void SupportAndIgnoreComments()
        {
            var json          = @"/* Comments */
                {/* Comments */
                ""name"": /* Comments */ ""test"",
                ""address"": {
                    ""street"": ""Something street"", /* Comments */
                    ""zipcode"": ""12345""
                }
            }";
            var jsonConfigSrc = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("address:street"));
            Assert.Equal("12345", jsonConfigSrc.Get("address:zipcode"));
        }
Exemplo n.º 10
0
        public void LoadKeyValuePairsFromValidJson()
        {
            var json          = @"
{
    'firstname': 'test',
    'test.last.name': 'last.name',
        'residential.address': {
            'street.name': 'Something street',
            'zipcode': '12345'
        }
}";
            var jsonConfigSrc = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("firstname"));
            Assert.Equal("last.name", jsonConfigSrc.Get("test.last.name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("residential.address:STREET.name"));
            Assert.Equal("12345", jsonConfigSrc.Get("residential.address:zipcode"));
        }