public void MultipleVariablesCanBeUnsetAtTheSameTime()
        {
            var count = Variable.Variables.Count;
            var unsetVariable = new UnsetVariable();
            var calculate = new Calculate();

            Assert.AreEqual("x = 2", _inputHandler.HandleInput("set x = 2"));
            Assert.AreEqual("y = 4", _inputHandler.HandleInput("set y = 4"));
            Assert.AreEqual("z = 6", _inputHandler.HandleInput("set z = 6"));
            Assert.AreEqual(count + 3, Variable.Variables.Count);

            Assert.AreEqual("Variable x unset.", _inputHandler.HandleInput("unset x"));
            Assert.Throws<CalculatorException>(() => unsetVariable.HandleInput("unset x"));
            Assert.AreEqual(count + 2, Variable.Variables.Count);

            Assert.AreEqual("Variables z, y unset.", _inputHandler.HandleInput("unset z,    y"));
            Assert.AreEqual(count, Variable.Variables.Count);

            Assert.Throws<CalculatorException>(() => unsetVariable.HandleInput("unset y, z"));
            Assert.Throws<CalculatorException>(() => unsetVariable.HandleInput("unset z"));
            Assert.Throws<CalculatorException>(() => calculate.HandleInput("x"));
            Assert.Throws<CalculatorException>(() => calculate.HandleInput("y"));
            Assert.Throws<CalculatorException>(() => calculate.HandleInput("z"));

            Assert.AreEqual("y = 4", _inputHandler.HandleInput("set y = 4"));
            Assert.AreEqual("z = 6", _inputHandler.HandleInput("set z = 6"));
            Assert.AreEqual("Variables y, z unset.", _inputHandler.HandleInput("unset y;  z"));
        }
Exemple #2
0
        /// <summary>
        /// Handles the input.
        /// </summary>
        /// <param name="initialInput">The initial input.</param>
        /// <returns></returns>
        public override string HandleInput(string initialInput)
        {
            var calculate = new Calculate();
            var index = 0;

            if ( StringParse.StringStartsWith(initialInput, Command) )
            {
                index = Command.Length;
            }

            try
            {
                calculate.HandleInput(initialInput.Substring(index));
            }
            catch (CalculatorException e)
            {
                e.ErrorIndex += index;
                throw e;
            }
            return Calculate.DoubleToFraction(Variable.ANS.Value);
        }
        public void VariablesCanBeUnset()
        {
            var count = Variable.Variables.Count;

            Assert.AreEqual("x = 2", _inputHandler.HandleInput("set x = 2"));
            Assert.AreEqual(count + 1, Variable.Variables.Count);

            Assert.AreEqual("Variable x unset.", _inputHandler.HandleInput("unset x"));
            Assert.AreEqual(count, Variable.Variables.Count);

            var unsetVariable = new UnsetVariable();
            var calculate = new Calculate();

            Assert.Throws<CalculatorException>(() => unsetVariable.HandleInput("unset x"));
            Assert.Throws<CalculatorException>(() => calculate.HandleInput("x"));
        }