Example #1
0
        /// <summary>
        /// Adds the default command (implied if program arguments don't begin with a configured command key).
        /// </summary>
        /// <typeparam name="TRunInfo">The run info Type this command is mapped to.</typeparam>
        /// <param name="defaultCommand">The default command configuration object.</param>
        /// <param name="postBuildCallback">An optional callback that's invoked with the built TRunInfo run info object.</param>
        /// <returns>The CommandStore instance.</returns>
        public CommandStore AddDefault <TRunInfo>(DefaultCommand <TRunInfo> defaultCommand,
                                                  Action <TRunInfo> postBuildCallback = null)
            where TRunInfo : class
        {
            if (defaultCommand == null)
            {
                throw new CommandValidationException(
                          "Command must be provided.",
                          CommandValidationError.NullObject, commandLevel: -1);
            }

            if (IsCommand(CommandStore.DefaultKey))
            {
                throw new CommandValidationException(
                          "Default command has already been configured.",
                          CommandValidationError.DuplicateKey, commandLevel: -1);
            }

            DefaultCommandValidator.Validate(defaultCommand);

            Func <string[], Pipeline <TRunInfo> > pipelineFactory = args =>
            {
                Queue <Stage <TRunInfo> > stages = _stagesFactory.Create <TRunInfo>(defaultCommand, postBuildCallback);
                return(new Pipeline <TRunInfo>(stages, args, defaultCommand, _parser, globalOptions: null));
            };

            _pipelineFactoryMap.Add(CommandStore.DefaultKey, pipelineFactory);

            _helpManager.ConfigureForDefaultCommand(defaultCommand);

            return(this);
        }
        public void WhenValidatingInvalidValidatableObjectCommandThenReturnsFalse()
        {
            // Assign
            ICommandValidator     validator = new DefaultCommandValidator();
            ICommand              command   = new ValidatableObjectCommand(false);
            CommandHandlerRequest request   = new CommandHandlerRequest(this.configuration, command);

            // Act
            bool result = validator.Validate(request);

            // Assert
            Assert.False(result);
            Assert.Equal(2, request.ModelState.Sum(kvp => kvp.Value.Errors.Count));
        }
        public void WhenValidatingValidValidatableObjectCommandThenReturnsTrue()
        {
            // Assign
            ICommandValidator     validator = new DefaultCommandValidator();
            ICommand              command   = new ValidatableObjectCommand(true);
            CommandHandlerRequest request   = new CommandHandlerRequest(this.configuration, command);

            // Act
            bool result = validator.Validate(request);

            // Assert
            Assert.True(result);
            Assert.Equal(0, request.ModelState.Count);
        }
        public void WhenValidatingInvalidDataAnnotationsValidatableCommandThenReturnsFalse()
        {
            // Assign
            ICommandValidator validator = new DefaultCommandValidator();
            ICommand          command   = new DataAnnotationsValidatableCommand {
                Property1 = "01234567890123456789"
            };
            CommandHandlerRequest request = new CommandHandlerRequest(this.configuration, command);

            // Act
            bool result = validator.Validate(request);

            // Assert
            Assert.False(result);
            Assert.Equal(1, request.ModelState.Count);
        }
        public void WhenValidatingCommandWithUriThenReturnsTrue()
        {
            // Assign
            ICommandValidator      validator = new DefaultCommandValidator();
            CommandWithUriProperty command   = new CommandWithUriProperty();

            command.Property1 = new Uri("/test/values", UriKind.Relative);
            CommandHandlerRequest request = new CommandHandlerRequest(this.configuration, command);

            // Act
            bool result = validator.Validate(request);

            // Assert
            // A lots of properties of Uri throw exceptions but its still valid
            Assert.True(result);
            Assert.Equal(0, request.ModelState.Count);
        }
        public void WhenValidatingInvalidMixedValidatableCommandThenReturnsFalse()
        {
            // Assign
            ICommandValidator       validator = new DefaultCommandValidator();
            MixedValidatableCommand command   = new MixedValidatableCommand(false);

            command.Property1 = "123456789456132456";
            CommandHandlerRequest request = new CommandHandlerRequest(this.configuration, command);

            // Act
            bool result = validator.Validate(request);

            // Assert
            Assert.False(result);

            // Validator ignore IValidatableObject validation until DataAnnotations succeed.
            Assert.Equal(1, request.ModelState.Count);
        }