Skip to content

Commit

Permalink
Add tests for flag validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chadmyers committed Apr 19, 2012
1 parent 74b921e commit 18b99c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/FubuCore.Testing/CommandLine/FlagTester.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using FubuCore.CommandLine;
using FubuCore.Conversion;
Expand Down Expand Up @@ -49,6 +50,22 @@ public void flag_is_selectively_optional_if_attribute_states_specific_usages()
forProp(x => x.EnumFlag).OptionalForUsage("b").ShouldBeTrue();
forProp(x => x.EnumFlag).OptionalForUsage("c").ShouldBeFalse();
}

[Test]
public void should_provide_useful_error_message_when_no_value_provided()
{
typeof(InvalidUsageException).ShouldBeThrownBy(() =>
forProp(x => x.AliasFlag).Handle(new FlagTarget(), new Queue<string>(new[] { "-a" })))
.Message.ShouldEqual("No value specified for flag -a.");
}

[Test]
public void should_provide_useful_error_message_for_invalid_enum_value()
{
typeof(InvalidUsageException).ShouldBeThrownBy(() =>
forProp(x => x.EnumFlag).Handle(new FlagTarget(), new Queue<string>(new[] { "-e", "x" })))
.Message.ShouldEqual("'x' is not a valid value for flag [-e, --enum]");
}
}

public enum FlagEnum
Expand Down
2 changes: 1 addition & 1 deletion src/FubuCore/CommandLine/Flag.cs
Expand Up @@ -41,7 +41,7 @@ private void checkEnum(Type propertyType, string rawValue)
{
if( propertyType.CanBeCastTo<Enum>() && !Enum.IsDefined(propertyType, rawValue))
{
throw new InvalidUsageException("'{0}' is not a valid value for argument [{1}]".ToFormat(rawValue, InputParser.ToFlagAliases(_property)));
throw new InvalidUsageException("'{0}' is not a valid value for flag [{1}]".ToFormat(rawValue, InputParser.ToFlagAliases(_property)));
}
}

Expand Down

0 comments on commit 18b99c8

Please sign in to comment.