public void ArgumentParser_Sets_Argument_Names_And_Values_For_Different_Argument_Types()
        {
            const string charArgumentName = "char argument name";
            const char charArgumentValue = 'A';
            const string intArgumentName = "int argument name";
            const int intArgumentValue = 49;

            var charArgument = new CharArgument(new List<string> { charArgumentName });
            var intArgument = new IntArgument(new List<string> { intArgumentName });
            var allowedArguments = new List<IArgument> { charArgument, intArgument };

            var stringArgsArray = new string[] { };
            var argumentToValueMap = new Dictionary<string, string>
            {
                { charArgumentName, charArgumentValue.ToString() },
                { intArgumentName, intArgumentValue.ToString() }
            };
            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(charArgument.ParsedArgumentName, Is.EqualTo(charArgumentName));
            Assert.That(charArgument.ParsedArgumentValue, Is.EqualTo(charArgumentValue));
            Assert.That(charArgument.ParsedSuccessfully, Is.True);

            Assert.That(intArgument.ParsedArgumentName, Is.EqualTo(intArgumentName));
            Assert.That(intArgument.ParsedArgumentValue, Is.EqualTo(intArgumentValue));
            Assert.That(intArgument.ParsedSuccessfully, Is.True);
        }
Пример #2
0
        public void PossibleArgumentNames_Get_Passes_Through_To_Internal_Argument()
        {
            var expectedArgumentNames = MockRepository.GenerateStub<List<string>>();
            _argument.Expect(a => a.PossibleArgumentNames).Return(expectedArgumentNames);

            var testObject = new IntArgument(_argument, _stringParser);
            var actualArgumentNames = testObject.PossibleArgumentNames;

            Assert.That(actualArgumentNames, Is.EqualTo(expectedArgumentNames));
        }
Пример #3
0
        public void ParsedArgumentName_Get_Passes_Through_To_Internal_Argument()
        {
            const string expectedArgumentName = "some argument name";
            _argument.Expect(a => a.ParsedArgumentName).Return(expectedArgumentName);

            var testObject = new IntArgument(_argument, _stringParser);
            var actualArgumentName = testObject.ParsedArgumentName;

            Assert.That(actualArgumentName, Is.EqualTo(expectedArgumentName));
        }
Пример #4
0
        public void TrySetArgument_Returns_InvalidArgumentName_When_Argument_Name_Is_Incorrect()
        {
            const string argumentName = "some arg";
            _argument.Expect(a => a.TrySetArgumentName(argumentName)).Return(SetArgumentDataResult.InvalidArgumentName);

            var testObject = new IntArgument(_argument, _stringParser);
            var actual = testObject.TrySetArgumentName(argumentName);

            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentName));
            Assert.That(testObject.ParsedSuccessfully, Is.False);
            Assert.That(testObject.ParsedArgumentValue, Is.EqualTo(default(int)));
        }
Пример #5
0
        public void ArgumentNameStringComparison_Get_And_Set_Pass_Through_To_Internal_Argument()
        {
            _argument.Expect(a => a.ArgumentNameStringComparison).SetPropertyWithArgument(StringComparison.CurrentCulture);
            _argument.Expect(a => a.ArgumentNameStringComparison).Return(StringComparison.InvariantCulture);

            var testObject = new IntArgument(_argument, _stringParser)
            {
                ArgumentNameStringComparison = StringComparison.CurrentCulture
            };
            var returnedStringComparison = testObject.ArgumentNameStringComparison;

            Assert.That(returnedStringComparison, Is.EqualTo(StringComparison.InvariantCulture));
        }
        public void ArgumentParser_Returns_False_If_FailOnUnknownArgument_Set_To_True_And_UnknownArgument_Encountered()
        {
            const string charArgumentName = "char argument name";
            const char charArgumentValue = 'A';
            const string intArgumentName = "int argument name";

            var charArgument = new CharArgument(new List<string> { charArgumentName });
            var intArgument = new IntArgument(new List<string> { intArgumentName }) {IsRequired = false};
            var allowedArguments = new List<IArgument> { charArgument, intArgument };

            var stringArgsArray = new string[] { };
            var argumentToValueMap = new Dictionary<string, string>
            {
                { charArgumentName, charArgumentValue.ToString() },
                { intArgumentName, "nonparsable int" }
            };
            var unknownArgumentStrings = new List<string>
            {
                "someArgumentThatCouldn'tBeMapped"
            };
            var argumentMapResult = new ArgumentValueMapperResult { ArgumentToValueMap = argumentToValueMap, UnknownArgumentStrings = unknownArgumentStrings };

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

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

            Assert.That(actualResult.ParsingSuccessful, Is.False);

            Assert.That(charArgument.ParsedArgumentName, Is.EqualTo(charArgumentName));
            Assert.That(charArgument.ParsedArgumentValue, Is.EqualTo(charArgumentValue));
            Assert.That(charArgument.ParsedSuccessfully, Is.True);

            Assert.That(intArgument.ParsedArgumentName, Is.EqualTo(intArgumentName));
            Assert.That(intArgument.ParsedArgumentValue, Is.EqualTo(0));
            Assert.That(intArgument.ParsedSuccessfully, Is.False);

            Assert.That(actualResult.UnparsableArguments["someArgumentThatCouldn'tBeMapped"], Is.Null);
        }
Пример #7
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);
            }
        }
Пример #8
0
        public void TrySetArgument_Returns_InvalidArgumentValue_When_Value_Expected_But_Not_Set()
        {
            const string argumentName = "someArg";
            var testObject = new IntArgument(_argument, _stringParser);

            int parsedValue;
            _stringParser.Expect(s => s.TryParse(null, out parsedValue)).Return(false);
            _argument.Expect(a => a.TrySetArgumentName(argumentName)).Return(SetArgumentDataResult.Success);

            var actual = testObject.TrySetArgumentNameAndValue(argumentName, null);
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.InvalidArgumentValue));
            Assert.That(testObject.ParsedSuccessfully, Is.False);
            Assert.That(testObject.ParsedArgumentValue, Is.EqualTo(default(int)));
        }
Пример #9
0
        public void TrySetArgument_Returns_Success_And_Sets_Parsing_Related_Properties_When_No_Problems_Encountered()
        {
            const string argumentName = "someArg";
            const string argumentValue = "some argument";
            const int expectedArgumentValue = 2;
            var testObject = new IntArgument(_argument, _stringParser);

            int parsedValue;
            _stringParser.Expect(s => s.TryParse(argumentValue, out parsedValue)).OutRef(expectedArgumentValue).Return(true);
            _argument.Expect(a => a.TrySetArgumentName(argumentName)).Return(SetArgumentDataResult.Success);

            var actual = testObject.TrySetArgumentNameAndValue(argumentName, argumentValue);
            Assert.That(actual, Is.EqualTo(SetArgumentDataResult.Success));
            Assert.That(testObject.ParsedSuccessfully, Is.True);
            Assert.That(testObject.ParsedArgumentValue, Is.EqualTo(expectedArgumentValue));
        }
        public void ArgumentParser_Sets_ParsingSuccessful_To_True_If_Argument_Could_Not_Be_Parsed_But_Was_Not_Required_And_FailOnUnknownArgument_Set_To_False()
        {
            const string charArgumentName = "char argument name";
            const char charArgumentValue = 'A';
            const string intArgumentName = "int argument name";

            var charArgument = new CharArgument(new List<string> { charArgumentName });
            var intArgument = new IntArgument(new List<string> { intArgumentName }) {IsRequired = false};
            var allowedArguments = new List<IArgument> { charArgument, intArgument };

            var stringArgsArray = new string[] { };
            var argumentToValueMap = new Dictionary<string, string>
            {
                { charArgumentName, charArgumentValue.ToString() },
                { intArgumentName, "nonparsable int" }
            };
            var argumentMapResult = new ArgumentValueMapperResult { ArgumentToValueMap = argumentToValueMap };

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

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

            Assert.That(actualResult.ParsingSuccessful, Is.True);

            Assert.That(charArgument.ParsedArgumentName, Is.EqualTo(charArgumentName));
            Assert.That(charArgument.ParsedArgumentValue, Is.EqualTo(charArgumentValue));
            Assert.That(charArgument.ParsedSuccessfully, Is.True);

            Assert.That(intArgument.ParsedArgumentName, Is.EqualTo(intArgumentName));
            Assert.That(intArgument.ParsedArgumentValue, Is.EqualTo(0));
            Assert.That(intArgument.ParsedSuccessfully, Is.False);
        }
        public void ArgumentParser_Sets_Argument_Parsing_Result_Unparsed_Values_With_Mapped_Values_That_Fail_Map_To_A_Valid_Argument()
        {
            const string charArgumentName = "char argument name";
            const char charArgumentValue = 'A';
            const string intArgumentName = "int argument name";

            var charArgument = new CharArgument(new List<string> { charArgumentName });
            var intArgument = new IntArgument(new List<string> { intArgumentName });
            var allowedArguments = new List<IArgument> { charArgument, intArgument };

            var stringArgsArray = new string[] { };
            var argumentToValueMap = new Dictionary<string, string>
            {
                { charArgumentName, charArgumentValue.ToString() },
                { intArgumentName, "nonparsable int" }
            };
            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.False);

            Assert.That(charArgument.ParsedArgumentName, Is.EqualTo(charArgumentName));
            Assert.That(charArgument.ParsedArgumentValue, Is.EqualTo(charArgumentValue));
            Assert.That(charArgument.ParsedSuccessfully, Is.True);

            Assert.That(intArgument.ParsedArgumentName, Is.EqualTo(intArgumentName));
            Assert.That(intArgument.ParsedArgumentValue, Is.EqualTo(0));
            Assert.That(intArgument.ParsedSuccessfully, Is.False);

            Assert.That(actualResult.UnparsableArguments[intArgumentName], Is.EqualTo("nonparsable int"));
        }