Ejemplo n.º 1
0
        public void HelpTextWrappingTest()
        {
            var args = new MyTestCmdLineObject();

            args.InitializeFromCmdLine();
            var vals = new[] { 1, 2 };

            Debug.WriteLine(args.GetHelpText(40));
            Assert.IsTrue(args.IsValid());
        }
Ejemplo n.º 2
0
        public void PartialNameTest()
        {
            MyTestCmdLineObject target;

            string[] args;

            target = new MyTestCmdLineObject();
            args   = new[] { "/Hell", "Hi Brian" };
            target.InitializeFromCmdLine(args);
            Assert.AreEqual("Hi Brian", target.Hello);
        }
Ejemplo n.º 3
0
        public void InitializeFromCmdLine_Alias_Initialized()
        {
            // full alias
            var cmdLine = new MyTestCmdLineObject();

            cmdLine.InitializeFromCmdLine(new[] { "/crap", "Christine" });
            Assert.AreEqual("Christine", cmdLine.StuffILike[0]);

            // partial alias
            cmdLine = new MyTestCmdLineObject();
            cmdLine.InitializeFromCmdLine(new[] { "/c", "Christine" });
            Assert.AreEqual("Christine", cmdLine.StuffILike[0]);
        }
Ejemplo n.º 4
0
        public void ValidateSetTest()
        {
            var args = new MyTestCmdLineObject();

            args.InitializeFromCmdLine("/SampleColor", "red");
            Assert.IsTrue(args.IsValid());
            Assert.AreEqual("red", args.SampleColor);

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor", "green");
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor", "blue");
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor", "Blue");
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor", "purple");
            Assert.IsFalse(args.IsValid());
            Debug.WriteLine(args.ErrorText);

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor2", "pink");
            args.Properties["SampleColor2"].Validators.Add(new SetValidator <string>("pink", "purple", "puce"));
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/SampleColor2", "pink");
            // Uses a custom equality comparer (a BizArk class).
            args.Properties["SampleColor2"].Validators.Add(
                new SetValidator <string>(new EqualityComparer((a, b) => { return(a == b); }), "pink"));
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/NumberOfScoops", "2");
            var vals = new[] { 1, 2 };

            args.Properties["NumberOfScoops"].Validators.Add(new SetValidator <int>(vals));
            Debug.WriteLine(args.GetHelpText(50));
            Assert.IsTrue(args.IsValid());
        }
Ejemplo n.º 5
0
        public void InitializeFromCmdLine_ArgumentValidationTest()
        {
            var args = new MyTestCmdLineObject();

            args.InitializeFromCmdLine("/NumberOfScoops", "2");
            Assert.AreEqual(2, args.NumberOfScoops);
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/NumberOfScoops", "chocolate");
            Assert.AreEqual(1, args.NumberOfScoops);
            Assert.IsFalse(args.IsValid());
            Debug.WriteLine(args.GetHelpText(200));

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/FavoriteNumbers", "1", "2", "3");
            Assert.AreEqual(3, args.FavoriteNumbers.Length);
            Assert.AreEqual(1, args.FavoriteNumbers[0]);
            Assert.AreEqual(2, args.FavoriteNumbers[1]);
            Assert.AreEqual(3, args.FavoriteNumbers[2]);
            Assert.IsTrue(args.IsValid());

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/FavoriteNumbers", "Red", "Green", "Blue");
            Assert.AreEqual(1, args.NumberOfScoops);
            Assert.IsFalse(args.IsValid());
            Debug.WriteLine(args.GetHelpText(200));

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/FavoriteCar", "Ford");
            Assert.IsFalse(args.IsValid());
            Debug.WriteLine(args.GetHelpText(200));

            args = new MyTestCmdLineObject();
            args.InitializeFromCmdLine("/NumberOfScoops", "5");
            Assert.IsFalse(args.IsValid());
            Debug.WriteLine(args.GetHelpText(200));
        }
Ejemplo n.º 6
0
        public void SaveAndRestoreTest()
        {
            string settingsPath = @"C:\garb\Test.xml";

            if (!Directory.Exists(@"C:\garb"))
            {
                Directory.CreateDirectory(@"C:\garb");
            }
            var cmdLine    = new MyTestCmdLineObject();
            var stuffILike = new[] { "Cookies", "Candy", "Ice Cream" };

            Assert.AreNotEqual("Hi", cmdLine.Hello);
            cmdLine.InitializeFromCmdLine("/H", "Hi", "/S", "Cookies", "Candy", "Ice Cream");
            Assert.AreEqual("Hi", cmdLine.Hello);
            AssertEx.AreEqual(stuffILike, cmdLine.StuffILike);
            cmdLine.SaveToXml(settingsPath);
            Assert.IsTrue(File.Exists(settingsPath));

            cmdLine = new MyTestCmdLineObject();
            Assert.AreNotEqual("Hi", cmdLine.Hello);
            cmdLine.RestoreFromXml(settingsPath);
            Assert.AreEqual("Hi", cmdLine.Hello);
            AssertEx.AreEqual(stuffILike, cmdLine.StuffILike);
        }
Ejemplo n.º 7
0
        public void InitializeTest()
        {
            MyTestCmdLineObject target;

            string[] args;

            target = new MyTestCmdLineObject();
            args   = new string[] {};
            target.InitializeFromCmdLine(args);
            Assert.IsNull(target.Hello);
            Assert.IsNull(target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "/H", "Hi Brian!" };
            target.InitializeFromCmdLine(args);
            Assert.That(target.Hello, Is.EqualTo("Hi Brian!"));
            Assert.IsNull(target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "/h", "Hi Brian!" };
            target.InitializeFromCmdLine(args);
            Assert.AreEqual("Hi Brian!", target.Hello);
            Assert.IsNull(target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "/h", "Hi Brian!", "/G", "Goodbye Christine." };
            target.InitializeFromCmdLine(args);
            Assert.AreEqual("Hi Brian!", target.Hello);
            Assert.AreEqual("Goodbye Christine.", target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "Hi Brian!", "/G", "Goodbye Christine." };
            target.InitializeFromCmdLine(args);
            Assert.AreEqual("Hi Brian!", target.Hello);
            Assert.AreEqual("Goodbye Christine.", target.Goodbye);

            target = new MyTestCmdLineObject2();
            args   = new[] { "Goodbye Christine." };
            target.InitializeFromCmdLine(args);
            Assert.IsNull(target.Hello);
            Assert.AreEqual("Goodbye Christine.", target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "TEST", "/h", "Hi Brian!", "/G", "Goodbye Christine.", "TEST" };
            target.InitializeFromCmdLine(args);
            Assert.AreEqual("Hi Brian!", target.Hello);
            Assert.AreEqual("Goodbye Christine.", target.Goodbye);

            target = new MyTestCmdLineObject();
            args   = new[] { "/I" };
            target.InitializeFromCmdLine(args);
            Assert.IsTrue(target.DoesLikeIceCream);

            target = new MyTestCmdLineObject();
            args   = new[] { "/I-" };
            target.InitializeFromCmdLine(args);
            Assert.IsFalse(target.DoesLikeIceCream);

            target = new MyTestCmdLineObject();
            args   = new[] { "/I", "Yes" };
            target.InitializeFromCmdLine(args);
            Assert.IsTrue(target.DoesLikeIceCream);

            target = new MyTestCmdLineObject();
            args   = new[] { "/I", "No" };
            target.InitializeFromCmdLine(args);
            Assert.IsFalse(target.DoesLikeIceCream);

            target = new MyTestCmdLineObject();
            args   = new[] { "/S", "Cars", "Computers", "Food" };
            target.InitializeFromCmdLine(args);
            string[] expectedStuff = { "Cars", "Computers", "Food" };
            AssertEx.AreEqual(expectedStuff, target.StuffILike);
        }