public void ConvertToStringTest()
        {
            var converter = new BooleanConverter();

            Assert.AreEqual("True", converter.ConvertToString(CultureInfo.CurrentCulture, true));

            Assert.AreEqual("False", converter.ConvertToString(CultureInfo.CurrentCulture, false));

            Assert.AreEqual("", converter.ConvertToString(CultureInfo.CurrentCulture, null));
            Assert.AreEqual("1", converter.ConvertToString(CultureInfo.CurrentCulture, 1));
        }
        public void ConvertToStringTest()
        {
            var converter = new BooleanConverter();

            Assert.Equal("True", converter.ConvertToString(true));

            Assert.Equal("False", converter.ConvertToString(false));

            Assert.Equal("", converter.ConvertToString(null));
            Assert.Equal("1", converter.ConvertToString(1));
        }
        public void ConvertToStringTest()
        {
            var converter            = new BooleanConverter();
            var typeConverterOptions = new TypeConverterOptions
            {
                CultureInfo = CultureInfo.CurrentCulture
            };

            Assert.AreEqual("True", converter.ConvertToString(typeConverterOptions, true));

            Assert.AreEqual("False", converter.ConvertToString(typeConverterOptions, false));

            Assert.AreEqual("", converter.ConvertToString(typeConverterOptions, null));
            Assert.AreEqual("1", converter.ConvertToString(typeConverterOptions, 1));
        }
Beispiel #4
0
        public void ConvertToStringTest()
        {
            var converter = new BooleanConverter();

            var propertyMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            Assert.AreEqual("True", converter.ConvertToString(true, null, propertyMapData));

            Assert.AreEqual("False", converter.ConvertToString(false, null, propertyMapData));

            Assert.AreEqual("", converter.ConvertToString(null, null, propertyMapData));
            Assert.AreEqual("1", converter.ConvertToString(1, null, propertyMapData));
        }
Beispiel #5
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
        /// <param name="booleanConverter">The boolean converter.</param>
        /// <returns></returns>
        public virtual bool GetValue(string sectionName, string keyName, bool defaultValue,
                                     BooleanConverter booleanConverter = null)
        {
            booleanConverter = booleanConverter ?? Settings.BooleanConverter;

            var booleanValue = GetRawValue <string>(sectionName, keyName, null);

            if (string.IsNullOrWhiteSpace(booleanValue))
            {
                SetValue(sectionName, keyName,
                         null == booleanConverter
                         // if some day Boolean.ToString(IFormatProvider) will work
                         // https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
                        ? defaultValue.ToString(Settings.Culture).ToLowerInvariant()
                        : booleanConverter.ConvertToString(defaultValue));
                return(defaultValue);
            }

#pragma warning disable IDE0046 // Convert to conditional expression
            foreach (var converter in YesNoBoolConverters)
            {
                if (converter.Yes.Equals(booleanValue.Trim(), StringComparison.InvariantCultureIgnoreCase) ||
                    converter.No.Equals(booleanValue.Trim(), StringComparison.InvariantCultureIgnoreCase))
                {
                    return(converter.Yes.Equals(booleanValue, StringComparison.InvariantCultureIgnoreCase));
                }
            }
#pragma warning restore IDE0046 // Convert to conditional expression

            if (bool.TryParse(booleanValue, out var parseBoolean))
            {
                return(parseBoolean);
            }

            // if some day Boolean.ToString(IFormatProvider) will work
            // https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
            if (true.ToString(Settings.Culture).ToLowerInvariant().Equals(booleanValue, StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            if (booleanConverter == null || !booleanConverter.CanConvertFrom(typeof(string)))
            {
                return(defaultValue);
            }

            var value = booleanConverter.ConvertFrom(booleanValue);
            return(value is bool convertedBoolean
                ? convertedBoolean
                : defaultValue);
        }
Beispiel #6
0
        public void ConvertToString_SingleBooleanFalseValue_UsesValue()
        {
            var converter     = new BooleanConverter();
            var memberMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions =
                {
                    BooleanFalseValues = { "Foo" },
                },
            };

            var value = converter.ConvertToString(false, null, memberMapData);

            Assert.Equal("Foo", value);
        }
Beispiel #7
0
        public void ConvertToString_MultipleBooleanTrueValues_UsesFirstValue()
        {
            var converter     = new BooleanConverter();
            var memberMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions =
                {
                    BooleanTrueValues = { "Foo", "Bar" },
                },
            };

            var value = converter.ConvertToString(true, null, memberMapData);

            Assert.Equal("Foo", value);
        }
Beispiel #8
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        /// <param name="booleanConverter">The boolean converter.</param>
        /// <returns></returns>
        public virtual bool SetValue(string sectionName, string keyName, bool value,
                                     BooleanConverter booleanConverter = null)
        {
            var booleanValue = GetRawValue <string>(sectionName, keyName, null);

            // ReSharper disable once InvertIf
            if (!string.IsNullOrWhiteSpace(booleanValue))
            {
                // Check if current value matches one of boolean converters
                foreach (var converter in YesNoBoolConverters)
                {
                    if (Equals(converter.Yes, booleanValue.Trim()) || Equals(converter.No, booleanValue.Trim()))
                    {
                        return(SetValue(sectionName, keyName, value ? converter.Yes : converter.No));
                    }
                }
            }

            booleanConverter = booleanConverter ?? Settings.BooleanConverter;
            return(SetValue(sectionName, keyName, (null == booleanConverter)
                ? value.ToString(Settings.Culture ?? CultureInfo.InvariantCulture).ToLowerInvariant()
                : booleanConverter.ConvertToString(value)
                            ));
        }