public void ParseInvalidExpressionStringTest() { string expression = "'Test'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidExpressionTooManyTermsTest() { string expression = "$(Name) == 'Test' == $(Name)"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidExpressionMissingParenthesesTest() { string expression = "($(Name) == 'Test'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseFileNotNameContainsExpressionTest() { string expressionText = "!($(File.Name) : '.Designer.')"; IConditionExpression expression = ConditionExpressionParser.Instance.Parse( expressionText); Assert.IsNotNull(expression, "Expected an expression instance."); UnaryOperatorExpression negateExpression = expression as UnaryOperatorExpression; Assert.IsNotNull(negateExpression, "Expected a unary operator expression."); Assert.AreEqual(UnaryExpressionOperator.Negate, negateExpression.Operator, "Unexpected operator."); BinaryOperatorExpression operatorExpression = negateExpression.InnerExpression as BinaryOperatorExpression; Assert.IsNotNull(operatorExpression, "Expected a binary operator expression."); Assert.AreEqual(BinaryExpressionOperator.Contains, operatorExpression.Operator, "Unexpected operator."); FileAttributeExpression attributeExpression = operatorExpression.Left as FileAttributeExpression; Assert.IsNotNull(attributeExpression, "Unexpected left node type."); Assert.AreEqual(FileAttributeType.Name, attributeExpression.FileAttribute, "Attribute expression was not parsed correctly."); StringExpression stringExpression = operatorExpression.Right as StringExpression; Assert.IsNotNull(stringExpression, "Unexpected right node type."); Assert.AreEqual(".Designer.", stringExpression.Text, "String expression was not parsed correctly."); }
public void ParseParentNameContainsExpressionTest() { string expressionText = "$(Parent.Name) : 'Converter'"; IConditionExpression expression = ConditionExpressionParser.Instance.Parse( expressionText); Assert.IsNotNull(expression, "Expected an expression instance."); BinaryOperatorExpression operatorExpression = expression as BinaryOperatorExpression; Assert.IsNotNull(operatorExpression, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Contains, operatorExpression.Operator, "Unexpected operator."); ElementAttributeExpression attributeExpression = operatorExpression.Left as ElementAttributeExpression; Assert.IsNotNull(attributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, attributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); Assert.AreEqual(ElementAttributeScope.Parent, attributeExpression.Scope, "Attribute scope was not parsed correctly."); StringExpression stringExpression = operatorExpression.Right as StringExpression; Assert.IsNotNull(stringExpression, "Unexpected right node type."); Assert.AreEqual("Converter", stringExpression.Text, "String expression was not parsed correctly."); }
public void ParseStringExpressionEscapedApostropheEndTest() { string expressionText = "$(Name) == 'Test it'''"; IConditionExpression expression = ConditionExpressionParser.Instance.Parse( expressionText); Assert.IsNotNull(expression, "Expected an expression instance."); BinaryOperatorExpression operatorExpression = expression as BinaryOperatorExpression; Assert.IsNotNull(operatorExpression, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Equal, operatorExpression.Operator, "Unexpected operator."); ElementAttributeExpression attributeExpression = operatorExpression.Left as ElementAttributeExpression; Assert.IsNotNull(attributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, attributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); Assert.AreEqual(ElementAttributeScope.Element, attributeExpression.Scope, "Attribute scope was not parsed correctly."); StringExpression stringExpression = operatorExpression.Right as StringExpression; Assert.IsNotNull(stringExpression, "Expected a string expression."); Assert.AreEqual("Test it'", stringExpression.Text, "Unexpected expression text."); }
public void ParseInvalidExpressionMissingQuoteTest() { string expression = "$(Name) == 'Test"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
private void A_filter_condition(IConditionExpression filterCondition) { whenCondition = filterCondition; IExpressionEvaluator evaluator = new ExpressionEvaluator(); filter = evaluator.Evaluate <Device>(whenCondition); }
public void ParseNegateExpressionTest() { string[] variations = new string[] { "!($(Name) : 'Test')", "!(($(Name) : 'Test'))", "!((($(Name) : 'Test')))" }; foreach (string variation in variations) { IConditionExpression expression = ConditionExpressionParser.Instance.Parse( variation); Assert.IsNotNull(expression, "Expected an expression instance from {0}", variation); UnaryOperatorExpression negateExpression = expression as UnaryOperatorExpression; Assert.IsNotNull(negateExpression, "Expected a unary operator expression from {0}", variation); Assert.AreEqual(UnaryExpressionOperator.Negate, negateExpression.Operator, "Unexpected operator."); BinaryOperatorExpression binaryOperatorExpression = negateExpression.InnerExpression as BinaryOperatorExpression; Assert.IsNotNull(binaryOperatorExpression, "Expected a binary operator expression."); Assert.AreEqual(BinaryExpressionOperator.Contains, binaryOperatorExpression.Operator, "Unexpected operator."); ElementAttributeExpression attributeExpression = binaryOperatorExpression.Left as ElementAttributeExpression; Assert.IsNotNull(attributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, attributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); Assert.AreEqual(ElementAttributeScope.Element, attributeExpression.Scope, "Attribute scope was not parsed correctly."); StringExpression stringExpression = binaryOperatorExpression.Right as StringExpression; Assert.IsNotNull(stringExpression, "Unexpected right node type."); Assert.AreEqual("Test", stringExpression.Text, "String expression was not parsed correctly."); } }
public void ParseInvalidNestedExpressionTest() { string expression = "$(File.Name) : 'Test' And ()"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidUnaryOperatorTest() { string expression = "#($(File.Name) : 'Or')"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
private static IExpression Simplify(IConditionExpression e) { var a = e.Condition; e.TrueExpression = Simplify(e.TrueExpression); e.FalseExpression = Simplify(e.FalseExpression); var b = e.TrueExpression; var c = e.FalseExpression; int v = ToBooleanConstant(b); if (v == 1) { return(new BinaryExpression(a, c, BinaryOperator.BooleanOr)); } if (v == 0) { return(new BinaryExpression(Not(a), c, BinaryOperator.BooleanAnd)); } v = ToBooleanConstant(c); if (v == 0) { return(new BinaryExpression(a, b, BinaryOperator.BooleanAnd)); } if (v == 1) { return(new BinaryExpression(Not(a), b, BinaryOperator.BooleanOr)); } return(e); }
public void ParseInvalidFileAttributeScopeTest() { string expression = "$(File.Blah) : 'Test'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
/// <summary> /// Gets the scope required to evaluate the condition. /// </summary> /// <param name="expression">Condition expression.</param> /// <returns>Element attribute scope.</returns> private ElementAttributeScope GetRequiredScope(IConditionExpression expression) { ElementAttributeScope scope = ElementAttributeScope.Element; if (expression != null) { ElementAttributeExpression attributeExpression = expression as ElementAttributeExpression; if (attributeExpression != null) { scope = attributeExpression.Scope; } else { ElementAttributeScope leftScope = GetRequiredScope(expression.Left); ElementAttributeScope rightScope = GetRequiredScope(expression.Right); if (leftScope > rightScope) { scope = leftScope; } else { scope = rightScope; } } } return(scope); }
public void ParseInvalidExpressionMissingOperatorTest() { string expression = "$(Name) == 'Test' $(Name) == 'Foo'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidExpressionNoOperatorTest() { string expression = "$(Name)"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidExpressionExtraParenthesesTest2() { string expression = "$(Name) == 'Test')"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidCasedBinaryOperatorTest() { string expression = "$(File.Name) : 'Or' or $(File.Name) : 'And'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
public void ParseInvalidAttributeTest() { string expression = "$(Or) : 'Test'"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
/// <summary> /// Gets the expression value. /// </summary> /// <param name="expression">The expression.</param> /// <param name="element">The element.</param> /// <returns>The expression value as text.</returns> private static string GetExpressionValue(IConditionExpression expression, ICodeElement element) { string value = string.Empty; if (expression != null && element != null) { StringExpression stringExpression = expression as StringExpression; if (stringExpression != null) { value = stringExpression.Text; } else { ElementAttributeExpression attributeExpression = expression as ElementAttributeExpression; if (attributeExpression.Scope == ElementAttributeScope.Parent) { element = element.Parent; } if (attributeExpression != null) { value = ElementUtilities.GetAttribute(attributeExpression.ElementAttribute, element); } } } return(value); }
public void ParseInvalidExpressionNoTermsTest() { string expression = "()"; IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }
private static void WriteCondition(LanguageWriter w, IConditionExpression exp) { WriteExpression(w, exp.Condition, PREC_COND > GetExpressionPrecedence(exp.Condition)); w.Write(" ? "); WriteExpression(w, exp.Then, false); w.Write(" : "); WriteExpression(w, exp.Else, false); }
/// <summary> /// Creates a new operator expression. /// </summary> /// <param name="operatorType">Type of the operator.</param> /// <param name="left">The left expression.</param> /// <param name="right">The right expression.</param> public BinaryOperatorExpression( BinaryExpressionOperator operatorType, IConditionExpression left, IConditionExpression right) { _operatorType = operatorType; _left = left; _right = right; }
public void ParseEmptyTest() { Assert.Throws <ArgumentException>( delegate { IConditionExpression expression = ConditionExpressionParser.Instance.Parse( string.Empty); }); }
public Delegate Evaluate(IConditionExpression conditionExpression, Type contextType) { var contextParameter = Expression.Parameter(contextType, "ctx"); var expression = conditionExpression.Process(contextParameter, contextType); var lambda = Expression.Lambda(expression, contextParameter); var func = lambda.Compile(); return(func); }
private void I_evaluate_context_with_condition(string left, Operator op, string right) { conditionExpression = new LeafExpression() { Left = left, Operator = op, Right = right }; }
public void ParseNullTest() { Assert.Throws <ArgumentNullException>( delegate { IConditionExpression expression = ConditionExpressionParser.Instance.Parse( null); }); }
public Func <T, bool> Evaluate <T>(IConditionExpression conditionExpression) where T : class { var contextType = typeof(T); var contextParameter = Expression.Parameter(contextType, "ctx"); var expression = conditionExpression.Process(contextParameter, contextType); var @delegate = Expression.Lambda <Func <T, bool> >(expression, contextParameter); var func = @delegate.Compile(); return(func); }
private void I_evaluate_device_with_condition(string left, Operator op, string right, params string[] additionalArgs) { conditionExpression = new LeafExpression() { Left = left, Operator = op, Right = right, OperatorArgs = additionalArgs }; }
public void ParseInvalidNestedExpressionTest() { string expression = "$(File.Name) : 'Test' And ()"; Assert.Throws <FormatException>( delegate { IConditionExpression conditionExpression = ConditionExpressionParser.Instance.Parse( expression); }); }
public virtual void VisitConditionExpression(IConditionExpression value) { this.VisitExpression(value.Condition); this.VisitExpression(value.Then); this.VisitExpression(value.Else); }
/// <summary> /// Gets the scope required to evaluate the condition. /// </summary> /// <param name="expression">Condition expression.</param> /// <returns>Element attribute scope.</returns> private ElementAttributeScope GetRequiredScope(IConditionExpression expression) { ElementAttributeScope scope = ElementAttributeScope.Element; if (expression != null) { ElementAttributeExpression attributeExpression = expression as ElementAttributeExpression; if (attributeExpression != null) { scope = attributeExpression.Scope; } else { ElementAttributeScope leftScope = GetRequiredScope(expression.Left); ElementAttributeScope rightScope = GetRequiredScope(expression.Right); if (leftScope > rightScope) { scope = leftScope; } else { scope = rightScope; } } } return scope; }
/// <summary> /// Creates a new ElementFilter. /// </summary> /// <param name="conditionExpression">The condition expression.</param> public ElementFilter(string conditionExpression) { _conditionExpression = ConditionExpressionParser.Instance.Parse(conditionExpression); _requiredScope = GetRequiredScope(_conditionExpression); }
public Transition(int id, string name) : base(id) { this.condition = new AllwaysTrueConditionExpression(); this.name = name; }
/// <summary> /// Creates a new FileFilter. /// </summary> /// <param name="conditionExpression">The condition expression text.</param> public FileFilter(string conditionExpression) { _conditionExpression = ConditionExpressionParser.Instance.Parse(conditionExpression); }
private void WriteConditionExpression(IConditionExpression expression, IFormatter formatter) { formatter.Write("("); this.WriteExpression(expression.Condition, formatter); formatter.Write(" "); formatter.WriteKeyword("?"); formatter.Write(" "); this.WriteExpression(expression.Then, formatter); formatter.Write(" "); formatter.WriteKeyword(":"); formatter.Write(" "); this.WriteExpression(expression.Else, formatter); formatter.Write(")"); }
/// <summary> /// Creates a new unary operator expression. /// </summary> /// <param name="operatorType">Type of the operator.</param> /// <param name="innerExpression">The inner expression.</param> public UnaryOperatorExpression( UnaryExpressionOperator operatorType, IConditionExpression innerExpression) { _operatorType = operatorType; _innerExpression = innerExpression; }
public virtual IExpression TransformConditionExpression(IConditionExpression value) { value.Condition = this.TransformExpression(value.Condition); value.Then = this.TransformExpression(value.Then); value.Else = this.TransformExpression(value.Else); return value; }