Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made the boolean converter a bit more robust for 'n', 'N', 'No' strin…

…gs. Closes GH-65
  • Loading branch information...
commit eff8cddaf2442d0036473267718cea91f81a2376 1 parent 7a5ce9e
@jeremydmiller jeremydmiller authored
View
8 src/FubuCore.Testing/Binding/BooleanFamilyTester.cs
@@ -5,6 +5,7 @@
using FubuCore.Reflection;
using FubuTestingSupport;
using NUnit.Framework;
+using System.Linq;
namespace FubuCore.Testing.Binding
{
@@ -44,11 +45,10 @@ public class BooleanFamily_can_convert_various_string_inputs_to_boolean
{
private bool WithValue(string value)
{
- return BindingScenario<BooleanTarget>.For(x =>
- {
- x.Data(o => o.IsTrue, value);
+ var scenario = BindingScenario<BooleanTarget>.For(x => { x.Data(o => o.IsTrue, value); });
+ scenario.Problems.Any().ShouldBeFalse();
- }).Model.IsTrue;
+ return scenario.Model.IsTrue;
}
[Test]
View
16 src/FubuCore/Binding/BooleanFamily.cs
@@ -9,8 +9,9 @@ namespace FubuCore.Binding
[Description("Converts to booean values, HTML checkbox friendly conversion")]
public class BooleanFamily : StatelessConverter
{
- private static TypeConverter _converter = TypeDescriptor.GetConverter(typeof(bool));
- private static IList<string> _aliases = new List<string> { "yes", "y" };
+ private static readonly TypeConverter _converter = TypeDescriptor.GetConverter(typeof(bool));
+ private static readonly IList<string> _positives = new List<string> { "yes", "y" };
+ private static readonly IList<string> _negatives = new List<string> { "no", "n" };
public const string CheckboxOn = "on";
public override bool Matches(PropertyInfo property)
@@ -26,10 +27,13 @@ public override object Convert(IPropertyContext context)
var valueString = rawValue.ToString();
- return valueString.Contains(context.Property.Name)
- || valueString.EqualsIgnoreCase(CheckboxOn)
- || _aliases.Any(x => x.Equals(valueString, StringComparison.OrdinalIgnoreCase))
- || (bool)_converter.ConvertFrom(rawValue);
+ if (valueString.IsEmpty()) return false;
+ if (valueString.Contains(context.Property.Name)) return true;
+ if (valueString.EqualsIgnoreCase(CheckboxOn)) return true;
+ if (_positives.Any(x => x.Equals(valueString, StringComparison.OrdinalIgnoreCase))) return true;
+ if (_negatives.Any(x => x.Equals(valueString, StringComparison.OrdinalIgnoreCase))) return false;
+
+ return (bool)_converter.ConvertFrom(rawValue);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.