public void BuildExpression_ValidBinaryExpressionStack_ReturnsBinaryFilterExpression()
        {
            _baseStrategy.Stub(x => x.BuildExpression(null))
            .IgnoreArguments()
            .Return(FilterExpression.MemberAccess("TestInt"));

            var stack = new Stack <Token>();

            stack.Push(new Token
            {
                TokenType = TokenType.Name,
                Value     = "TestInt"
            });

            stack.Push(new Token
            {
                TokenType = TokenType.Integer,
                Value     = "42"
            });

            stack.Push(new Token
            {
                TokenType = TokenType.BinaryOperator,
                Value     = "ne"
            });

            var expression = _binaryStrategy.BuildExpression(stack);

            Assert.That(expression, Is.Not.Null);
            Assert.That(expression, Is.TypeOf <BinaryFilterExpression>());
        }
        public void Equals_FilterQueryPartNull_ReturnsFalse()
        {
            var             queryPart = new FilterQueryPart(FilterExpression.Binary(FilterExpression.MemberAccess("Hello"), FilterExpressionOperator.LessThan, FilterExpression.Constant(4)));
            FilterQueryPart other     = null;

            var actual = queryPart.Equals(other);

            Assert.That(actual, Is.False);
        }
        public void Equals_Self_ReturnsTrue()
        {
            var queryPart = new FilterQueryPart(FilterExpression.Binary(FilterExpression.MemberAccess("Hello"), FilterExpressionOperator.LessThan, FilterExpression.Constant(4)));
            var other     = queryPart;

            var actual = queryPart.Equals(other);

            Assert.That(actual, Is.True);
        }
        public void Equals_AnotherDifferentFilterQueryPart_ReturnsFalse()
        {
            var queryPart = new FilterQueryPart(FilterExpression.MemberAccess("Hello"));
            var other     = new FilterQueryPart(FilterExpression.MemberAccess("World"));

            var actual = queryPart.Equals(other);

            Assert.That(actual, Is.False);
        }
        public void Equals_AnotherEquivalentFilterQueryPart_ReturnsTrue()
        {
            var queryPart = new FilterQueryPart(FilterExpression.MemberAccess("Hello"));
            var other     = new FilterQueryPart(FilterExpression.MemberAccess("Hello"));

            var actual = queryPart.Equals(other);

            Assert.That(actual, Is.True);
        }
Esempio n. 6
0
        public void GetHashCode_TwoMembers_ReturnsCorrectResult()
        {
            var queryPart = new ExpandQueryPart(FilterExpression.MemberAccess("Hello"), FilterExpression.MemberAccess("World"));

            var expected = "Members:Hello,World;".GetHashCode();
            var actual   = queryPart.GetHashCode();

            Assert.That(actual, Is.EqualTo(expected));
        }
Esempio n. 7
0
        public void Parse_StringContainingIntegerTen_ReturnsCorrectQueryPart()
        {
            var result = _strategy.Parse(Type, "TestProperty");

            Assert.That(result, Is.InstanceOf <ExpandQueryPart>());
            Assert.That(((ExpandQueryPart)result).Members, Is.Not.Null);
            Assert.That(((ExpandQueryPart)result).Members.First(), Is.EqualTo(FilterExpression.MemberAccess("TestProperty")));
            Assert.That(result.ToString(), Is.EqualTo("$expand=TestProperty"));
        }
        public void Equals_ObjectOfDifferentType_ReturnsFalse()
        {
            var    queryPart = new FilterQueryPart(FilterExpression.Binary(FilterExpression.MemberAccess("Hello"), FilterExpressionOperator.LessThan, FilterExpression.Constant(4)));
            object other     = String.Empty;

            var actual = queryPart.Equals(other);

            Assert.That(actual, Is.False);
        }
        public void Translate_EqualsFilterExpressionRightTypeAssignableFromLeftType_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.Binary(FilterExpression.MemberAccess("TestString"), FilterExpressionOperator.Equal, FilterExpression.MemberAccess("TestObject"));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestString == (string)x.TestObject);

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_EqualsFilterExpressionRightConstant_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.Binary(FilterExpression.MemberAccess("TestString"), FilterExpressionOperator.Equal, FilterExpression.Constant("hello"));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestString == "hello");

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_MemberOfMemberAccess_ThrowsException()
        {
            var filterExpression = FilterExpression.MemberAccess(FilterExpression.MemberAccess("TestChild"), "TestGuid");

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestChild.TestGuid);

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_CastMethodCallFilterExpression_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.MethodCall(Function.Cast, FilterExpression.MemberAccess("TestObject"), FilterExpression.Constant(typeof(string)));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => (string)x.TestObject);

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_IndexOfFilterExpression_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.MethodCall(Function.IndexOf, FilterExpression.MemberAccess("TestString"), FilterExpression.Constant("hi"));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestString.IndexOf("hi"));

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_GreaterThanOrEqualToFilterExpression_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.Binary(FilterExpression.MemberAccess("TestInt"), FilterExpressionOperator.GreaterThanOrEqual, FilterExpression.Constant(42));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestInt >= 42);

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void Translate_EqualsFilterExpressionLeftDecimalRightInt_ReturnsCorrectLinqExpression()
        {
            var filterExpression = FilterExpression.Binary(FilterExpression.MemberAccess("TestDecimal"), FilterExpressionOperator.Equal, FilterExpression.MemberAccess("TestInt"));

            var expression = _filterExpressionTranslator.Translate(filterExpression);

            var expected = GetLambdaBody(x => x.TestDecimal == (decimal)x.TestInt);

            Assert.That(expression.ToString(), Is.EqualTo(expected.ToString()));
        }
Esempio n. 16
0
        public FilterExpression BuildExpression(Stack <Token> stack)
        {
            // pop off the member access operator ('.')
            stack.Pop();

            var token = stack.Pop();

            var memberName = token.Value;

            var memberParent = _filterExpressionParserStrategy.BuildExpression(stack);

            var result = FilterExpression.MemberAccess(memberParent, memberName);

            return(result);
        }
Esempio n. 17
0
        public void Parse_ValidUriWithExpandQueryParameter_ReturnsCompleteODataQueryObjectWithCorrectProperties()
        {
            var uri = new Uri("http://www.site.com/path/Model?$expand=TestProperty");

            var result = _parser.Parse(uri);

            Assert.That(result.CountPredicate, Is.Null);
            Assert.That(result.ExpandPredicate, Is.Not.Null);
            Assert.That(result.ExpandPredicate.Members, Is.Not.Null);
            Assert.That(result.ExpandPredicate.Members.First(), Is.EqualTo(FilterExpression.MemberAccess("TestProperty")));
            Assert.That(result.FilterPredicate, Is.Null);
            Assert.That(result.FormatPredicate, Is.Null);
            Assert.That(result.InlineCountPredicate, Is.Null);
            Assert.That(result.OrderByPredicate, Is.Null);
            Assert.That(result.SelectPredicate, Is.Null);
            Assert.That(result.SkipPredicate, Is.Null);
            Assert.That(result.SkipTokenPredicate, Is.Null);
            Assert.That(result.TopPredicate, Is.Null);
        }
        public FilterExpression BuildExpression(Stack <Token> stack)
        {
            if (stack == null)
            {
                throw new ArgumentNullException("stack", "Cannot build expression from null expression stack.");
            }

            if (stack.Count < 1)
            {
                throw new ArgumentException("Cannot build expression from empty expression stack.", "stack");
            }

            var token = stack.Pop();

            if (token == null)
            {
                throw new ArgumentException("Cannot build expression from null token.");
            }

            return(FilterExpression.MemberAccess(token.Value));
        }
        public void Translate_TestIntEqualsThree_ReturnsOneResult()
        {
            var left  = FilterExpression.MemberAccess("TestInt");
            var right = FilterExpression.Constant(3);

            var filterExpression = FilterExpression.Binary(left, FilterExpressionOperator.Equal, right);

            var query = new ODataQuery
            {
                FilterPredicate = ODataQueryPart.Filter(filterExpression)
            };

            var expression = _translator.Translate <TestModel>(query);

            var fn = (Func <IQueryable <TestModel>, IQueryable <TestModel> >)expression.Compile();

            var result = fn(_source).ToList();

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].TestInt, Is.EqualTo(3));
        }
        public void Translate_InvalidMemberAccess_ThrowsException()
        {
            var filterExpression = FilterExpression.MemberAccess(Guid.NewGuid().ToString());

            Assert.That(() => _filterExpressionTranslator.Translate(filterExpression), Throws.ArgumentException);
        }
Esempio n. 21
0
        public void ToString_TwoMembers_ReturnsCommaSeperatedString()
        {
            var selectQueryPart = new SelectQueryPart(FilterExpression.MemberAccess("TestString"), FilterExpression.MemberAccess("TestInt"));

            Assert.That(selectQueryPart.ToString(), Is.EqualTo("$select=TestString, TestInt"));
        }
Esempio n. 22
0
        public void ToString_TwoMembers_EmptyString()
        {
            var expandQueryPart = new ExpandQueryPart(FilterExpression.MemberAccess("TestString"), FilterExpression.MemberAccess("TestInt"));

            Assert.That(expandQueryPart.ToString(), Is.EqualTo("$expand=TestString, TestInt"));
        }
Esempio n. 23
0
        public void GetHashCode_FilterExpression_ReturnsFilterExpressionHashCode()
        {
            var filterExpression = FilterExpression.Binary(FilterExpression.Constant(4), FilterExpressionOperator.LessThanOrEqual, FilterExpression.MemberAccess("HelloWorld"));

            var queryPart = new FilterQueryPart(filterExpression);

            Assert.That(queryPart.GetHashCode(), Is.EqualTo(filterExpression.GetHashCode()));
        }
Esempio n. 24
0
        public void FilterQueryPart_InheritsFromIEquatable()
        {
            var actual = new FilterQueryPart(FilterExpression.Binary(FilterExpression.MemberAccess("Hello"), FilterExpressionOperator.LessThan, FilterExpression.Constant(4)));

            Assert.That(actual, Is.InstanceOf <IEquatable <FilterQueryPart> >());
        }