private string FormatComparisonExpression(IFilterTree tree) { var attributeText = tree.GetChild(0).Token.Value; var operatorText = TokenMappings.TokenTypeToLegacyValueMap[tree.Token.Type]; var contentText = TokenMappings.IsListOperation(tree.Token) ? FormatValueList(tree.GetChild(1)) : FormatValue(tree.GetChild(1)); return($"{attributeText} {operatorText} [{contentText}]"); }
private string FormatLogicalExpression(IFilterTree tree, bool isolate) { var operatorText = TokenMappings.TokenTypeToDefaultValueMap[tree.Token.Type]; string resultExpression; if (TokenMappings.IsBinaryLogicalOperation(tree.Token)) { var operandTexts = new List <string>(); foreach (var childItem in tree.GetChildren()) { operandTexts.Add(FormatExpression(childItem, true)); } resultExpression = string.Join($" {operatorText} ", operandTexts); } else if (TokenMappings.IsUnaryLogicalOperation(tree.Token)) { var childNode = tree.GetChild(0); var operandText = FormatExpression(childNode, false); resultExpression = TokenMappings.IsBinaryLogicalOperation(childNode.Token) ? $"{operatorText} ({operandText})" : $"{operatorText} {operandText}"; } else { throw new FormaterException($"Encountered unexpected node of type {tree.Token.Type}."); } return(isolate ? $"({resultExpression})" : resultExpression); }
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is <see langword="null" />.</exception> public static bool CompareTrees([NotNull] IFilterTree expected, [NotNull] IFilterTree actual) { if (expected == null) { throw new ArgumentNullException(nameof(expected)); } if (actual == null) { throw new ArgumentNullException(nameof(actual)); } if (expected.Token.Type != actual.Token.Type) { return(false); } if (expected.ChildCount != actual.ChildCount) { return(false); } if (IsContentToken(expected.Token.Type)) { if (expected.Token.Type == TokenType.Real) { var expectedValue = ToDouble(expected.Token.Value); var actualValue = ToDouble(actual.Token.Value); // a precision of 10^-12 is enough (same as defined in Zeiss.PiWeb.Common.Util.DoubleCompareHelper) return(Math.Abs(expectedValue - actualValue) <= 1e-12); } if (expected.Token.Type == TokenType.Integer) { var expectedValue = int.Parse(expected.Token.Value); var actualValue = int.Parse(actual.Token.Value); return(expectedValue == actualValue); } if (!string.Equals(expected.Token.Value, actual.Token.Value)) { return(false); } } for (var i = 0; i < expected.ChildCount; ++i) { if (!CompareTrees(expected.GetChild(i), actual.GetChild(i))) { return(false); } } return(true); }