Skip to content

Commit 16d0f45

Browse files
committed
use comparison / IsCaseSensitive defined in the parser.
1 parent cc553c0 commit 16d0f45

File tree

7 files changed

+75
-62
lines changed

7 files changed

+75
-62
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParser.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<ItemGroup>
7878
<Compile Include="CommandLineOptionFormatterTests.cs" />
7979
<Compile Include="FluentCommandLineParserBuilderTests.cs" />
80+
<Compile Include="FluentCommandLineParserMSpecTests.cs" />
8081
<Compile Include="FluentCommandLineParserTests.cs">
8182
<SubType>Code</SubType>
8283
</Compile>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Fclp.Tests.Internals;
2+
using Machine.Specifications;
3+
4+
namespace Fclp.Tests
5+
{
6+
class FluentCommandLineParserMSpecTests
7+
{
8+
[Subject(typeof(Fclp.FluentCommandLineParser))]
9+
abstract class FluentCommandLineParserTestContext : TestContextBase<Fclp.FluentCommandLineParser>
10+
{
11+
Establish context = () =>
12+
CreateSut();
13+
}
14+
15+
sealed class IsCaseSensitive
16+
{
17+
abstract class IsCaseSensitiveTestContext : FluentCommandLineParserTestContext { }
18+
19+
class when_enabled : IsCaseSensitiveTestContext
20+
{
21+
Because of = () => sut.IsCaseSensitive = true;
22+
23+
It should_return_enabled = () =>
24+
sut.IsCaseSensitive.ShouldBeTrue();
25+
26+
It should_set_the_comparison_type_to_case_sensitive = () =>
27+
sut.StringComparison.ShouldEqual(Fclp.FluentCommandLineParser.CaseSensitiveComparison);
28+
}
29+
30+
class when_disabled : IsCaseSensitiveTestContext
31+
{
32+
Because of = () => sut.IsCaseSensitive = false;
33+
34+
It should_return_enabled = () =>
35+
sut.IsCaseSensitive.ShouldBeFalse();
36+
37+
It should_set_the_comparison_type_to_ignore_case = () =>
38+
sut.StringComparison.ShouldEqual(Fclp.FluentCommandLineParser.IgnoreCaseComparison);
39+
}
40+
}
41+
}
42+
}

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,6 @@ public void Ensure_Parser_Calls_The_Callback_With_Expected_String_When_Using_Sho
125125
{
126126
const string expected = "my-expected-string";
127127
RunTest(expected, expected);
128-
129-
//const string key = "s";
130-
//string actual = null;
131-
132-
//var parser = CreateFluentParser();
133-
134-
//parser
135-
// .Setup<string>(key)
136-
// .Callback(val => actual = val);
137-
138-
//CallParserWithAllKeyVariations(parser, key, expected, (args, result) =>
139-
//{
140-
// string msg = "Executed with args: " + FormatArgs(args);
141-
// Assert.AreEqual(expected, actual, msg);
142-
// Assert.IsFalse(result.HasErrors, msg);
143-
// Assert.IsFalse(result.Errors.Any(), msg);
144-
//});
145128
}
146129

147130
[Test]

FluentCommandLineParser.Tests/Internals/Validators/NoDuplicateOptionValidatorTests.cs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,6 @@ abstract class NoDuplicateOptionValidatorTestContext : TestContextBase<NoDuplica
4545
};
4646
}
4747

48-
sealed class IsCaseSensitive
49-
{
50-
abstract class IsCaseSensitiveTestContext : NoDuplicateOptionValidatorTestContext
51-
{ }
52-
53-
class when_enabled : IsCaseSensitiveTestContext
54-
{
55-
Because of = () => sut.IsCaseSensitive = true;
56-
57-
It should_return_enabled = () =>
58-
sut.IsCaseSensitive.ShouldBeTrue();
59-
60-
It should_set_the_comparison_type_to_case_sensitive = () =>
61-
sut.ComparisonType.ShouldEqual(StringComparison.CurrentCulture);
62-
}
63-
64-
class when_disabled : IsCaseSensitiveTestContext
65-
{
66-
Because of = () => sut.IsCaseSensitive = false;
67-
68-
It should_return_enabled = () =>
69-
sut.IsCaseSensitive.ShouldBeFalse();
70-
71-
It should_set_the_comparison_type_to_ignore_case = () =>
72-
sut.ComparisonType.ShouldEqual(StringComparison.CurrentCultureIgnoreCase);
73-
}
74-
}
75-
7648
sealed class Validate
7749
{
7850
[Subject("Validate")]
@@ -106,7 +78,7 @@ protected static ICommandLineOption CreateOptionWith(string shortName = null, st
10678

10779
existingOption.SetupGet(it => it.HasLongName).Returns(longName != null);
10880
existingOption.SetupGet(it => it.HasShortName).Returns(shortName != null);
109-
existingOption.SetupGet(it => it.ShortName).Returns(shortName);
81+
existingOption.SetupGet(it => it.ShortName).Returns(shortName);
11082
existingOption.SetupGet(it => it.LongName).Returns(longName);
11183

11284
return existingOption.Object;
@@ -125,7 +97,7 @@ sealed class when_case_sensitive
12597
{
12698
abstract class CaseSensitiveTestContext : ValidateTestContext
12799
{
128-
Establish context = () => sut.IsCaseSensitive = true;
100+
Establish context = () => parser.SetupGet(it => it.IsCaseSensitive).Returns(true);
129101
}
130102

131103
class when_an_existing_option_contains_the_same_short_name_but_it_differs_by_case : CaseSensitiveTestContext
@@ -204,14 +176,14 @@ class when_an_existing_option_contains_the_same_short_AND_long_name : CaseSensit
204176
};
205177

206178
It should_throw_an_error = () => error.ShouldNotBeNull();
207-
}
179+
}
208180
}
209181

210182
sealed class when_ignore_case
211183
{
212184
abstract class IgnoreCaseTestContext : ValidateTestContext
213185
{
214-
Establish context = () => sut.IsCaseSensitive = false;
186+
Establish context = () => parser.SetupGet(it => it.IsCaseSensitive).Returns(false);
215187
}
216188

217189
class when_an_existing_option_contains_the_same_short_name_but_it_differs_by_case : IgnoreCaseTestContext
@@ -290,7 +262,7 @@ class when_an_existing_option_contains_the_same_short_AND_long_name : IgnoreCase
290262
};
291263

292264
It should_throw_an_error = () => error.ShouldNotBeNull();
293-
}
265+
}
294266
}
295267
}
296268
}

FluentCommandLineParser/FluentCommandLineParser.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ namespace Fclp
3939
/// </summary>
4040
public class FluentCommandLineParser : IFluentCommandLineParser
4141
{
42+
/// <summary>
43+
/// The <see cref="StringComparison"/> type used for case sensitive comparisons.
44+
/// </summary>
45+
public const StringComparison CaseSensitiveComparison = StringComparison.CurrentCulture;
46+
47+
/// <summary>
48+
/// The <see cref="StringComparison"/> type used for case in-sensitive comparisons.
49+
/// </summary>
50+
public const StringComparison IgnoreCaseComparison = StringComparison.CurrentCultureIgnoreCase;
51+
4252
List<ICommandLineOption> _options;
4353
ICommandLineOptionFactory _optionFactory;
4454
ICommandLineParserEngine _parserEngine;
@@ -48,13 +58,19 @@ public class FluentCommandLineParser : IFluentCommandLineParser
4858
ICommandLineOptionValidator _optionValidator;
4959

5060
/// <summary>
51-
/// Gets the <see cref="StringComparison"/> to use when matching values.
61+
/// Gets or sets whether values that differ by case are considered different.
5262
/// </summary>
53-
internal StringComparison StringComparison
63+
public bool IsCaseSensitive
5464
{
55-
get { return StringComparison.CurrentCulture; }
65+
get { return StringComparison == CaseSensitiveComparison; }
66+
set { StringComparison = value ? CaseSensitiveComparison : IgnoreCaseComparison; }
5667
}
5768

69+
/// <summary>
70+
/// Gets the <see cref="StringComparison"/> to use when matching values.
71+
/// </summary>
72+
internal StringComparison StringComparison { get; private set; }
73+
5874
/// <summary>
5975
/// Gets the list of Options
6076
/// </summary>

FluentCommandLineParser/IFluentCommandLineParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,10 @@ public interface IFluentCommandLineParser
104104
/// Returns the Options that have been setup for this parser.
105105
/// </summary>
106106
IEnumerable<ICommandLineOption> Options { get; }
107+
108+
/// <summary>
109+
/// Gets or sets whether values that differ by case are considered different.
110+
/// </summary>
111+
bool IsCaseSensitive { get; set; }
107112
}
108113
}

FluentCommandLineParser/Internals/Validators/NoDuplicateOptionValidator.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,11 @@ public NoDuplicateOptionValidator(IFluentCommandLineParser parser)
4848
/// <summary>
4949
/// Gets the <see cref="StringComparison"/> type used for duplicates.
5050
/// </summary>
51-
public StringComparison ComparisonType { get; private set; }
52-
53-
/// <summary>
54-
/// Gets or sets whether values that differ by case are considered different.
55-
/// </summary>
56-
public bool IsCaseSensitive
51+
private StringComparison ComparisonType
5752
{
58-
get { return ComparisonType == StringComparison.CurrentCulture; }
59-
set { ComparisonType = value ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; }
53+
get { return _parser.IsCaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; }
6054
}
61-
55+
6256
/// <summary>
6357
/// Verifies that the specified <see cref="ICommandLineOption"/> will not cause any duplication.
6458
/// </summary>

0 commit comments

Comments
 (0)