Пример #1
0
        public void ConstraintInfoTest()
        {
            IBooleanOperator trueExpression = new GreaterThanOperator(Three, Two);
            var trueTree      = new BooleanExpressionTree("true", trueExpression);
            var constraintOne = new ConstraintInfo(trueTree);

            Console.Write("Created constraint: ");
            Console.WriteLine(constraintOne);
            Console.WriteLine("ConstraintInfoTest: testing name of anonymous constraint...");
            Expect(constraintOne.Name == string.Empty);
            Console.WriteLine("ConstraintInfoTest: testing Predicate property (constraintOne)...");
            Expect(constraintOne.Predicate == trueTree);

            const string     constraintName  = "ConstraintTwo";
            IBooleanOperator falseExpression = new LessThanOperator(Three, Two);
            var falseTree     = new BooleanExpressionTree("false", falseExpression);
            var constraintTwo = new ConstraintInfo(constraintName, falseTree);

            Console.Write("Created constraint: ");
            Console.WriteLine(constraintTwo);
            Console.WriteLine("ConstraintInfoTest: testing Name of named constraint...");
            Expect(constraintTwo.Name == constraintName);
            Console.WriteLine("ConstraintInfoTest: testing Predicate property (constraintTwo)...");
            Expect(constraintTwo.Predicate == falseTree);
        }
        public void Format_WhenNotInclusive_ExpectNotInclusiveQuery()
        {
            // Arrange
            var greaterThanFormatter = new GreaterThanFormatter();

            var node = new GreaterThanOperator
            {
                Name      = "ColumnName",
                Value     = 23,
                Inclusive = false,
                Statement = "ColumnName:[*,23]",
            };

            // Act
            var stopwatch       = Stopwatch.StartNew();
            var sqlDataResponse = greaterThanFormatter.Format(node, 0);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(sqlDataResponse.Sql, Is.EqualTo("ColumnName > @ColumnName0"));
            Assert.That(sqlDataResponse.Params.First().Value, Is.EqualTo(23));
        }
Пример #3
0
        public void Visit(GreaterThanOperator node)
        {
            VisitBinary(node);

            ITerminalWrapper leftWrapper  = ReferenceTables.GetValue(node.Left);
            ITerminalWrapper rightWrapper = ReferenceTables.GetValue(node.Right);

            ReferenceTables.SetValue(node, ((dynamic)leftWrapper) > ((dynamic)rightWrapper));
        }
Пример #4
0
            public void OperatorIsMatch_WhenValuesAreEqual_ReturnsFalse()
            {
                // Arrange
                var sut = new GreaterThanOperator();

                sut.SetParameter("7");

                // Act
                var res = sut.OperatorIsMatch(7);

                // Assert
                res.Should().BeFalse("7 is not greater than 7");
            }
Пример #5
0
            public void OperatorIsMatch_WhenValueIsNotGreaterThan_ReturnsFalse()
            {
                // Arrange
                var sut = new GreaterThanOperator();

                sut.SetParameter("100");

                // Act
                var res = sut.OperatorIsMatch(20);

                // Assert
                res.Should().BeFalse("20 is not greater than 100");
            }
Пример #6
0
            public void OperatorIsMatch_WhenValueIsGreaterThan_ReturnsTrue()
            {
                // Arrange
                var sut = new GreaterThanOperator();

                sut.SetParameter("10");

                // Act
                var res = sut.OperatorIsMatch(50);

                // Assert
                res.Should().BeTrue("50 is greater than 10");
            }
Пример #7
0
        public void ExitComparison(QueryParser.ComparisonContext context)
        {
            var opToken = context.REL_OP();

            if (opToken == null)
            {
                return;
            }

            var op    = opToken.Symbol;
            var right = _expressions.Pop();
            var left  = _expressions.Pop();

            BinaryOperatorExpression value = null;

            switch (op.Text)
            {
            case "<":
                value = new LessThanOperator(op.Line, op.Column, left, right);
                break;

            case "<=":
                value = new LessThanOrEqualOperator(op.Line, op.Column, left, right);
                break;

            case "<>":
            case "!=":
                value = new NotEqualOperator(op.Line, op.Column, left, right);
                break;

            case "==":
            case "=":
                value = new EqualOperator(op.Line, op.Column, left, right);
                break;

            case ">":
                value = new GreaterThanOperator(op.Line, op.Column, left, right);
                break;

            case ">=":
                value = new GreaterThanOrEqualOperator(op.Line, op.Column, left, right);
                break;
            }

            Trace.Assert(value != null, "Invalid comparison operator " + op.Text);

            _expressions.Push(value);
        }
Пример #8
0
        public void CreateExpressionTest1()
        {
            // Arrange
            Expression parameterExpression = Expression.Parameter(typeof(Employee), "sample");
            Expression expressionProperty  = Expression.Property(parameterExpression, nameof(Employee.Age));
            Expression expressionConstant  = Expression.Constant(20);

            Expression expectedExpression = Expression.GreaterThan(expressionProperty, expressionConstant);

            var _operator = new GreaterThanOperator();

            // Act
            Expression resultExpression = _operator.CreateExpression(expressionProperty, expressionConstant);

            // Assert
            Assert.AreEqual(expectedExpression.ToString(), resultExpression.ToString(), "Expression not correct");
        }
Пример #9
0
        public void BooleanExpressionTreeTest()
        {
            const string     name       = "bit";
            IBooleanOperator expression = new GreaterThanOperator(Three, Two);
            var bet = new BooleanExpressionTree(name, expression);

            Console.Write("Created new BooleanExpressionTree: ");
            Console.WriteLine(bet);
            Console.WriteLine("BooleanExpressionTreeTest: test Name property...");
            Expect(bet.Name == name);
            Console.WriteLine("BooleanExpressionTreeTest: test expression and ResolveReferences()...");
            IDictionary <string, IBoolean> bmap = new Dictionary <string, IBoolean>();
            IBoolean predicate = bet.ResolveReferences(bmap, null);

            Expect(predicate.Value == true);
            Console.WriteLine("BooleanExpressionTreeTest: testing map size (1)...");
            Expect(bmap.Count == 1);
            Console.WriteLine("BooleanExpressionTreeTest: testing map contents...");
            Expect(bmap[name] == predicate);
        }
Пример #10
0
        private static ExpTreeNode GetAsExpressionTree(this ParseTreeNode parsedNode, ExpTreeNode parentExpNode)
        {
            //The current node is a variable / value token. Create a value node and return it back
            if (!parsedNode.ChildNodes.Any())
            {
                switch (parsedNode.Term.Name)
                {
                case "variable":
                {
                    var varName = parsedNode.FindTokenAndGetValue <string>();
                    return(new VariableValue(varName, parentExpNode));
                }

                case "boolean":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <bool>(), parentExpNode));

                case "integer":
                case "decimal":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <double>(), parentExpNode));

                case "SingleQuoteString":
                case "DoubleQuoteString":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <string>(), parentExpNode));

                default:
                    throw parsedNode.GetException($"Invalid token type '{parsedNode.Term.Name}' in expression");
                }
            }

            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 1)
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[0], parentExpNode));
            }

            //Ignore parenthesis, the middle non-terminal is what we want
            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 3 && parsedNode.ChildNodes[0]?.Token?.Text == "(")
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[1], parentExpNode));
            }

            //Binary operator
            else if (parsedNode.ChildNodes.Count == 3)
            {
                BinaryOperatorBase binaryOp;
                var opStr = parsedNode.ChildNodes[1].FindToken().ValueString;
                switch (opStr)
                {
                // Math
                case "+":
                    binaryOp = new AdditionOperator(parentExpNode);
                    break;

                case "-":
                    binaryOp = new SubtractionOperator(parentExpNode);
                    break;

                case "*":
                    binaryOp = new MultiplacationOperator(parentExpNode);
                    break;

                case "/":
                    binaryOp = new DivisionOperator(parentExpNode);
                    break;

                case "%":
                    binaryOp = new ModulusOperator(parentExpNode);
                    break;

                case "^":
                    binaryOp = new PowerOperator(parentExpNode);
                    break;

                // Bool
                case "~==":
                    binaryOp = new LooseEqualsOperator(parentExpNode);
                    break;

                case "~!=":
                    binaryOp = new LooseNotEqualsOperator(parentExpNode);
                    break;

                case "==":
                    binaryOp = new EqualsOperator(parentExpNode);
                    break;

                case "!=":
                    binaryOp = new NotEqualsOperator(parentExpNode);
                    break;

                case "like":
                    binaryOp = new LikeOperator(parentExpNode);
                    break;

                case "match":
                    binaryOp = new MatchOperator(parentExpNode);
                    break;

                case ">":
                    binaryOp = new GreaterThanOperator(parentExpNode);
                    break;

                case ">=":
                    binaryOp = new GreaterThanOrEqualOperator(parentExpNode);
                    break;

                case "<":
                    binaryOp = new LessThanOperator(parentExpNode);
                    break;

                case "<=":
                    binaryOp = new LessThanOrEqualOperator(parentExpNode);
                    break;

                case "&&":
                    binaryOp = new AndOperator(parentExpNode);
                    break;

                case "||":
                    binaryOp = new OrOperator(parentExpNode);
                    break;

                default:
                    throw parsedNode.ChildNodes[1].GetException($"Unrecognized operator '{opStr}'");
                }

                binaryOp.LeftOperand  = GetAsExpressionTree(parsedNode.ChildNodes[0], binaryOp);
                binaryOp.RightOperand = GetAsExpressionTree(parsedNode.ChildNodes[2], binaryOp);

                //Optimize
                if (binaryOp.LeftOperand is LiteralValue && binaryOp.RightOperand is LiteralValue)
                {
                    return(new LiteralValue(binaryOp.Eval(), parentExpNode));
                }

                return(binaryOp);
            }

            // Unary operator
            else if (parsedNode.ChildNodes.Count == 2)
            {
                var opVal = parsedNode.ChildNodes[0].FindToken().Value;
                UnaryOperatorBase unaryOp;
                if (parsedNode.ChildNodes[0].FindToken().Value is TypeCode convertType)
                {
                    unaryOp = new TypeConversionOperator(convertType, parentExpNode);
                }
                else
                {
                    var opStr = opVal.ToString();
                    switch (opStr)
                    {
                    case "!":
                        unaryOp = new NotOperator(parentExpNode);
                        break;

                    //Property Checking
                    case "lengthof":
                        unaryOp = new LengthOperator(parentExpNode);
                        break;

                    case "typeof":
                        unaryOp = new TypeOperator(parentExpNode);
                        break;

                    default:
                        unaryOp = new GenericUnaryMathFunctionOperator(opStr, parentExpNode);
                        break;
                    }
                }

                unaryOp.Operand = GetAsExpressionTree(parsedNode.ChildNodes[1], unaryOp);

                //Optimize
                if (unaryOp.Operand is LiteralValue)
                {
                    return(new LiteralValue(unaryOp.Eval(), parentExpNode));
                }

                return(unaryOp);
            }

            else
            {
                throw parsedNode.GetException($"Invalid number of tokens ({parsedNode.ChildNodes.Count})");
            }
        }
Пример #11
0
 public void Visit(GreaterThanOperator node)
 {
 }
Пример #12
0
 public Bitmap Visit(GreaterThanOperator expr)
 {
     return(ComputeFunction(expr));
 }
 public virtual void Visit(GreaterThanOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Visit(binaryOperator.Right);
 }
Пример #14
0
 public override void Visit(GreaterThanOperator binaryOperator)
 {
     WriteBinaryExpression(binaryOperator, ">");
 }
 public override void Visit(GreaterThanOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Write(" > ");
     Visit(binaryOperator.Right);
 }
Пример #16
0
 public void Visit(GreaterThanOperator node)
 {
     VisitBinary(node);
     CheckOperandsAreOfSameType(node);
 }