public void TestArgumentParser_Parse_NonString()
        {
            // Int & Enum values
            string[]   args       = { "/s:svalue", "/a:a1value", "/iv:23", "/ev:Third" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.AreEqual("svalue", testParams.String);
            Assert.AreEqual(1, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.AreEqual(23, testParams.Count);
            Assert.AreEqual(TestOrdinal.Third, testParams.OrdinalVal);

            // Int & Enum arrays
            args       = new [] { "-s:value", "-a:a1value", "-ia:4,5", "/ea:First,Second" };
            testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.AreEqual("value", testParams.String);
            Assert.AreEqual(1, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.AreEqual(2, testParams.IntArray.Count());
            Assert.IsTrue(testParams.IntArray.Contains(4));
            Assert.IsTrue(testParams.IntArray.Contains(5));
            Assert.AreEqual(2, testParams.EnumArray.Count());
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.First));
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.Second));
        }
        public void TestArgumentParser_Parse_Basic()
        {
            string[]   args       = { "/s", "s-hyphen-value", "/a", "a1value", "a2value", "-f" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.IsTrue(testParams.IsFree);
            Assert.AreEqual("s-hyphen-value", testParams.String);
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.IsTrue(testParams.Array.Contains("a2value"));

            args       = new [] { "-s:value", "-a", "a1value", "a2value" };
            testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.AreEqual("value", testParams.String);
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.IsTrue(testParams.Array.Contains("a2value"));

            args       = new [] { "/flag", "/a:a1value,a2value", "/s=value" };
            testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.IsTrue(testParams.IsFree);
            Assert.AreEqual("value", testParams.String);
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.IsTrue(testParams.Array.Contains("a2value"));
        }
        public void TestArgumentParser_Parse_MissingParams()
        {
            string[]   args       = { "/a", "a1value", "a2value" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNull(testParams);

            // Ensure error message is displayed
            Assert.AreEqual(1, Logger.ErrorLog.Count);
            Assert.AreEqual("Error: Argument [-s or --str] is required.", Logger.ErrorLog.First());
        }
        public void TestArgumentParser_Parse_UnrecognizedParam()
        {
            string[]   args       = { "/xyz", "value" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNull(testParams);

            // Ensure error message is displayed
            Assert.AreEqual(1, Logger.ErrorLog.Count);
            Assert.AreEqual("Error: Unrecogized parameter '/xyz'.", Logger.ErrorLog.First());
        }
        public void TestArgumentParser_Parse_DuplicateParams()
        {
            string[]   args       = { "/s:sval1", "/a:a1value", "/s:sval2" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNull(testParams);

            // Ensure error message is displayed
            Assert.AreEqual(1, Logger.ErrorLog.Count);
            Assert.AreEqual("Error: Argument 's' re-defined with value 'sval2'.", Logger.ErrorLog.First());
        }
        public void TestArgumentParser_Parse_Defaults()
        {
            // Default values set correctly
            string[]   args       = { "/s:svalue", "/a:a1value" };
            TestParams testParams = ArgumentParser <TestParams> .Parse(args);

            Assert.IsNotNull(testParams);
            Assert.AreEqual("svalue", testParams.String);
            Assert.AreEqual(1, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("a1value"));
            Assert.AreEqual(true, testParams.FreeDefault);
            Assert.AreEqual(42, testParams.Count);
            Assert.AreEqual(2, testParams.EnumArray.Count());
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.Second));
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.Third));
        }
Beispiel #7
0
        public void TestArgumentParserUtils_TrySetPropertyValue_Single()
        {
            IDictionary <string, ArgumentData> data = ArgumentParserUtils.GetArgumentData <TestParams>();
            TestParams testParams = new TestParams();

            Assert.IsTrue(testParams.TrySetPropertyValue(data["s"].Property, "value"));
            Assert.AreEqual("value", testParams.String);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["iv"].Property, "7"));
            Assert.AreEqual(7, testParams.Count);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["ev"].Property, "First"));
            Assert.AreEqual(TestOrdinal.First, testParams.OrdinalVal);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["f"].Property, "true"));
            Assert.AreEqual(true, testParams.IsFree);
        }
Beispiel #8
0
        public void TestArgumentParserUtils_TrySetPropertyValue_Invalid()
        {
            IDictionary <string, ArgumentData> data = ArgumentParserUtils.GetArgumentData <TestParams>();
            TestParams testParams = new TestParams();

            Assert.IsFalse(testParams.TrySetPropertyValue(data["iv"].Property, "NaN"));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["ev"].Property, "Sec"));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["df"].Property, "NotTrue"));

            Assert.IsFalse(testParams.TrySetPropertyValue(data["ia"].Property, new[] { "NaN", "1" }));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["ea"].Property, new[] { "First", "Sec" }));

            // Ensure error messages are displayed
            IList <string> errorLog = Logger.ErrorLog;

            Assert.AreEqual(5, errorLog.Count);
            Assert.AreEqual("Error: Given value 'NaN' should be of type 'Int32'", errorLog[0]);
            Assert.AreEqual("Error: Unknown value 'Sec' given. Valid values - [First, Second, Third]", errorLog[1]);
            Assert.AreEqual("Error: Given value 'NotTrue' should be of type 'Boolean'", errorLog[2]);

            Assert.AreEqual("Error: Given values 'NaN, 1' should be a list of type 'Int32'", errorLog[3]);
            Assert.AreEqual("Error: Given list 'First, Sec' contains an unknown value. Valid values - [First, Second, Third]", errorLog[4]);
        }
Beispiel #9
0
        public void TestArgumentParserUtils_TrySetPropertyValue_Array()
        {
            IDictionary <string, ArgumentData> data = ArgumentParserUtils.GetArgumentData <TestParams>();
            TestParams testParams = new TestParams();

            // string array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["a"].Property, new[] { "value1", "value2" }));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("value1"));
            Assert.IsTrue(testParams.Array.Contains("value2"));

            // int array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["ia"].Property, new[] { "2", "3" }));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.IntArray.Contains(2));
            Assert.IsTrue(testParams.IntArray.Contains(2));

            // Enum array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["ea"].Property, new[] { "First", "Second" }));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.First));
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.Second));
        }
        public void TestArgumentParserUtils_TrySetPropertyValue_Single()
        {
            IDictionary<string, ArgumentData> data = ArgumentParserUtils.GetArgumentData<TestParams>();
            TestParams testParams = new TestParams();

            Assert.IsTrue(testParams.TrySetPropertyValue(data["s"].Property, "value"));
            Assert.AreEqual("value", testParams.String);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["iv"].Property, "7"));
            Assert.AreEqual(7, testParams.Count);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["ev"].Property, "First"));
            Assert.AreEqual(TestOrdinal.First, testParams.OrdinalVal);

            Assert.IsTrue(testParams.TrySetPropertyValue(data["f"].Property, "true"));
            Assert.AreEqual(true, testParams.IsFree);
        }
        public void TestArgumentParserUtils_TrySetPropertyValue_Invalid()
        {
            IDictionary<string, ArgumentData> data = ArgumentParserUtils.GetArgumentData<TestParams>();
            TestParams testParams = new TestParams();

            Assert.IsFalse(testParams.TrySetPropertyValue(data["iv"].Property, "NaN"));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["ev"].Property, "Sec"));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["df"].Property, "NotTrue"));

            Assert.IsFalse(testParams.TrySetPropertyValue(data["ia"].Property, new[] { "NaN", "1"}));
            Assert.IsFalse(testParams.TrySetPropertyValue(data["ea"].Property, new[] { "First", "Sec"}));

            // Ensure error messages are displayed
            IList<string> errorLog = Logger.ErrorLog;
            Assert.AreEqual(5, errorLog.Count);
            Assert.AreEqual("Error: Given value 'NaN' should be of type 'Int32'", errorLog[0]);
            Assert.AreEqual("Error: Unknown value 'Sec' given. Valid values - [First, Second, Third]", errorLog[1]);
            Assert.AreEqual("Error: Given value 'NotTrue' should be of type 'Boolean'", errorLog[2]);

            Assert.AreEqual("Error: Given values 'NaN, 1' should be a list of type 'Int32'", errorLog[3]);
            Assert.AreEqual("Error: Given list 'First, Sec' contains an unknown value. Valid values - [First, Second, Third]", errorLog[4]);
        }
        public void TestArgumentParserUtils_TrySetPropertyValue_Array()
        {
            IDictionary<string, ArgumentData> data = ArgumentParserUtils.GetArgumentData<TestParams>();
            TestParams testParams = new TestParams();

            // string array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["a"].Property, new[] {"value1", "value2"}));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.Array.Contains("value1"));
            Assert.IsTrue(testParams.Array.Contains("value2"));

            // int array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["ia"].Property, new[] { "2", "3" }));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.IntArray.Contains(2));
            Assert.IsTrue(testParams.IntArray.Contains(2));

            // Enum array
            Assert.IsTrue(testParams.TrySetPropertyValue(data["ea"].Property, new[] { "First", "Second" }));
            Assert.AreEqual(2, testParams.Array.Count());
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.First));
            Assert.IsTrue(testParams.EnumArray.Contains(TestOrdinal.Second));
        }