public void AddFlagParameter_DescriptorAlreadyAddedWithDifferentCasing_ThrowsException() { // arrange var sut = new CommandDescriptor.Builder("command", GetType(), Substitute.For <ITextResolver>()); var property = CreateProperty(); var descriptor1 = new FlagParameterDescriptor("name", property, Substitute.For <ITextResolver>(), false); var descriptor2 = new FlagParameterDescriptor("NAME", property, Substitute.For <ITextResolver>(), false); sut.AddFlagParameter(descriptor1); Action sutAction = () => sut.AddFlagParameter(descriptor2); // act, assert var ex = Assert.Throws <InvalidOperationException>(sutAction); Assert.Contains("Descriptor 'NAME' has already been added.", ex.Message); }
private void AddParameters(Type type, CommandDescriptor.Builder builder) { var properties = type.GetProperties(); foreach (var property in properties) { var requiredAttribute = property.GetCustomAttribute(typeof(RequiredAttribute), true); var descriptions = ExtractDescriptions(property); var attribute = property.GetCustomAttribute(typeof(NumberedParameterAttribute), true); if (attribute != null) { var numberedParameterAttribute = attribute as NumberedParameterAttribute; var descriptor = new NumberedParameterDescriptor( numberedParameterAttribute.Number, numberedParameterAttribute.PlaceholderText, property, descriptions, requiredAttribute != null ); builder.AddNumberedParameter(descriptor); } attribute = property.GetCustomAttribute(typeof(NamedParameterAttribute), true); if (attribute != null) { var namedParameterAttribute = attribute as NamedParameterAttribute; var descriptor = new NamedParameterDescriptor( namedParameterAttribute.Name, namedParameterAttribute.ValuePlaceholderText, property, descriptions, requiredAttribute != null ); builder.AddNamedParameter(descriptor); } attribute = property.GetCustomAttribute(typeof(FlagParameterAttribute), true); if (attribute != null) { if (requiredAttribute != null) { var typeName = type.FullName; var propertyPath = $"{typeName}.{property.Name}"; throw new InvalidParameterDefinitionException(property, string.Format(Texts.FlagParametersCannotBeRequired, propertyPath)); } var flagParameterAttribute = attribute as FlagParameterAttribute; var descriptor = new FlagParameterDescriptor( flagParameterAttribute.Name, property, descriptions, requiredAttribute != null ); builder.AddFlagParameter(descriptor); } } }
public void AddFlagParameter_DescriptorIsNull_ThrowsException() { // arrange var sut = new CommandDescriptor.Builder("command", GetType(), Substitute.For <ITextResolver>()); Action sutAction = () => sut.AddFlagParameter(null); // act, assert var ex = Assert.Throws <ArgumentNullException>(sutAction); Assert.Equal("descriptor", ex.ParamName); }
public void Build_AfterFlagParameterAdded_ReturnsCommandDescriptorWithFlagParameter() { // arrange var sut = new CommandDescriptor.Builder("command", GetType(), Substitute.For <ITextResolver>()); var property = CreateProperty(); var descriptor = new FlagParameterDescriptor("name", property, Substitute.For <ITextResolver>(), false); sut.AddFlagParameter(descriptor); // act var commandDescriptor = sut.Build(); // assert Assert.Equal(1, commandDescriptor.FlagParameters.Count); Assert.Equal(descriptor, commandDescriptor.FlagParameters[0]); }