From d70e6ca06402d0fc785788be94d25267d98b98e3 Mon Sep 17 00:00:00 2001 From: salaros Date: Tue, 19 Jun 2018 20:40:35 +0300 Subject: [PATCH] improvement ConfigParser.GetValue and SetValue methods added ConfigParserSettings.BooleanConverter and covered all these changes with unit tests --- src/ConfigParser.cs | 33 ++++++++++--------- src/ConfigParserSettings.cs | 9 +++++ .../BooleanConverters/YesNoConverter.cs | 12 ++++--- tests/ConfigParserTests.cs | 7 +++- tests/Resources/Values/boolean.ini | 4 +++ 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/ConfigParser.cs b/src/ConfigParser.cs index 8844aa1..3091f12 100644 --- a/src/ConfigParser.cs +++ b/src/ConfigParser.cs @@ -186,6 +186,8 @@ public virtual string GetValue(string sectionName, string keyName, string defaul public virtual bool GetValue(string sectionName, string keyName, bool defaultValue, BooleanConverter booleanConverter = null) { + booleanConverter = booleanConverter ?? Settings.BooleanConverter; + var booleanValue = GetRawValue(sectionName, keyName, null); if (string.IsNullOrWhiteSpace(booleanValue)) { @@ -198,13 +200,6 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal return defaultValue; } - if (booleanConverter != null && booleanConverter.CanConvertFrom(typeof(string))) - { - var value = booleanConverter.ConvertFrom(booleanValue); - if (value is bool convertedBoolean) - return convertedBoolean; - } - #pragma warning disable IDE0046 // Convert to conditional expression foreach (var converter in YesNoBoolConverters) { @@ -216,14 +211,21 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal } #pragma warning restore IDE0046 // Convert to conditional expression - return bool.TryParse(booleanValue, out var parseBoolean) - ? parseBoolean - // if some day Boolean.ToString(IFormatProvider) will work - // https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1 - : true.ToString(Settings.Culture).ToLowerInvariant().Equals( - booleanValue, - StringComparison.InvariantCultureIgnoreCase - ); + 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; } /// @@ -437,6 +439,7 @@ public virtual bool SetValue(string sectionName, string keyName, bool value, } } + booleanConverter = booleanConverter ?? Settings.BooleanConverter; return SetValue(sectionName, keyName, (null == booleanConverter) ? value.ToString(Settings.Culture ?? CultureInfo.InvariantCulture).ToLowerInvariant() : booleanConverter.ConvertToString(value) diff --git a/src/ConfigParserSettings.cs b/src/ConfigParserSettings.cs index b6b22d8..dc134c1 100644 --- a/src/ConfigParserSettings.cs +++ b/src/ConfigParserSettings.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; using System.Linq; using System.Text; @@ -43,6 +44,14 @@ static ConfigParserSettings() /// public Encoding Encoding { get; set; } = null; + /// + /// Gets or sets the boolean converter. + /// + /// + /// The boolean converter. + /// + public BooleanConverter BooleanConverter { get; set; } = null; + /// /// Gets the new line string. /// diff --git a/src/Helpers/BooleanConverters/YesNoConverter.cs b/src/Helpers/BooleanConverters/YesNoConverter.cs index d85f2a5..bbcb9c5 100644 --- a/src/Helpers/BooleanConverters/YesNoConverter.cs +++ b/src/Helpers/BooleanConverters/YesNoConverter.cs @@ -64,11 +64,13 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var booleanText = value as string; - return no.Equals(booleanText, StringComparison.OrdinalIgnoreCase) - ? false - : yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase) - ? true - : base.ConvertFrom(context, culture, value); + if (no.Equals(booleanText, StringComparison.OrdinalIgnoreCase)) + return false; + + if (yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase)) + return true; + + return null; } /// diff --git a/tests/ConfigParserTests.cs b/tests/ConfigParserTests.cs index 6f64073..2a001dd 100644 --- a/tests/ConfigParserTests.cs +++ b/tests/ConfigParserTests.cs @@ -281,10 +281,15 @@ public void BooleanValuesAreParsedCorrectly() Encoding = Encoding.UTF8, // if some day Boolean.ToString(IFormatProvider) will work // https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1 - Culture = new CultureInfo("en-US") + Culture = new CultureInfo("en-US"), + BooleanConverter = new YesNoConverter("vero", "falso") } ); + const string valoriItaliani = "ValoriItaliani"; // [ValoriItaliani] + Assert.True(configFileEnglish.GetValue(valoriItaliani, "positivo", false)); // positivo = vero + Assert.False(configFileEnglish.GetValue(valoriItaliani, "sampleOff", true)); // sampleOff = falso + const string simpleSection = "Simple"; // [Simple] Assert.False(configFileEnglish.GetValue(simpleSection, "empty", false)); // empty= Assert.True(configFileEnglish.GetValue(simpleSection, "numericTrue", false)); // numericTrue=1 diff --git a/tests/Resources/Values/boolean.ini b/tests/Resources/Values/boolean.ini index 5d33cda..b4bd794 100644 --- a/tests/Resources/Values/boolean.ini +++ b/tests/Resources/Values/boolean.ini @@ -16,3 +16,7 @@ sampleOff=Off [EnabledDisabled] sampleOn=Enabled sampleOff=disabled + +[ValoriItaliani] +positivo = vero +sampleOff = falso