public async Task When_dependency_resolver_is_missing_and_command_without_default_constructor_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyCommand>("test"); //// Act await processor.ProcessAsync(new string[] { "test" }); // exception //// Assert }
public async Task WhenArgumentIsEnumThenItShouldBeLoadedCorrectly() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyEnumCommand>("test"); //// Act var command = (MyEnumCommand)await processor.ProcessAsync(new string[] { "test", "/myenum:def" }); //// Assert Assert.AreEqual(MyEnum.Def, command.MyEnum); }
public async Task When_running_command_with_int32_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyArgumentCommand>("test"); //// Act var command = (MyArgumentCommand)await processor.ProcessAsync(new string[] { "test", "/int32:123" }); //// Assert Assert.IsTrue(123 == command.Int32); }
public async Task When_dependency_resolver_is_missing_and_command_without_default_constructor_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyCommand>("test"); //// Act await processor.ProcessAsync(new string[] { "test" }); // exception //// Assert }
public async Task When_array_has_empty_default_then_property_is_correctly_initialized() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyDefaultArrayCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test" }); var command = (MyDefaultArrayCommand)result.Last().Command; //// Assert Assert.AreEqual(0, command.MyStrings.Length); }
public void When_dependency_resolver_is_missing_and_command_without_default_constructor_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyCommand>("test"); //// Act var process = processor.ProcessAsync(new[] { "test" }); //// Assert Assert.ThrowsAsync <InvalidOperationException>(async() => await process); }
public async Task When_running_command_with_quoted_string_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test", "/string:abc def" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.AreEqual("abc def", command.String); }
public async Task When_argument_is_string_array_then_it_can_be_defined_as_comma_separated_string() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyArrayCommand>("test"); //// Act var command = (MyArrayCommand)await processor.ProcessAsync(new string[] { "test", "/mystrings:a,b,c" }); //// Assert Assert.AreEqual(3, command.MyStrings.Length); Assert.AreEqual("a", command.MyStrings[0]); Assert.AreEqual("b", command.MyStrings[1]); Assert.AreEqual("c", command.MyStrings[2]); }
public async Task When_command_is_chained_and_output_is_ignored_then_first_command_does_not_influence_second_command() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<SumCommand>("sum"); processor.RegisterCommand<SubtractCommand>("subtract"); var args = new string[] { "sum", "/a:6", "/b:10", "=", "subtract", "/a: 20", "/b:7" }; //// Act var result = await processor.ProcessAsync(args); //// Assert Assert.AreEqual(13, result.Last().Output); }
public async Task When_array_has_empty_default_then_property_is_correctly_initialized() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyDefaultArrayCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test" }); var command = (MyDefaultArrayCommand)result.Last().Command; //// Assert Assert.AreEqual(0, command.MyStrings.Length); }
public async Task When_positional_argument_starts_with_slash_then_it_is_correctly_read() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyCommand>("test"); //// Act var result = await processor.ProcessAsync(new[] { "test", "/foo/bar/test.cs" }); var command = (MyCommand)result.Last().Command; //// Assert Assert.Equal("/foo/bar/test.cs", command.First); }
public async Task WhenArgumentIsEnumThenItShouldBeLoadedCorrectly() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyEnumCommand>("test"); //// Act var result = await processor.ProcessAsync(new[] { "test", "/myenum:def" }); var command = (MyEnumCommand)result.Last().Command; //// Assert Assert.Equal(MyEnum.Def, command.MyEnum); }
public async Task When_running_command_with_boolean_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); //// Act var result = await processor.ProcessAsync(new[] { "test", "/boolean:true" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.True(command.Boolean); }
public async Task When_running_command_with_int64_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test", "/int64:123" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.IsTrue(123 == command.Int64); }
public async Task When_command_is_chained_then_output_is_passed_as_input_to_second_command() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <SumCommand>("sum"); processor.RegisterCommand <SubtractCommand>("subtract"); var args = new [] { "sum", "/a:6", "/b:10", "=", "subtract", "/b:7" }; //// Act var result = await processor.ProcessAsync(args); //// Assert Assert.Equal(9, result.Last().Output); }
public async Task When_command_is_chained_and_output_is_ignored_then_first_command_does_not_influence_second_command() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <SumCommand>("sum"); processor.RegisterCommand <SubtractCommand>("subtract"); var args = new string[] { "sum", "/a:6", "/b:10", "=", "subtract", "/a: 20", "/b:7" }; //// Act var result = await processor.ProcessAsync(args); //// Assert Assert.AreEqual(13, result.Last().Output); }
public async Task When_running_command_with_datetime_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test", "/datetime:2014-5-3" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.AreEqual(3, command.DateTime.Day); Assert.AreEqual(5, command.DateTime.Month); Assert.AreEqual(2014, command.DateTime.Year); }
public async Task When_argument_is_string_array_then_it_can_be_defined_as_comma_separated_string() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArrayCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test", "/mystrings:a,b,c" }); var command = (MyArrayCommand)result.Last().Command; //// Assert Assert.AreEqual(3, command.MyStrings.Length); Assert.AreEqual("a", command.MyStrings[0]); Assert.AreEqual("b", command.MyStrings[1]); Assert.AreEqual("c", command.MyStrings[2]); }
public async Task When_array_has_default_items_then_property_is_correctly_initialized() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyDefault2ArrayCommand>("test"); //// Act var result = await processor.ProcessAsync(new[] { "test" }); var command = (MyDefault2ArrayCommand)result.Last().Command; //// Assert Assert.Equal(3, command.MyStrings.Length); Assert.Equal("a", command.MyStrings[0]); Assert.Equal("b", command.MyStrings[1]); Assert.Equal("c", command.MyStrings[2]); }
public async Task When_running_command_with_unexpected_arguments_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); //// Act async Task ProcessAsyncThrowsException() { var result = await processor.ProcessAsync(new [] { "test", "first", "second", "/third:third", "/Fourth" }); var command = (MyArgumentCommand)result.Last().Command; } //// Assert var exception = await Assert.ThrowsAsync <UnusedArgumentException>(async() => await ProcessAsyncThrowsException()); Assert.Equal("Unrecognised arguments are present: [Used arguments (1) != Provided arguments (4) -> Check [second, /third:third, /Fourth]]", exception.Message); }
public async Task When_running_command_with_incorrect_full_datetime_parameter_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); ////Act async Task ProcessAsyncThrowsException() { var result = await processor.ProcessAsync(new [] { "test", "first", "/datetime:2014-5-3", "12:13:14", "/TestSwitch" }); var command = (MyArgumentCommand)result.Last().Command; } //// Assert var exception = await Assert.ThrowsAsync <UnusedArgumentException>(async() => await ProcessAsyncThrowsException()); Assert.Equal("Unrecognised arguments are present: [Used arguments (3) != Provided arguments (4) -> Check [12:13:14]]", exception.Message); }
public async Task When_running_command_with_full_datetime_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); ////Act var result = await processor.ProcessAsync(new[] { "test", "first", "/datetime:2014-5-3 12:13:14", "/TestSwitch" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.Equal(14, command.DateTime.Second); Assert.Equal(13, command.DateTime.Minute); Assert.Equal(12, command.DateTime.Hour); Assert.Equal(3, command.DateTime.Day); Assert.Equal(5, command.DateTime.Month); Assert.Equal(2014, command.DateTime.Year); }
public async Task When_running_command_with_incorrect_full_datetime_parameter_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); ////Act try { var result = await processor.ProcessAsync(new string[] { "test", "first", "/datetime:2014-5-3", "12:13:14", "/TestSwitch" }); var command = (MyArgumentCommand)result.Last().Command; Assert.Fail(); } catch (UnusedArgumentException ex) { Assert.AreEqual(ex.Message, "Unrecognised arguments are present: [12:13:14]"); } }
public async Task When_running_command_with_unexpected_arguments_then_exception_is_thrown() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand <MyArgumentCommand>("test"); ////Act try { var result = await processor.ProcessAsync(new string[] { "test", "first", "second", "/third:third", "/Fourth" }); var command = (MyArgumentCommand)result.Last().Command; Assert.Fail(); } catch (UnusedArgumentException ex) { Assert.AreEqual(ex.Message, "Unrecognised arguments are present: [second, /third:third, /Fourth]"); } }
/// <summary>Processes the command line arguments.</summary> /// <param name="args">The arguments.</param> /// <returns>The result.</returns> public async Task <int> ProcessAsync(string[] args) { _host.WriteMessage("toolchain v" + OpenApiDocument.ToolchainVersion + " (NJsonSchema v" + JsonSchema.ToolchainVersion + ")\n"); _host.WriteMessage("Visit http://NSwag.org for more information.\n"); WriteBinDirectory(); if (args.Length == 0) { _host.WriteMessage("Execute the 'help' command to show a list of all the available commands.\n"); } try { var processor = new CommandLineProcessor(_host); processor.RegisterCommandsFromAssembly(typeof(SwaggerToCSharpControllerCommand).GetTypeInfo().Assembly); var stopwatch = new Stopwatch(); stopwatch.Start(); await processor.ProcessAsync(args).ConfigureAwait(false); stopwatch.Stop(); _host.WriteMessage("\nDuration: " + stopwatch.Elapsed + "\n"); } catch (Exception exception) { _host.WriteError(exception.ToString()); return(-1); } WaitWhenDebuggerAttached(); return(0); }
public async Task When_running_command_with_boolean_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyArgumentCommand>("test"); //// Act var result = await processor.ProcessAsync(new string[] { "test", "/boolean:true" }); var command = (MyArgumentCommand)result.Last().Command; //// Assert Assert.IsTrue(command.Boolean); }
public async Task When_running_command_with_quoted_string_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyArgumentCommand>("test"); //// Act var command = (MyArgumentCommand)await processor.ProcessAsync(new string[] { "test", "/string:abc def" }); //// Assert Assert.AreEqual("abc def", command.String); }
public async Task When_running_command_with_datetime_parameter_then_they_are_correctly_set() { //// Arrange var processor = new CommandLineProcessor(new ConsoleHost()); processor.RegisterCommand<MyArgumentCommand>("test"); //// Act var command = (MyArgumentCommand)await processor.ProcessAsync(new string[] { "test", "/datetime:2014-5-3" }); //// Assert Assert.AreEqual(3, command.DateTime.Day); Assert.AreEqual(5, command.DateTime.Month); Assert.AreEqual(2014, command.DateTime.Year); }