public void HasVariablesWithVariable() { Variable variable = new Variable("X"); MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123)); Assert.IsTrue(expr.HasVariable()); }
public static List MakeList(IList<object> elements, Variable tail) { if (elements.Count == 1) return new List(elements[0], tail); return new List(elements[0], MakeList(elements.Skip(1).ToList(), tail)); }
public void CreateSimpleVariableExpression() { Variable variable = new Variable("X"); VariableExpression expression = new VariableExpression(variable); Assert.AreEqual(variable, expression.Variable); }
public void EvaluateUndefinedVariableExpression() { Variable variable = new Variable("X"); Context context = new Context(); VariableExpression expression = new VariableExpression(variable); Assert.AreEqual(variable, expression.Evaluate(context, true)); }
public void EvaluateVariableExpression() { Variable variable = new Variable("X"); Context context = new Context(); context.SetValue("X", 1); VariableExpression expression = new VariableExpression(variable); Assert.AreEqual(1, expression.Evaluate(context)); }
public void RaiseIfEvaluateUndefinedVariableExpression() { Variable variable = new Variable("X"); Context context = new Context(); VariableExpression expression = new VariableExpression(variable); try { expression.Evaluate(context, false); Assert.Fail(); } catch (Exception ex) { Assert.AreEqual("variable 'X' is unbound", ex.Message); } }
public void Equals() { Variable variable1 = new Variable("X"); Variable variable2 = new Variable("Y"); Variable variable3 = new Variable("X"); Assert.AreEqual(variable1, variable3); Assert.AreEqual(variable3, variable1); Assert.AreEqual(variable1.GetHashCode(), variable3.GetHashCode()); Assert.IsFalse(variable1.Equals(null)); Assert.IsFalse(variable1.Equals(123)); Assert.IsFalse(variable1.Equals("foo")); Assert.AreNotEqual(variable1, variable2); Assert.AreNotEqual(variable2, variable1); }
public void MatchOneAtomAndTwoVariablesInTupleWithConcreteTuple() { Context context = new Context(); Atom p = new Atom("point"); Variable x = new Variable("X"); Variable y = new Variable("Y"); MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new AtomExpression(p), new VariableExpression(x), new VariableExpression(y) }), new TupleExpression(new IExpression[] { new AtomExpression(new Atom("point")), new ConstantExpression(1), new ConstantExpression(2) })); expr.Evaluate(context); var result1 = context.GetValue("X"); var result2 = context.GetValue("Y"); Assert.IsNotNull(result1); Assert.AreEqual(1, result1); Assert.IsNotNull(result2); Assert.AreEqual(2, result2); }
public void MatchTwoVariablesWithList() { Context context = new Context(); Variable headvar = new Variable("H"); Variable tailvar = new Variable("T"); var list = new List(headvar, tailvar); var list2 = new List(1, new List(2, new List(3, null))); MatchExpression expr = new MatchExpression(new ConstantExpression(list), new ConstantExpression(list2)); expr.Evaluate(context); var result1 = context.GetValue("H"); var result2 = context.GetValue("T"); Assert.IsNotNull(result1); Assert.AreEqual(1, result1); Assert.IsNotNull(result2); Assert.IsInstanceOfType(result2, typeof(List)); Assert.AreEqual("[2,3]", result2.ToString()); }
public void RaiseIfMatchVariableInTupleWithTwoDifferentValues() { Context context = new Context(); Variable x = new Variable("X"); MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new VariableExpression(x), new VariableExpression(x) }), new TupleExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) })); try { expr.Evaluate(context); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidOperationException)); Assert.AreEqual("no match of right hand side value {1,2}", ex.Message); } }
public void RaiseIfMatchBoundVariableWithInteger() { Context context = new Context(); Variable variable = new Variable("X"); context.SetValue("X", 1); MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123)); try { expr.Evaluate(context); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidOperationException)); Assert.AreEqual("no match of right hand side value 123", ex.Message); } }
public void MatchVariableWithInteger() { Context context = new Context(); Variable variable = new Variable("X"); MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123)); Assert.AreEqual(123, expr.Evaluate(context)); Assert.AreEqual(123, context.GetValue("X")); }
public void VariableToString() { Variable variable = new Variable("X"); Assert.AreEqual("X", variable.ToString()); }
public void FirstVariable() { Variable variable = new Variable("X"); Assert.AreEqual(variable, variable.FirstVariable()); }
public void CreateSimpleVariable() { Variable variable = new Variable("X"); Assert.AreEqual("X", variable.Name); }
public VariableExpression(Variable variable) { this.variable = variable; }