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
        }
예제 #2
0
        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);
        }
예제 #3
0
        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
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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]);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
        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);
        }
예제 #13
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[] { "test", "/boolean:true" });

            var command = (MyArgumentCommand)result.Last().Command;

            //// Assert
            Assert.True(command.Boolean);
        }
예제 #14
0
        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);
        }
예제 #15
0
        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);
        }
예제 #16
0
        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);
        }
예제 #17
0
        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);
        }
예제 #18
0
        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]);
        }
예제 #19
0
        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]);
        }
예제 #20
0
        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);
        }
예제 #21
0
        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);
        }
예제 #22
0
        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);
        }
예제 #23
0
        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]");
            }
        }
예제 #24
0
        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]");
            }
        }
예제 #25
0
        /// <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);
        }
예제 #26
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);
        }
예제 #27
0
        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);
        }
예제 #28
0
        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);
        }