Beispiel #1
0
        public void Array_Numbers()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            // array
            //
            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:1,2,3",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(3, printer.PrintedTexts.Count);
            Assert.AreEqual("test_1", printer.PrintedTexts[0]);
            Assert.AreEqual("test_2", printer.PrintedTexts[1]);
            Assert.AreEqual("test_3", printer.PrintedTexts[2]);

            // JSON
            //
            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:[1,2,3]",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(6, printer.PrintedTexts.Count);
            Assert.AreEqual("test_1", printer.PrintedTexts[3]);
            Assert.AreEqual("test_2", printer.PrintedTexts[4]);
            Assert.AreEqual("test_3", printer.PrintedTexts[5]);
        }
Beispiel #2
0
        public void Array_Numbers()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            // array
            //
            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:1,2,3",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.EqualTo(new[] { "test_1", "test_2", "test_3" }));

            // JSON
            //
            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:[1,2,3]",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.EqualTo(new[] { "test_1", "test_2", "test_3", "test_1", "test_2", "test_3" }));
        }
Beispiel #3
0
        public void Array_Enum()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            // using array convertion
            //
            Parser.Run(new[]
            {
                "printenums",
                "/enums:Upper,Lower",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.EqualTo(new[] { "test_Upper", "test_Lower" }));

            // using JSON deserialization
            //
            Parser.Run(new[]
            {
                "printenums",
                "/enums:['Upper','Lower']",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Count.EqualTo(4));
            Expect(printer.PrintedTexts[2], Is.EqualTo("test_Upper"));
            Expect(printer.PrintedTexts[3], Is.EqualTo("test_Lower"));
        }
Beispiel #4
0
        public void Array_Enum()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            // using array convertion
            //
            Parser.Run(new[]
            {
                "printenums",
                "/enums:Upper,Lower",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(2, printer.PrintedTexts.Count);
            Assert.AreEqual("test_Upper", printer.PrintedTexts[0]);
            Assert.AreEqual("test_Lower", printer.PrintedTexts[1]);

            // using JSON deserialization
            //
            Parser.Run(new[]
            {
                "printenums",
                "/enums:['Upper','Lower']",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(4, printer.PrintedTexts.Count);
            Assert.AreEqual("test_Upper", printer.PrintedTexts[2]);
            Assert.AreEqual("test_Lower", printer.PrintedTexts[3]);
        }
Beispiel #5
0
        public void Array_Enum_NoInput()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            Parser.Run(new[]
            {
                "printenums",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(0, printer.PrintedTexts.Count);
        }
Beispiel #6
0
        public void Array_Enum_NoInput()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            Parser.Run(new[]
            {
                "printenums",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.Empty);
        }
Beispiel #7
0
        public void Required_Exception()
        {
            var printer = new Printer();
            var sample = new Sample_07 { Printer = printer };

            Expect(() => Parser.Run(new[]
                                        {
                                            "print",
                                            "/message:world",
                                        }, sample),
                   Throws.InstanceOf<CommandLineParserException>());
        }
Beispiel #8
0
        public void Array_Strings_WithSeparator()
        {
            var printer = new Printer();
            var sample = new Sample_69 { Printer = printer };

            // array
            //
            Parser.Run(new[]
            {
                "print",
                "/messages:a|b|c",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.EqualTo(new[] {"test_a", "test_b", "test_c"}));

            sample.Printer.Reset();

            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:1-78-100",
                "/prefix:test_",
            }, sample);

            Expect(printer.PrintedTexts, Is.EqualTo(new[] { "test_1", "test_78", "test_100" }));
        }
Beispiel #9
0
        public void RegisterParameterHandler_CallsTheHandler_UseTheValue()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            var debug = String.Empty;

            var p = new Parser<Sample_02>();
            p.Register.ParameterHandler<string>("debug", str => debug = str);

            p.Run("print /c=5 /msg=test /prefix=hello_ /debug=true".Split(' '), sample);

            Expect(debug, Is.EqualTo("true"));
        }
Beispiel #10
0
        public void Execute_Verb()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Parser.Run(new[]
            {
                "print",
                "/c=5",
                "/msg=test",
                "/prefix=hello_",
            }, sample);

            Assert.AreEqual(5, printer.PrintedTexts.Count);
            Assert.IsTrue(printer.PrintedTexts.All(t => t.Equals("hello_test")));
        }
Beispiel #11
0
        public void Interception_Registered_Called()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            var p = new Parser<Sample_02>();

            var preInterceptorCalled = false;
            var postInterceptorCalled = false;

            p.Register.PreVerbInterceptor(c => preInterceptorCalled = true);
            p.Register.PostVerbInterceptor(c => postInterceptorCalled = true);

            p.Run(new[]
            {
                "print",
                "/c=5",
                "/msg=test",
                "/prefix=hello_",
            }, sample);

            Assert.AreEqual(5, printer.PrintedTexts.Count);
            Assert.IsTrue(printer.PrintedTexts.All(t => t.Equals("hello_test")));

            Assert.IsTrue(preInterceptorCalled);
            Assert.IsTrue(postInterceptorCalled);
        }
Beispiel #12
0
 public void Foo(string x, Uri u)
 {
     Printer.Print(x + u);
 }
Beispiel #13
0
 public void Print()
 {
     Printer.Print("x");
 }
Beispiel #14
0
 public void Print([DefaultValue("bar")] string foo)
 {
     Printer.Print(foo);
 }
Beispiel #15
0
 public void Help(string help)
 {
     Printer.Print(help);
 }
Beispiel #16
0
 public void Zoo([DefaultValue("5")] int n)
 {
     Printer.Print(n.ToString());
 }
Beispiel #17
0
 public void Foo(string x, Guid g)
 {
     Printer.Print(x + g);
 }
Beispiel #18
0
 public void Help(string h, string boo)
 {
     Printer.Print("help");
 }
Beispiel #19
0
        public void Execute_DefaultVerb_Switch()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Parser.Run(new[]
            {
                "/c=5",
                "/msg=test",
                "/prefix=hello_",
                "/u",
            }, sample);

            Assert.AreEqual(5, printer.PrintedTexts.Count);
            Assert.IsTrue(printer.PrintedTexts.All(t => t.Equals("HELLO_TEST")));
        }
Beispiel #20
0
 public void Foo()
 {
     Printer.Print("works!");
 }
Beispiel #21
0
 public void Help(string h)
 {
     Printer.Print("help");
 }
Beispiel #22
0
 public void Foo(string x, int y)
 {
     Printer.Print("works!");
 }
Beispiel #23
0
        public void Execute_NoParameterValue()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Parser.Run(new[]
            {
                "print",
                "/c=5",
                "/msg=",
                "/prefix=hello_",
            }, sample);
        }
Beispiel #24
0
 public void Print(string foo)
 {
     Printer.Print(foo);
 }
Beispiel #25
0
        public void Array_Strings()
        {
            var printer = new Printer();
            var sample = new Sample_08 { Printer = printer };

            // array
            //
            Parser.Run(new[]
            {
                "print",
                "/messages:a,b,c",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(3, printer.PrintedTexts.Count);
            Assert.AreEqual("test_a", printer.PrintedTexts[0]);
            Assert.AreEqual("test_b", printer.PrintedTexts[1]);
            Assert.AreEqual("test_c", printer.PrintedTexts[2]);

            // JSON
            //
            Parser.Run(new[]
            {
                "print",
                "/messages:['a','b','c']",
                "/prefix:test_"
            }, sample);

            Assert.AreEqual(6, printer.PrintedTexts.Count);
            Assert.AreEqual("test_a", printer.PrintedTexts[3]);
            Assert.AreEqual("test_b", printer.PrintedTexts[4]);
            Assert.AreEqual("test_c", printer.PrintedTexts[5]);
        }
Beispiel #26
0
        public void Array_Strings_WithSeparator()
        {
            var printer = new Printer();
            var sample = new Sample_69 { Printer = printer };

            // array
            //
            Parser.Run(new[]
            {
                "print",
                "/messages:a|b|c",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(3, printer.PrintedTexts.Count);
            Assert.AreEqual("test_a", printer.PrintedTexts[0]);
            Assert.AreEqual("test_b", printer.PrintedTexts[1]);
            Assert.AreEqual("test_c", printer.PrintedTexts[2]);

            sample.Printer.Reset();

            Parser.Run(new[]
            {
                "printnumbers",
                "/numbers:1-78-100",
                "/prefix:test_",
            }, sample);

            Assert.AreEqual(3, printer.PrintedTexts.Count);
            Assert.AreEqual("test_1", printer.PrintedTexts[0]);
            Assert.AreEqual("test_78", printer.PrintedTexts[1]);
            Assert.AreEqual("test_100", printer.PrintedTexts[2]);
        }
Beispiel #27
0
        public void MapArguments_InvalidPrefix_Exception()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Parser.Run(new[]
            {
                "print",
                "*c=5",
                "/msg=test",
                "/prefix=hello_",
            }, sample);
        }
Beispiel #28
0
        public void RegisteredVerbInterceptorTest()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            var list = new List<string>();
            var p = new Parser<Sample_02>();
            p.RegisterInterceptor(x => list.Add(x.Verb));

            p.Run("print /c=5 /msg=test /prefix=hello_".Split(' '), sample);
            Assert.AreEqual(1, list.Count());
            Assert.AreEqual("print", list[0]);
        }
Beispiel #29
0
        public void RegisterParameterHandler_CallsTheHandler_IgnoreTheValue()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            int x = 0;

            var p = new Parser<Sample_02>();

            // with and without description for coverage
            //
            p.Register.ParameterHandler("dec,d", delegate { x--; });
            p.Register.ParameterHandler("inc", delegate { x++; }, "description");

            p.Run("print /c=5 /msg=test /prefix=hello_ /inc".Split(' '), sample);

            Expect(x, Is.EqualTo(1));

            p.Run("print /c=5 /msg=test /prefix=hello_ /dec".Split(' '), sample);
            p.Run("print /c=5 /msg=test /prefix=hello_ /d".Split(' '), sample);

            Expect(x, Is.EqualTo(-1));
        }
Beispiel #30
0
        public void Required_Exception()
        {
            try
            {
                var printer = new Printer();
                var sample = new Sample_07 { Printer = printer };

                Parser.Run(new[]
                {
                    "print",
                    "/message:world",
                }, sample);

                Assert.Fail();
            }
            catch (CommandLineException)
            {
            }
        }
Beispiel #31
0
        public void Required()
        {
            var printer = new Printer();
            var sample = new Sample_07 { Printer = printer };

            Parser.Run(new[]
            {
                "print",
                "/prefix:hello",
            }, sample);

            Assert.AreEqual(1, printer.PrintedTexts.Count);
            Assert.AreEqual("HELLO", printer.PrintedTexts[0]);
        }
Beispiel #32
0
 public void Foo(int h)
 {
     Printer.Print("a");
 }
Beispiel #33
0
 public void ShowHelp(string help)
 {
     Printer.Print("help");
 }
Beispiel #34
0
        public void Execute_Verb()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Parser.Run(new[]
            {
                "print",
                "/c=5",
                "/msg=test",
                "/prefix=hello_",
            }, sample);

            Expect(printer.PrintedTexts, Count.EqualTo(5));
            Expect(printer.PrintedTexts, All.EqualTo("hello_test"));
        }
Beispiel #35
0
        public void Execute_DefaultVerbWithoutDefaultValue()
        {
            var printer = new Printer();
            var sample = new Sample_02_No_Default { Printer = printer };

            Parser.Run(new string[]
            {
            }, sample);

            Assert.AreEqual(null, printer.PrintedTexts[0]);
        }
Beispiel #36
0
 public void Bar()
 {
     Printer.Print("zoo");
 }
Beispiel #37
0
        public void Execute_DuplicateParameterNames()
        {
            var printer = new Printer();
            var sample = new Sample_05 { Printer = printer };

            Expect(() => Parser.Run(new[]
                                        {
                                            "p",
                                        }, sample),
                    Throws.InstanceOf<InvalidOperationException>()
                    .With.Message.ContainsSubstring("Duplicate parameter names found in Print: c, x"));
        }
Beispiel #38
0
 public void Foo(string h, int n)
 {
     Printer.Print("a");
 }
Beispiel #39
0
        public void Execute_NoParameterValue()
        {
            var printer = new Printer();
            var sample = new Sample_02 { Printer = printer };

            Expect(() => Parser.Run(new[]
                                        {
                                            "print",
                                            "/c=5",
                                            "/msg=",
                                            "/prefix=hello_",
                                        }, sample),
                   Throws.InstanceOf<MissingArgumentValueException>());
        }
Beispiel #40
0
        public void Execute_DuplicateParameterNames()
        {
            try
            {
                var printer = new Printer();
                var sample = new Sample_05 { Printer = printer };

                Parser.Run(new[]
                {
                    "p",
                }, sample);

                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                Assert.IsTrue(ex.Message.Contains("Duplicate parameter names found in Print: c, x"));
            }
        }
Beispiel #41
0
        public void Execute_WithEnum()
        {
            var printer = new Printer();
            var sample = new Sample_04 { Printer = printer };

            Parser.Run(new[]
            {
                "/count=5",
                "/msg=test",
                "/prefix=hello_",
                "/c:Upper",
            }, sample);

            Assert.AreEqual(5, printer.PrintedTexts.Count);
            Assert.IsTrue(printer.PrintedTexts.All(t => t.Equals("HELLO_TEST")));
        }
Beispiel #42
0
 public void Foo(string x)
 {
     Printer.Print(x);
 }