-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove constraint on T for ParseArguments with factory (#590)
* Remove constraint on T for ParseArguments with factory * Make it possible to use factory for classes without public empty constructor * Allow types with explicitly defined interface implementations * Add test for #70 * Make sure explicit interface implementations can be parsed * Add test to make sure parser can detect explicit interface implementations
- Loading branch information
1 parent
19e2c95
commit 00ae0f2
Showing
9 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/CommandLine.Tests/Fakes/Mutable_Without_Empty_Constructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. | ||
|
||
namespace CommandLine.Tests.Fakes | ||
{ | ||
class Mutable_Without_Empty_Constructor | ||
{ | ||
[Option("amend", HelpText = "Used to amend the tip of the current branch.")] | ||
public bool Amend { get; set; } | ||
|
||
private Mutable_Without_Empty_Constructor() | ||
{ | ||
} | ||
|
||
public static Mutable_Without_Empty_Constructor Create() | ||
{ | ||
return new Mutable_Without_Empty_Constructor(); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/CommandLine.Tests/Fakes/Options_With_Only_Explicit_Interface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace CommandLine.Tests.Fakes | ||
{ | ||
class Options_With_Only_Explicit_Interface : IInterface_With_Two_Scalar_Options | ||
{ | ||
bool IInterface_With_Two_Scalar_Options.Verbose { get; set; } | ||
|
||
string IInterface_With_Two_Scalar_Options.InputFile { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Linq; | ||
using CommandLine.Tests.Fakes; | ||
using CommandLine.Text; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
//Issue #591 | ||
//When options class is only having explicit interface declarations, it should be detected as mutable. | ||
|
||
namespace CommandLine.Tests.Unit | ||
{ | ||
public class Issue591ests | ||
{ | ||
[Fact] | ||
public void Parse_option_with_only_explicit_interface_implementation() | ||
{ | ||
string actual = string.Empty; | ||
|
||
var arguments = new[] { "--inputfile", "file2.txt" }; | ||
var result = Parser.Default.ParseArguments<Options_With_Only_Explicit_Interface>(arguments); | ||
result.WithParsed(options => { | ||
actual = ((IInterface_With_Two_Scalar_Options)options).InputFile; | ||
}); | ||
|
||
actual.Should().Be("file2.txt"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Linq; | ||
using CommandLine.Tests.Fakes; | ||
using CommandLine.Text; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
//Issue #70 | ||
//When the factory overload is used for ParseArguments, there should be no constraint not having an empty constructor. | ||
|
||
namespace CommandLine.Tests.Unit | ||
{ | ||
public class Issue70Tests | ||
{ | ||
[Fact] | ||
public void Create_instance_with_factory_method_should_not_fail() | ||
{ | ||
bool actual = false; | ||
|
||
var arguments = new[] { "--amend" }; | ||
var result = Parser.Default.ParseArguments(() => Mutable_Without_Empty_Constructor.Create(), arguments); | ||
result.WithParsed(options => { | ||
actual = options.Amend; | ||
}); | ||
|
||
actual.Should().BeTrue(); | ||
} | ||
} | ||
} |