Exemplo n.º 1
0
 public void TrySetArgument_Returns_InvalidArgumentName_When_Argument_Name_Is_Incorrect()
 {
     var testObject = new Argument((List<string>)null);
     var actual = testObject.TrySetArgumentName("some arg");
     Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentName));
     Assert.That(testObject.ParsedArgumentName, Is.Null);
 }
Exemplo n.º 2
0
        public void TrySetArgument_Returns_Success_When_Argument_Name_Is_Correct_And_No_Value_Required()
        {
            const string argumentName = "someArg";
            var testObject = new Argument(new List<string> {argumentName});

            var actual = testObject.TrySetArgumentName(argumentName);
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.Success));
            Assert.That(testObject.ParsedArgumentName, Is.EqualTo(argumentName));
        }
Exemplo n.º 3
0
        public void TrySetArgument_Returns_InvalidArgumentName_When_Argument_Name_Does_Not_Match_Any_Of_The_Possible_Values()
        {
            const string possibleArgumentName1 = "someArg";
            const string possibleArgumentName2 = "someArg2";
            var testObject = new Argument(new List<string> { possibleArgumentName1, possibleArgumentName2 });

            var actual = testObject.TrySetArgumentName("someInvalidArg");
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentName));
            Assert.That(testObject.ParsedArgumentName, Is.Null);

            actual = testObject.TrySetArgumentName("someArg3");
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentName));
            Assert.That(testObject.ParsedArgumentName, Is.Null);
        }
Exemplo n.º 4
0
        public void TrySetArgument_Returns_Success_When_Argument_Name_Matches_Any_Of_The_Possible_Values()
        {
            const string possibleArgumentName1 = "someArg";
            const string possibleArgumentName2 = "someArg2";
            var testObject = new Argument(new List<string> { possibleArgumentName1, possibleArgumentName2 });

            var actual = testObject.TrySetArgumentName(possibleArgumentName1);
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.Success));
            Assert.That(testObject.ParsedSuccessfully, Is.True);
            Assert.That(testObject.ParsedArgumentName, Is.EqualTo(possibleArgumentName1));

            actual = testObject.TrySetArgumentName(possibleArgumentName2);
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.Success));
            Assert.That(testObject.ParsedSuccessfully, Is.True);
            Assert.That(testObject.ParsedArgumentName, Is.EqualTo(possibleArgumentName2));
        }
Exemplo n.º 5
0
        public void TrySetArgument_Ignores_Case_Based_On_StringComparison_Property()
        {
            const string argumentName = "someArg";
            var testObject = new Argument(new List<string> { argumentName })
            {
                ArgumentNameStringComparison = StringComparison.CurrentCultureIgnoreCase
            };

            var actual = testObject.TrySetArgumentName(argumentName.ToLower());
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.Success));

            testObject = new Argument(new List<string> { argumentName })
            {
                ArgumentNameStringComparison = StringComparison.CurrentCulture
            };

            actual = testObject.TrySetArgumentName(argumentName.ToLower());
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentName));
            Assert.That(testObject.ParsedArgumentName, Is.Null);
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            var helpArgument = new Argument(new List<string> { "help", "h", "?" }) {IsRequired = false};
            var fileTypeArgument = new CharArgument(new List<string> { "fileType", "type" });
            var fileNameArgument = new StringArgument(new List<string> { "fileName", "file" });
            var refDateArgument = new DateTimeArgument("refdate") {IsRequired = false};
            var countArgument = new IntArgument(new List<string> { "argumentCount", "count" }) {IsRequired = false};
            var allowDuplicateArgument = new BoolArgument("allowduplicatefile")
            {
                ArgumentNameStringComparison = StringComparison.CurrentCulture, //Case-sensitive
                IsRequired = false,
                UsageDescription = "Indicates whether or not to allow duplicate files"
            };

            var argumentParser = new ArgumentParser();
            IList<IArgument> allowedArguments = new List<IArgument>
            {
                helpArgument,
                fileTypeArgument,
                fileNameArgument,
                refDateArgument,
                countArgument,
                allowDuplicateArgument
            };

            var allArgumentsParsingResult = argumentParser.Parse(args, allowedArguments);

            //Solely for testing
            Console.WriteLine(JsonConvert.SerializeObject(allArgumentsParsingResult, Formatting.Indented));

            if (helpArgument.ParsedSuccessfully || !allArgumentsParsingResult.ParsingSuccessful)
            {
                var usage = argumentParser.GetUsage(ProgramDescription, allowedArguments);
                Console.WriteLine(usage);
            }
        }
        public void ArgumentParser_Sets_Argument_Name_And_Value_When_Appropriate()
        {
            const string argumentName = "argumentName";
            const string argumentWithValueName = "SomeArgumentWithValueName";
            const string argumentValue = "some argument value";

            var argument = new Argument(new List<string> { argumentName });
            var stringArgument = new StringArgument(new List<string> { argumentWithValueName });
            var allowedArguments = new List<IArgument> {argument, stringArgument };

            var stringArgsArray = new string[]{};
            var argumentToValueMap = new Dictionary<string, string>
            {
                { argumentName, null },
                { argumentWithValueName, argumentValue}
            };
            var argumentMapResult = new ArgumentValueMapperResult { ArgumentToValueMap = argumentToValueMap};

            _argumentValueMapper.Expect(a => a.GetArgumentToValueMap(stringArgsArray, _testObject.ArgumentDelimeters, _testObject.ValueDelimeters)).Return(argumentMapResult);

            var actualResult = _testObject.Parse(stringArgsArray, allowedArguments);

            Assert.That(actualResult.ParsingSuccessful, Is.True);
            Assert.That(argument.ParsedArgumentName, Is.EqualTo(argumentName));
            Assert.That(argument.ParsedSuccessfully, Is.True);

            Assert.That(stringArgument.ParsedArgumentName, Is.EqualTo(argumentWithValueName));
            Assert.That(stringArgument.ParsedArgumentValue, Is.EqualTo(argumentValue));
            Assert.That(stringArgument.ParsedSuccessfully, Is.True);
        }