public void ShouldReturnLiteralWhenNoSuffixLiteralTokenLong()
        {
            ExpressionLexer lexer  = new ExpressionLexer(int.MaxValue + "000", false, false);
            object          result = lexer.ReadLiteralToken();

            result.Should().BeOfType <long>().And.Be(((long)int.MaxValue) * 1000);
        }
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenDecimal()
        {
            ExpressionLexer lexer  = new ExpressionLexer("3258.678765765489753678965390", false, false);
            object          result = lexer.ReadLiteralToken();

            result.Should().BeOfType <decimal>().And.Be(3258.678765765489753678965390m);
        }
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenShortTimeOfDay()
        {
            ExpressionLexer lexer  = new ExpressionLexer("12:30:03", false, false);
            object          result = lexer.ReadLiteralToken();

            result.Should().BeOfType <TimeOfDay>().And.Be((new TimeOfDay(12, 30, 3, 0)));
        }
        public void ShouldReturnDateLiteralWhenNoSuffixDateLiteralToken()
        {
            ExpressionLexer lexer  = new ExpressionLexer("2014-09-19", false, false);
            object          result = lexer.ReadLiteralToken();

            result.Should().BeOfType <Date>().And.Be(new Date(2014, 9, 19));
        }
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenInt()
        {
            ExpressionLexer lexer  = new ExpressionLexer("5", false, false);
            object          result = lexer.ReadLiteralToken();

            result.Should().BeOfType <int>().And.Be(5);
        }
Esempio n. 6
0
 public void ShouldReturnLiteralWhenNoSuffixLiteralTokenFloat()
 {
     // significant figures: float is 7, double is 15/16, decimal is 28
     ExpressionLexer lexer = new ExpressionLexer("123.001", false, false);
     object result = lexer.ReadLiteralToken();
     result.Should().BeOfType<float>().And.Be(123.001f);
 }
Esempio n. 7
0
        public void ShouldThrowWhenNotLiteralToken()
        {
            ExpressionLexer lexer = new ExpressionLexer("potato", false, false);
            Action          read  = () => lexer.ReadLiteralToken();

            read.Throws <ODataException>(ODataErrorStrings.ExpressionLexer_ExpectedLiteralToken("potato"));
        }
Esempio n. 8
0
 public void ShouldReturnDateTimeOffSetLiteralWhenNoSuffixDateLiteralToken()
 {
     ExpressionLexer lexer = new ExpressionLexer("2014-09-19T12:13:14+00:00", false, false);
     object result = lexer.ReadLiteralToken();
     result.Should()
         .BeOfType<DateTimeOffset>()
         .And.Be(new DateTimeOffset(2014, 9, 19, 12, 13, 14, new TimeSpan(0, 0, 0)));
 }
Esempio n. 9
0
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenDecimal()
        {
            ExpressionLexer lexer        = new ExpressionLexer("3258.678765765489753678965390", false, false);
            object          result       = lexer.ReadLiteralToken();
            var             decimalValue = Assert.IsType <decimal>(result);

            Assert.Equal(3258.678765765489753678965390m, decimalValue);
        }
Esempio n. 10
0
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenLong()
        {
            ExpressionLexer lexer     = new ExpressionLexer(int.MaxValue + "000", false, false);
            object          result    = lexer.ReadLiteralToken();
            var             longValue = Assert.IsType <long>(result);

            Assert.Equal(((long)int.MaxValue) * 1000, longValue);
        }
Esempio n. 11
0
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenShortTimeOfDay()
        {
            ExpressionLexer lexer     = new ExpressionLexer("12:30:03", false, false);
            object          result    = lexer.ReadLiteralToken();
            var             timeOfDay = Assert.IsType <TimeOfDay>(result);

            Assert.Equal(new TimeOfDay(12, 30, 3, 0), timeOfDay);
        }
Esempio n. 12
0
        public void ShouldReturnDateLiteralWhenNoSuffixDateLiteralToken()
        {
            ExpressionLexer lexer  = new ExpressionLexer("2014-09-19", false, false);
            object          result = lexer.ReadLiteralToken();
            var             date   = Assert.IsType <Date>(result);

            Assert.Equal(new Date(2014, 9, 19), date);
        }
Esempio n. 13
0
        public void ShouldReturnDateTimeOffSetLiteralWhenNoSuffixDateLiteralToken()
        {
            ExpressionLexer lexer  = new ExpressionLexer("2014-09-19T12:13:14+00:00", false, false);
            object          result = lexer.ReadLiteralToken();
            var             dto    = Assert.IsType <DateTimeOffset>(result);

            Assert.Equal(new DateTimeOffset(2014, 9, 19, 12, 13, 14, new TimeSpan(0, 0, 0)), dto);
        }
Esempio n. 14
0
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenInt()
        {
            ExpressionLexer lexer     = new ExpressionLexer("5", false, false);
            object          result    = lexer.ReadLiteralToken();
            int             intResult = Assert.IsType <int>(result);

            Assert.Equal(5, intResult);
        }
Esempio n. 15
0
        public void ShouldReturnLiteralWhenNoSuffixLiteralTokenDouble()
        {
            // significant figures: float is 7, double is 15/16, decimal is 28
            ExpressionLexer lexer       = new ExpressionLexer("1234567.001", false, false);
            object          result      = lexer.ReadLiteralToken();
            var             doubleValue = Assert.IsType <double>(result);

            Assert.Equal(1234567.001d, doubleValue);
        }
Esempio n. 16
0
        /// <summary>
        /// Converts the given <paramref name="value"/> to a corresponding CLR type. Expects the
        /// <paramref name="value"/> to have already been properly unescaped from an actual Uri.
        /// </summary>
        /// <param name="value">Value from a Uri to be converted.</param>
        /// <param name="version">Version to be compliant with.</param>
        /// <param name="model">Optional model to perform verification against.</param>
        /// <param name="typeReference">Optional IEdmTypeReference to perform verification against.
        ///  Callers must provide a <paramref name="model"/> containing this type if it is specified.</param>
        /// <returns>A CLR object that the <paramref name="value"/> represents or an EnumNode.</returns>
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            ExceptionUtils.CheckArgumentNotNull(value, "value");
            if (typeReference != null && model == null)
            {
                throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }

            if (model == null)
            {
                model = Microsoft.OData.Edm.EdmCoreModel.Instance;
            }

            // Let ExpressionLexer try to get a primitive
            ExpressionLexer lexer = new ExpressionLexer(value, false /*moveToFirstToken*/, false /*useSemicolonDelimeter*/);
            Exception       error;
            ExpressionToken token;

            lexer.TryPeekNextToken(out token, out error);

            if (token.Kind == ExpressionTokenKind.BracedExpression && typeReference != null && typeReference.IsStructured())
            {
                return(ODataUriConversionUtils.ConvertFromResourceValue(value, model, typeReference));
            }

            if (token.Kind == ExpressionTokenKind.BracketedExpression)
            {
                return(ODataUriConversionUtils.ConvertFromCollectionValue(value, model, typeReference));
            }

            QueryNode enumConstNode;

            if ((token.Kind == ExpressionTokenKind.Identifier) && // then try parsing the entire text as enum value
                EnumBinder.TryBindIdentifier(lexer.ExpressionText, null, model, out enumConstNode))
            {
                return(((ConstantNode)enumConstNode).Value);
            }

            object result = lexer.ReadLiteralToken();

            // If we have a typeReference then perform verification and convert if necessary
            if (typeReference != null)
            {
                result = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(result, value, model, typeReference);
            }

            return(result);
        }
Esempio n. 17
0
        /// <summary>
        /// Converts the given <paramref name="value"/> to a corresponding CLR type. Expects the 
        /// <paramref name="value"/> to have already been properly unescaped from an actual Uri.
        /// </summary>
        /// <param name="value">Value from a Uri to be converted.</param>
        /// <param name="version">Version to be compliant with.</param>
        /// <param name="model">Optional model to perform verification against.</param>
        /// <param name="typeReference">Optional IEdmTypeReference to perform verification against. 
        ///  Callers must provide a <paramref name="model"/> containing this type if it is specified.</param>
        /// <returns>A CLR object that the <paramref name="value"/> represents or an EnumNode.</returns>
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            ExceptionUtils.CheckArgumentNotNull(value, "value");
            if (typeReference != null && model == null)
            {
                throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }

            if (model == null)
            {
                model = Microsoft.OData.Edm.Library.EdmCoreModel.Instance;
            }

            // Let ExpressionLexer try to get a primitive
            ExpressionLexer lexer = new ExpressionLexer(value, false /*moveToFirstToken*/, false /*useSemicolonDelimeter*/);
            Exception error;
            ExpressionToken token;

            lexer.TryPeekNextToken(out token, out error);

            if (token.Kind == ExpressionTokenKind.BracketedExpression)
            {
                return ODataUriConversionUtils.ConvertFromComplexOrCollectionValue(value, model, typeReference);
            }

            QueryNode enumConstNode;
            if ((token.Kind == ExpressionTokenKind.Identifier)  // then try parsing the entire text as enum value
                && EnumBinder.TryBindIdentifier(lexer.ExpressionText, null, model, out enumConstNode))
            {
                return ((ConstantNode)enumConstNode).Value;
            }

            object result = lexer.ReadLiteralToken();

            // If we have a typeReference then perform verification and convert if necessary
            if (typeReference != null)
            {
                result = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(result, model, typeReference);
            }

            return result;
        }