コード例 #1
0
        public void TestGetSetOption()
        {
            Assert.AreEqual(DefaultCommands.Get | DefaultCommands.Set | DefaultCommands.Help, DefaultCommands.Default);
            //defaults the DefaultCommands to Get/Set/Help via the enum value of DefaultCommands.Default
            CommandInterpreter ci = new CommandInterpreter(new TestCommands());

            Assert.AreEqual(2, ci.Options.Length);
            Assert.AreEqual("Other", ci.Options[0].DisplayName);
            Assert.AreEqual(typeof(int), ci.Options[0].Type);
            Assert.AreEqual("SomeData", ci.Options[1].DisplayName);
            Assert.AreEqual(typeof(String), ci.Options[1].Type);

            TextWriter stdout = Console.Out;

            try
            {
                Console.SetOut(new StringWriter());                // <= Get will also write to console

                Assert.AreEqual(-1, ci.Get("other"));              //default was applied
                ci.Set("other", 1);
                Assert.AreEqual(1, ci.Get("other"));

                Assert.AreNotEqual("abc", ci.Get("somedata"));
                ci.Set("somedata", "abc");
                Assert.AreEqual("abc", ci.Get("somedata"));
            }
            finally
            { Console.SetOut(stdout); }

            string result;

            result = Capture(ci, "GET Somedata");
            Assert.AreEqual("abc", result);
            //Set without args lists options
            result = Capture(ci, "SET");
            Assert.IsTrue(result.ToUpper().Contains("somedata".ToUpper()));
            //Set without value returns the current value
            result = Capture(ci, "SET somedata");
            Assert.AreEqual("abc", result);
            result = Capture(ci, "SET somedata 123");
            Assert.AreEqual("", result);
            result = Capture(ci, "GET somedata");
            Assert.AreEqual("123", result);
        }