Permalink
Browse files

BooleanFamily supports y/n syntax

  • Loading branch information...
jmarnold committed Nov 20, 2012
1 parent 3f89927 commit 4254574e324130414a91257a55b1f070a5a8f9cd
Showing with 22 additions and 0 deletions.
  1. +18 −0 src/FubuCore.Testing/Binding/BooleanFamilyTester.cs
  2. +4 −0 src/FubuCore/Binding/BooleanFamily.cs
@@ -74,6 +74,24 @@ public void treats_on_string_as_true()
WithValue("ON").ShouldBeTrue();
}
+ [Test]
+ public void treats_no_string_as_false()
+ {
+ WithValue("no").ShouldBeFalse();
+ WithValue("NO").ShouldBeFalse();
+ WithValue("n").ShouldBeFalse();
+ WithValue("N").ShouldBeFalse();
+ }
+
+ [Test]
+ public void treats_yes_string_as_true()
+ {
+ WithValue("yes").ShouldBeTrue();
+ WithValue("YES").ShouldBeTrue();
+ WithValue("y").ShouldBeTrue();
+ WithValue("Y").ShouldBeTrue();
+ }
+
[Test]
public void treats_empty_string_as_false()
{
@@ -1,5 +1,7 @@
using System;
+using System.Collections.Generic;
using System.ComponentModel;
+using System.Linq;
using System.Reflection;
namespace FubuCore.Binding
@@ -8,6 +10,7 @@ namespace FubuCore.Binding
public class BooleanFamily : StatelessConverter
{
private static TypeConverter _converter = TypeDescriptor.GetConverter(typeof(bool));
+ private static IList<string> _aliases = new List<string> { "yes", "y" };
public const string CheckboxOn = "on";
public override bool Matches(PropertyInfo property)
@@ -25,6 +28,7 @@ public override object Convert(IPropertyContext context)
return valueString.Contains(context.Property.Name)
|| valueString.EqualsIgnoreCase(CheckboxOn)
+ || _aliases.Any(x => x.Equals(valueString, StringComparison.OrdinalIgnoreCase))
|| (bool)_converter.ConvertFrom(rawValue);
}
}

0 comments on commit 4254574

Please sign in to comment.