Provide example for SpEL's exponential power operator (^)
This commit is contained in:
parent
e34ad6bf5f
commit
24d6565cad
|
|
@ -290,30 +290,33 @@ Java::
|
|||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
----
|
||||
// Addition
|
||||
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
|
||||
int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
|
||||
|
||||
// Subtraction
|
||||
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
|
||||
int four = parser.parseExpression("1 - -3").getValue(int.class); // 4
|
||||
|
||||
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
|
||||
double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
|
||||
|
||||
// Multiplication
|
||||
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
|
||||
int six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
|
||||
|
||||
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
|
||||
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0
|
||||
|
||||
// Division
|
||||
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
|
||||
int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2
|
||||
|
||||
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
|
||||
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0
|
||||
|
||||
// Modulus
|
||||
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
|
||||
int three = parser.parseExpression("7 % 4").getValue(int.class); // 3
|
||||
|
||||
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
|
||||
int one = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1
|
||||
|
||||
// Exponential power
|
||||
int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE
|
||||
|
||||
// Operator precedence
|
||||
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
|
||||
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
|
||||
----
|
||||
|
||||
Kotlin::
|
||||
|
|
@ -343,7 +346,12 @@ Kotlin::
|
|||
|
||||
val one = parser.parseExpression("8 / 5 % 2").getValue(Int::class.java) // 1
|
||||
|
||||
// Operator precedence
|
||||
// -- Exponential power --
|
||||
|
||||
var maxInt = parser.parseExpression("(2^31) - 1").getValue(Int::class.java) // Integer.MAX_VALUE
|
||||
|
||||
// -- Operator precedence --
|
||||
|
||||
val minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Int::class.java) // -21
|
||||
----
|
||||
======
|
||||
|
|
|
|||
|
|
@ -392,6 +392,15 @@ class ParsingTests {
|
|||
parseCheck("7--", "7--");
|
||||
parseCheck("foo--", "foo--");
|
||||
}
|
||||
|
||||
@Test
|
||||
void mathOperatorPower() {
|
||||
parseCheck("3^2", "(3 ^ 2)");
|
||||
parseCheck("3.0d^2.0d", "(3.0 ^ 2.0)");
|
||||
parseCheck("3L^2L", "(3 ^ 2)");
|
||||
parseCheck("(2^32)^2", "((2 ^ 32) ^ 2)");
|
||||
parseCheck("new java.math.BigDecimal('5') ^ 3", "(new java.math.BigDecimal('5') ^ 3)");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
|
|
|||
|
|
@ -73,17 +73,17 @@ class SpelDocumentationTests extends AbstractExpressionTests {
|
|||
|
||||
@Test
|
||||
void methodInvocation() {
|
||||
evaluate("'Hello World'.concat('!')","Hello World!",String.class);
|
||||
evaluate("'Hello World'.concat('!')", "Hello World!", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void beanPropertyAccess() {
|
||||
evaluate("new String('Hello World'.bytes)","Hello World",String.class);
|
||||
evaluate("new String('Hello World'.bytes)", "Hello World", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrayLengthAccess() {
|
||||
evaluate("'Hello World'.bytes.length",11,Integer.class);
|
||||
evaluate("'Hello World'.bytes.length", 11, Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -307,39 +307,44 @@ class SpelDocumentationTests extends AbstractExpressionTests {
|
|||
@Test
|
||||
void mathematicalOperators() {
|
||||
// Addition
|
||||
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
|
||||
int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
|
||||
assertThat(two).isEqualTo(2);
|
||||
|
||||
// Subtraction
|
||||
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
|
||||
int four = parser.parseExpression("1 - -3").getValue(int.class); // 4
|
||||
assertThat(four).isEqualTo(4);
|
||||
|
||||
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
|
||||
double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
|
||||
assertThat(d).isCloseTo(-9000.0d, within((double) 0));
|
||||
|
||||
// Multiplication
|
||||
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
|
||||
int six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
|
||||
assertThat(six).isEqualTo(6);
|
||||
|
||||
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
|
||||
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0
|
||||
assertThat(twentyFour).isCloseTo(24.0d, within((double) 0));
|
||||
|
||||
// Division
|
||||
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
|
||||
int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2
|
||||
assertThat(minusTwo).isEqualTo(-2);
|
||||
|
||||
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
|
||||
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0
|
||||
assertThat(one).isCloseTo(1.0d, within((double) 0));
|
||||
|
||||
// Modulus
|
||||
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
|
||||
int three = parser.parseExpression("7 % 4").getValue(int.class); // 3
|
||||
assertThat(three).isEqualTo(3);
|
||||
|
||||
int oneInt = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
|
||||
int oneInt = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1
|
||||
assertThat(oneInt).isEqualTo(1);
|
||||
|
||||
// Exponential power
|
||||
int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE
|
||||
assertThat(maxInt).isEqualTo(Integer.MAX_VALUE);
|
||||
|
||||
// Operator precedence
|
||||
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
|
||||
|
||||
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
|
||||
assertThat(minusTwentyOne).isEqualTo(-21);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue