예제 #1
0
        public void TestKeyValueOnlyParsing_Good()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .OverrideTypeParsers(() => new TypeParserContainer(false, new KeyValueParser(), new ObjectParser()))
                .AddSwitch <KeyValuePair <string, int> >("NameAge")
                .AddSwitch <string>("FirstName")
                .FinishBuilding("/NameAge:Yizzy:30");

            TypeParserContainer _container = _paramObj.GetPropertyValue <TypeParserContainer>("TypeParser");

            Assert.IsTrue(_container.TypeParsers.Count() == 2, "Only 2 type parsers should have existed");

            bool _parseErr = false;

            try
            {
                _paramObj.CheckParams();
            }
            catch { _parseErr = true; }
            Assert.IsFalse(_parseErr, "String and int parsers were explicated, so this should NOT have failed");

            KeyValuePair <string, int>?_namgeAge = _paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge");

            Assert.IsNotNull(_namgeAge);
            Assert.IsTrue(_namgeAge.Value.Key == "Yizzy");
            Assert.IsTrue(_namgeAge.Value.Value == 30);
        }
예제 #2
0
        public void TestKeyValueOnlyParsing_Bad()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .OverrideTypeParsers(() => new TypeParserContainer(false, new KeyValueParser()))
                .AddSwitch <KeyValuePair <string, int> >("NameAge")
                .AddSwitch <string>("FirstName")
                .FinishBuilding("/NameAge:Yizzy:30");

            TypeParserContainer _container = _paramObj.GetPropertyValue <TypeParserContainer>("TypeParser");

            Assert.IsTrue(_container.TypeParsers.Count() == 1, "More than 1 type parser should not have existed");

            bool _parseErr = false;

            try
            {
                _paramObj.CheckParams();
            }
            catch { _parseErr = true; }
            Assert.IsTrue(_parseErr, "String and int parsers were not explicated, so this should have failed");
            KeyValuePair <string, int> _namgeAge =
                _paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge");

            Assert.IsNull(_namgeAge.Key, "Name key should have been null");
            Assert.IsTrue(_namgeAge.Value == 0, "Age value should have been null");
        }
예제 #3
0
        public void Test_Override_DateTimeParsing_Good()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .AddSwitch <DateTime>("Bday")
                .AddSwitch <int>("Age")
                .AddSwitch <string>("Name")
                .OverrideTypeParsers(() => new TypeParserContainer(true, new DefaultTypeContainer(), new DayFirstDashOnlyDateTimeParser()))
                .FinishBuilding("/Bday:28-11-1987", "/Age:30", "/Name:Yisrael Lax");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj);
            Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == new DateTime(1987, 11, 28));
            Assert.IsTrue(_paramObj.GetPropertyValue <int>("Age") == 30);
            Assert.IsTrue(_paramObj.GetPropertyValue <string>("Name") == "Yisrael Lax");
        }
예제 #4
0
        public void Test_TypeTypeParser_FriendName_Bad()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <Type>("PersonType")
                                     .FinishBuilding("/PersonType:NotReal");

            Assert.IsTrue(_paramObj.GetPropertyValue <Type>("PersonType") == null);
        }
예제 #5
0
        public void Test_Override_DateTimeParsing_Bad()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .AddSwitch <DateTime>("Bday")
                .AddSwitch <int>("Age")
                .AddSwitch <string>("Name")
                .FinishBuilding("/Bday:28/11/1987", "/Age:30", "/Name:Yisrael Lax");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj,
                                                      "Parsing should have failed b/c incorrect DateTime format",
                                                      true,
                                                      "string was not recognized as a valid datetime");
            Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == default(DateTime));
            Assert.IsTrue(_paramObj.GetPropertyValue <int>("Age") == 30);
            Assert.IsTrue(_paramObj.GetPropertyValue <string>("Name") == "Yisrael Lax");
        }
예제 #6
0
        public void Test_FlagsEnumParsing_Good()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <MyPets>("Pets")
                                     .FinishBuilding("/Pets:Dog,Turtle");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj);
            Assert.IsTrue(_paramObj.GetPropertyValue <MyPets>("Pets") == (MyPets.Dog | MyPets.Turtle));
        }
예제 #7
0
        public void Test_BasicEnumParsing_Good()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <MyColors>("Color")
                                     .FinishBuilding("/Color:Yellow");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj);
            Assert.IsTrue(_paramObj.GetPropertyValue <MyColors>("Color") == MyColors.Yellow);
        }
예제 #8
0
        public void Test_TypeTypeParser_SwitchValueList_ClassName_Good()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <Type>("PersonType", "PersonType", true, -1, "Employee", "Customer")
                                     .FinishBuilding("/PersonType:EmployeePersonType");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj, "PersonType parsing failed");
            Assert.IsTrue(_paramObj.GetPropertyValue <Type>("PersonType") == typeof(EmployeePersonType));
        }
예제 #9
0
        public void TestAllDefaultParsing_Not_AllValuesSet()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .AddSwitch <string[]>("Nums")
                .AddSwitch <bool>("IsItTrue")
                .AddSwitch <KeyValuePair <string, int> >("NameAge")
                .AddSwitch <MyColors>("Color")
                .AddSwitch <int>("Height")
                .AddSwitch <SecureString>("pw")
                .FinishBuilding("/IsItTrue:true",
                                "/NameAge:yiz:30", "/Color:Blue", "/pw:passw0rd");

            bool _parseErr = false;

            try
            {
                _paramObj.CheckParams();
            }
            catch { _parseErr = true; }
            Assert.IsFalse(_parseErr, "Check params failed");
            Assert.IsTrue(_paramObj.GetPropertyValue <string[]>("Nums") == null);
            Assert.IsTrue(_paramObj.GetPropertyValue <bool>("IsItTrue") == true);
            Assert.IsTrue(_paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge").Value == 30);
            Assert.IsTrue(_paramObj.GetPropertyValue <MyColors>("Color") == MyColors.Blue);
            Assert.IsTrue(_paramObj.GetPropertyValue <int>("Height") == 0);
            Assert.IsTrue(_paramObj.GetPropertyValue <SecureString>("pw").Length == 8);
        }
예제 #10
0
        public void TestEnumArray_Good()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <MyColors[]>("Colors")
                                     .FinishBuilding("/Colors:Orange,Yellow");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj);
            MyColors[] _colors = _paramObj.GetPropertyValue <MyColors[]>("Colors");
            Assert.IsNotNull(_colors);
            Assert.IsTrue(_colors.Length == 2);
            Assert.IsTrue(_colors[0] == MyColors.Orange);
            Assert.IsTrue(_colors[1] == MyColors.Yellow);
        }
예제 #11
0
        public void TestKeyValueArray_Good()
        {
            ParamsObject _paramObj = DynamicParamsCreator
                                     .Create()
                                     .AddSwitch <KeyValuePair <string, int>[]>("NameAges")
                                     .FinishBuilding("/NameAges:Yisrael:30,Srully:10,Yitschak:40");

            ParamsObjectTestHelpers.AssertCheckParams(_paramObj);
            KeyValuePair <string, int>[] _nameAges = _paramObj.GetPropertyValue <KeyValuePair <string, int>[]>("NameAges");
            Assert.IsNotNull(_nameAges);
            Assert.IsTrue(_nameAges.Length == 3);
            Assert.IsTrue(_nameAges[1].Key == "Srully");
            Assert.IsTrue(_nameAges[1].Value == 10);
        }
예제 #12
0
        public void TestStringInput()
        {
            //string switchName = "", bool required = false, int defaultOrdinal = -1, string helpText = "", bool dontAllowValues = false
            ParamsObject _input = (ParamsObject)DynamicTypeCreator
                                  .Create <ParamsObject>("MyParams")
                                  .AddPassThroughCtors()
                                  .AddAutoProperty <string>("FirstName")
                                  .AddPropertyAttribute <SwitchAttribute>(new Type[] { typeof(string), typeof(bool), typeof(int), typeof(string), typeof(bool) }, new object[] { "", false, -1, "", false })
                                  .AddAutoProperty <string>("LastName")
                                  .AddPropertyAttribute <SwitchAttribute>(new Type[] { typeof(string), typeof(bool), typeof(int), typeof(string), typeof(bool) }, new object[] { "", false, -1, "", false })
                                  .AddAutoProperty <int>("Age")
                                  .AddPropertyAttribute <SwitchAttribute>(new Type[] { typeof(string), typeof(bool), typeof(int), typeof(string), typeof(bool) }, new object[] { "", false, -1, "", false })
                                  .AddAutoProperty <bool>("IsItTrue")
                                  .AddPropertyAttribute <SwitchAttribute>(new Type[] { typeof(string), typeof(bool), typeof(int), typeof(string), typeof(bool) }, new object[] { "", false, -1, "", false })
                                  .FinishBuildingType()
                                  .GetConstructor(new Type[] { typeof(string) })
                                  .Invoke(new object[] { "/FirstName:Yisrael /LastName:Lax /IsItTrue /Age:30" });

            Assert.IsTrue(_input.GetPropertyValue <string>("FirstName") == "Yisrael");
            Assert.IsTrue(_input.GetPropertyValue <string>("LastName") == "Lax");
            Assert.IsTrue(_input.GetPropertyValue <int>("Age") == 30);
            Assert.IsTrue(_input.GetPropertyValue <bool>("IsItTrue") == true);
        }
예제 #13
0
        public void Test_DateTimeParsing_Bad()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .AddSwitch <DateTime>("Bday")
                .AddSwitch <int>("Age")
                .AddSwitch <string>("Name")
                .FinishBuilding("/Bday:28/11/1987", "/Age:30", "/Name:Yisrael Lax");

            bool _parseErr = false;

            try
            {
                _paramObj.CheckParams();
            }
            catch { _parseErr = true; }
            Assert.IsTrue(_parseErr, "Parsing should have failed b/c incorrect DateTime format");
            Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == default(DateTime));
        }
예제 #14
0
        public void Test_DateTimeParsing_Good()
        {
            ParamsObject _paramObj =
                DynamicParamsCreator
                .Create()
                .AddSwitch <DateTime>("Bday")
                .AddSwitch <int>("Age")
                .AddSwitch <string>("Name")
                .FinishBuilding("/Bday:11/28/1987", "/Age:30", "/Name:Yisrael Lax");

            bool _parseErr = false;

            try
            {
                _paramObj.CheckParams();
            }
            catch { _parseErr = true; }
            Assert.IsFalse(_parseErr, "Parsing failed");
            DateTime _bday = _paramObj.GetPropertyValue <DateTime>("Bday");

            Assert.IsTrue(_bday == new DateTime(1987, 11, 28), "Bday is incorrect");
        }