public void EvaluateAndTest() { IConditionExpression nameExpression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); IConditionExpression attributeExpression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new ElementAttributeExpression(ElementAttributeType.Access), new StringExpression("Protected")); IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.And, nameExpression, attributeExpression); FieldElement element = new FieldElement(); element.Name = "Test"; element.Access = CodeAccess.Protected; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "Foo"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); element.Name = "Test"; element.Access = CodeAccess.Private; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
public void ToStringInvalidOperatorTest() { ElementAttributeExpression attributeExpression = new ElementAttributeExpression(ElementAttributeType.Name); StringExpression stringExpression = new StringExpression("Test"); BinaryOperatorExpression operatorExpression = new BinaryOperatorExpression( (BinaryExpressionOperator) int.MinValue, attributeExpression, stringExpression); Assert.AreEqual(string.Format("($(Element.Name) {0} 'Test')", int.MinValue), operatorExpression.ToString()); }
public void ToStringTest() { ElementAttributeExpression attributeExpression = new ElementAttributeExpression( ElementAttributeType.Name); StringExpression stringExpression = new StringExpression("Test"); BinaryOperatorExpression operatorExpression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, attributeExpression, stringExpression); Assert.AreEqual("($(Element.Name) == 'Test')", operatorExpression.ToString()); }
public void EvaluateElementNameContainsTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); FieldElement element = new FieldElement(); element.Name = "Test"; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "OnTest1"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "Foo"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
/// <summary> /// Takes in a list of expressions and operator expression placeholders and /// builds an expression tree node. /// </summary> /// <param name="originalNodes">Original nodes.</param> /// <param name="originalExpression">Original expression.</param> /// <returns>A condition expression.</returns> private static IConditionExpression AssembleExpressionTree( ReadOnlyCollection<IConditionExpression> originalNodes, string originalExpression) { IConditionExpression conditionExpression = null; List<IConditionExpression> nodes = new List<IConditionExpression>(originalNodes); // // Build a queue that represents the binary operator precedence // Queue<BinaryExpressionOperator> operatorPrecedence = new Queue<BinaryExpressionOperator>(); operatorPrecedence.Enqueue(BinaryExpressionOperator.Equal); operatorPrecedence.Enqueue(BinaryExpressionOperator.NotEqual); operatorPrecedence.Enqueue(BinaryExpressionOperator.Contains); operatorPrecedence.Enqueue(BinaryExpressionOperator.Matches); operatorPrecedence.Enqueue(BinaryExpressionOperator.And); operatorPrecedence.Enqueue(BinaryExpressionOperator.Or); // // Loop through the nodes and merge them by operator precedence // BinaryExpressionOperator currentOperator = operatorPrecedence.Dequeue(); while (nodes.Count > 1) { for (int nodeIndex = 1; nodeIndex < nodes.Count - 1; nodeIndex++) { OperatorExpressionPlaceholder operatorExpressionPlaceHolder = nodes[nodeIndex] as OperatorExpressionPlaceholder; if (operatorExpressionPlaceHolder != null && operatorExpressionPlaceHolder.Operator == currentOperator) { IConditionExpression left = nodes[nodeIndex - 1]; IConditionExpression right = nodes[nodeIndex + 1]; if ((operatorExpressionPlaceHolder.Operator == BinaryExpressionOperator.Equal || operatorExpressionPlaceHolder.Operator == BinaryExpressionOperator.Contains) && !(left is LeafExpression && right is LeafExpression)) { OnInvalidExpression(originalExpression); } BinaryOperatorExpression operatorExpression = new BinaryOperatorExpression( operatorExpressionPlaceHolder.Operator, left, right); nodes[nodeIndex] = operatorExpression; nodes.Remove(left); nodes.Remove(right); // // Restart processing of this level // nodeIndex = 0; } } if (operatorPrecedence.Count > 0) { currentOperator = operatorPrecedence.Dequeue(); } else { break; } } // // At the end of everything, we should have a single binary or unary // condition expression. Anything else is invalid and a format exception // will be thrown. // if (nodes.Count == 1) { conditionExpression = nodes[0] as BinaryOperatorExpression; if (conditionExpression == null) { conditionExpression = nodes[0] as UnaryOperatorExpression; } } if (conditionExpression == null) { OnInvalidExpression(originalExpression); } return conditionExpression; }
public void EvaluateInvalidOperatorTest() { IConditionExpression expression = new BinaryOperatorExpression( (BinaryExpressionOperator) int.MinValue, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); FieldElement element = new FieldElement(); element.Name = "Test"; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); }
public void EvaluateFilePathContainsTest() { string testFile1 = Path.GetTempFileName(); string testFile2 = Path.GetTempFileName(); try { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new FileAttributeExpression(FileAttributeType.Path), new StringExpression(Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(testFile1)))); FileInfo file = new FileInfo(testFile1); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsTrue(result, "Unexpected expression evaluation result."); file = new FileInfo(testFile2); result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsFalse(result, "Unexpected expression evaluation result."); } finally { try { File.Delete(testFile1); File.Delete(testFile2); } catch { } } }
public void EvaluateFileNullTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new FileAttributeExpression(FileAttributeType.Name), new StringExpression("Test")); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, null as FileInfo); }
public void EvaluateFileNameContainsTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new FileAttributeExpression(FileAttributeType.Name), new StringExpression("Test")); FileInfo file = new FileInfo("Test"); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsTrue(result, "Unexpected expression evaluation result."); file = new FileInfo("Blah"); result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
public void EvaluateFileAttributesContainsTest() { string testFile = Path.GetTempFileName(); try { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new FileAttributeExpression(FileAttributeType.Attributes), new StringExpression("ReadOnly")); FileInfo file = new FileInfo(testFile); file.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsTrue(result, "Unexpected expression evaluation result."); file.Attributes = FileAttributes.Normal; file = new FileInfo(testFile); result = ConditionExpressionEvaluator.Instance.Evaluate( expression, file); Assert.IsFalse(result, "Unexpected expression evaluation result."); } finally { try { File.Delete(testFile); } catch { } } }
public void EvaluateElementParentAttributesContainsTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new ElementAttributeExpression(ElementAttributeType.Attributes, ElementAttributeScope.Parent), new StringExpression("Attribute2")); FieldElement element = new FieldElement(); element.Name = "Test"; TypeElement typeElement = new TypeElement(); typeElement.Type = TypeElementType.Structure; typeElement.Name = "TestType"; typeElement.AddChild(element); typeElement.AddAttribute(new AttributeElement("Attribute1")); typeElement.AddAttribute(new AttributeElement("Attribute24")); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); typeElement.ClearAttributes(); result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
public void EvaluateElementNullTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, null as ICodeElement); }
public void EvaluateElementNameMatchesTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Matches, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("IDisposable\\..*")); MethodElement element = new MethodElement(); element.Name = "IDisposable.Dispose"; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "IDisposable.Test"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "IDisposable"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
/// <summary> /// Takes in a list of expressions and operator expression placeholders and /// builds an expression tree node. /// </summary> /// <param name="originalNodes">Original nodes.</param> /// <param name="originalExpression">Original expression.</param> /// <returns>A condition expression.</returns> private static IConditionExpression AssembleExpressionTree( ReadOnlyCollection <IConditionExpression> originalNodes, string originalExpression) { IConditionExpression conditionExpression = null; List <IConditionExpression> nodes = new List <IConditionExpression>(originalNodes); // // Build a queue that represents the binary operator precedence // Queue <BinaryExpressionOperator> operatorPrecedence = new Queue <BinaryExpressionOperator>(); operatorPrecedence.Enqueue(BinaryExpressionOperator.Equal); operatorPrecedence.Enqueue(BinaryExpressionOperator.NotEqual); operatorPrecedence.Enqueue(BinaryExpressionOperator.Contains); operatorPrecedence.Enqueue(BinaryExpressionOperator.Matches); operatorPrecedence.Enqueue(BinaryExpressionOperator.And); operatorPrecedence.Enqueue(BinaryExpressionOperator.Or); // // Loop through the nodes and merge them by operator precedence // BinaryExpressionOperator currentOperator = operatorPrecedence.Dequeue(); while (nodes.Count > 1) { for (int nodeIndex = 1; nodeIndex < nodes.Count - 1; nodeIndex++) { OperatorExpressionPlaceholder operatorExpressionPlaceHolder = nodes[nodeIndex] as OperatorExpressionPlaceholder; if (operatorExpressionPlaceHolder != null && operatorExpressionPlaceHolder.Operator == currentOperator) { IConditionExpression left = nodes[nodeIndex - 1]; IConditionExpression right = nodes[nodeIndex + 1]; if ((operatorExpressionPlaceHolder.Operator == BinaryExpressionOperator.Equal || operatorExpressionPlaceHolder.Operator == BinaryExpressionOperator.Contains) && !(left is LeafExpression && right is LeafExpression)) { OnInvalidExpression(originalExpression); } BinaryOperatorExpression operatorExpression = new BinaryOperatorExpression( operatorExpressionPlaceHolder.Operator, left, right); nodes[nodeIndex] = operatorExpression; nodes.Remove(left); nodes.Remove(right); // // Restart processing of this level // nodeIndex = 0; } } if (operatorPrecedence.Count > 0) { currentOperator = operatorPrecedence.Dequeue(); } else { break; } } // // At the end of everything, we should have a single binary or unary // condition expression. Anything else is invalid and a format exception // will be thrown. // if (nodes.Count == 1) { conditionExpression = nodes[0] as BinaryOperatorExpression; if (conditionExpression == null) { conditionExpression = nodes[0] as UnaryOperatorExpression; } } if (conditionExpression == null) { OnInvalidExpression(originalExpression); } return(conditionExpression); }