Skip to content

Commit 784f311

Browse files
committed
Issue #40: Adding support for long
Adding test cases for long and long type list Adding xunit.runner.console nuget package to run the integration tests.
1 parent 2f02152 commit 784f311

12 files changed

+118
-1
lines changed

.nuget/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Machine.Specifications.Runner.Console" version="0.9.0" />
4+
<package id="xunit.runner.console" version="2.0.0" />
45
</packages>

FluentCommandLineParser.Tests/FluentCommandLineParser.Tests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
</Reference>
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="Integration\Int64InlineDataAttribute.cs" />
84+
<Compile Include="Integration\Lists\Int64ListInlineDataAttribute.cs" />
8385
<Compile Include="UriTests.cs" />
8486
<Compile Include="CommandLineOptionFormatterTests.cs" />
8587
<Compile Include="FluentCommandLineParserBuilderTests.cs" />

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,33 @@ public void Ensure_Can_Parse_Mulitple_Arguments_Containing_Negative_Integers_To_
15571557
Assert.IsTrue(actual.Contains(321));
15581558
}
15591559

1560+
[Test]
1561+
public void Ensure_Can_Parse_Arguments_Containing_Long_To_A_List()
1562+
{
1563+
var parser = CreateFluentParser();
1564+
long value1 = 21474836471;
1565+
long value2 = 21474836472;
1566+
long value3 = 214748364723;
1567+
long value4 = 21474836471233;
1568+
1569+
var actual = new List<long>();
1570+
1571+
parser.Setup<List<long>>("longs")
1572+
.Callback(actual.AddRange);
1573+
1574+
var result = parser.Parse(new[] { "--longs", "--", value1.ToString(), value2.ToString(), value3.ToString(), value4.ToString() });
1575+
1576+
Assert.IsFalse(result.HasErrors);
1577+
Assert.IsFalse(result.EmptyArgs);
1578+
Assert.IsFalse(result.HelpCalled);
1579+
1580+
Assert.AreEqual(4, actual.Count());
1581+
Assert.IsTrue(actual.Contains(value1));
1582+
Assert.IsTrue(actual.Contains(value2));
1583+
Assert.IsTrue(actual.Contains(value3));
1584+
Assert.IsTrue(actual.Contains(value4));
1585+
}
1586+
15601587
#endregion
15611588

15621589
#endregion Top Level Tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Fclp.Tests.Integration
2+
{
3+
public class Int64InlineDataAttribute : SimpleShortOptionsAreParsedCorrectlyAttribute
4+
{
5+
public Int64InlineDataAttribute(string args, long expected)
6+
: base(args, expectedInt64: expected)
7+
{
8+
}
9+
}
10+
}

FluentCommandLineParser.Tests/Integration/IntegrationTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class IntegrationTests : TestContextBase<Fclp.FluentCommandLineParser>
5656
[Int32InlineData("-i 123", 123)]
5757
[Int32InlineData("-i:123", 123)]
5858
[Int32InlineData("-i=123", 123)]
59+
[Int64InlineData("-l 2147483649", 2147483649)]
60+
[Int64InlineData("-l:2147483649", 2147483649)]
61+
[Int64InlineData("-l=2147483649", 2147483649)]
5962
[DoubleInlineData("-d 123.456", 123.456)]
6063
[DoubleInlineData("-d:123.456", 123.456)]
6164
[DoubleInlineData("-d=123.456", 123.456)]
@@ -70,6 +73,7 @@ public void SimpleShortOptionsAreParsedCorrectly(
7073
bool? expectedBoolean,
7174
string expectedString,
7275
int? expectedInt32,
76+
long? expectedInt64,
7377
double? expectedDouble,
7478
TestEnum? expectedEnum)
7579
{
@@ -78,12 +82,14 @@ public void SimpleShortOptionsAreParsedCorrectly(
7882
bool? actualBoolean = null;
7983
string actualString = null;
8084
int? actualInt32 = null;
85+
long? actualInt64 = null;
8186
double? actualDouble = null;
8287
TestEnum? actualEnum = null;
8388

8489
sut.Setup<bool>('b').Callback(b => actualBoolean = b);
8590
sut.Setup<string>('s').Callback(s => actualString = s);
8691
sut.Setup<int>('i').Callback(i => actualInt32 = i);
92+
sut.Setup<long>('l').Callback(l => actualInt64 = l);
8793
sut.Setup<double>('d').Callback(d => actualDouble = d);
8894
sut.Setup<TestEnum>('e').Callback(d => actualEnum = d);
8995

@@ -96,6 +102,7 @@ public void SimpleShortOptionsAreParsedCorrectly(
96102
actualBoolean.ShouldEqual(expectedBoolean);
97103
actualString.ShouldEqual(expectedString);
98104
actualInt32.ShouldEqual(expectedInt32);
105+
actualInt64.ShouldEqual(expectedInt64);
99106
actualDouble.ShouldEqual(expectedDouble);
100107
actualEnum.ShouldEqual(expectedEnum);
101108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Fclp.Tests.Integration
2+
{
3+
public class Int64ListInlineDataAttribute : ArgumentInlineDataAttribute
4+
{
5+
public Int64ListInlineDataAttribute(string args, params long[] listItems)
6+
: base(args, listItems)
7+
{
8+
}
9+
}
10+
}

FluentCommandLineParser.Tests/Integration/Lists/ListTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public void should_create_list_with_expected_int32_items(string arguments, IEnum
6262
should_contain_list_with_expected_items(arguments, expectedItems);
6363
}
6464

65+
[Theory]
66+
[Int64ListInlineData("--list 2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
67+
[Int64ListInlineData("-list 2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
68+
[Int64ListInlineData("/list 2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
69+
[Int64ListInlineData("/list:2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
70+
[Int64ListInlineData("/list=2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
71+
[Int64ListInlineData("--list:2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
72+
[Int64ListInlineData("--list=2147483650 3147483651 4147483652", 2147483650, 3147483651, 4147483652)]
73+
public void should_create_list_with_expected_int64_items(string arguments, IEnumerable<long> expectedItems)
74+
{
75+
should_contain_list_with_expected_items(arguments, expectedItems);
76+
}
77+
6578
[Theory]
6679
[DoubleListInlineData("--list 123.456 321.987 098.123465", 123.456, 321.987, 098.123465)]
6780
[DoubleListInlineData("-list 123.456 321.987 098.123465", 123.456, 321.987, 098.123465)]

FluentCommandLineParser.Tests/Integration/SimpleShortOptionsAreParsedCorrectlyAttribute.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public SimpleShortOptionsAreParsedCorrectlyAttribute(
3434
bool? expectedBoolean = null,
3535
string expectedString = null,
3636
int? expectedInt32 = null,
37+
long? expectedInt64 = null,
3738
double? expectedDouble = null,
3839
TestEnum? expectedEnum = null)
39-
: base(arguments, expectedBoolean, expectedString, expectedInt32, expectedDouble, expectedEnum)
40+
: base(arguments, expectedBoolean, expectedString, expectedInt32, expectedInt64, expectedDouble, expectedEnum)
4041
{
4142

4243
}

FluentCommandLineParser.Tests/Internals/CommandLineOptionParserFactoryTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,29 @@ public void Ensure_Factory_Supports_Out_Of_The_Box_Parsers()
9696

9797
var stringParser = factory.CreateParser<string>();
9898
var int32Parser = factory.CreateParser<int>();
99+
var int64Parser = factory.CreateParser<long>();
99100
var doubleParser = factory.CreateParser<double>();
100101
var dtParser = factory.CreateParser<DateTime>();
101102
var boolParser = factory.CreateParser<bool>();
102103

103104
Assert.IsInstanceOf<StringCommandLineOptionParser>(stringParser);
104105
Assert.IsInstanceOf<Int32CommandLineOptionParser>(int32Parser);
106+
Assert.IsInstanceOf<Int64CommandLineOptionParser>(int64Parser);
105107
Assert.IsInstanceOf<DoubleCommandLineOptionParser>(doubleParser);
106108
Assert.IsInstanceOf<DateTimeCommandLineOptionParser>(dtParser);
107109
Assert.IsInstanceOf<BoolCommandLineOptionParser>(boolParser);
108110
}
109111

112+
[Test]
113+
public void Ensure_Factory_Supports_List_Of_Int64()
114+
{
115+
var factory = new CommandLineOptionParserFactory();
116+
117+
var int64ListParser = factory.CreateParser<System.Collections.Generic.List<long>>();
118+
119+
Assert.IsInstanceOf<ListCommandLineOptionParser<long>>(int64ListParser);
120+
}
121+
110122
[Test]
111123
public void Ensure_Factory_Supports_Enum()
112124
{

FluentCommandLineParser/FluentCommandLineParser.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Internals\Parsing\ICommandLineParserEngine.cs" />
8888
<Compile Include="Internals\Parsing\OptionArgumentParser.cs" />
8989
<Compile Include="Internals\Parsing\CommandLineOptionGrouper.cs" />
90+
<Compile Include="Internals\Parsing\OptionParsers\Int64CommandLineOptionParser.cs" />
9091
<Compile Include="Internals\Parsing\OptionParsers\NullableCommandLineOptionParser.cs" />
9192
<Compile Include="Internals\Parsing\OptionParsers\UriCommandLineOptionParser.cs" />
9293
<Compile Include="Internals\Parsing\OptionParsers\BoolCommandLineOptionParser.cs" />

FluentCommandLineParser/Internals/Parsing/OptionParsers/CommandLineOptionParserFactory.cs

+2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ public CommandLineOptionParserFactory()
4040
this.Parsers = new Dictionary<Type, object>();
4141
this.AddOrReplace(new BoolCommandLineOptionParser());
4242
this.AddOrReplace(new Int32CommandLineOptionParser());
43+
this.AddOrReplace(new Int64CommandLineOptionParser());
4344
this.AddOrReplace(new StringCommandLineOptionParser());
4445
this.AddOrReplace(new DateTimeCommandLineOptionParser());
4546
this.AddOrReplace(new DoubleCommandLineOptionParser());
4647
this.AddOrReplace(new UriCommandLineOptionParser());
4748
this.AddOrReplace(new ListCommandLineOptionParser<string>(this));
4849
this.AddOrReplace(new ListCommandLineOptionParser<int>(this));
50+
this.AddOrReplace(new ListCommandLineOptionParser<long>(this));
4951
this.AddOrReplace(new ListCommandLineOptionParser<double>(this));
5052
this.AddOrReplace(new ListCommandLineOptionParser<DateTime>(this));
5153
this.AddOrReplace(new ListCommandLineOptionParser<bool>(this));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Globalization;
2+
3+
namespace Fclp.Internals.Parsing.OptionParsers
4+
{
5+
/// <summary>
6+
/// Parser used to convert to <see cref="System.Int64"/>.
7+
/// </summary>
8+
public class Int64CommandLineOptionParser : ICommandLineOptionParser<long>
9+
{
10+
/// <summary>
11+
/// Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.
12+
/// </summary>
13+
/// <param name="parsedOption"></param>
14+
/// <returns></returns>
15+
public long Parse(ParsedOption parsedOption)
16+
{
17+
return long.Parse(parsedOption.Value, CultureInfo.CurrentCulture);
18+
}
19+
20+
/// <summary>
21+
/// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>.
22+
/// </summary>
23+
/// <param name="parsedOption"></param>
24+
/// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns>
25+
public bool CanParse(ParsedOption parsedOption)
26+
{
27+
long result;
28+
return long.TryParse(parsedOption.Value, out result);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)