-
Notifications
You must be signed in to change notification settings - Fork 46
/
InterpreterNumberExpressionTest.scala
238 lines (165 loc) · 6.35 KB
/
InterpreterNumberExpressionTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.feel.impl.interpreter
import org.camunda.feel.impl.FeelIntegrationTest
import org.camunda.feel.syntaxtree._
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
/** @author
* Philipp Ossler
*/
class InterpreterNumberExpressionTest extends AnyFlatSpec with Matchers with FeelIntegrationTest {
"A number" should "add to '4'" in {
eval("2+4") should be(ValNumber(6))
}
it should "add to '4' and '6'" in {
eval("2+4+6") should be(ValNumber(12))
}
it should "add to not-a-number" in {
eval("2 + true") should be(ValNull)
eval("false + 3") should be(ValNull)
}
it should "subtract from '2'" in {
eval("4-2") should be(ValNumber(2))
}
it should "subtract to not-a-number" in {
eval("2 - true") should be(ValNull)
eval("false - 3") should be(ValNull)
}
it should "add and subtract" in {
eval("2+4-3+1") should be(ValNumber(4))
}
it should "multiply by '3'" in {
eval("3*3") should be(ValNumber(9))
}
it should "multiply to not-a-number" in {
eval("2 * true") should be(ValNull)
eval("false * 3") should be(ValNull)
}
it should "divide by '4'" in {
eval("8/4") should be(ValNumber(2))
}
it should "be null if divide by zero" in {
eval("2 / 0") should be(ValNull)
}
it should "divide to not-a-number" in {
eval("2 / true") should be(ValNull)
eval("false / 3") should be(ValNull)
}
it should "multiply and divide" in {
eval("3*4/2*5") should be(ValNumber(30))
}
it should "exponentiate by '3'" in {
eval("2**3") should be(ValNumber(8))
}
it should "exponentiate twice" in {
// all operators are left associative
eval("2**2**3") should be(ValNumber(64))
}
it should "exponentiate by '3.1'" in {
eval("2**3.1") should be(ValNumber(8.574187700290345))
}
it should "negate" in {
eval("-2") should be(ValNumber(-2))
}
it should "negate and multiply" in {
eval("2 * -3") should be(ValNumber(-6))
}
it should "add and multiply" in {
eval("2 + 3 * 4") should be(ValNumber(14))
eval("2 * 3 + 4") should be(ValNumber(10))
}
it should "multiply and exponentiate" in {
eval("2**3 * 4") should be(ValNumber(32))
eval("3 * 4**2") should be(ValNumber(48))
}
it should "compare with '='" in {
eval("x=2", Map("x" -> 2)) should be(ValBoolean(true))
eval("x=2", Map("x" -> 3)) should be(ValBoolean(false))
eval("(x * 2) = 4", Map("x" -> 2)) should be(ValBoolean(true))
eval("(x * 2) = 4", Map("x" -> 3)) should be(ValBoolean(false))
eval("x = -1", Map("x" -> -1)) should be(ValBoolean(true))
eval("x = -1", Map("x" -> 1)) should be(ValBoolean(false))
}
it should "compare with '!='" in {
eval("x!=2", Map("x" -> 2)) should be(ValBoolean(false))
eval("x!=2", Map("x" -> 3)) should be(ValBoolean(true))
}
it should "compare with '<'" in {
eval("x<2", Map("x" -> 1)) should be(ValBoolean(true))
eval("x<2", Map("x" -> 2)) should be(ValBoolean(false))
}
it should "compare with '<='" in {
eval("x<=2", Map("x" -> 2)) should be(ValBoolean(true))
eval("x<=2", Map("x" -> 3)) should be(ValBoolean(false))
}
it should "compare with '>'" in {
eval("x>2", Map("x" -> 2)) should be(ValBoolean(false))
eval("x>2", Map("x" -> 3)) should be(ValBoolean(true))
}
it should "compare with '>='" in {
eval("x>=2", Map("x" -> 2)) should be(ValBoolean(true))
eval("x>=2", Map("x" -> 1)) should be(ValBoolean(false))
}
it should "compare with null" in {
eval("2 = null") should be(ValBoolean(false))
eval("null = 2") should be(ValBoolean(false))
eval("null != 2") should be(ValBoolean(true))
eval("2 > null") should be(ValNull)
eval("null < 2") should be(ValNull)
}
it should "compare null with 'in' operator" in {
eval("null in < 2") should be(ValNull)
eval("null in (2..4)") should be(ValNull)
}
it should "compare with 'between _ and _'" in {
eval("x between 2 and 4", Map("x" -> 1)) should be(ValBoolean(false))
eval("x between 2 and 4", Map("x" -> 2)) should be(ValBoolean(true))
eval("x between 2 and 4", Map("x" -> 3)) should be(ValBoolean(true))
eval("x between 2 and 4", Map("x" -> 4)) should be(ValBoolean(true))
eval("x between 2 and 4", Map("x" -> 5)) should be(ValBoolean(false))
}
it should "compare with 'in'" in {
eval("x in < 2", Map("x" -> 1)) should be(ValBoolean(true))
eval("x in < 2", Map("x" -> 2)) should be(ValBoolean(false))
eval("x in (2,4,6)", Map("x" -> 4)) should be(ValBoolean(true))
eval("x in (2,4,6)", Map("x" -> 5)) should be(ValBoolean(false))
}
it should "compare with 'in' with interval (..)" in {
eval("x in (2 .. 4)", Map("x" -> 3)) should be(ValBoolean(true))
eval("x in (2 .. 4)", Map("x" -> 4)) should be(ValBoolean(false))
eval("3 in (2..4)") should be(ValBoolean(true))
eval("4 in (2..4)") should be(ValBoolean(false))
}
it should "compare with 'in' with interval [..]" in {
eval("3 in [2 .. 4]") should be(ValBoolean(true))
eval("4 in [2 .. 4]") should be(ValBoolean(true))
}
it should "compare with 'in' (multiple positive tests)" in {
eval("5 in (< 3, >= 5)") should be(ValBoolean(true))
}
it should "compare multiplication with 'in'" in {
eval("2 * 3 in > 3") should be(ValBoolean(true))
}
it should "be null if nAn" in {
eval("x", Map("x" -> Double.NaN)) should be(ValNull)
}
it should "be null if infinity" in {
eval("x", Map("x" -> Double.PositiveInfinity)) should be(ValNull)
eval("x", Map("x" -> Double.NegativeInfinity)) should be(ValNull)
}
}