예제 #1
0
        public void SmokeTest_ArgumentParser_Ints()
        {
            var possibleSeparators = new[] { "=", "--", "*", "|", "()", "==", "#", "%", "^", "$", ";" };

            foreach (var splitter in possibleSeparators)
            {
                var count       = 100;
                var typeBuilder = new DynamicTypeBuilder();
                var args        = new List <string>();
                for (int i = 0; i <= count; i++)
                {
                    typeBuilder = typeBuilder.WithProperty <int>($"MyProperty{i}", splitter, $"--my--property-value{i}");
                    typeBuilder = typeBuilder.WithProperty <double>($"SomeOtherProperty{i}", splitter, $"-x{i}");
                    args.Add($"--my--property-value{i}{splitter}{i}");
                    args.Add($"-x{i}{splitter}{i/3.0}");
                }
                ;

                var testObj = typeBuilder.Build();
                var parser  = new AttributeParser();
                parser.Parse(testObj, args.ToArray());
                dynamic dyn = testObj;

                Assert.AreEqual(33, dyn.MyProperty33);
                Assert.AreEqual(50, dyn.MyProperty50);
                Assert.AreEqual(100, dyn.MyProperty100);

                Assert.AreEqual(11, dyn.SomeOtherProperty33, 1e-9);
                Assert.AreEqual(50 / 3.0, dyn.SomeOtherProperty50, 1e-9);
                Assert.AreEqual(100 / 3.0, dyn.SomeOtherProperty100, 1e-9);
            }
        }
예제 #2
0
        public void AttributeParser_EmptyArgs_NoProperties()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <Empty>();

            Assert.IsNotNull(result);
        }
예제 #3
0
        public void AttributeParser_RequiredCustomTypeProperty_WithoutCustomConverterThrowsException()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_CustomPropertyType>("--someprop:mystringvalue");

            Assert.AreEqual(result.MyProp.SomeValue, 100);
        }
예제 #4
0
        public void AttributeParser_RequiredDoubleProperty()
        {
            var parser = new AttributeParser();

            Assert.AreEqual(560.99, parser.Parse <TestClass_DoubleRequired>("--somedouble:560.99").DoubleProp);
            Assert.AreEqual(-5, parser.Parse <TestClass_DoubleRequired>("--somedouble:-5").DoubleProp);
            Assert.AreEqual(0, parser.Parse <TestClass_DoubleRequired>("--somedouble:0").DoubleProp);
        }
예제 #5
0
        public void AttributeParser_RequiredIntProperty()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_IntRequired>("--someInt:560");

            Assert.AreEqual(560, result.IntArg);
        }
예제 #6
0
        public void AttributeParser_SingleBoolPropertyAlliased()
        {
            var parser = new AttributeParser();

            Assert.IsTrue(parser.Parse <TestClass_BoolAlliased>("--somebool").Arg1);
            Assert.IsTrue(parser.Parse <TestClass_BoolAlliased>("--somebool1").Arg1);
            Assert.IsTrue(parser.Parse <TestClass_BoolAlliased>("XYZ").Arg1);
        }
예제 #7
0
        public void AttributeParser_SingleBoolPropertyAlliased_WithExplicitValues()
        {
            var parser = new AttributeParser();

            Assert.IsTrue(parser.Parse <TestClass_BoolAlliased>("--somebool:true").Arg1);
            Assert.IsTrue(parser.Parse <TestClass_BoolAlliased>("--somebool1:true").Arg1);
            Assert.IsFalse(parser.Parse <TestClass_BoolAlliased>("XYZ:false").Arg1);
        }
예제 #8
0
        public void AttributeParser_InputOutput()
        {
            var parser = new AttributeParser();
            var result = parser.Parse <TestClass_InputOutput>("50", "200");

            Assert.AreEqual(50, result.Input);
            Assert.AreEqual(200, result.Output);
        }
예제 #9
0
        public void AttributeParser_MainOptionString()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_MainOption_String>("SomeMainArgument");

            Assert.IsNotNull(result);
            Assert.AreEqual("SomeMainArgument", result.Arg1);
        }
예제 #10
0
        public void AttributeParser_SingleStringProperty()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_String>("Arg1:MyValue");

            Assert.IsNotNull(result);
            Assert.AreEqual("MyValue", result.Arg1);
        }
예제 #11
0
        public void AttributeParser_SingleBoolProperty()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_Bool>("Arg1");

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Arg1);
        }
예제 #12
0
        public void AttributeParser_SingleStringPropertyAlliased_DoesNotRespectPropName()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_StringAlliased>("Arg1:MyValue");

            Assert.IsNotNull(result);
            Assert.AreNotEqual("MyValue", result.Arg1);
        }
예제 #13
0
        public void AttributeParser_SingleStringPropertyAlliased()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_StringAlliased>("--str-value-xxx=MyValue");

            Assert.IsNotNull(result);
            Assert.AreEqual("MyValue", result.Arg1);
        }
예제 #14
0
        public void AttributeParser_SingleStringPropertySpecialAlias()
        {
            var parser = new AttributeParser();
            // "/p:SomeAlias"
            var result = parser.Parse <TestClass_IntWithSpecialAlias>("/p:SomeAlias:123123");

            Assert.IsNotNull(result);
            Assert.AreEqual(123123, result.Arg1);
        }
예제 #15
0
        public void AttributeParser_MainOptionInt()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_MainOption_Int>("51");

            Assert.IsNotNull(result);
            Assert.AreEqual(51, result.Arg1);
        }
예제 #16
0
        public void AttributeParser_MainOptionWithOtherProperties()
        {
            var parser = new AttributeParser();
            // For example
            // C:\> myProgram 51 -o SomeOtherStuff
            var result = parser.Parse <TestClass_MainOptionWithOtherProps>("51", "-o", "SomeOtherStuff");

            Assert.IsNotNull(result);
            Assert.AreEqual(51, result.Arg1);
            Assert.AreEqual("SomeOtherStuff", result.SomeValue);
        }
예제 #17
0
        public void AttributeParser_MultipleStringProperties_ButNoArgumentsMatching()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_MultipleStrings>("Arg5:MyValue", "Arg7:Weird", "Arg9:SomeOtherValue");

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Arg1));
            Assert.IsTrue(string.IsNullOrEmpty(result.Arg2));
            Assert.IsTrue(string.IsNullOrEmpty(result.Arg3));
        }
예제 #18
0
        public void AttributeParser_MultipleStringProperties()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_MultipleStrings>("Arg1:MyValue", "Arg3:Weird", "Arg2:SomeOtherValue");

            Assert.IsNotNull(result);
            Assert.AreEqual("MyValue", result.Arg1);
            Assert.AreEqual("SomeOtherValue", result.Arg2);
            Assert.AreEqual("Weird", result.Arg3);
        }
예제 #19
0
        public void TestVariations()
        {
            string[] testStrings = Resources.AttributeTestStrings.Split('\n');
            for (int i = 0; i < testStrings.Length; i++)
            {
                testStrings[i] = testStrings[i].Trim('\r');
            }

            Console.WriteLine("Error,Input,SetterVisibility,Type,Required,MaxLength,Name,IsIdentity,InitialValue");
            foreach (string s in testStrings)
            {
                ParseResult result = AttributeParser.Parse(s);
                Console.WriteLine($"{(result == null ? "\"True\"" : "")},\"{s}\",\"{result?.SetterVisibility}\",\"{result?.Type}\",\"{result?.Required}\",\"{result?.MaxLength}\",\"{result?.Name}\",\"{result?.IsIdentity}\",\"{result?.InitialValue}\"");
            }
        }
예제 #20
0
        public void AttributeParser_OutputPath()
        {
            var parser = new AttributeParser();

            parser.RegisterCustomConverter <FileInfo>(i =>
            {
                if (i == @"c:\SomePath\SubPath")
                {
                    return(new FileInfo(i));
                }
                return(null);
            });

            var result = parser.Parse <TestClass_Path>(@"-o", @"c:\SomePath\SubPath");

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.OutputPath);
        }
예제 #21
0
        public void AttributeParser_RequiredCustomTypeProperty()
        {
            var parser = new AttributeParser();

            parser.RegisterCustomConverter <CustomType>(i =>
            {
                if (i == "mystringvalue")
                {
                    return(new CustomType {
                        SomeValue = 100
                    });
                }
                return(null);
            });

            var result = parser.Parse <TestClass_CustomPropertyType>("--someprop:mystringvalue");

            Assert.AreEqual(result.MyProp.SomeValue, 100);
        }
예제 #22
0
        public void AttributeParser_RequiredIntProperty_WrongFormatException()
        {
            var parser = new AttributeParser();

            parser.Parse <TestClass_IntRequired>("--someInt:56xx0");
        }
예제 #23
0
        public void AttributeParser_SingleStringPropertyWithCustomSplitter()
        {
            var parser = new AttributeParser();

            Assert.AreEqual("MyValue", parser.Parse <TestClass_StringPropertyWithCustomSplitter>("-val##MyValue").Arg1);
        }
예제 #24
0
        public void AttributeParser_MissingRequiredProperty_ProduceException()
        {
            var parser = new AttributeParser();

            var result = parser.Parse <TestClass_StringRequired>();
        }