public void Then_property_is_ignored()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new Settings(100);

            settings.ReadFrom(mySettings);

            Assert.IsFalse(settings.HasAppSetting("IntValue"));
        }
        public void And_reading_from_it_is_enabled_then_property_is_read_from()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithIgnoredProperty(100);
            mySettings.DoubleValue = 1.1d;

            settings.ReadFrom(mySettings);

            Assert.AreEqual(1.1d, settings.GetValue<double>("DoubleValue"));
        }
        public void Then_read_and_write_should_succeed()
        {
            var fileName = Guid.NewGuid() + ".config";
            var fullPathToConfigurationFile = TestHelpers.GetFullPathToConfigurationFile(fileName);

            try
            {
                var settings = new AppSettings(fullPathToConfigurationFile, FileOption.None);
                var mySettings = new TempSettings()
                    {
                        NonEmptyStringValue = "aaa",
                        IntValue = 123,
                        DoubleValue = 123.12d,
                        LocalizedValue = 456.789
                    };

                settings.ReadFrom(mySettings);

                Assert.AreEqual("aaa", settings.GetValue("NonEmptyStringValue"));
                Assert.IsFalse(settings.HasAppSetting("IntValue"));
                Assert.AreEqual(null, settings.GetValue<int?>("EmptyIntValue"));
                Assert.AreEqual(123.12d, settings.GetValue<double>("DoubleValue"));
                Assert.AreEqual(456.789, settings.GetValue<double>("DoubleFinnishLocale", CultureInfo.GetCultureInfo("fi-FI")));
                Assert.AreEqual("456,789", settings.GetValue("DoubleFinnishLocale"));

                settings.Save();

                var otherSettings = new TempSettings();
                settings.WriteInto(otherSettings);

                Assert.AreEqual("aaa", otherSettings.NonEmptyStringValue);
                Assert.AreEqual(0, otherSettings.IntValue);
                Assert.AreEqual(null, otherSettings.NullableIntValue);
                Assert.AreEqual(123.12d, otherSettings.DoubleValue);
                Assert.AreEqual(456.789, otherSettings.LocalizedValue);
            }
            finally
            {
                TestHelpers.DeleteIfExists(fullPathToConfigurationFile);
            }
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            try
            {
                // Open the default app.config file which in this case is SimpleExample.exe.config
                // and it is located in the same folder as the SimpleExample.exe.
                settings = AppSettings.CreateForAssembly(Assembly.GetEntryAssembly(), FileOption.FileMustExist);

                // Write all the settings into our own object
                var mySettings = new MyApplicationSettings();
                settings.WriteInto(mySettings);

                mySettings.StringValue = new string(mySettings.StringValue.Reverse().ToArray());

                // Read everything back into settings and save
                settings.ReadFrom(mySettings);
                settings.Save();
            }
            catch (Exception exp)
            {
                Console.Error.WriteLine(exp.Message);
                throw;
            }
        }
        public void And_CultureName_is_null_Then_InvariantCulture_is_used()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithNullCultureName() { FinnishLocaleDoubleValue = 1.1d };

            settings.ReadFrom(mySettings);

            // Since we are not using specific locale the InvariantCulture writes
            // the value normally 1.1. If we would have specified fi-FI locale the
            // value would have been 1,1
            Assert.AreEqual("1.1", settings.GetValue("DoubleWithFinnishLocale"));
            Assert.AreEqual(1.1d, settings.GetValue<double>("DoubleWithFinnishLocale"));
        }
        public void And_IsConnectionString_is_true_then_setting_is_saved_as_connection_string()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithConnectionString()
                {
                    DatabaseConnectionString = @"Data Source=localhost;Initial Catalog=MyDb;User Id=username;Password=password;"
                };

            settings.ReadFrom(mySettings);

            var value = settings.GetConnectionString("MyDb");
            Assert.AreEqual(value, mySettings.DatabaseConnectionString);
        }
        public void Then_CultureName_is_used()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithCultureName() { FinnishLocaleDoubleValue = 1.1d };

            settings.ReadFrom(mySettings);

            Assert.AreEqual(1.1d, settings.GetValue<double>("DoubleWithFinnishLocale", CultureInfo.GetCultureInfo("fi-FI")));
            Assert.AreEqual("1,1", settings.GetValue("DoubleWithFinnishLocale"));
        }
        public void Then_SettingName_is_used()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithSettingsAttribute() { Value = 1 };

            settings.ReadFrom(mySettings);

            Assert.AreEqual(1, settings.GetValue<int>("SomeIntValue"));
        }
        public void And_SettigName_is_null_Then_property_name_is_used()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithNullSettingName() { IntValue = 1 };

            settings.ReadFrom(mySettings);

            Assert.AreEqual(1, settings.GetValue<int>("IntValue"));
        }
        public void Then_write_only_properties_should_be_skipped()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithWriteOnlyProperty();
            mySettings.NonEmptyStringValue = "abc";

            settings.ReadFrom(mySettings);

            Assert.AreEqual("abc", settings.GetValue("NonEmptyStringValue"));
            Assert.IsFalse(settings.HasAppSetting("DoubleValue"));
        }
        public void Then_protected_getters_should_be_used()
        {
            var settings = new AppSettings("filename", FileOption.None);
            var mySettings = new SettingsWithProtectedGetters("a");

            settings.ReadFrom(mySettings);

            Assert.AreEqual("a", settings.GetValue("NonEmptyStringValue"));
        }
        public void Then_existing_setting_is_overwritten()
        {
            // Initialize with value "a"
            var settings = new AppSettings("filename", FileOption.None);
            settings.SetValue("NonEmptyStringValue", "a");

            // Overwrite a with b
            var mySettings = new SettingsWithPublicGetters("b", 1, null);

            settings.ReadFrom(mySettings);

            Assert.AreEqual("b", settings.GetValue("NonEmptyStringValue"));
        }
        public void Then_all_public_properties_should_be_get()
        {
            var nonEmptyString = "a";
            var anyInt = 1;

            var settings = new AppSettings("filename", FileOption.None);

            var mySettings = new SettingsWithPublicGetters(nonEmptyString, anyInt, null);
            mySettings.DoubleValue = 1.1d;

            settings.ReadFrom(mySettings);

            Assert.AreEqual(nonEmptyString, settings.GetValue("NonEmptyStringValue"));
            Assert.AreEqual(anyInt, settings.GetValue<int>("IntValue"));
            Assert.AreEqual(null, settings.GetValue<int?>("EmptyIntValue"));
            Assert.AreEqual(1.1d, settings.GetValue<double>("DoubleValue"));
        }