| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.felix.gogo.runtime.expr; |
| 20 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
import org.apache.felix.gogo.runtime.Expression; |
| 23 |
|
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.Map; |
| 26 |
|
| 27 |
public class ExpressionTest extends TestCase { |
| 28 |
|
| 29 |
public void testExpr() { |
| 30 |
|
| 31 |
Map<String, Object> variables = new HashMap<String, Object>(); |
| 32 |
variables.put("a", 2d); |
| 33 |
variables.put("b", 5l); |
| 34 |
variables.put("c", 1l); |
| 35 |
variables.put("d", 2l); |
| 36 |
variables.put("s", " foo "); |
| 37 |
variables.put("t", "bar"); |
| 38 |
|
| 39 |
assertEquals(4l, new Expression("c+=1, d+=2").eval(variables)); |
| 40 |
|
| 41 |
assertEquals(" foo ", new Expression("\" foo \"").eval()); |
| 42 |
assertEquals(" foo bar", new Expression("s + t").eval(variables)); |
| 43 |
assertEquals(1l, new Expression("s < t").eval(variables)); |
| 44 |
assertEquals(1l, new Expression("s > t || t == \"bar\"").eval(variables)); |
| 45 |
|
| 46 |
assertEquals(3l, new Expression("a += 1").eval(variables)); |
| 47 |
assertEquals(3l, variables.get("a")); |
| 48 |
|
| 49 |
assertEquals(30l, new Expression("10 + 20 | 30").eval()); |
| 50 |
|
| 51 |
assertEquals(8l, new Expression("a + b").eval(variables)); |
| 52 |
assertEquals(3l, new Expression("if(a < b, a, b)").eval(variables)); |
| 53 |
|
| 54 |
assertEquals(16l, new Expression("2 + 2 << 2").eval()); |
| 55 |
assertEquals(8l, new Expression("2 | 2 << 2").eval()); |
| 56 |
} |
| 57 |
|
| 58 |
} |