public void Then_property_is_ignored()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new Settings(100);

            settings.WriteInto(mySettings);

            Assert.AreEqual(100, mySettings.IntValue);
        }
        public void And_writing_for_it_is_enabled_then_property_is_written_into()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithIgnoredProperty(100);

            settings.WriteInto(mySettings);

            Assert.AreEqual(1.1d, mySettings.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);
            }
        }
示例#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 Then_read_only_properties_should_be_skipped()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithReadOnlyProperty();

            settings.WriteInto(mySettings);

            Assert.AreEqual(double.MinValue, mySettings.DoubleValue);
            Assert.AreEqual("abc", mySettings.NonEmptyStringValue);
        }
        public void Then_protected_setters_should_be_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithProtectedSetters();

            settings.WriteInto(mySettings);

            Assert.AreEqual("abc", mySettings.NonEmptyStringValue);
        }
        public void Then_if_conversion_fails_exception_is_thrown()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithInvalidType();

            Assert.Throws<AppSettingException>(() => settings.WriteInto(mySettings));
        }
        public void Then_all_public_properties_should_be_set()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithPublicSetters(null, int.MinValue, 123);

            settings.WriteInto(mySettings);

            Assert.AreEqual("abc", mySettings.NonEmptyStringValue);
            Assert.AreEqual(1, mySettings.IntValue);
            Assert.IsNull(mySettings.EmptyIntValue);
            Assert.AreEqual(1.1d, mySettings.DoubleValue);
        }
        public void Then_SettingName_is_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);

            var mySettings = new SettingsWithSettingsAttribute();
            settings.WriteInto(mySettings);

            Assert.AreEqual(1, mySettings.Value);
        }
        public void Then_CultureName_is_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);

            var mySettings = new SettingsWithCultureName();
            settings.WriteInto(mySettings);

            Assert.AreEqual(1.1d, mySettings.FinnishLocaleDoubleValue);
        }
        public void And_SettigName_is_null_Then_property_name_is_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);

            var mySettings = new SettingsWithNullSettingName();
            settings.WriteInto(mySettings);

            Assert.AreEqual(1, mySettings.IntValue);
        }
        public void And_IsOptional_is_true_and_value_is_not_found_DefaultValue_is_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);

            var mySettings = new SettingWithOptionalNonExistingProperty();
            settings.WriteInto(mySettings);

            Assert.AreEqual(123, mySettings.IntValue);
        }
        public void And_IsConnectionString_is_true_then_setting_is_read_from_connection_strings()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
            var mySettings = new SettingsWithConnectionString();

            settings.WriteInto(mySettings);

            var value = settings.GetConnectionString("MyDatabase");
            Assert.AreEqual(value, mySettings.DatabaseConnectionString);
            Assert.AreEqual("A", mySettings.OptionalConnectionString);
        }
        public void And_CultureName_is_null_Then_InvariantCulture_is_used()
        {
            var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);

            var mySettings = new SettingsWithNullCultureName();
            settings.WriteInto(mySettings);

            // The setting contains value of 1,1 which is valid when using
            // Finnish locale. Since the CultureName is null we are using
            // InvariantCulture which will turn 1,1 into 11.0
            Assert.AreEqual(11.0d, mySettings.FinnishLocaleDoubleValue);
        }