public void FillProperty_WithNullArgumentParameter_Throws() { var property = typeof(TestArguments).GetTypeInfo().DeclaredProperties.First(a => a.Name == "RequiredString"); Assert.Throws <ArgumentNullException>(() => ArgumentPropertiesHelper.FillProperty <TestArguments>(null, property, "value", Enumerable.Empty <TypeParser>()) ); }
public void FillProperty_StringProperty_IsParsedCorrectly() { const string testValue = "TestValue"; var typeParsers = new List <TypeParser>() { new StringTypeParser() }; var property = typeof(TestArguments).GetTypeInfo().DeclaredProperties.First(a => a.Name == "RequiredString"); var arguments = new TestArguments(); ArgumentPropertiesHelper.FillProperty(arguments, property, testValue, typeParsers); Assert.Equal(testValue, arguments.RequiredString); }
/// <inheritdoc /> public virtual ParseResult <T> Parse <T>(string[] arguments, IEnumerable <IArgumentValidator> validators, IEnumerable <ITypeParser> typeParsers) where T : Arguments, new() { _ = arguments ?? throw new ArgumentNullException(nameof(arguments)); _ = validators ?? throw new ArgumentNullException(nameof(validators)); _ = typeParsers ?? throw new ArgumentNullException(nameof(typeParsers)); var argumentsObject = new T(); var allValidationErrors = new List <ValidationError>(); var properties = typeof(T).GetTypeInfo().GetRuntimeProperties(); foreach (var currentProperty in properties) { var argumentIsSpecified = this.TryFindRawArgumentValue(arguments, currentProperty.Name, out var argumentValue); if (argumentIsSpecified) { ArgumentPropertiesHelper.FillProperty(argumentsObject, currentProperty, argumentValue, typeParsers); } var isValid = ValidationHelper.TryValidate( validators, currentProperty, argumentIsSpecified, argumentValue, out var validationErrors); if (!isValid) { allValidationErrors.AddRange(validationErrors); } } argumentsObject.IsHelpRequested = this.ContainsHelpArgument(arguments); var result = new ParseResult <T>(argumentsObject, allValidationErrors); return(result); }
public void FillProperty_WithNullTypeParsersParameter_Throws() { Assert.Throws <ArgumentNullException>(() => ArgumentPropertiesHelper.FillProperty(new TestArguments(), null, "value", null) ); }
public void FillProperty_WithEmptyPropertyValueParameter_DoesNotThrow() { var property = typeof(TestArguments).GetTypeInfo().DeclaredProperties.First(a => a.Name == "RequiredString"); ArgumentPropertiesHelper.FillProperty(new TestArguments(), property, "", Enumerable.Empty <TypeParser>()); }
public void FillProperty_WithNullPropertyParameter_Throws() { Assert.Throws <ArgumentNullException>(() => ArgumentPropertiesHelper.FillProperty(new TestArguments(), null, "value", Enumerable.Empty <TypeParser>()) ); }