Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added integration tests to fix binding bug
  • Loading branch information
mookid8000 committed Sep 10, 2010
1 parent 05de136 commit d2fe989
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/GoCommando.Tests/GoCommando.Tests.csproj
Expand Up @@ -53,6 +53,7 @@
<Compile Include="FixtureBase.cs" />
<Compile Include="Helpers\TestBinder.cs" />
<Compile Include="Helpers\TestHelper.cs" />
<Compile Include="Integration\ErrorTestCases.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
23 changes: 17 additions & 6 deletions src/GoCommando.Tests/Helpers/TestArgParser.cs
Expand Up @@ -5,7 +5,7 @@
using GoCommando.Parameters;
using NUnit.Framework;

namespace GoCommando.Tests
namespace GoCommando.Tests.Helpers
{
[TestFixture]
public class TestArgParser : FixtureBase
Expand Down Expand Up @@ -43,15 +43,15 @@ public void EmptyArrayYieldsNoArguments()
[Test]
public void DoesNotThrowIfItsAFlag()
{
var parameter = parser.Parse(new[]{"/param"}).Cast<NamedCommandLineParameter>().Single();
var parameter = parser.Parse(new[]{"-param"}).Cast<NamedCommandLineParameter>().Single();
Assert.AreEqual("param", parameter.Name);
Assert.AreEqual("True", parameter.Value);
}

[Test]
public void CanParseArrayOfArguments()
{
var args = new []{"some.assembly", "another.assembly", "/param2:bimmelim", "/param1:boom"};
var args = new []{"some.assembly", "another.assembly", "-param2:bimmelim", "-param1:boom"};
var parameters = parser.Parse(args);

Assert.AreEqual(4, parameters.Count);
Expand All @@ -69,8 +69,19 @@ public void CanParseArrayOfArguments()
[Test]
public void ThrowsIfPositionalParametersAreMixedWithNamedParameters()
{
Assert.Throws<FormatException>(() => parser.Parse(new[] {"/named:parameter", "positional.parameter"}));
Assert.Throws<FormatException>(() => parser.Parse(new[] {"/named.flag", "positional.parameter"}));
Assert.Throws<FormatException>(() => parser.Parse(new[] {"-named:parameter", "positional.parameter"}));
Assert.Throws<FormatException>(() => parser.Parse(new[] {"-named.flag", "positional.parameter"}));
}

[Test]
public void AcceptsSlashAsPositionalParameter()
{
var args = new []{"/src/somepath", "/src/anotherpath"};
var parameters = parser.Parse(args);

Assert.AreEqual(2, parameters.Count);
Assert.IsTrue(parameters[0] is PositionalCommandLineParameter);
Assert.IsTrue(parameters[1] is PositionalCommandLineParameter);
}

void AssertYieldsNoParameters(string[] args)
Expand All @@ -82,4 +93,4 @@ void AssertYieldsNoParameters(string[] args)
argsAsString);
}
}
}
}
84 changes: 84 additions & 0 deletions src/GoCommando.Tests/Integration/ErrorTestCases.cs
@@ -0,0 +1,84 @@
using GoCommando.Api;
using GoCommando.Attributes;
using NUnit.Framework;
using DescriptionAttribute = NUnit.Framework.DescriptionAttribute;

namespace GoCommando.Tests.Integration
{
[TestFixture]
public class ErrorTestCases
{
[SetUp]
public void SetUp()
{
CommandoWithTwoArguments.SomePathStatic = null;
CommandoWithTwoArguments.AnotherPathStatic = null;
}

[Test, Description("This error was due to named argument syntax using slash, thus resulting in arguments with forward slash (e.g. an URL) being interpreted as such")]
public void ReproduceError()
{
var errorCode = Go.Run<CommandoWithTwoArguments>(new[] {"/src/somepath", "/src/anotherpath"});

Assert.AreEqual(0, errorCode);
Assert.AreEqual("/src/somepath", CommandoWithTwoArguments.SomePathStatic);
Assert.AreEqual("/src/anotherpath", CommandoWithTwoArguments.AnotherPathStatic);
}

class CommandoWithTwoArguments : ICommando
{
[PositionalArgument]
public string SomePath { get; set; }

[PositionalArgument]
public string AnotherPath { get; set; }

public void Run()
{
SomePathStatic = SomePath;
AnotherPathStatic = AnotherPath;
}

public static string SomePathStatic { get; set; }
public static string AnotherPathStatic { get; set; }
}

[Test, Description("This was the actual parameters resulting in an error")]
public void HowAboutThis()
{
var errorCode = Go.Run<CommandoWithThreeArguments>(new[]
{
@"src\Noder.Test\bin\Debug\Noder.Test.dll",
@"src\Noder.Test\Features\00_basics.feature"
});

Assert.AreEqual(0, errorCode);
Assert.AreEqual(@"src\Noder.Test\bin\Debug\Noder.Test.dll", CommandoWithThreeArguments.SomePathStatic);
Assert.AreEqual(@"src\Noder.Test\Features\00_basics.feature", CommandoWithThreeArguments.AnotherPathStatic);
Assert.IsFalse(CommandoWithThreeArguments.SomeFlagStatic);
}

class CommandoWithThreeArguments : ICommando
{
[PositionalArgument]
public string SomePath { get; set; }

[PositionalArgument]
public string AnotherPath { get; set; }

[NamedArgument("flag", "f")]
public bool SomeFlag { get; set; }

public void Run()
{
SomePathStatic = SomePath;
AnotherPathStatic = AnotherPath;
SomeFlagStatic = SomeFlag;
}

public static string SomePathStatic { get; set; }
public static string AnotherPathStatic { get; set; }
public static bool SomeFlagStatic { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion src/GoCommando/Go.cs
Expand Up @@ -99,7 +99,7 @@ public BindingContext()

static bool RequiredParameterMissing(BindingReport bindingReport)
{
return bindingReport.PropertiesNotBound.Any();
return bindingReport.RequiredPropertiesNotBound.Any();
}

static void ShowHelpText(ICommando commando)
Expand Down
2 changes: 1 addition & 1 deletion src/GoCommando/Helpers/ArgParser.cs
Expand Up @@ -31,7 +31,7 @@ public List<CommandLineParameter> Parse(string[] args)
CommandLineParameter ToCommandLineParameter(string arg, ParserContext context)
{
return
!arg.StartsWith("/")
!arg.StartsWith("-")
? CreatePositionalCommandLineParameter(arg, context)
: CreateNamedCommandLineParameter(arg, context);
}
Expand Down
10 changes: 8 additions & 2 deletions src/GoCommando/Helpers/Binder.cs
Expand Up @@ -35,7 +35,12 @@ public BindingContext()

readonly BindingReport report = new BindingReport();

public int Position { get; set; }
public int Position { get; private set; }

public void IncrementPosition()
{
Position++;
}

public BindingReport Report
{
Expand All @@ -48,7 +53,6 @@ void Bind(object commando, PropertyInfo property, List<CommandLineParameter> par
if (attribute is PositionalArgumentAttribute)
{
BindPositional(commando, property, parameters, (PositionalArgumentAttribute) attribute, context);
context.Position++;
}
else if (attribute is NamedArgumentAttribute)
{
Expand Down Expand Up @@ -98,6 +102,7 @@ void BindPositional(object commando, PropertyInfo property, List<CommandLinePara
if (parameter == null)
{
context.Report.PropertiesNotBound.Add(property);
context.Report.RequiredPropertiesNotBound.Add(property);

if (!attribute.Required) return;

Expand All @@ -107,6 +112,7 @@ void BindPositional(object commando, PropertyInfo property, List<CommandLinePara
property.SetValue(commando, Mutate(parameter, property), null);

context.Report.PropertiesBound.Add(property);
context.IncrementPosition();
}

object Mutate(CommandLineParameter parameter, PropertyInfo property)
Expand Down
6 changes: 6 additions & 0 deletions src/GoCommando/Helpers/BindingReport.cs
Expand Up @@ -5,6 +5,7 @@ namespace GoCommando.Helpers
{
public class BindingReport
{
readonly List<PropertyInfo> requiredPropertiesNotBound = new List<PropertyInfo>();
readonly List<PropertyInfo> propertiesNotBound = new List<PropertyInfo>();
readonly List<PropertyInfo> propertiesBound = new List<PropertyInfo>();

Expand All @@ -17,5 +18,10 @@ public List<PropertyInfo> PropertiesBound
{
get { return propertiesBound; }
}

public List<PropertyInfo> RequiredPropertiesNotBound
{
get { return requiredPropertiesNotBound; }
}
}
}

0 comments on commit d2fe989

Please sign in to comment.