Skip to content

Commit c726818

Browse files
committed
Further tests around Enum support
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.
1 parent 86aaaf7 commit c726818

12 files changed

+320
-23
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParser.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<SubType>Code</SubType>
8484
</Compile>
8585
<Compile Include="FluentCommandLineParser\Behaviour\InvalidOptionSetupBehaviour.cs" />
86+
<Compile Include="FluentCommandLineParser\TestContext\TestEnum.cs" />
8687
<Compile Include="FluentCommandLineParser\TestContext\TestException.cs" />
8788
<Compile Include="FluentCommandLineParser\TestContext\FluentCommandLineParserTestContext.cs" />
8889
<Compile Include="FluentCommandLineParser\TestContext\SettingUpALongOptionTestContext.cs" />
@@ -91,6 +92,8 @@
9192
<Compile Include="FluentCommandLineParser\when_executing_parse_operation\with_options_that_are_specified_in_the_args.cs" />
9293
<Compile Include="Integration\BoolInlineDataAttribute.cs" />
9394
<Compile Include="Integration\DoubleInlineDataAttribute.cs" />
95+
<Compile Include="Integration\EnumInlineDataAttribute.cs" />
96+
<Compile Include="Integration\Int32EnumInlineDataAttribute.cs" />
9497
<Compile Include="Integration\Int32InlineDataAttribute.cs" />
9598
<Compile Include="Integration\IntegrationTests.cs" />
9699
<Compile Include="Integration\Lists\ArgumentInlineDataAttribute.cs" />
@@ -100,6 +103,7 @@
100103
<Compile Include="Integration\Lists\ListTests.cs" />
101104
<Compile Include="Integration\Lists\StringListInlineDataAttribute.cs" />
102105
<Compile Include="Integration\NCrunchExcelDataAttribute.cs" />
106+
<Compile Include="Integration\SimpleShortOptionsAreParsedCorrectlyAttribute.cs" />
103107
<Compile Include="Integration\StringInlineDataAttribute.cs" />
104108
<Compile Include="Internals\AnonymousMock.cs" />
105109
<Compile Include="Internals\CommandLineOptionGrouperTests.cs" />
@@ -168,6 +172,9 @@
168172
<SubType>Designer</SubType>
169173
</None>
170174
</ItemGroup>
175+
<ItemGroup>
176+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
177+
</ItemGroup>
171178
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
172179
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
173180
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#region License
2+
// TestEnum.cs
3+
// Copyright (c) 2014, Simon Williams
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
7+
// d that the following conditions are met:
8+
//
9+
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
10+
// following disclaimer.
11+
//
12+
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+
// the following disclaimer in the documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
// POSSIBILITY OF SUCH DAMAGE.
23+
#endregion
24+
25+
namespace Fclp.Tests.FluentCommandLineParser
26+
{
27+
public enum TestEnum
28+
{
29+
Value0 = 0,
30+
Value1 = 1
31+
}
32+
}

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

Lines changed: 109 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Linq;
2929
using Fclp.Internals;
3030
using Fclp.Internals.Errors;
31+
using Fclp.Tests.FluentCommandLineParser;
3132
using Moq;
3233
using NUnit.Framework;
3334

@@ -289,14 +290,80 @@ public void Ensure_Negative_Double_Can_Be_Specified_With_Unix_Style()
289290

290291
#region Enum Option
291292

292-
enum TestEnum
293+
[Test]
294+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
293295
{
294-
Value0 = 0,
295-
Value1 = 1
296+
const TestEnum expected = TestEnum.Value1;
297+
298+
TestEnum actual = TestEnum.Value0;
299+
300+
var parser = CreateFluentParser();
301+
302+
parser
303+
.Setup<TestEnum>('e')
304+
.Callback(val => actual = val);
305+
306+
parser.Parse(new[] { "-e", expected.ToString() });
307+
308+
Assert.AreEqual(expected, actual);
309+
}
310+
311+
[Test]
312+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
313+
{
314+
const TestEnum expected = TestEnum.Value1;
315+
316+
TestEnum actual = TestEnum.Value0;
317+
318+
var parser = CreateFluentParser();
319+
320+
parser
321+
.Setup<TestEnum>('e', "enum")
322+
.Callback(val => actual = val);
323+
324+
parser.Parse(new[] { "--enum", expected.ToString() });
325+
326+
Assert.AreEqual(expected, actual);
296327
}
297328

298329
[Test]
299-
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
330+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Int32_Enum()
331+
{
332+
const TestEnum expected = TestEnum.Value1;
333+
334+
TestEnum actual = TestEnum.Value0;
335+
336+
var parser = CreateFluentParser();
337+
338+
parser
339+
.Setup<TestEnum>('e')
340+
.Callback(val => actual = val);
341+
342+
parser.Parse(new[] { "-e", ((int)expected).ToString(CultureInfo.InvariantCulture) });
343+
344+
Assert.AreEqual(expected, actual);
345+
}
346+
347+
[Test]
348+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum()
349+
{
350+
const TestEnum expected = TestEnum.Value1;
351+
352+
TestEnum actual = TestEnum.Value0;
353+
354+
var parser = CreateFluentParser();
355+
356+
parser
357+
.Setup<TestEnum>('e', "enum")
358+
.Callback(val => actual = val);
359+
360+
parser.Parse(new[] { "--enum", ((int)expected).ToString(CultureInfo.InvariantCulture) });
361+
362+
Assert.AreEqual(expected, actual);
363+
}
364+
365+
[Test]
366+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Lowercase_String()
300367
{
301368
const TestEnum expected = TestEnum.Value1;
302369

@@ -314,7 +381,7 @@ public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short
314381
}
315382

316383
[Test]
317-
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
384+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum_And_Lowercase_String()
318385
{
319386
const TestEnum expected = TestEnum.Value1;
320387

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

325392
parser
326-
.Setup<TestEnum>("e", "enum")
393+
.Setup<TestEnum>('e', "enum")
327394
.Callback(val => actual = val);
328395

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

398+
Assert.AreEqual(expected, actual);
399+
}
400+
401+
[Test]
402+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option_And_Uppercase_String()
403+
{
404+
const TestEnum expected = TestEnum.Value1;
405+
406+
TestEnum actual = TestEnum.Value0;
407+
408+
var parser = CreateFluentParser();
409+
410+
parser
411+
.Setup<TestEnum>('e')
412+
.Callback(val => actual = val);
413+
414+
parser.Parse(new[] { "-e", expected.ToString().ToUpperInvariant() });
415+
416+
Assert.AreEqual(expected, actual);
417+
}
418+
419+
[Test]
420+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option_And_Int32_Enum_And_Uppercase_String()
421+
{
422+
const TestEnum expected = TestEnum.Value1;
423+
424+
TestEnum actual = TestEnum.Value0;
425+
426+
var parser = CreateFluentParser();
427+
428+
parser
429+
.Setup<TestEnum>('e', "enum")
430+
.Callback(val => actual = val);
431+
432+
parser.Parse(new[] { "--enum", expected.ToString().ToUpperInvariant() });
433+
331434
Assert.AreEqual(expected, actual);
332435
}
333436

FluentCommandLineParser.Tests/Integration/BoolInlineDataAttribute.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25-
using Xunit.Extensions;
26-
2725
namespace Fclp.Tests.Integration
2826
{
29-
public class BoolInlineDataAttribute : InlineDataAttribute
27+
public class BoolInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
3028
{
3129
public BoolInlineDataAttribute(string args, bool expected)
32-
: base(args, expected, null, null, null)
30+
: base(args, expectedBoolean: expected)
3331
{
3432
}
3533
}

FluentCommandLineParser.Tests/Integration/DoubleInlineDataAttribute.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25-
using Xunit.Extensions;
26-
2725
namespace Fclp.Tests.Integration
2826
{
29-
public class DoubleInlineDataAttribute : InlineDataAttribute
27+
public class DoubleInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
3028
{
3129
public DoubleInlineDataAttribute(string args, double expected)
32-
: base(args, null, null, null, expected)
30+
: base(args, expectedDouble: expected)
3331
{
3432
}
3533
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#region License
2+
// EnumInlineDataAttribute.cs
3+
// Copyright (c) 2014, Simon Williams
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
7+
// d that the following conditions are met:
8+
//
9+
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
10+
// following disclaimer.
11+
//
12+
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+
// the following disclaimer in the documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
// POSSIBILITY OF SUCH DAMAGE.
23+
#endregion
24+
25+
using Fclp.Tests.FluentCommandLineParser;
26+
27+
namespace Fclp.Tests.Integration
28+
{
29+
public class EnumInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
30+
{
31+
public EnumInlineDataAttribute(string args, TestEnum expected)
32+
: base(args, expectedEnum: expected)
33+
{
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#region License
2+
// Int32EnumInlineDataAttribute.cs
3+
// Copyright (c) 2014, Simon Williams
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
7+
// d that the following conditions are met:
8+
//
9+
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
10+
// following disclaimer.
11+
//
12+
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+
// the following disclaimer in the documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
// POSSIBILITY OF SUCH DAMAGE.
23+
#endregion
24+
25+
using Fclp.Tests.FluentCommandLineParser;
26+
27+
namespace Fclp.Tests.Integration
28+
{
29+
public class Int32EnumInlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
30+
{
31+
public Int32EnumInlineDataAttribute(string args, TestEnum expected)
32+
: base(args, expectedEnum: expected)
33+
{
34+
}
35+
}
36+
}

FluentCommandLineParser.Tests/Integration/Int32InlineDataAttribute.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25-
using Xunit.Extensions;
26-
2725
namespace Fclp.Tests.Integration
2826
{
29-
public class Int32InlineDataAttribute : InlineDataAttribute
27+
public class Int32InlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
3028
{
3129
public Int32InlineDataAttribute(string args, int expected)
32-
: base(args, null, null, expected, null)
30+
: base(args, expectedInt32: expected)
3331
{
3432
}
3533
}

FluentCommandLineParser.Tests/Integration/IntegrationTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25+
using Fclp.Tests.FluentCommandLineParser;
2526
using Fclp.Tests.Internals;
2627
using Machine.Specifications;
2728
using Xunit;
@@ -50,24 +51,33 @@ public class IntegrationTests : TestContextBase<Fclp.FluentCommandLineParser>
5051
[DoubleInlineData("-d 123.456", 123.456)]
5152
[DoubleInlineData("-d:123.456", 123.456)]
5253
[DoubleInlineData("-d=123.456", 123.456)]
54+
[Int32EnumInlineData("-e 1", TestEnum.Value1)]
55+
[Int32EnumInlineData("-e:1", TestEnum.Value1)]
56+
[Int32EnumInlineData("-e=1", TestEnum.Value1)]
57+
[EnumInlineData("-e Value1", TestEnum.Value1)]
58+
[EnumInlineData("-e:Value1", TestEnum.Value1)]
59+
[EnumInlineData("-e=Value1", TestEnum.Value1)]
5360
public void SimpleShortOptionsAreParsedCorrectly(
5461
string arguments,
5562
bool? expectedBoolean,
5663
string expectedString,
5764
int? expectedInt32,
58-
double? expectedDouble)
65+
double? expectedDouble,
66+
TestEnum? expectedEnum)
5967
{
6068
sut = new Fclp.FluentCommandLineParser();
6169

6270
bool? actualBoolean = null;
6371
string actualString = null;
6472
int? actualInt32 = null;
6573
double? actualDouble = null;
74+
TestEnum? actualEnum = null;
6675

6776
sut.Setup<bool>('b').Callback(b => actualBoolean = b);
6877
sut.Setup<string>('s').Callback(s => actualString = s);
6978
sut.Setup<int>('i').Callback(i => actualInt32 = i);
7079
sut.Setup<double>('d').Callback(d => actualDouble = d);
80+
sut.Setup<TestEnum>('e').Callback(d => actualEnum = d);
7181

7282
var args = ParseArguments(arguments);
7383

@@ -79,6 +89,7 @@ public void SimpleShortOptionsAreParsedCorrectly(
7989
actualString.ShouldEqual(expectedString);
8090
actualInt32.ShouldEqual(expectedInt32);
8191
actualDouble.ShouldEqual(expectedDouble);
92+
actualEnum.ShouldEqual(expectedEnum);
8293
}
8394

8495
[Theory]

0 commit comments

Comments
 (0)