tangled
alpha
login
or
join now
zenfyr.dev
/
EvalEx
0
fork
atom
a fork of EvalEx by ezylang with a handful of breaking changes
0
fork
atom
overview
issues
pulls
pipelines
Apply spotless.
zenfyr.dev
2 years ago
217da492
ad3eefe9
+71
-47
1 changed file
expand all
collapse all
unified
split
src
test
java
com
ezylang
evalex
ExpressionEvaluatorDataAccessorTest.java
+71
-47
src/test/java/com/ezylang/evalex/ExpressionEvaluatorDataAccessorTest.java
reviewed
···
1
1
+
/*
2
2
+
Copyright 2012-2024 Udo Klimaschewski
3
3
+
4
4
+
Licensed under the Apache License, Version 2.0 (the "License");
5
5
+
you may not use this file except in compliance with the License.
6
6
+
You may obtain a copy of the License at
7
7
+
8
8
+
http://www.apache.org/licenses/LICENSE-2.0
9
9
+
10
10
+
Unless required by applicable law or agreed to in writing, software
11
11
+
distributed under the License is distributed on an "AS IS" BASIS,
12
12
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
+
See the License for the specific language governing permissions and
14
14
+
limitations under the License.
15
15
+
*/
1
16
package com.ezylang.evalex;
2
17
3
18
import com.ezylang.evalex.data.DataAccessorIfc;
4
19
import com.ezylang.evalex.data.EvaluationValue;
5
20
import com.ezylang.evalex.data.types.DataAccessorValue;
6
21
import com.ezylang.evalex.parser.ParseException;
7
7
-
import org.assertj.core.api.Assertions;
8
8
-
import org.junit.jupiter.api.Test;
9
9
-
10
22
import java.lang.reflect.Field;
11
23
import java.lang.reflect.Modifier;
12
24
import java.math.BigDecimal;
···
15
27
import java.util.Map;
16
28
import java.util.Set;
17
29
import java.util.function.Function;
30
30
+
import org.assertj.core.api.Assertions;
31
31
+
import org.junit.jupiter.api.Test;
18
32
19
33
public class ExpressionEvaluatorDataAccessorTest extends BaseExpressionEvaluatorTest {
20
34
21
21
-
@Test
22
22
-
void testBasicAccessorAccess() throws ParseException, EvaluationException {
23
23
-
DataAccessorValue value = DataAccessorValue.of(reflectiveAccessor(new TestClass()));
35
35
+
@Test
36
36
+
void testBasicAccessorAccess() throws ParseException, EvaluationException {
37
37
+
DataAccessorValue value = DataAccessorValue.of(reflectiveAccessor(new TestClass()));
24
38
25
25
-
Assertions.assertThat(createExpression("test_obj.testField")
39
39
+
Assertions.assertThat(
40
40
+
createExpression("test_obj.testField")
26
41
.evaluate(builder -> builder.parameter("test_obj", value)))
27
27
-
.extracting(EvaluationValue::getBooleanValue).isEqualTo(true);
42
42
+
.extracting(EvaluationValue::getBooleanValue)
43
43
+
.isEqualTo(true);
28
44
29
29
-
Assertions.assertThat(createExpression("test_obj.testIntField")
30
30
-
.evaluate(builder -> builder.parameter("test_obj", value)))
31
31
-
.extracting(EvaluationValue::getNumberValue).isEqualTo(BigDecimal.valueOf(45));
45
45
+
Assertions.assertThat(
46
46
+
createExpression("test_obj.testIntField")
47
47
+
.evaluate(builder -> builder.parameter("test_obj", value)))
48
48
+
.extracting(EvaluationValue::getNumberValue)
49
49
+
.isEqualTo(BigDecimal.valueOf(45));
32
50
33
33
-
Assertions.assertThat(createExpression("test_obj.coolString")
34
34
-
.evaluate(builder -> builder.parameter("test_obj", value)))
35
35
-
.extracting(EvaluationValue::getStringValue).isEqualTo("Hello World!");
36
36
-
37
37
-
Assertions.assertThatThrownBy(() -> createExpression("test_obj.noString")
51
51
+
Assertions.assertThat(
52
52
+
createExpression("test_obj.coolString")
38
53
.evaluate(builder -> builder.parameter("test_obj", value)))
39
39
-
.isInstanceOf(EvaluationException.class)
40
40
-
.hasMessage("Field 'noString' not found in structure");
41
41
-
}
54
54
+
.extracting(EvaluationValue::getStringValue)
55
55
+
.isEqualTo("Hello World!");
42
56
43
43
-
public static class TestClass {
44
44
-
public boolean testField = true;
45
45
-
public int testIntField = 45;
46
46
-
public final String coolString = "Hello World!";
47
47
-
}
57
57
+
Assertions.assertThatThrownBy(
58
58
+
() ->
59
59
+
createExpression("test_obj.noString")
60
60
+
.evaluate(builder -> builder.parameter("test_obj", value)))
61
61
+
.isInstanceOf(EvaluationException.class)
62
62
+
.hasMessage("Field 'noString' not found in structure");
63
63
+
}
48
64
49
49
-
public static DataAccessorIfc reflectiveAccessor(Object object) {
50
50
-
Map<String, Function<Object, Object>> fields = new HashMap<>();
51
51
-
Set<String> invalid = new HashSet<>();
52
52
-
return (variable, token, context) -> {
53
53
-
if (fields.containsKey(variable)) return context.expression().convertValue(fields.get(variable).apply(object));
54
54
-
if (invalid.contains(variable)) return null;
65
65
+
public static class TestClass {
66
66
+
public boolean testField = true;
67
67
+
public int testIntField = 45;
68
68
+
public final String coolString = "Hello World!";
69
69
+
}
55
70
56
56
-
for (Field field : object.getClass().getFields()) {
57
57
-
if (!field.getName().equals(variable)) continue;
58
58
-
if (Modifier.isStatic(field.getModifiers())) continue;
59
59
-
Function<Object, Object> function = object1 -> {
60
60
-
try {
61
61
-
return field.get(object1);
62
62
-
} catch (IllegalAccessException e) {
63
63
-
throw new RuntimeException(e);
64
64
-
}
65
65
-
};
66
66
-
fields.put(variable, function);
67
67
-
return context.expression().convertValue(function.apply(object));
68
68
-
}
69
69
-
invalid.add(variable);
70
70
-
return null;
71
71
-
};
72
72
-
}
71
71
+
public static DataAccessorIfc reflectiveAccessor(Object object) {
72
72
+
Map<String, Function<Object, Object>> fields = new HashMap<>();
73
73
+
Set<String> invalid = new HashSet<>();
74
74
+
return (variable, token, context) -> {
75
75
+
if (fields.containsKey(variable))
76
76
+
return context.expression().convertValue(fields.get(variable).apply(object));
77
77
+
if (invalid.contains(variable)) return null;
78
78
+
79
79
+
for (Field field : object.getClass().getFields()) {
80
80
+
if (!field.getName().equals(variable)) continue;
81
81
+
if (Modifier.isStatic(field.getModifiers())) continue;
82
82
+
Function<Object, Object> function =
83
83
+
object1 -> {
84
84
+
try {
85
85
+
return field.get(object1);
86
86
+
} catch (IllegalAccessException e) {
87
87
+
throw new RuntimeException(e);
88
88
+
}
89
89
+
};
90
90
+
fields.put(variable, function);
91
91
+
return context.expression().convertValue(function.apply(object));
92
92
+
}
93
93
+
invalid.add(variable);
94
94
+
return null;
95
95
+
};
96
96
+
}
73
97
}