public void CommandLine_Validate_OnCommandPreExecute() { Logger.LogMessage("Testing valid option '-v'"); m_isCalled = false; CLIApplication.VersionOptionSwitch = "-version"; m_application = new CLIApplication( applicationName: "MyTool.exe", description: "Example for running the program", versionString: "Version 2.0", exampleCommands: new List <string>() { "MyTool.exe -wa a", "MyTool.exe -xa" }, onCommandPreExecute: () => { m_isCalled = true; }); m_application.ConfigureCommand <CleanupCommand>(); Assert.IsFalse(m_isCalled, "isCalled should be false before the call is done."); // Validate onCommandPreExecute is being executed ValidateCommand(true, m_application, new string[] { "cleanup" }); Assert.IsTrue(m_isCalled, "isCalled should be true after the call is done."); }
public void CommandLine_Valid_Version_Different_Switch_Option() { Logger.LogMessage("Testing valid option '-v'"); CLIApplication.VersionOptionSwitch = "-version"; m_application = new CLIApplication( applicationName: "MyTool.exe", description: "Example for running the program", versionString: "Version 2.0", exampleCommands: new List <string>() { "MyTool.exe -wa a", "MyTool.exe -xa" }, onCommandPreExecute: null); // Validate option version ValidateCommand(true, m_application, new string[] { "-version" }); }
public void CommandLine_Valid_Help_Different_Switch_Option() { Log.Comment("Testing valid option '-h'"); CLIApplication.HelpOptionSwitches = "-help"; m_application = new CLIApplication( applicationName: "MyTool.exe", description: "Example for running the program", versionString: "Version 2.0", exampleCommands: new List <string>() { "MyTool.exe -wa a", "MyTool.exe -xa" }, onCommandPreExecute: null); // Validate option -help ValidateCommand(true, m_application, new string[] { "-help" }); }
private static void ValidateCommand(bool isValidScenario, CLIApplication application, string[] args) { try { if (isValidScenario) { // the command should be valid Assert.AreEqual(application.Execute(args), 0); } else { // the command should be invalid Assert.AreNotEqual(application.Execute(args), 0); } } catch (Exception e) { Assert.Fail(e.Message); } }
public new void TestCleanup() { Logger.LogMessage("Cleaning CommandLineTests test."); m_application = null; }
public new void TestInitialize() { Logger.LogMessage("Initializing CommandLineTests test."); CLIApplication.HelpDescriptionText = "Help description."; CLIApplication.VersionDescriptionText = "Version description"; CLIApplication.HelpOptionSwitches = "-h"; CLIApplication.VersionOptionSwitch = "-v"; m_application = new CLIApplication( applicationName: "MyTool.exe", description: "Example for running the program", versionString: "Version 2.0", exampleCommands: new List <string>() { "MyTool.exe -wa a", "MyTool.exe -xa" }, onCommandPreExecute: null); m_application.SetupInputs = (CommandLineApplication commandLineApplication) => { ConfiguredInputs configuredInputs = new ConfiguredInputs(); // Setup arguments CommandArgument firstArgument = commandLineApplication.Argument( "firstArgument", "this is the first argument"); Action <string> firstArgumentValidator = (string data) => { if (data == "invalidFirstArgument") { throw new CommandParsingException(commandLineApplication, "Error in first argument invalidArgument."); } }; configuredInputs.Map["firstArgument"] = new ArgumentConfiguration( firstArgument, isRequired: false, validationRoutine: firstArgumentValidator); CommandArgument secondArgument = commandLineApplication.Argument( "secondArgument", "this is the second argument"); Action <string> secondArgumentValidator = (string data) => { if (data == "invalidSecondArgument") { throw new CommandParsingException(commandLineApplication, "Error in second argument invalidArgument."); } }; configuredInputs.Map["secondArgument"] = new ArgumentConfiguration( secondArgument, isRequired: false, validationRoutine: secondArgumentValidator); // Setup -wa option CommandOption waOption = commandLineApplication.Option( "-wa", "MyTool.exe -wa something", CommandOptionType.SingleValue); Action <string> waOptionValidator = (string data) => { if (data == "invalid") { throw new CommandParsingException(commandLineApplication, "Error in command wa."); } }; configuredInputs.Map["wa"] = new OptionConfiguration( waOption, isRequired: false, validationRoutine: waOptionValidator); // Setup -bad option CommandOption badOption = commandLineApplication.Option( "-bad", "MyTool.exe -bad something", CommandOptionType.SingleValue); Action <string> badOptionValidator = (string data) => { Logger.LogMessage("Running bad command."); throw new FileNotFoundException(string.Format("File {0} not found", data)); }; configuredInputs.Map["bad"] = new OptionConfiguration( badOption, isRequired: false, validationRoutine: badOptionValidator); // Setup -multipleValue option CommandOption multipleValueOption = commandLineApplication.Option( "-multipleValue", "MyTool.exe -multipleValue something1 -multipleValue something2", CommandOptionType.MultipleValue); Action <List <string> > multipleValueValidator = (List <string> values) => { Logger.LogMessage("Running multipleValue."); foreach (string value in values) { m_numberOfElements++; } }; configuredInputs.Map["multipleValue"] = new OptionConfiguration( multipleValueOption, isRequired: false, validationRoutineMultipleValue: multipleValueValidator); // Setup -multipleValue2 option CommandOption multipleValue2Option = commandLineApplication.Option( "-multipleValue2", "MyTool.exe -multipleValue2 something1 -multipleValue2 something2", CommandOptionType.MultipleValue); Action <string> multipleValue2Validator = (string values) => { Logger.LogMessage("Running multipleValue."); m_numberOfElements++; }; configuredInputs.Map["multipleValue2"] = new OptionConfiguration( multipleValue2Option, isRequired: false, validationRoutine: multipleValue2Validator); // Setup -xa option CommandOption xaOption = commandLineApplication.Option( "-xa", "MyTool.exe -xa", CommandOptionType.NoValue); configuredInputs.Map["xa"] = new OptionConfiguration( xaOption, isRequired: false, validationRoutine: null); return(configuredInputs); }; m_application.OnExecute = (ConfiguredInputs configuredInputs) => { Logger.LogMessage("Running on excecute for options."); return(0); }; m_application.ConfigureCommand <CleanupCommand>(); }
public CLIApplicationUnitTest() { _mockProductService = new Mock <IProductService>(); _cLIApplication = new CLIApplication(_mockProductService.Object); }
public new void TestCleanup() { Log.Comment("Cleaning CommandLineTests test."); m_application = null; }