Skip to content

Commit 1350384

Browse files
committed
added new validators around the option names separating the logic into its own concern
1 parent 7da1002 commit 1350384

21 files changed

+903
-75
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParser.Tests.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Reference Include="Machine.Specifications.Clr4">
4848
<HintPath>..\packages\Machine.Specifications.0.5.12\lib\net40\Machine.Specifications.Clr4.dll</HintPath>
4949
</Reference>
50+
<Reference Include="Microsoft.CSharp" />
5051
<Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
5152
<SpecificVersion>False</SpecificVersion>
5253
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
@@ -55,12 +56,13 @@
5556
<SpecificVersion>False</SpecificVersion>
5657
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
5758
</Reference>
58-
<Reference Include="Ploeh.AutoFixture">
59-
<HintPath>..\packages\AutoFixture.3.0.3\lib\net40\Ploeh.AutoFixture.dll</HintPath>
59+
<Reference Include="Ploeh.AutoFixture, Version=3.0.8.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
60+
<SpecificVersion>False</SpecificVersion>
61+
<HintPath>..\packages\AutoFixture.3.0.8\lib\net40\Ploeh.AutoFixture.dll</HintPath>
6062
</Reference>
61-
<Reference Include="Ploeh.AutoFixture.AutoMoq, Version=3.0.3.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
63+
<Reference Include="Ploeh.AutoFixture.AutoMoq, Version=3.0.8.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
6264
<SpecificVersion>False</SpecificVersion>
63-
<HintPath>..\packages\AutoFixture.AutoMoq.3.0.3\lib\net40\Ploeh.AutoFixture.AutoMoq.dll</HintPath>
65+
<HintPath>..\packages\AutoFixture.AutoMoq.3.0.8\lib\net40\Ploeh.AutoFixture.AutoMoq.dll</HintPath>
6466
</Reference>
6567
<Reference Include="System" />
6668
<Reference Include="xunit, Version=1.9.1.1600, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
@@ -96,12 +98,15 @@
9698
<Compile Include="Integration\Lists\StringListInlineDataAttribute.cs" />
9799
<Compile Include="Integration\NCrunchExcelDataAttribute.cs" />
98100
<Compile Include="Integration\StringInlineDataAttribute.cs" />
101+
<Compile Include="Internals\AnonymousMock.cs" />
99102
<Compile Include="Internals\CommandLineParserEngineMark2Tests.cs" />
100103
<Compile Include="Internals\CommandLineParserEngineMark2TestsXUnit.cs" />
101104
<Compile Include="Internals\CommandLineParserEngine\TestContext\CommandLineParserEngineTestContext.cs" />
102105
<Compile Include="Internals\CommandLineParserEngine\when_args_contains_a_boolean_option_that_ends_with_no_sign.cs" />
103106
<Compile Include="Internals\HelpCommandLineOptionTests.cs" />
104107
<Compile Include="Internals\TestContextBase.cs" />
108+
<Compile Include="Internals\Validators\CommandLineOptionNameValidatorTests.cs" />
109+
<Compile Include="Internals\Validators\NoDuplicateOptionValidatorTests.cs" />
105110
<Compile Include="TestContext\TestContext.cs" />
106111
<Compile Include="FluentCommandLineParser\TestContext\TestType.cs" />
107112
<Compile Include="FluentCommandLineParser\when_executing_parse_operation\with_options_that_have_not_been_setup.cs" />

FluentCommandLineParser.Tests/FluentCommandLineParser/when_setting_up_a_new_option/with_a_short_name/with_a_short_name_that_is_a_control_char.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
#region License
2-
// with_a_short_name_that_is_empty.cs
3-
// Copyright (c) 2013, 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.Behaviour;
26-
using Fclp.Tests.FluentCommandLineParser.TestContext;
27-
using Machine.Specifications;
28-
1+
#region License
2+
// with_a_short_name_that_is_a_control_char.cs
3+
// Copyright (c) 2013, 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.Behaviour;
26+
using Fclp.Tests.FluentCommandLineParser.TestContext;
27+
using Machine.Specifications;
28+
2929
namespace Fclp.Tests.FluentCommandLineParser
3030
{
3131
namespace when_setting_up_a_new_option

FluentCommandLineParser.Tests/FluentCommandLineParser/when_using_an_option_factory/that_is_custom.cs

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

25-
using System.Globalization;
2625
using Fclp.Internals;
2726
using Fclp.Tests.FluentCommandLineParser.TestContext;
2827
using Machine.Specifications;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#region License
2+
// AnonymousMock.cs
3+
// Copyright (c) 2013, 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 System.Linq;
26+
using System.Linq.Expressions;
27+
using System.Reflection;
28+
using Moq;
29+
using Ploeh.AutoFixture;
30+
using Ploeh.AutoFixture.Kernel;
31+
32+
namespace Fclp.Tests.Internals
33+
{
34+
/// <summary>
35+
/// http://pol84.tumblr.com/post/23032897078/autofixture-and-interfaces
36+
/// </summary>
37+
public static class AnonymousMock
38+
{
39+
public static Mock<T> CreateMock<T>(
40+
this ISpecimenBuilder composer) where T : class
41+
{
42+
return new Factory<T>(composer).CreateAnonymousMock();
43+
}
44+
45+
private class Factory<T> where T : class
46+
{
47+
private readonly ISpecimenBuilder composer;
48+
private Mock<T> mock;
49+
private MethodInfo setupGet;
50+
private PropertyInfo property;
51+
private object returnsGetter;
52+
private MethodInfo returns;
53+
54+
internal Factory(ISpecimenBuilder composer)
55+
{
56+
this.composer = composer;
57+
}
58+
59+
internal Mock<T> CreateAnonymousMock()
60+
{
61+
mock = new Mock<T>();
62+
setupGet = mock.GetType().GetMethod("SetupGet");
63+
typeof(T).GetProperties()
64+
.Where(p => p.CanRead)
65+
.ToList()
66+
.ForEach(p => { property = p; SetupGet(); });
67+
return mock;
68+
}
69+
70+
private void SetupGet()
71+
{
72+
var genericSetupGet = setupGet
73+
.MakeGenericMethod(property.PropertyType);
74+
var expression = GetPropertyLambdaExpression();
75+
returnsGetter = genericSetupGet
76+
.Invoke(mock, new object[] { expression });
77+
UseReturnsGetter();
78+
}
79+
80+
private void UseReturnsGetter()
81+
{
82+
returns = returnsGetter.GetType().GetMethods()
83+
.Single(m => m.Name == "Returns" && m.GetParameters()
84+
.Any(p => p.ParameterType == property.PropertyType));
85+
Returns();
86+
}
87+
88+
private void Returns()
89+
{
90+
var propertyValue = GetValueFromFixture();
91+
returns.Invoke(returnsGetter, new object[] { propertyValue });
92+
}
93+
94+
private LambdaExpression GetPropertyLambdaExpression()
95+
{
96+
var typeParameter = Expression.Parameter(typeof(T));
97+
var propertyExpression = Expression
98+
.Property(typeParameter, property);
99+
return Expression.Lambda(propertyExpression, typeParameter);
100+
}
101+
102+
private object GetValueFromFixture()
103+
{
104+
var createAnonymous = typeof(SpecimenFactory).GetMethods()
105+
.Single(m => m.Name == "Create"
106+
&& m.GetParameters().All(p => p.ParameterType
107+
== typeof(ISpecimenBuilder)));
108+
var genericCreateAnonymous = createAnonymous
109+
.MakeGenericMethod(property.PropertyType);
110+
return genericCreateAnonymous
111+
.Invoke(null, new object[] { composer });
112+
}
113+
}
114+
}
115+
}

FluentCommandLineParser.Tests/Internals/TestContextBase.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#endregion
2424

2525
using System;
26+
using System.Collections.Generic;
2627
using System.Linq;
2728
using Fclp.Internals.Extensions;
2829
using Machine.Specifications;
@@ -80,7 +81,44 @@ protected static void FreezeMock<TType>(out Mock<TType> obj) where TType : class
8081

8182
protected static void Create<TType>(out TType obj)
8283
{
83-
obj = fixture.Create<TType>();
84+
obj = Create<TType>();
85+
}
86+
87+
protected static TType Create<TType>()
88+
{
89+
return fixture.Create<TType>();
90+
}
91+
92+
protected static void CreateMock<TType>(out Mock<TType> obj) where TType : class
93+
{
94+
obj = CreateMock<TType>();
95+
}
96+
97+
protected static Mock<TType> CreateMock<TType>() where TType : class
98+
{
99+
return fixture.CreateMock<TType>();
100+
}
101+
102+
protected static List<TType> CreateEmptyList<TType>()
103+
{
104+
return new List<TType>();
105+
}
106+
107+
protected static void CreateEmptyList<TType>(out List<TType> list)
108+
{
109+
list = CreateEmptyList<TType>();
110+
}
111+
112+
protected static List<TType> CreateManyAsList<TType>(params TType[] additionalItemsToAdd)
113+
{
114+
var list = fixture.CreateMany<TType>(5).ToList();
115+
list.AddRange(additionalItemsToAdd);
116+
return list;
117+
}
118+
119+
protected static string CreateStringOfLength(int length)
120+
{
121+
return new string(Create<char>(), length);
84122
}
85123
}
86124
}

0 commit comments

Comments
 (0)