Skip to content

Commit

Permalink
Add more test coverage to make sure any behaviour with required argum…
Browse files Browse the repository at this point in the history
…ents and command interaction is trapped
  • Loading branch information
Nick-Lucas committed Feb 4, 2017
1 parent 9153e6c commit a74cd43
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/EntryPoint/BaseCliArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class BaseCliArguments : BaseHelpable {
public BaseCliArguments(string utilityName) {
UtilityName = utilityName;
}
internal BaseCliArguments() { }
internal BaseCliArguments() : this("") { }

internal string UtilityName { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using EntryPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using EntryPointTests.Arguments.Helpers;

namespace EntryPointTests.Arguments.AppOptionModels {
public class RequiredCliArguments : BaseCliArguments {
public RequiredCliArguments() : base("Test") { }

[Required]
[OptionParameter("my-option", 'o')]
public bool MyOption { get; set; }

public override void OnHelpInvoked(string helpText) {
throw new HelpTriggeredSuccessException();
}
}
}
40 changes: 40 additions & 0 deletions test/EntryPointTests/Arguments/RequiredTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using EntryPoint;
using EntryPoint.Exceptions;
using EntryPointTests.Arguments.AppOptionModels;
using EntryPointTests.Arguments.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace EntryPointTests.Arguments {
public class RequiredTests {
[Fact]
public void RequiredProvided() {
string[] args = {
"--my-option=true"
};
var a = Cli.Parse<RequiredCliArguments>(args);
Assert.Equal(true, a.MyOption);
}

[Fact]
public void RequiredNotProvided() {
string[] args = {

};
Assert.Throws<RequiredException>(
() => Cli.Parse<RequiredCliArguments>(args));
}

[Fact]
public void RequiredNotProvided_HelpInvoked() {
string[] args = {
"--help"
};
Assert.Throws<HelpTriggeredSuccessException>(
() => Cli.Parse<RequiredCliArguments>(args));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using EntryPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EntryPointTests.Commands.BaseCommandsHelpers {
public class ArgumentModel_RequiredOptions : BaseCliArguments {
[Required]
[OptionParameter("my-option", 'o')]
public bool MyOption { get; set; }

public override void OnHelpInvoked(string helpText) {
throw new EntryPointTests.Arguments.Helpers.HelpTriggeredSuccessException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using EntryPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EntryPointTests.Commands.BaseCommandsHelpers {
public class CommandModel_RequiredOptions : BaseCliCommands {
[Command("Main")]
public void Main(string[] args) {
var a = Cli.Parse<ArgumentModel_RequiredOptions>(args);
throw new Helpers.CommandExecutedException(a.MyOption.ToString());
}
}
}
41 changes: 41 additions & 0 deletions test/EntryPointTests/Commands/CommandArgumentsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using EntryPoint;
using EntryPoint.Exceptions;
using EntryPointTests.Commands.BaseCommandsHelpers;
using EntryPointTests.Commands.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace EntryPointTests.Commands {
public class CommandArgumentsTests {
[Fact]
public void Command_RequiredArguments_HelpGenerator() {
string[] args = {
"Main", "--help"
};
Assert.Throws<Arguments.Helpers.HelpTriggeredSuccessException>(
() => Cli.Execute<CommandModel_RequiredOptions>(args));
}

[Fact]
public void Command_RequiredArguments_NotProvided() {
string[] args = {
"Main"
};
Assert.Throws<RequiredException>(
() => Cli.Execute<CommandModel_RequiredOptions>(args));
}

[Fact]
public void Command_RequiredArguments_OK() {
string[] args = {
"Main", "--my-option=true"
};
var e = Assert.Throws<CommandExecutedException>(
() => Cli.Execute<CommandModel_RequiredOptions>(args));
Assert.Equal(true.ToString(), e.ParamName);
}
}
}
2 changes: 1 addition & 1 deletion test/Example/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"MainHelp": {
"commandName": "Project",
"commandLineArgs": "Main --help"
"commandLineArgs": "Primary --help"
},
"TestArgs": {
"commandName": "Project",
Expand Down

0 comments on commit a74cd43

Please sign in to comment.