Skip to content

Commit

Permalink
Further tests around Enum support
Browse files Browse the repository at this point in the history
Follows #23

- Test against parsing standard/uppercase/lowercase string values.
- Test to ensure that a custom enum parser is given priority over default one.
- Test that int32 are parsed into enum correctly.
  • Loading branch information
siywilliams committed Nov 6, 2014
1 parent 86aaaf7 commit c726818
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="FluentCommandLineParser\Behaviour\InvalidOptionSetupBehaviour.cs" />
<Compile Include="FluentCommandLineParser\TestContext\TestEnum.cs" />
<Compile Include="FluentCommandLineParser\TestContext\TestException.cs" />
<Compile Include="FluentCommandLineParser\TestContext\FluentCommandLineParserTestContext.cs" />
<Compile Include="FluentCommandLineParser\TestContext\SettingUpALongOptionTestContext.cs" />
Expand All @@ -91,6 +92,8 @@
<Compile Include="FluentCommandLineParser\when_executing_parse_operation\with_options_that_are_specified_in_the_args.cs" />
<Compile Include="Integration\BoolInlineDataAttribute.cs" />
<Compile Include="Integration\DoubleInlineDataAttribute.cs" />
<Compile Include="Integration\EnumInlineDataAttribute.cs" />
<Compile Include="Integration\Int32EnumInlineDataAttribute.cs" />
<Compile Include="Integration\Int32InlineDataAttribute.cs" />
<Compile Include="Integration\IntegrationTests.cs" />
<Compile Include="Integration\Lists\ArgumentInlineDataAttribute.cs" />
Expand All @@ -100,6 +103,7 @@
<Compile Include="Integration\Lists\ListTests.cs" />
<Compile Include="Integration\Lists\StringListInlineDataAttribute.cs" />
<Compile Include="Integration\NCrunchExcelDataAttribute.cs" />
<Compile Include="Integration\SimpleShortOptionsAreParsedCorrectlyAttribute.cs" />
<Compile Include="Integration\StringInlineDataAttribute.cs" />
<Compile Include="Internals\AnonymousMock.cs" />
<Compile Include="Internals\CommandLineOptionGrouperTests.cs" />
Expand Down Expand Up @@ -168,6 +172,9 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#region License
// TestEnum.cs
// Copyright (c) 2014, Simon Williams
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
// d that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#endregion

namespace Fclp.Tests.FluentCommandLineParser
{
public enum TestEnum
{
Value0 = 0,
Value1 = 1
}
}
115 changes: 109 additions & 6 deletions FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using System.Linq;
using Fclp.Internals;
using Fclp.Internals.Errors;
using Fclp.Tests.FluentCommandLineParser;
using Moq;
using NUnit.Framework;

Expand Down Expand Up @@ -289,14 +290,80 @@ public void Ensure_Negative_Double_Can_Be_Specified_With_Unix_Style()

#region Enum Option

enum TestEnum
[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
{
Value0 = 0,
Value1 = 1
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e')
.Callback(val => actual = val);

parser.Parse(new[] { "-e", expected.ToString() });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
{
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e', "enum")
.Callback(val => actual = val);

parser.Parse(new[] { "--enum", expected.ToString() });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Int32_Enum()
{
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e')
.Callback(val => actual = val);

parser.Parse(new[] { "-e", ((int)expected).ToString(CultureInfo.InvariantCulture) });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum()
{
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e', "enum")
.Callback(val => actual = val);

parser.Parse(new[] { "--enum", ((int)expected).ToString(CultureInfo.InvariantCulture) });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Lowercase_String()
{
const TestEnum expected = TestEnum.Value1;

Expand All @@ -314,7 +381,7 @@ public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum_And_Lowercase_String()
{
const TestEnum expected = TestEnum.Value1;

Expand All @@ -323,11 +390,47 @@ public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_
var parser = CreateFluentParser();

parser
.Setup<TestEnum>("e", "enum")
.Setup<TestEnum>('e', "enum")
.Callback(val => actual = val);

parser.Parse(new[] { "--enum", expected.ToString().ToLowerInvariant() });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Uppercase_String()
{
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e')
.Callback(val => actual = val);

parser.Parse(new[] { "-e", expected.ToString().ToUpperInvariant() });

Assert.AreEqual(expected, actual);
}

[Test]
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum_And_Uppercase_String()
{
const TestEnum expected = TestEnum.Value1;

TestEnum actual = TestEnum.Value0;

var parser = CreateFluentParser();

parser
.Setup<TestEnum>('e', "enum")
.Callback(val => actual = val);

parser.Parse(new[] { "--enum", expected.ToString().ToUpperInvariant() });

Assert.AreEqual(expected, actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Xunit.Extensions;

namespace Fclp.Tests.Integration
{
public class BoolInlineDataAttribute : InlineDataAttribute
public class BoolInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
{
public BoolInlineDataAttribute(string args, bool expected)
: base(args, expected, null, null, null)
: base(args, expectedBoolean: expected)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Xunit.Extensions;

namespace Fclp.Tests.Integration
{
public class DoubleInlineDataAttribute : InlineDataAttribute
public class DoubleInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
{
public DoubleInlineDataAttribute(string args, double expected)
: base(args, null, null, null, expected)
: base(args, expectedDouble: expected)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#region License
// EnumInlineDataAttribute.cs
// Copyright (c) 2014, Simon Williams
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
// d that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Fclp.Tests.FluentCommandLineParser;

namespace Fclp.Tests.Integration
{
public class EnumInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
{
public EnumInlineDataAttribute(string args, TestEnum expected)
: base(args, expectedEnum: expected)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#region License
// Int32EnumInlineDataAttribute.cs
// Copyright (c) 2014, Simon Williams
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
// d that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Fclp.Tests.FluentCommandLineParser;

namespace Fclp.Tests.Integration
{
public class Int32EnumInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
{
public Int32EnumInlineDataAttribute(string args, TestEnum expected)
: base(args, expectedEnum: expected)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Xunit.Extensions;

namespace Fclp.Tests.Integration
{
public class Int32InlineDataAttribute : InlineDataAttribute
public class Int32InlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
{
public Int32InlineDataAttribute(string args, int expected)
: base(args, null, null, expected, null)
: base(args, expectedInt32: expected)
{
}
}
Expand Down
13 changes: 12 additions & 1 deletion FluentCommandLineParser.Tests/Integration/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using Fclp.Tests.FluentCommandLineParser;
using Fclp.Tests.Internals;
using Machine.Specifications;
using Xunit;
Expand Down Expand Up @@ -50,24 +51,33 @@ public class IntegrationTests : TestContextBase<Fclp.FluentCommandLineParser>
[DoubleInlineData("-d 123.456", 123.456)]
[DoubleInlineData("-d:123.456", 123.456)]
[DoubleInlineData("-d=123.456", 123.456)]
[Int32EnumInlineData("-e 1", TestEnum.Value1)]
[Int32EnumInlineData("-e:1", TestEnum.Value1)]
[Int32EnumInlineData("-e=1", TestEnum.Value1)]
[EnumInlineData("-e Value1", TestEnum.Value1)]
[EnumInlineData("-e:Value1", TestEnum.Value1)]
[EnumInlineData("-e=Value1", TestEnum.Value1)]
public void SimpleShortOptionsAreParsedCorrectly(
string arguments,
bool? expectedBoolean,
string expectedString,
int? expectedInt32,
double? expectedDouble)
double? expectedDouble,
TestEnum? expectedEnum)
{
sut = new Fclp.FluentCommandLineParser();

bool? actualBoolean = null;
string actualString = null;
int? actualInt32 = null;
double? actualDouble = null;
TestEnum? actualEnum = null;

sut.Setup<bool>('b').Callback(b => actualBoolean = b);
sut.Setup<string>('s').Callback(s => actualString = s);
sut.Setup<int>('i').Callback(i => actualInt32 = i);
sut.Setup<double>('d').Callback(d => actualDouble = d);
sut.Setup<TestEnum>('e').Callback(d => actualEnum = d);

var args = ParseArguments(arguments);

Expand All @@ -79,6 +89,7 @@ public void SimpleShortOptionsAreParsedCorrectly(
actualString.ShouldEqual(expectedString);
actualInt32.ShouldEqual(expectedInt32);
actualDouble.ShouldEqual(expectedDouble);
actualEnum.ShouldEqual(expectedEnum);
}

[Theory]
Expand Down
Loading

0 comments on commit c726818

Please sign in to comment.