DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlExpressionParseTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 
18 using Deveel.Data.Sql.Objects;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Sql.Expressions {
23  [TestFixture]
24  public sealed class SqlExpressionParseException {
25  [Test]
26  public void SimpleNumericAdd() {
27  const string s = "6578 + 76.32";
28 
29  SqlExpression sqlExp = null;
30  Assert.DoesNotThrow(() => sqlExp = SqlExpression.Parse(s));
31  Assert.IsNotNull(sqlExp);
32  Assert.IsInstanceOf<SqlBinaryExpression>(sqlExp);
33  Assert.AreEqual(SqlExpressionType.Add, sqlExp.ExpressionType);
34 
35  var binExp = (SqlBinaryExpression) sqlExp;
36  Assert.IsInstanceOf<SqlConstantExpression>(binExp.Left);
37  Assert.IsInstanceOf<SqlConstantExpression>(binExp.Right);
38  Assert.IsTrue(binExp.CanEvaluate);
39  }
40 
41  [Test]
42  public void SimpleNumericSubtract() {
43  const string s = "642.221 - 116.32";
44 
45  SqlExpression sqlExp = null;
46  Assert.DoesNotThrow(() => sqlExp = SqlExpression.Parse(s));
47  Assert.IsNotNull(sqlExp);
48  Assert.IsInstanceOf<SqlBinaryExpression>(sqlExp);
49  Assert.AreEqual(SqlExpressionType.Subtract, sqlExp.ExpressionType);
50 
51  var binExp = (SqlBinaryExpression) sqlExp;
52  Assert.IsInstanceOf<SqlConstantExpression>(binExp.Left);
53  Assert.IsInstanceOf<SqlConstantExpression>(binExp.Right);
54  Assert.IsTrue(binExp.CanEvaluate);
55  }
56 
57  [Test]
59  const string s = "75664 + 907 * 87";
60 
61  SqlExpression sqlExp = null;
62  Assert.DoesNotThrow(() => sqlExp = SqlExpression.Parse(s));
63  Assert.IsNotNull(sqlExp);
64  Assert.IsInstanceOf<SqlBinaryExpression>(sqlExp);
65  Assert.AreEqual(SqlExpressionType.Add, sqlExp.ExpressionType);
66 
67  var binExp = (SqlBinaryExpression) sqlExp;
68  Assert.IsInstanceOf<SqlConstantExpression>(binExp.Left);
69  Assert.IsInstanceOf<SqlBinaryExpression>(binExp.Right);
70  Assert.IsTrue(binExp.CanEvaluate);
71 
72  SqlExpression evalExp = null;
73  Assert.DoesNotThrow(() => evalExp = binExp.Evaluate());
74  Assert.IsNotNull(evalExp);
75  Assert.IsInstanceOf<SqlConstantExpression>(evalExp);
76 
77  var value = ((SqlConstantExpression) evalExp).Value;
78  Assert.IsInstanceOf<SqlNumber>(value.Value);
79  Assert.AreEqual(154573, ((SqlNumber)value.Value).ToInt32());
80  }
81  }
82 }
static SqlExpression Parse(string s)
Parses the given SQL string to an expression that can be evaluated.
SqlExpressionType
All the possible type of SqlExpression supported
DataObject Value
Gets the constant value of the expression.
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
An expression that holds a constant value.
Defines the base class for instances that represent SQL expression tree nodes.
abstract SqlExpressionType ExpressionType
Gets the type code of this SQL expression.