forked from liquid-java/liquidjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableResolverTest.java
More file actions
145 lines (120 loc) · 6.01 KB
/
VariableResolverTest.java
File metadata and controls
145 lines (120 loc) · 6.01 KB
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
package liquidjava.rj_language.opt;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Map;
import org.junit.jupiter.api.Test;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.GroupExpression;
import liquidjava.rj_language.ast.LiteralInt;
import liquidjava.rj_language.ast.UnaryExpression;
import liquidjava.rj_language.ast.Var;
class VariableResolverTest {
@Test
void testSingleEqualityNotExtracted() {
// x == 1 should not extract because it's a single equality
Expression varX = new Var("x");
Expression one = new LiteralInt(1);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Map<String, Expression> result = VariableResolver.resolve(xEquals1);
assertTrue(result.isEmpty(), "Single equality should not extract variable mapping");
}
@Test
void testConjunctionExtractsVariables() {
// x + y && x == 1 && y == 2 should extract x -> 1, y -> 2
Expression varX = new Var("x");
Expression varY = new Var("y");
Expression one = new LiteralInt(1);
Expression two = new LiteralInt(2);
Expression xPlusY = new BinaryExpression(varX, "+", varY);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Expression yEquals2 = new BinaryExpression(varY, "==", two);
Expression conditions = new BinaryExpression(xEquals1, "&&", yEquals2);
Expression fullExpr = new BinaryExpression(xPlusY, "&&", conditions);
Map<String, Expression> result = VariableResolver.resolve(fullExpr);
assertEquals(2, result.size(), "Should extract both variables");
assertEquals("1", result.get("x").toString());
assertEquals("2", result.get("y").toString());
}
@Test
void testSingleComparisonNotExtracted() {
// x > 0 should not extract anything
Expression varX = new Var("x");
Expression zero = new LiteralInt(0);
Expression xGreaterZero = new BinaryExpression(varX, ">", zero);
Map<String, Expression> result = VariableResolver.resolve(xGreaterZero);
assertTrue(result.isEmpty(), "Single comparison should not extract variable mapping");
}
@Test
void testSingleArithmeticExpression() {
// x + 1 should not extract anything
Expression varX = new Var("x");
Expression one = new LiteralInt(1);
Expression xPlusOne = new BinaryExpression(varX, "+", one);
Map<String, Expression> result = VariableResolver.resolve(xPlusOne);
assertTrue(result.isEmpty(), "Single arithmetic expression should not extract variable mapping");
}
@Test
void testDisjunctionWithEqualities() {
// x == 1 || y == 2 should not extract anything
Expression varX = new Var("x");
Expression varY = new Var("y");
Expression one = new LiteralInt(1);
Expression two = new LiteralInt(2);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Expression yEquals2 = new BinaryExpression(varY, "==", two);
Expression disjunction = new BinaryExpression(xEquals1, "||", yEquals2);
Map<String, Expression> result = VariableResolver.resolve(disjunction);
assertTrue(result.isEmpty(), "Disjunction should not extract variable mappings");
}
@Test
void testNegatedEquality() {
// !(x == 1) should not extract because it's a single equality
Expression varX = new Var("x");
Expression one = new LiteralInt(1);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Expression notXEquals1 = new UnaryExpression("!", xEquals1);
Map<String, Expression> result = VariableResolver.resolve(notXEquals1);
assertTrue(result.isEmpty(), "Negated equality should not extract variable mapping");
}
@Test
void testGroupedEquality() {
// (x == 1) should not extract because it's a single equality
Expression varX = new Var("x");
Expression one = new LiteralInt(1);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Expression grouped = new GroupExpression(xEquals1);
Map<String, Expression> result = VariableResolver.resolve(grouped);
assertTrue(result.isEmpty(), "Grouped single equality should not extract variable mapping");
}
@Test
void testCircularDependency() {
// x == y && y == x should not extract anything due to circular dependency
Expression varX = new Var("x");
Expression varY = new Var("y");
Expression xEqualsY = new BinaryExpression(varX, "==", varY);
Expression yEqualsX = new BinaryExpression(varY, "==", varX);
Expression conjunction = new BinaryExpression(xEqualsY, "&&", yEqualsX);
Map<String, Expression> result = VariableResolver.resolve(conjunction);
assertTrue(result.isEmpty(), "Circular dependency should not extract variable mappings");
}
@Test
void testUnusedEqualitiesShouldBeIgnored() {
// z > 0 && x == 1 && y == 2 && z == 3
Expression varX = new Var("x");
Expression varY = new Var("y");
Expression varZ = new Var("z");
Expression one = new LiteralInt(1);
Expression two = new LiteralInt(2);
Expression three = new LiteralInt(3);
Expression zero = new LiteralInt(0);
Expression zGreaterZero = new BinaryExpression(varZ, ">", zero);
Expression xEquals1 = new BinaryExpression(varX, "==", one);
Expression yEquals2 = new BinaryExpression(varY, "==", two);
Expression zEquals3 = new BinaryExpression(varZ, "==", three);
Expression conditions = new BinaryExpression(xEquals1, "&&", new BinaryExpression(yEquals2, "&&", zEquals3));
Expression fullExpr = new BinaryExpression(zGreaterZero, "&&", conditions);
Map<String, Expression> result = VariableResolver.resolve(fullExpr);
assertEquals(1, result.size(), "Should only extract used variable z");
assertEquals("3", result.get("z").toString());
}
}