Beispiel #1
0
        static void Main(string[] args)
        {
            var host = new ConsoleHost();
            host.WriteMessage("NSwag command line: v" + typeof(SwaggerInfo).Assembly.GetName().Version + "\n");
            host.WriteMessage("Visit http://NSwag.org for more information.\n");
            host.WriteMessage("Execute the 'help' command to show a list of all the available commands.\n");

            try
            {
                var processor = new CommandLineProcessor(host);

                processor.RegisterCommand<WebApiToSwaggerCommand>("webapi2swagger");

                processor.RegisterCommand<JsonSchemaToCSharpCommand>("jsonschema2csharp");
                processor.RegisterCommand<JsonSchemaToTypeScriptCommand>("jsonschema2typescript");
                
                processor.RegisterCommand<SwaggerToCSharpCommand>("swagger2csharp");
                processor.RegisterCommand<SwaggerToTypeScriptCommand>("swagger2typescript");

                processor.Process(args);
            }
            catch (Exception exception)
            {
                var savedForegroundColor = Console.ForegroundColor; 
                Console.ForegroundColor = ConsoleColor.Red;
                host.WriteMessage(exception.ToString());
                Console.ForegroundColor = savedForegroundColor;
            }

            if (Debugger.IsAttached)
            {
                Console.WriteLine("Press <any> key to exit...");
                Console.ReadKey();
            }
        }
Beispiel #2
0
        private static void RegisterCommands(CommandLineProcessor processor)
        {
            // Add our derived NSwagWrapper.Commands:
            processor.RegisterCommand(typeof(PreProcessedSwaggerToCSharpControllerCommand));
            processor.RegisterCommand(typeof(PreProcessedSwaggerToCSharpClientCommand));
            processor.RegisterCommand(typeof(PreProcessedExecuteDocumentCommand));

            // Add all original commands EXCEPT the original versions of the above
            foreach (var nswagCommandType in typeof(SwaggerToCSharpClientCommand).Assembly.GetTypes().Where(t =>
            {
                return(t.IsAbstract == false &&
                       t.IsInterface == false &&
                       t != typeof(ExecuteDocumentCommand) &&
                       t != typeof(SwaggerToCSharpClientCommand) &&
                       t != typeof(SwaggerToCSharpControllerCommand) &&
                       t.CustomAttributes.Any(ca =>
                {
                    return ca.AttributeType == typeof(CommandAttribute);
                }) &&
                       typeof(IConsoleCommand).IsAssignableFrom(t));
            }))
            {
                processor.RegisterCommand(nswagCommandType);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var host = new ConsoleHost();
            host.WriteMessage("NSwag command line: v" + typeof(SwaggerInfo).Assembly.GetName().Version + "\n");
            host.WriteMessage("Visit http://NSwag.org for more information.");
            host.WriteMessage("\n");

            try
            {
                var processor = new CommandLineProcessor(host);

                processor.RegisterCommand<CSharpCommand>("csharp");
                processor.RegisterCommand<TypeScriptCommand>("typescript");

                processor.Process(args);
            }
            catch (Exception exception)
            {
                var savedForegroundColor = System.Console.ForegroundColor;
                System.Console.ForegroundColor = ConsoleColor.Red;
                host.WriteMessage(exception.ToString());
                System.Console.ForegroundColor = savedForegroundColor;
            }

            if (Debugger.IsAttached)
                System.Console.ReadKey();
        }
Beispiel #4
0
        //LiteDB connection
        //static LiteDatabase db = new LiteDatabase(@"Users.db");
        static void Main(string[] args)
        {
            var processor = new CommandLineProcessor(new ConsoleHost(!args.Contains("/noninteractive")));

            //var processor = new CommandLineProcessor(new ConsoleHost());
            processor.RegisterCommand <InsertCommand>("insert");
            processor.RegisterCommand <ListCommand>("list");
            processor.Process(args);
            //Console.ReadKey();
        }
        public void When_adding_command_with_same_name_then_exception_is_thrown()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());
            processor.RegisterCommand<MyCommand>("Test");

            //// Act
            processor.RegisterCommand<MyCommand>("test"); // exception

            //// Assert
        }
        public void When_adding_command_with_same_name_then_exception_is_thrown()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            processor.RegisterCommand <MyCommand>("Test");

            //// Act
            processor.RegisterCommand <MyCommand>("test"); // exception

            //// Assert
        }
Beispiel #7
0
        public void When_adding_command_with_same_name_then_exception_is_thrown()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            processor.RegisterCommand <MyCommand>("Test");

            //// Act
            void RegisterCommand() => processor.RegisterCommand <MyCommand>("test");

            //// Assert
            Assert.Throws <InvalidOperationException>(() => RegisterCommand());
        }
Beispiel #8
0
        static int Main(string[] args)
        {
            var host = new ConsoleHost();

            host.WriteMessage("NSwag command line: NSwag toolchain v" + SwaggerService.ToolchainVersion + " (NJsonSchema v" + JsonSchema4.ToolchainVersion + ")\n");
            host.WriteMessage("Visit http://NSwag.org for more information.\n");

            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.RegisterCommand <NSwagDocumentCommand>("run");

                processor.RegisterCommand <AssemblyTypeToSwaggerCommand>("types2swagger");
                processor.RegisterCommand <WebApiToSwaggerCommand>("webapi2swagger");

                processor.RegisterCommand <JsonSchemaToCSharpCommand>("jsonschema2csclient");
                processor.RegisterCommand <JsonSchemaToTypeScriptCommand>("jsonschema2tsclient");

                processor.RegisterCommand <SwaggerToCSharpClientCommand>("swagger2csclient");
                processor.RegisterCommand <SwaggerToCSharpControllerCommand>("swagger2cscontroller");
                processor.RegisterCommand <SwaggerToTypeScriptClientCommand>("swagger2tsclient");

                var stopwatch = new Stopwatch();
                stopwatch.Start();
                var results = processor.Process(args);
                stopwatch.Stop();

                var output  = results.Last()?.Output;
                var service = output as SwaggerService;
                if (service != null)
                {
                    host.WriteMessage(service.ToJson());
                }
                else if (output != null)
                {
                    host.WriteMessage(output.ToString());
                }

                host.WriteMessage("\nDuration: " + stopwatch.Elapsed);
            }
            catch (Exception exception)
            {
                var savedForegroundColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                host.WriteMessage(exception.ToString());
                Console.ForegroundColor = savedForegroundColor;

                WaitWhenDebuggerAttached();
                return(-1);
            }

            WaitWhenDebuggerAttached();
            return(0);
        }
Beispiel #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);
        }
Beispiel #10
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);
        }
Beispiel #11
0
 static void Main(string[] args)
 {
     var processor = new CommandLineProcessor(new ConsoleHost());
     processor.RegisterCommand<CloneCommand>("clone");
     processor.Process(args);
     Console.ReadKey();
 }
Beispiel #12
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);
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            var processor = new CommandLineProcessor(new ConsoleHost(!args.Contains("/noninteractive")));

            processor.RegisterCommand <CloneCommand>("clone");
            processor.ProcessWithExceptionHandling(args);
            Console.ReadKey();
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            var host = new ConsoleHost();

            host.WriteMessage("NSwag command line: v" + typeof(SwaggerInfo).Assembly.GetName().Version + "\n");
            host.WriteMessage("Visit http://NSwag.org for more information.\n");

            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.RegisterCommand <AssemblyTypeToSwaggerCommand>("types2swagger");
                processor.RegisterCommand <WebApiToSwaggerCommand>("webapi2swagger");

                processor.RegisterCommand <JsonSchemaToCSharpCommand>("jsonschema2csclient");
                processor.RegisterCommand <JsonSchemaToTypeScriptCommand>("jsonschema2tsclient");

                processor.RegisterCommand <SwaggerToCSharpClientCommand>("swagger2csclient");
                processor.RegisterCommand <SwaggerToCSharpControllerCommand>("swagger2cscontroller");
                processor.RegisterCommand <SwaggerToTypeScriptClientCommand>("swagger2tsclient");

                var stopwatch = new Stopwatch();
                stopwatch.Start();
                var results = processor.Process(args);
                stopwatch.Stop();

                var output  = results.Last()?.Output;
                var service = output as SwaggerService;
                if (service != null)
                {
                    host.WriteMessage(service.ToJson());
                }
                else if (output != null)
                {
                    host.WriteMessage(output.ToString());
                }

                host.WriteMessage("\nDuration: " + stopwatch.Elapsed);
            }
            catch (Exception exception)
            {
                var savedForegroundColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                host.WriteMessage(exception.ToString());
                Console.ForegroundColor = savedForegroundColor;
            }

            if (Debugger.IsAttached)
            {
                Console.WriteLine("Press <any> key to exit...");
                Console.ReadKey();
            }
        }
        public void When_adding_a_command_then_the_command_is_in_the_commands_list()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            //// Act
            processor.RegisterCommand<MyCommand>("test");

            //// Assert
            Assert.IsTrue(processor.Commands.ContainsKey("test"));
        }
        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
        }
Beispiel #17
0
        public void When_adding_a_command_then_the_command_is_in_the_commands_list()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            //// Act
            processor.RegisterCommand <MyCommand>("test");

            //// Assert
            Assert.True(processor.Commands.ContainsKey("test"));
        }
        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 command = (MyArgumentCommand)await processor.ProcessAsync(new string[] { "test", "/int64:123" });

            //// Assert
            Assert.IsTrue(123 == command.Int64);
        }
Beispiel #19
0
        public void When_adding_command_with_upper_case_then_it_is_converted_to_lower_case()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            //// Act
            processor.RegisterCommand <MyCommand>("Test");

            //// Assert
            Assert.False(processor.Commands.ContainsKey("Test"));
            Assert.True(processor.Commands.ContainsKey("test"));
        }
        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 void When_adding_command_with_upper_case_then_it_is_converted_to_lower_case()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost(), new MyDependencyResolver());

            //// Act
            processor.RegisterCommand<MyCommand>("Test");

            //// Assert
            Assert.IsFalse(processor.Commands.ContainsKey("Test"));
            Assert.IsTrue(processor.Commands.ContainsKey("test"));
        }
        public void When_first_argument_is_existing_command_name_then_command_is_executed()
        {
            //// Arrange
            var resolver = new MyDependencyResolver(); 
            var processor = new CommandLineProcessor(new ConsoleHost(), resolver);
            processor.RegisterCommand<MyCommand>("test");

            //// Act
            var command = (MyCommand)processor.Process(new string[] { "test" });

            //// Assert
            Assert.IsNotNull(command);
        }
Beispiel #24
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);
        }
Beispiel #25
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);
        }
Beispiel #26
0
        public void When_no_dependency_resolver_is_present_static_ctor_is_not_used()
        {
            //// Arrange
            var processor = new CommandLineProcessor(new ConsoleHost());

            processor.RegisterCommand <CommandWithStaticCtor>("test");

            //// Act
            var result  = processor.Process(new[] { "test" });
            var command = result.Last().Command as CommandWithStaticCtor;

            //// Assert
            Assert.NotNull(command);
        }
Beispiel #27
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);
        }
Beispiel #28
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);
        }
Beispiel #29
0
        public void When_first_argument_is_existing_command_name_then_command_is_executed()
        {
            //// Arrange
            var resolver  = new MyDependencyResolver();
            var processor = new CommandLineProcessor(new ConsoleHost(), resolver);

            processor.RegisterCommand <MyCommand>("test");

            //// Act
            var result  = processor.Process(new[] { "test" });
            var command = result.Last().Command as MyCommand;

            //// Assert
            Assert.NotNull(command);
        }
Beispiel #30
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);
        }
Beispiel #31
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);
        }
Beispiel #32
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);
        }
Beispiel #33
0
        static void Main(string[] args)
        {
            var host = new ConsoleHost();

            host.WriteMessage("NSwag command line: v" + typeof(SwaggerInfo).Assembly.GetName().Version + "\n");
            host.WriteMessage("Visit http://NSwag.org for more information.\n");
            host.WriteMessage("Execute the 'help' command to show a list of all the available commands.\n");

            try
            {
                var processor = new CommandLineProcessor(host);

                processor.RegisterCommand <WebApiToSwaggerCommand>("webapi2swagger");

                processor.RegisterCommand <JsonSchemaToCSharpCommand>("jsonschema2csharp");
                processor.RegisterCommand <JsonSchemaToTypeScriptCommand>("jsonschema2typescript");

                processor.RegisterCommand <SwaggerToCSharpCommand>("swagger2csharp");
                processor.RegisterCommand <SwaggerToTypeScriptCommand>("swagger2typescript");

                processor.Process(args);
            }
            catch (Exception exception)
            {
                var savedForegroundColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                host.WriteMessage(exception.ToString());
                Console.ForegroundColor = savedForegroundColor;
            }

            if (Debugger.IsAttached)
            {
                Console.WriteLine("Press <any> key to exit...");
                Console.ReadKey();
            }
        }
Beispiel #34
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);
        }
        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]);
        }
Beispiel #36
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);
        }
Beispiel #37
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]);
        }
Beispiel #38
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]);
        }
        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]");
            }
        }
Beispiel #44
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_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);
        }
        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);
        }