private static ICallable BuildFactorialFunction() { IExpression condition = new CompareExpression(ComparisonOperator.LessEqual, new VariableExpression("n"), new ConstantExpression(1)); ICommand return1 = new ReturnCommand(new ConstantExpression(1)); ICommand return2 = new ReturnCommand(new ArithmeticBinaryExpression( ArithmeticOperator.Multiply, new VariableExpression("n"), new InvokeExpression("Factorial", new IExpression[] { new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("n"), new ConstantExpression(1)) }))); ICommand ifcmd = new IfCommand(condition, return1, return2); ICallable factorial = new Function(new string[] { "n" }, ifcmd); return factorial; }
public void ExecuteIfCommandWhenTrue() { IExpression condition = new ConstantExpression(true); ICommand setCommand = new SetVariableCommand("x", new ConstantExpression(1)); IfCommand command = new IfCommand(condition, setCommand); BindingEnvironment environment = new BindingEnvironment(); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("x")); }
public void ExecuteIfCommandElseWhenFalse() { IExpression condition = new ConstantExpression(false); ICommand setXCommand = new SetVariableCommand("x", new ConstantExpression(1)); ICommand setYCommand = new SetVariableCommand("y", new ConstantExpression(2)); IfCommand command = new IfCommand(condition, setXCommand, setYCommand); BindingEnvironment environment = new BindingEnvironment(); command.Execute(environment); Assert.IsNull(environment.GetValue("x")); Assert.AreEqual(2, environment.GetValue("y")); }