public ParameterMetadata BuildParameterMetadata() { var parameterTranslations = GetParameterTranslations(); var ordinalDescriptors = new OrdinalParameterDescriptor[parameterTranslations.OrdinalParameterCount]; for (var i = 1; i <= ordinalDescriptors.Length; i++) { ordinalDescriptors[i - 1] = new OrdinalParameterDescriptor(i, parameterTranslations.SupportsOrdinalParameterMetadata ? parameterTranslations.GetOrdinalParameterExpectedType(i) : null, parameterTranslations.GetOrdinalParameterSqlLocation(i)); } var namedDescriptorMap = new Dictionary <string, NamedParameterDescriptor>(); foreach (var name in parameterTranslations.GetNamedParameterNames()) { namedDescriptorMap[name] = new NamedParameterDescriptor(name, parameterTranslations.GetNamedParameterExpectedType(name), parameterTranslations.GetNamedParameterSqlLocations(name), false); // description.JpaStyle); } return(new ParameterMetadata(ordinalDescriptors, namedDescriptorMap)); }
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 Ctor_WhenCalled_SetsProperties() { // arrange var property = CreateProperty(); var textResolver = Substitute.For <ITextResolver>(); // act var sut = new NamedParameterDescriptor("name", "value", property, textResolver, true); // assert Assert.Equal("name", sut.Name); Assert.Equal("value", sut.ValuePlaceholderText); Assert.Equal(property, sut.Property); Assert.True(sut.Required); }
public void Build_AfterNamedParameterAdded_ReturnsCommandDescriptorWithNamedParameter() { // arrange var sut = new CommandDescriptor.Builder("command", GetType(), Substitute.For <ITextResolver>()); var property = CreateProperty(); var descriptor = new NamedParameterDescriptor("name", "value", property, Substitute.For <ITextResolver>(), false); sut.AddNamedParameter(descriptor); // act var commandDescriptor = sut.Build(); // assert Assert.Equal(1, commandDescriptor.NamedParameters.Count); Assert.Equal(descriptor, commandDescriptor.NamedParameters["name"]); }
public void AddNamedParameter_DescriptorAlreadyAddedWithDifferentCasing_ThrowsException() { // arrange var sut = new CommandDescriptor.Builder("command", GetType(), Substitute.For <ITextResolver>()); var property = CreateProperty(); var descriptor1 = new NamedParameterDescriptor("name", "value", property, Substitute.For <ITextResolver>(), false); var descriptor2 = new NamedParameterDescriptor("NAME", "value", property, Substitute.For <ITextResolver>(), false); sut.AddNamedParameter(descriptor1); Action sutAction = () => sut.AddNamedParameter(descriptor2); // act, assert var ex = Assert.Throws <InvalidOperationException>(sutAction); Assert.Contains("Descriptor 'NAME' has already been added.", ex.Message); }