Skip to content

Commit 22547f1

Browse files
committed
Restructured parsing code into smaller, more focused methods. Renamed various methods to distinguish between allocating and parsing. Reorganized files. Added tests.
1 parent 5540680 commit 22547f1

35 files changed

+584
-496
lines changed

FancyHelpError/FancyConsole.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public void AddOption( List<string> keys, string? description = null, string? de
110110
} );
111111
}
112112

113-
public void Display() => ConsoleRenderer.RenderDocument( _document );
113+
public void Display()
114+
{
115+
ConsoleRenderer.RenderDocument( _document );
116+
Initialize();
117+
}
114118
}
115119
}

J4JCommandLine.Tests/ArrayPropertyTest.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ public void root_properties(
2323
var option = target!.Bind(x => x.IntArray, "x");
2424
var unkeyed = target!.BindUnkeyed(x => x.Unkeyed);
2525

26-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value, optValue,
27-
unkeyedValues);
26+
ValidateSetup(target, option, unkeyed, required);
27+
28+
target.Parse( new string[] { cmdLine } ).Should().Be( result );
29+
30+
target.Value.IntArray.Should().BeEquivalentTo(optValue);
31+
target.Value.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
2832
}
2933

3034
[Theory]
@@ -43,8 +47,12 @@ public void parameterless_properties(
4347
var option = target!.Bind(x => x.TestProperties.IntArray, "x");
4448
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
4549

46-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
47-
unkeyedValues);
50+
ValidateSetup(target, option, unkeyed, required);
51+
52+
target.Parse(new string[] { cmdLine }).Should().Be(result);
53+
54+
target.Value.TestProperties.IntArray.Should().BeEquivalentTo(optValue);
55+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
4856
}
4957

5058
[Theory]
@@ -67,20 +75,15 @@ public void parametered_properties(
6775
var option = target!.Bind(x => x.TestProperties.IntArray, "x");
6876
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
6977

70-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
71-
unkeyedValues);
78+
ValidateSetup(target, option, unkeyed, required);
79+
80+
target.Parse(new string[] { cmdLine }).Should().Be(result);
81+
82+
target.Value.TestProperties.IntArray.Should().BeEquivalentTo(optValue);
83+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
7284
}
7385

74-
private void ProcessTest<T>(
75-
string cmdLine,
76-
bool required,
77-
BindingTarget<T> target,
78-
Option option,
79-
Option unkeyed,
80-
bool desiredParseResult,
81-
Func<TestProperties> results,
82-
int[] optValue,
83-
int[] unkeyedValues)
86+
private void ValidateSetup<T>( BindingTarget<T> target, Option option, Option unkeyed, bool required )
8487
where T : class
8588
{
8689
target.Should().NotBeNull();
@@ -90,12 +93,6 @@ private void ProcessTest<T>(
9093

9194
if (required)
9295
option.Required();
93-
94-
var parseResults = target.Parse(new string[] { cmdLine });
95-
parseResults.Should().Be(desiredParseResult);
96-
97-
results().IntArray.Should().BeEquivalentTo(optValue);
98-
results().Unkeyed.Should().BeEquivalentTo(unkeyedValues);
9996
}
10097
}
10198
}

J4JCommandLine.Tests/HelpErrorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected class RootProperties
1818
}
1919

2020
[ Theory ]
21-
[ InlineData( "h", true ) ]
21+
[ InlineData( "h", false ) ]
2222
public void Trigger_help(
2323
string key,
2424
bool result )

J4JCommandLine.Tests/ListPropertyTest.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ public void root_properties(
2323
var option = target!.Bind(x => x.IntList, "x");
2424
var unkeyed = target!.BindUnkeyed(x => x.Unkeyed);
2525

26-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value, optValue,
27-
unkeyedValues);
26+
ValidateSetup(target, option, unkeyed, required);
27+
28+
target.Parse( new string[] { cmdLine } ).Should().Be( result );
29+
30+
target.Value.IntList.Should().BeEquivalentTo(optValue);
31+
target.Value.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
2832
}
2933

3034
[Theory]
@@ -43,8 +47,12 @@ public void parameterless_properties(
4347
var option = target!.Bind(x => x.TestProperties.IntList, "x");
4448
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
4549

46-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
47-
unkeyedValues);
50+
ValidateSetup(target, option, unkeyed, required);
51+
52+
target.Parse(new string[] { cmdLine }).Should().Be(result);
53+
54+
target.Value.TestProperties.IntList.Should().BeEquivalentTo(optValue);
55+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
4856
}
4957

5058
[Theory]
@@ -67,35 +75,23 @@ public void parametered_properties(
6775
var option = target!.Bind(x => x.TestProperties.IntList, "x");
6876
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
6977

70-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
71-
unkeyedValues);
78+
ValidateSetup(target, option, unkeyed, required);
79+
80+
target.Parse(new string[] { cmdLine }).Should().Be(result);
81+
82+
target.Value.TestProperties.IntList.Should().BeEquivalentTo(optValue);
83+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
7284
}
7385

74-
private void ProcessTest<T>(
75-
string cmdLine,
76-
bool required,
77-
BindingTarget<T> target,
78-
Option option,
79-
Option unkeyed,
80-
bool desiredParseResult,
81-
Func<TestProperties> results,
82-
int[] optValue,
83-
int[] unkeyedValues)
86+
private void ValidateSetup<T>( BindingTarget<T> target, Option option, Option unkeyed, bool required )
8487
where T : class
8588
{
8689
target.Should().NotBeNull();
87-
8890
option.Should().BeAssignableTo<MappableOption>();
8991
unkeyed.Should().BeAssignableTo<MappableOption>();
9092

9193
if (required)
9294
option.Required();
93-
94-
var parseResults = target.Parse(new string[] { cmdLine });
95-
parseResults.Should().Be(desiredParseResult);
96-
97-
results().IntList.Should().BeEquivalentTo(optValue);
98-
results().Unkeyed.Should().BeEquivalentTo(unkeyedValues);
9995
}
10096
}
10197
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using FluentAssertions;
3+
using J4JSoftware.CommandLine;
4+
using Xunit;
5+
6+
namespace J4JCommandLine.Tests
7+
{
8+
public class MultiParseTests
9+
{
10+
[Theory]
11+
[InlineData("-z 32", true, false, -1, new int[] { })]
12+
[InlineData("-x 32", true, true, 32, new int[] { })]
13+
[InlineData("-z 32", false, true, -1, new int[] { })]
14+
public void root_properties(
15+
string cmdLine,
16+
bool required,
17+
bool result,
18+
int optValue,
19+
int[] unkeyedValues)
20+
{
21+
var target = ServiceProvider.GetBindingTarget<TestProperties>(false);
22+
var option = target!.Bind( x => x.IntProperty, "x" );
23+
var unkeyed = target!.BindUnkeyed(x => x.Unkeyed);
24+
25+
ValidateSetup(target, option, unkeyed, required);
26+
27+
for( var idx = 0; idx < 3; idx++ )
28+
{
29+
target.Parse( new string[] { cmdLine } ).Should().Be( result );
30+
target.Value.IntProperty.Should().Be( optValue );
31+
target.Value.Unkeyed.Should().BeEquivalentTo( unkeyedValues );
32+
}
33+
}
34+
35+
[Theory]
36+
[InlineData("-z 32", true, false, -1, new int[] { })]
37+
[InlineData("-x 32", true, true, 32, new int[] { })]
38+
[InlineData("-z 32", false, true, -1, new int[] { })]
39+
public void parameterless_properties(
40+
string cmdLine,
41+
bool required,
42+
bool result,
43+
int optValue,
44+
int[] unkeyedValues)
45+
{
46+
var target = ServiceProvider.GetBindingTarget<ParameterlessConstructorParent>(false);
47+
var option = target!.Bind(x => x.TestProperties.IntProperty, "x");
48+
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
49+
50+
ValidateSetup(target, option, unkeyed, required);
51+
52+
for (var idx = 0; idx < 3; idx++)
53+
{
54+
target.Parse(new string[] { cmdLine }).Should().Be(result);
55+
target.Value.TestProperties.IntProperty.Should().Be(optValue);
56+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
57+
}
58+
}
59+
60+
[Theory]
61+
[InlineData("-z 32", true, false, -1, new int[] { })]
62+
[InlineData("-x 32", true, true, 32, new int[] { })]
63+
[InlineData("-z 32", false, true, -1, new int[] { })]
64+
public void parametered_properties(
65+
string cmdLine,
66+
bool required,
67+
bool result,
68+
int optValue,
69+
int[] unkeyedValues)
70+
{
71+
var target =
72+
ServiceProvider.GetBindingTarget<ParameteredConstructorParent>(
73+
false,
74+
new ParameteredConstructorParent( 52 ) );
75+
76+
var option = target!.Bind(x => x.TestProperties.IntProperty, "x");
77+
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
78+
79+
ValidateSetup(target, option, unkeyed, required);
80+
81+
for (var idx = 0; idx < 3; idx++)
82+
{
83+
target.Parse(new string[] { cmdLine }).Should().Be(result);
84+
target.Value.TestProperties.IntProperty.Should().Be(optValue);
85+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
86+
}
87+
}
88+
89+
private void ValidateSetup<T>( BindingTarget<T> target, Option option, Option unkeyed, bool required )
90+
where T : class
91+
{
92+
target.Should().NotBeNull();
93+
option.Should().BeAssignableTo<MappableOption>();
94+
unkeyed.Should().BeAssignableTo<MappableOption>();
95+
96+
option.SetDefaultValue(-1);
97+
98+
if( required )
99+
option.Required();
100+
}
101+
}
102+
}

J4JCommandLine.Tests/SimplePropertyTest.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ public void root_properties(
1919
int[] unkeyedValues)
2020
{
2121
var target = ServiceProvider.GetBindingTarget<TestProperties>(false);
22-
var option = target!.Bind(x => x.IntProperty, "x");
22+
var option = target!.Bind( x => x.IntProperty, "x" );
2323
var unkeyed = target!.BindUnkeyed(x => x.Unkeyed);
2424

25-
ProcessTest( cmdLine, required, target, option, unkeyed, result, () => target.Value, optValue,
26-
unkeyedValues );
25+
ValidateSetup(target, option, unkeyed, required);
26+
27+
target.Parse( new string[] { cmdLine } ).Should().Be( result );
28+
target.Value.IntProperty.Should().Be( optValue );
29+
target.Value.Unkeyed.Should().BeEquivalentTo( unkeyedValues );
2730
}
2831

2932
[Theory]
@@ -41,8 +44,11 @@ public void parameterless_properties(
4144
var option = target!.Bind(x => x.TestProperties.IntProperty, "x");
4245
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
4346

44-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
45-
unkeyedValues);
47+
ValidateSetup(target, option, unkeyed, required);
48+
49+
target.Parse(new string[] { cmdLine }).Should().Be(result);
50+
target.Value.TestProperties.IntProperty.Should().Be(optValue);
51+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
4652
}
4753

4854
[Theory]
@@ -64,38 +70,24 @@ public void parametered_properties(
6470
var option = target!.Bind(x => x.TestProperties.IntProperty, "x");
6571
var unkeyed = target!.BindUnkeyed(x => x.TestProperties.Unkeyed);
6672

67-
ProcessTest(cmdLine, required, target, option, unkeyed, result, () => target.Value.TestProperties, optValue,
68-
unkeyedValues);
73+
ValidateSetup(target, option, unkeyed, required);
74+
75+
target.Parse(new string[] { cmdLine }).Should().Be(result);
76+
target.Value.TestProperties.IntProperty.Should().Be(optValue);
77+
target.Value.TestProperties.Unkeyed.Should().BeEquivalentTo(unkeyedValues);
6978
}
7079

71-
private void ProcessTest<T>(
72-
string cmdLine,
73-
bool required,
74-
BindingTarget<T> target,
75-
Option option,
76-
Option unkeyed,
77-
bool desiredParseResult,
78-
Func<TestProperties> results,
79-
int optValue,
80-
int[] unkeyedValues )
80+
private void ValidateSetup<T>( BindingTarget<T> target, Option option, Option unkeyed, bool required )
8181
where T : class
8282
{
8383
target.Should().NotBeNull();
84-
8584
option.Should().BeAssignableTo<MappableOption>();
86-
option.SetDefaultValue(-1);
87-
8885
unkeyed.Should().BeAssignableTo<MappableOption>();
8986

90-
if (required)
91-
option.Required();
92-
93-
var parseResults = target.Parse(new string[] { cmdLine });
94-
parseResults.Should().Be( desiredParseResult );
87+
option.SetDefaultValue(-1);
9588

96-
results().Unkeyed.Should().BeEmpty();
97-
results().IntProperty.Should().Be(optValue);
98-
results().Unkeyed.Should().BeEquivalentTo(unkeyedValues);
89+
if( required )
90+
option.Required();
9991
}
10092
}
10193
}

0 commit comments

Comments
 (0)