Esempio n. 1
0
        public void CLIFlow_Run_ShouldRunProperCommandHandler_WhenSetupUsingDI()
        {
            //Arrange
            var stringOptionValue = "sampleString";
            var intOptionValue    = 102;

            var args = new string[]
            {
                "command1",
                "--boolOption",
                "--stringOption", stringOptionValue,
                "--intOption", intOptionValue.ToString()
            };

            var services = new ServiceCollection();

            services.ConfigureCLIFlow(mapping =>
            {
                mapping.Map <Command1, MockCommand1Handler>();
            });

            var provider = services.BuildServiceProvider();

            //Act
            var result = CLIFlow.Create().UseServiceProvider(provider).Run(args);

            //Assert
            var mockCommandHandler = provider.GetRequiredService <MockCommand1Handler>();

            Assert.AreEqual(0, result, "RunCLI method indicted error in returned exit code");
            Assert.IsTrue(mockCommandHandler.LastRunParameters.BoolOption);
            Assert.IsNull(mockCommandHandler.LastRunParameters.NotOptionProperty);
            Assert.AreEqual(stringOptionValue, mockCommandHandler.LastRunParameters.StringOption);
            Assert.AreEqual(intOptionValue, mockCommandHandler.LastRunParameters.IntOption);
        }
Esempio n. 2
0
        public void CLIFlow_Run_ShouldRunOnlyProperCommandHandler_WhenMultipleCommandHandlersAreMapped()
        {
            //Arrange
            var args = new string[]
            {
                "command1",
                "--boolOption", "true",
                "--stringOption", "stringOptionValue",
                "--intOption", "1"
            };

            var mockCommand1Handler = new MockCommand1Handler();
            var mockCommand2Handler = new MockCommand2Handler();
            var mapping             = new CommandMapping().Map <Command1>(mockCommand1Handler)
                                      .Map <Command2>(mockCommand2Handler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual(0, result, "RunCLI method indicted error in returned exit code");

            Assert.IsNull(mockCommand2Handler.LastRunParameters);
            Assert.IsNotNull(mockCommand1Handler.LastRunParameters);
        }
Esempio n. 3
0
        public void CLIFlow_Run_ShouldRunDefaultCommand_WhenNoCommandWasSpecified()
        {
            //Arrange
            var stringOptionValue = "sampleString";
            var intOptionValue    = 102;

            var args = new string[]
            {
                "--boolOption",
                "--stringOption", stringOptionValue,
                "--intOption", intOptionValue.ToString()
            };

            var mockCommandHandler = new MockCommand1Handler();
            var mapping            = new CommandMapping().MapDefault <Command1>(mockCommandHandler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual(0, result, "RunCLI method indicted error in returned exit code");
            Assert.IsTrue(mockCommandHandler.LastRunParameters.BoolOption);
            Assert.IsNull(mockCommandHandler.LastRunParameters.NotOptionProperty);
            Assert.AreEqual(stringOptionValue, mockCommandHandler.LastRunParameters.StringOption);
            Assert.AreEqual(intOptionValue, mockCommandHandler.LastRunParameters.IntOption);
        }
Esempio n. 4
0
        public void CLIFlow_Run_ShouldThrowInvalidCommmandException_ForEmptyArguments()
        {
            //Arrange
            var args = new string[] { };

            var mockCommand1Handler = new MockCommand1Handler();
            var mapping             = new CommandMapping().Map <Command1>(mockCommand1Handler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual((int)ErrorCode.UnspecifiedCommand, result);
        }
Esempio n. 5
0
        public void CLIFlow_Run_ShouldThrowInvalidCommmandException_ForNotMappedCommand()
        {
            var invalidCommandName = "invalidCommandName";

            //Arrange
            var args = new string[]
            {
                invalidCommandName,
                "--boolOption", "true",
            };

            var mockCommand1Handler = new MockCommand1Handler();
            var mapping             = new CommandMapping().Map <Command1>(mockCommand1Handler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual((int)ErrorCode.UnrecognizedCommand, result);
        }
Esempio n. 6
0
        public void CLIFlow_Run_ShouldThrowInvalidOptionException_ForInvalidOption()
        {
            //Arrange
            var args = new string[]
            {
                "command1",
                "--boolOption", "true",
                "--boolOptionXX", "true",
                "--stringOption", "stringOptionValue",
                "--intOption", "1"
            };

            var mockCommand1Handler = new MockCommand1Handler();
            var mapping             = new CommandMapping().Map <Command1>(mockCommand1Handler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual((int)ErrorCode.UnrecognizedOption, result);
        }
Esempio n. 7
0
        public void CLIFlow_Run_ShouldRunProperCommand_WhenCommandNameIncludeDash()
        {
            //Arrange
            var stringOptionValue = "sampleString";

            var args = new string[]
            {
                "command-sample",
                "--stringOption", stringOptionValue,
            };


            var mockCommandHandler = new MockCommand3Handler();
            var mapping            = new CommandMapping().MapDefault <Command3>(mockCommandHandler);

            //Act
            var result = CLIFlow.Create().UseMapping(mapping).Run(args);

            //Assert
            Assert.AreEqual(0, result, "RunCLI method indicted error in returned exit code");
            Assert.AreEqual(stringOptionValue, mockCommandHandler.LastRunParameters.StringOption);
        }
Esempio n. 8
0
        static int Main(string[] args)
        {
            var services = new ServiceCollection();

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Debug);
                builder.AddConsole();
            });
            services.ConfigureCLIFlow(mapping =>
            {
                mapping.Map <MoveCommand, MoveCommandHandler>()
                .MapDefault <HelpCommand, HelpCommandHandler>();
            });

            using (ServiceProvider provider = services.BuildServiceProvider())
            {
                logger = provider.GetRequiredService <ILogger <Program> >();

                return(CLIFlow.Create().UseServiceProvider(provider)
                       .UseFallback(ErrorHandling)
                       .Run(args));
            }
        }