public void GetDescription_ReturnsResourceIfTypeSet() { // Arrange OptionAttribute cmd = new OptionAttribute(typeof(MockResourceType), "ResourceName") { Description = "Not a string from a resouce." }; // Act var actual = cmd.GetDescription(); // Assert Assert.AreEqual("This is a Resource String.", actual); }
public void GetDescription_ReturnsDescriptionIfTypeNotSet() { // Arrange OptionAttribute cmd = new OptionAttribute("ResourceName"); // Act var actual = cmd.GetDescription(); // Assert Assert.AreEqual("ResourceName", actual); }
protected virtual void ProcessOption(OptionAttribute option) { if (option.Name == "Verbosity") Logger.Root.Verbosity = option.ParseAsEnum<Verbosity>(); if (option.Name == "LogVerb") Logger.Root.LogfileVerbosity = option.ParseAsEnum<Verbosity>(); if (option.Name == "DryRun") Monitor.Root.DryRun = option.ParseAsBoolean(); }
public void GetDescription_ReturnsResourceIfTypeSet() { // Arrange OptionAttribute cmd = new OptionAttribute(typeof(MockResourceType), "ResourceName"); // Act var actual = cmd.Description; // Assert Assert.Equal("This is a Resource String.", actual); }
public static OptionSpecification FromAttribute(OptionAttribute attribute, System.Type conversionType) { return new OptionSpecification( attribute.ShortName, attribute.LongName, attribute.Required, attribute.SetName, attribute.Min, attribute.Max, attribute.DefaultValue.ToMaybe(), conversionType, attribute.HelpText, attribute.MetaValue); }
public static OptionSpecification FromAttribute(OptionAttribute attribute, System.Type conversionType, System.Collections.Generic.IEnumerable<string> enumValues) { return new OptionSpecification( attribute.ShortName, attribute.LongName, attribute.Required, attribute.SetName, attribute.Min, attribute.Max, attribute.DefaultValue.ToMaybe(), conversionType, attribute.HelpText, attribute.MetaValue, enumValues); }
public static OptionSpecification FromAttribute(OptionAttribute attribute, Type conversionType, IEnumerable<string> enumValues) { return new OptionSpecification( attribute.ShortName, attribute.LongName, attribute.Required, attribute.SetName, attribute.Min == -1 ? Maybe.Nothing<int>() : Maybe.Just(attribute.Min), attribute.Max == -1 ? Maybe.Nothing<int>() : Maybe.Just(attribute.Max), attribute.Separator, attribute.Default.ToMaybe(), attribute.HelpText, attribute.MetaValue, enumValues, conversionType, conversionType.ToTargetType()); }
public void ExtractOptions_ReturnsEmptyCommandWhenCommandLineIsEmpty() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Message"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); var argsEnumerator = new List<string>().GetEnumerator(); // Act ICommand actualCommand = parser.ExtractOptions(ExpectedCommand, argsEnumerator); // Assert Assert.Equal(0, actualCommand.Arguments.Count); Assert.Null(((MockCommand)actualCommand).Message); }
public OptionInfo(OptionAttribute attribute, PropertyInfo property) { if (attribute == null) { throw new ArgumentNullException("attribute", "The attribute is mandatory"); } if (property == null) { throw new ArgumentNullException("property", "The property is mandatory"); } _required = attribute.Required; _helpText = attribute.HelpText; _shortName = attribute.ShortName; _longName = attribute.LongName; _mutuallyExclusiveSet = attribute.MutuallyExclusiveSet; _defaultValue = attribute.DefaultValue; _hasDefaultValue = attribute.HasDefaultValue; _attribute = attribute; _property = property; }
public void ExtractOptions_AddsArgumentsWhenItemsDoNotStartWithSlashOrDash() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Message"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); var argsEnumerator = new List<string>() { "optionOne", "optionTwo" }.GetEnumerator(); // Act ICommand actualCommand = parser.ExtractOptions(ExpectedCommand, argsEnumerator ); // Assert Assert.AreEqual(2, actualCommand.Arguments.Count); Assert.AreEqual("optionOne", actualCommand.Arguments[0]); Assert.AreEqual("optionTwo", actualCommand.Arguments[1]); Assert.IsNull(((MockCommand)actualCommand).Message); }
public void GetCommandOptions_ReturnsCorrectOpionAttributeAndPropertyInfo() { // Arrange CommandManager cm = new CommandManager(); ICommand cmd = new MockCommand(); cm.RegisterCommand(cmd); Dictionary<OptionAttribute, PropertyInfo> expected = new Dictionary<OptionAttribute, PropertyInfo>(); var expectedOptionAttributeOne = new OptionAttribute("A Option"); var expectedPropertyInfoOne = typeof(MockCommand).GetProperty("Message"); expected.Add(expectedOptionAttributeOne, expectedPropertyInfoOne); var expectedOptionAttributeTwo = new OptionAttribute("A Option Two"); var expectedPropertyInfoTwo = typeof(MockCommand).GetProperty("MessageTwo"); expected.Add(expectedOptionAttributeTwo, expectedPropertyInfoTwo); // Act IDictionary<OptionAttribute, PropertyInfo> actual = cm.GetCommandOptions(cmd); // Assert Assert.AreEqual(2, actual.Count); Assert.AreEqual(expectedOptionAttributeOne, actual.Keys.First()); Assert.AreEqual(expectedPropertyInfoOne, actual[expectedOptionAttributeOne]); Assert.AreEqual(expectedOptionAttributeTwo, actual.Keys.Last()); Assert.AreEqual(expectedPropertyInfoTwo, actual[expectedOptionAttributeTwo]); }
/// <summary> /// Creates a new instance of <see cref="ParsingOptionInfo"/> class. /// </summary> /// <param name="propertyInfo">A property to encapsulate.</param> /// <param name="attribute">Attribute containing configuration of the option.</param> public ParsingOptionInfo(PropertyInfo propertyInfo, OptionAttribute attribute) : base(propertyInfo) { this.attribute = attribute ?? throw new ArgumentNullException(nameof(attribute)); }
public void ExtractOptions_ParsesBoolOptionsAsFalseIfFollowedByDash() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("IsWorking"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); var argsEnumerator = new List<string>() { "-IsWorking-" }.GetEnumerator(); // Act ICommand actualCommand = parser.ExtractOptions(ExpectedCommand, argsEnumerator); // Assert Assert.IsFalse(((MockCommand)actualCommand).IsWorking); }
public void ExtractOptions_UsesShortenedForm() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Message"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); var argsEnumerator = new List<string>() { "/Mess", "foo bar" }.GetEnumerator(); // Act ICommand actualCommand = parser.ExtractOptions(ExpectedCommand, argsEnumerator); // Assert Assert.AreEqual("foo bar", ((MockCommand)actualCommand).Message); }
public void ExtractOptions_ThrowsWhenOptionHasNoValue() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Message"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); string expectedErrorMessage = "Missing option value for: '/Message'"; var argsEnumerator = new List<string>() { "/Message" }.GetEnumerator(); // Act & Assert ExceptionAssert.Throws<CommandLineException>(() => parser.ExtractOptions(ExpectedCommand, argsEnumerator), expectedErrorMessage); }
public void ExtractOptions_ThrowsIfCommandOptionIsAmbigious() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option 1"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Count"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); mockOptionAttribute = new OptionAttribute("Mock Option 2"); mockPropertyInfo = typeof(MockCommand).GetProperty("Counter"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); string expectedErrorMessage = "Ambiguous option 'Co'. Possible values: Count Counter."; var argsEnumerator = new List<string>() { "/Co", "null" }.GetEnumerator(); // Act & Assert ExceptionAssert.Throws<CommandLineException>(() => parser.ExtractOptions(ExpectedCommand, argsEnumerator), expectedErrorMessage); }
public void ExtractOptions_ThrowsIfCommandOptionDoesNotExist() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option 1"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Count"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); mockOptionAttribute = new OptionAttribute("Mock Option 2"); mockPropertyInfo = typeof(MockCommand).GetProperty("Counter"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var ExpectedCommand = new MockCommand(); string expectedErrorMessage = "Unknown option: '/37264752DOESNOTEXIST!!'"; var argsEnumerator = new List<string>() { "/37264752DOESNOTEXIST!!", "false" }.GetEnumerator(); // Act & Assert ExceptionAssert.Throws<CommandLineException>(() => parser.ExtractOptions(ExpectedCommand, argsEnumerator), expectedErrorMessage); }
public void ExtractOptions_ParsesBoolOptionsAsTrueIfPresent() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("IsWorking"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var command = new MockCommand(); var argsEnumerator = new List<string>() { "-IsWorking" }.GetEnumerator(); // Act parser.ExtractOptions(command, argsEnumerator); // Assert Assert.True(command.IsWorking); }
protected override void ProcessOption(OptionAttribute option) { base.ProcessOption(option); if (option.Name == "HashDir") this.HashDir = option.ParseAsString().ToLower(); else if (option.Name == "Exclude") this.ExcludeList = new ExcludeList(option.Value); else if (option.Name == "HashCacheLimit") { if (String.Compare(option.Value, "disabled", true) == 0) HashInfo.CacheLimit = long.MaxValue; else { var m = Regex.Match(option.Value, @"^(?<size>\d+)\s*((?<unit>k|m|g)(b|byte)?)?\s*$", RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); var u = m.Groups["unit"].Value; long f = 1; if (!String.IsNullOrEmpty(u)) { if (u.ToLower() == "k") f = 1024; else if (u.ToLower() == "m") f = 1 << 20; else if (u.ToLower() == "g") f = 1 << 30; } HashInfo.CacheLimit = long.Parse(m.Groups["size"].Value) * f; } } }
public void ExtractOptions_ThrowsIfUnableToConvertType() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Count"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var command = new MockCommand(); string expectedErrorMessage = "Invalid option value: '/Count null'"; var argsEnumerator = new List<string>() { "/Count", "null" }.GetEnumerator(); // Act & Assert ExceptionAssert.Throws<CommandLineException>(() => parser.ExtractOptions(command, argsEnumerator), expectedErrorMessage); }
public void ExtractOptions_ParsesOptionsThatStartWithDash() { // Arrange var cmdMgr = new Mock<ICommandManager>(); var MockCommandOptions = new Dictionary<OptionAttribute, PropertyInfo>(); var mockOptionAttribute = new OptionAttribute("Mock Option"); var mockPropertyInfo = typeof(MockCommand).GetProperty("Message"); MockCommandOptions.Add(mockOptionAttribute, mockPropertyInfo); cmdMgr.Setup(cm => cm.GetCommandOptions(It.IsAny<ICommand>())).Returns(MockCommandOptions); CommandLineParser parser = new CommandLineParser(cmdMgr.Object); var command = new MockCommand(); var argsEnumerator = new List<string>() { "-Message", "foo bar" }.GetEnumerator(); // Act parser.ExtractOptions(command, argsEnumerator); // Assert Assert.Equal("foo bar", command.Message); }
protected override void ProcessOption(OptionAttribute option) { base.ProcessOption(option); if (option.Name == "SkipLevel") this.SkipLevel = (int)option.ParseAsLong(new KeyValuePair<string, long>("disabled", int.MaxValue), new KeyValuePair<string, long>("off", int.MaxValue)); else if (option.Name == "Pattern") this.Pattern = option.ParseAsString(); else if (option.Name == "PrevBackupFolderRoot") this.PrevBackupFolderRoot = option.Value; else if (option.Name == "RestoreAttributes") this.restoreAttributes = option.ParseAsBoolean(); else if (option.Name == "RestoreLastWriteTime") this.restoreLastWriteTime = option.ParseAsBoolean(); else if (option.Name == "ClearArchiveBit") this.clearArchiveBit = option.ParseAsBoolean(); else if (option.Name == "MultiThread") this.multiThread = option.ParseAsBoolean(); else if (option.Name == "CheckFileLength") this.checkFileLength = option.ParseAsBoolean(); else if (option.Name == "UsePreviousBackup") this.usePreviousBackup = option.ParseAsBoolean(); }