public void IsVerbalParameter_ArgumentProcessorSettingWithAttribute_ResultAsExpected(Type type, Boolean expected) { ParameterObjectAttribute attribute = null; if (type == typeof(SwitchParameterAttribute)) { attribute = new SwitchParameterAttribute(); } else if (type == typeof(OptionParameterAttribute)) { attribute = new OptionParameterAttribute(); } else if (type == typeof(VerbalParameterAttribute)) { attribute = new VerbalParameterAttribute(); } else if (type == typeof(UnsupportedParameterAttribute)) { attribute = new UnsupportedParameterAttribute(); } ArgumentProcessorSetting setting = new ArgumentProcessorSetting(new TestPropertyInfo(typeof(Object)), attribute); Assert.That(setting.IsVerbalParameter(), Is.EqualTo(expected)); }
public void IsConverterSupported_WrongParameterObjectAttributeType_ResultIsFalse(Type type) { ParameterObjectAttribute attribute = null; if (type == typeof(SwitchParameterAttribute)) { attribute = new SwitchParameterAttribute(); } else if (type == typeof(VerbalParameterAttribute)) { attribute = new VerbalParameterAttribute(); } else if (type == typeof(UnsupportedParameterAttribute)) { attribute = new UnsupportedParameterAttribute(); } Assert.That(attribute.IsConverterSupported(null), Is.False); }
/// <summary> /// Initializes an instance of this class. /// </summary> /// <remarks> /// This method initializes an instance of this class and implicitly /// performs an argument validation. /// </remarks> public void Initialize() { try { this.Settings = new List <ArgumentProcessorSetting>(); VerbalParameterAttribute lastVerbal = null; Dictionary <String, PropertyInfo> solidLabels = new Dictionary <String, PropertyInfo>(); Dictionary <String, PropertyInfo> briefLabels = new Dictionary <String, PropertyInfo>(); BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty; PropertyInfo[] properties = this.Instance.GetType().GetProperties(flags); if (properties != null) { foreach (PropertyInfo property in properties) { IEnumerable <Attribute> attributes = property.GetCustomAttributes(); if (attributes != null) { foreach (Attribute current in attributes) { if (current is ParameterObjectAttribute) { ParameterObjectAttribute attribute = current as ParameterObjectAttribute; if (attribute is SwitchParameterAttribute || attribute is OptionParameterAttribute) { if (attribute.IsSolidLabel || attribute.IsBriefLabel) { if (attribute.IsSolidLabel) { if (!solidLabels.ContainsKey(attribute.SolidLabel)) { solidLabels[attribute.SolidLabel] = property; } else { throw new UtilizeViolationException( $"The solid label \"{attribute.SolidLabel}\" of property \"{property.Name}\" is already used " + $"by property \"{solidLabels[attribute.SolidLabel].Name}\". All solid labels must be unique."); } } if (attribute.IsBriefLabel) { if (!briefLabels.ContainsKey(attribute.BriefLabel)) { briefLabels[attribute.BriefLabel] = property; } else { throw new UtilizeViolationException( $"The brief label \"{attribute.BriefLabel}\" of property \"{property.Name}\" is already used " + $"by property \"{briefLabels[attribute.BriefLabel].Name}\". All brief labels must be unique."); } } } else { throw new UtilizeViolationException( $"Neither the solid label nor the brief label is used by property \"{property.Name}\". " + $"Empty labels are only supported by verbal parameter types."); } } else if (attribute is VerbalParameterAttribute) { if (lastVerbal == null) { lastVerbal = attribute as VerbalParameterAttribute; } else { throw new VerbalViolationException( $"More than one verbal parameter is not supported " + $"per instance of \"{this.Instance.GetType().Name}\"."); } } else { throw new SupportViolationException( $"A property attribute of type \"{attribute.GetType().Name}\" " + $"is not supported."); } if (attribute.IsSupportedDataType(property)) { this.Settings.Add(this.ApplyDefaultValue(new ArgumentProcessorSetting(property, attribute))); } else if (attribute.IsConverterSupported(property)) { this.Settings.Add(new ArgumentProcessorSetting(property, attribute, true)); } // TODO: Remove obsolete code later on. /* obsolete start */ else if (property.PropertyType.HasConverter()) { this.Settings.Add(new ArgumentProcessorSetting(property, attribute)); } /* obsolete end */ else { throw new SupportViolationException( $"Type \"{property.PropertyType}\" of property \"{property.Name}\" " + $"is not supported."); } } } } } } } catch (Exception exception) { exception.ThrowArgumentParserException(); } }
public void BriefLabel_SetProperty_ThrowsException(String actual) { VerbalParameterAttribute attribute = new VerbalParameterAttribute(); Assert.Throws <VerbalAttributeException>(() => { attribute.BriefLabel = actual; }); }