public void LoadMethodCanHandleEmptyValue()
        {
            var ini          = @"DefaultKey=";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(1, iniConfigSrc.Data.Count);
            Assert.Equal(string.Empty, iniConfigSrc.Data["DefaultKey"]);
        }
        public void ThrowExceptionWhenFoundInvalidLine()
        {
            var ini          = @"
ConnectionString
            ";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);
            var expectedMsg  = Resources.FormatError_UnrecognizedLineFormat("ConnectionString");

            var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void DoubleQuoteIsPartOfValueIfAppearInTheMiddleOfValue()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=Test\"Connection\"String\n" +
                      "Provider=Sql\"Client";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(2, iniConfigSrc.Data.Count);
            Assert.Equal("Test\"Connection\"String", iniConfigSrc.Data["ConnectionString:DefaultConnection"]);
            Assert.Equal("Sql\"Client", iniConfigSrc.Data["ConnectionString:Provider"]);
        }
        public void DoubleQuoteIsPartOfValueIfNotPaired()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=\"TestConnectionString\n" +
                      "Provider=SqlClient\"";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(2, iniConfigSrc.Data.Count);
            Assert.Equal("\"TestConnectionString", iniConfigSrc.Data["ConnectionString:DefaultConnection"]);
            Assert.Equal("SqlClient\"", iniConfigSrc.Data["ConnectionString:Provider"]);
        }
        public void CanCreateNewConfig()
        {
            var targetIni    = @"Data:DefaultConnection:ConnectionString=TestConnectionString
Data:DefaultConnection:Provider=SqlClient
";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Data["Data:DefaultConnection:ConnectionString"] = "TestConnectionString";
            iniConfigSrc.Data["Data:DefaultConnection:Provider"]         = "SqlClient";
            var cacheStream = new MemoryStream();

            iniConfigSrc.GenerateNewConfig(cacheStream);

            Assert.Equal(targetIni, StreamToString(cacheStream));
        }
        public void CommitMethodCanHandleEmptyValue()
        {
            var ini               = @"DefaultKey=";
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));
            iniConfigSrc.Set("DefaultKey", "Value");

            iniConfigSrc.Commit(StringToStream(ini), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal("DefaultKey=Value", newContents);
        }
        public void ThrowExceptionWhenKeyIsDuplicated()
        {
            var ini          = @"
            [Data:DefaultConnection]
            ConnectionString=TestConnectionString
            Provider=SqlClient
            [Data]
            DefaultConnection:ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);
            var expectedMsg  = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString");

            var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void LoadKeyValuePairsFromValidIniFileWithoutSectionHeader()
        {
            var ini          = @"
            DefaultConnection:ConnectionString=TestConnectionString
            DefaultConnection:Provider=SqlClient
            Data:Inventory:ConnectionString=AnotherTestConnectionString
            Data:Inventory:Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(4, iniConfigSrc.Data.Count);
            Assert.Equal("TestConnectionString", iniConfigSrc.Data["DefaultConnection:ConnectionString"]);
            Assert.Equal("SqlClient", iniConfigSrc.Data["DefaultConnection:Provider"]);
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Data["Data:Inventory:ConnectionString"]);
            Assert.Equal("MySql", iniConfigSrc.Data["Data:Inventory:Provider"]);
        }
        public void LoadKeyValuePairsFromValidIniFileWithQuotedValues()
        {
            var ini = "[DefaultConnection]\n" +
                      "ConnectionString=\"TestConnectionString\"\n" +
                      "Provider=\"SqlClient\"\n" +
                      "[Data:Inventory]\n" +
                      "ConnectionString=\"AnotherTestConnectionString\"\n" +
                      "Provider=\"MySql\"";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(4, iniConfigSrc.Data.Count);
            Assert.Equal("TestConnectionString", iniConfigSrc.Data["DefaultConnection:ConnectionString"]);
            Assert.Equal("SqlClient", iniConfigSrc.Data["DefaultConnection:Provider"]);
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Data["Data:Inventory:ConnectionString"]);
            Assert.Equal("MySql", iniConfigSrc.Data["Data:Inventory:Provider"]);
        }
        public void LoadKeyValuePairsFromValidIniFile()
        {
            var ini          = @"[DefaultConnection]
ConnectionString=TestConnectionString
Provider=SqlClient
[Data:Inventory]
ConnectionString=AnotherTestConnectionString
SubHeader:Provider=MySql";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(4, iniConfigSrc.Data.Count);
            Assert.Equal("TestConnectionString", iniConfigSrc.Data["defaultconnection:ConnectionString"]);
            Assert.Equal("SqlClient", iniConfigSrc.Data["DEFAULTCONNECTION:PROVIDER"]);
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Data["Data:Inventory:CONNECTIONSTRING"]);
            Assert.Equal("MySql", iniConfigSrc.Data["Data:Inventory:SubHeader:Provider"]);
        }
        public void CommitOperationThrowsExceptionWhenFindInvalidModificationAfterLoadOperation()
        {
            var ini               = @"
            ; Comments
            [Data:DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient";
            var modifiedIni       = string.Format("This is an invalid line{0}{1}", Environment.NewLine, ini);
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));

            var exception = Assert.Throws <FormatException>(
                () => iniConfigSrc.Commit(StringToStream(modifiedIni), outputCacheStream));

            Assert.Equal(Resources.FormatError_UnrecognizedLineFormat("This is an invalid line"), exception.Message);
        }
        public void CommitMethodUpdatesValues()
        {
            var ini               = @"; Comments
[Data:DefaultConnection]
# Comments
ConnectionString=TestConnectionString
/ Comments
Provider=SqlClient";
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));
            iniConfigSrc.Set("Data:DefaultConnection:ConnectionString", "NewTestConnectionString");

            iniConfigSrc.Commit(StringToStream(ini), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal(ini.Replace("TestConnectionString", "NewTestConnectionString"), newContents);
        }
        public void CommitMethodPreservesCommentsAndWhiteSpaces()
        {
            var ini               = @"
            ; Comments
            [Data:DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient";
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));

            iniConfigSrc.Commit(StringToStream(ini), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal(ini, newContents);
        }
        public void CommitOperationThrowsExceptionWhenKeysAreMissingInConfigFile()
        {
            var ini               = @"
            ; Comments
            [Data:DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient";
            var modifiedIni       = ini.Replace("Provider=SqlClient", string.Empty);
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));

            var exception = Assert.Throws <InvalidOperationException>(
                () => iniConfigSrc.Commit(StringToStream(modifiedIni), outputCacheStream));

            Assert.Equal(Resources.FormatError_CommitWhenKeyMissing("Data:DefaultConnection:Provider"),
                         exception.Message);
        }
        public void CommitOperationThrowsExceptionWhenFindNewlyAddedKeyAfterLoadOperation()
        {
            var ini         = @"
            ; Comments
            [Data:DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient";
            var modifiedIni = string.Format("NewKey1 = NewValue1{0}NewKey2 = NewValue2{0}{1}",
                                            Environment.NewLine, ini);
            var iniConfigSrc      = new IniFileConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            iniConfigSrc.Load(StringToStream(ini));

            var exception = Assert.Throws <InvalidOperationException>(
                () => iniConfigSrc.Commit(StringToStream(modifiedIni), outputCacheStream));

            Assert.Equal(Resources.FormatError_CommitWhenNewKeyFound("NewKey1"), exception.Message);
        }
        public void SupportAndIgnoreComments()
        {
            var ini          = @"
            ; Comments
            [DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient
            [Data:Inventory]
            ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);

            iniConfigSrc.Load(StringToStream(ini));

            Assert.Equal(4, iniConfigSrc.Data.Count);
            Assert.Equal("TestConnectionString", iniConfigSrc.Data["DefaultConnection:ConnectionString"]);
            Assert.Equal("SqlClient", iniConfigSrc.Data["DefaultConnection:Provider"]);
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Data["Data:Inventory:ConnectionString"]);
            Assert.Equal("MySql", iniConfigSrc.Data["Data:Inventory:Provider"]);
        }