|
| 1 | +package liquidjava.processor.context; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import liquidjava.rj_language.Predicate; |
| 10 | +import liquidjava.rj_language.ast.Expression; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import spoon.Launcher; |
| 15 | +import spoon.reflect.factory.Factory; |
| 16 | +import spoon.reflect.reference.CtTypeReference; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test suite for the AliasWrapper class |
| 20 | + */ |
| 21 | +class AliasWrapperTest { |
| 22 | + |
| 23 | + private Factory factory; |
| 24 | + private Context context; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void setUp() { |
| 28 | + Launcher launcher = new Launcher(); |
| 29 | + factory = launcher.getFactory(); |
| 30 | + context = Context.getInstance(); |
| 31 | + context.reinitializeAllContext(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void testConstructorWithBasicParameters() { |
| 36 | + Predicate pred = Predicate.createVar("x"); |
| 37 | + List<String> varNames = List.of("arg1"); |
| 38 | + List<String> varTypes = List.of("int"); |
| 39 | + |
| 40 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 41 | + |
| 42 | + assertNotNull(alias, "AliasWrapper should not be null"); |
| 43 | + assertEquals("myAlias", alias.getName(), "Name should be 'myAlias'"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testGetName() { |
| 48 | + Predicate pred = new Predicate(); |
| 49 | + List<String> varNames = List.of(); |
| 50 | + List<String> varTypes = List.of(); |
| 51 | + |
| 52 | + AliasWrapper alias = new AliasWrapper("testAlias", pred, varNames, varTypes); |
| 53 | + assertEquals("testAlias", alias.getName(), "Name should be 'testAlias'"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testGetVarNames() { |
| 58 | + Predicate pred = new Predicate(); |
| 59 | + List<String> varNames = List.of("x", "y", "z"); |
| 60 | + List<String> varTypes = List.of("int", "int", "int"); |
| 61 | + |
| 62 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 63 | + List<String> retrievedNames = alias.getVarNames(); |
| 64 | + |
| 65 | + assertEquals(3, retrievedNames.size(), "Should have 3 variable names"); |
| 66 | + assertEquals("x", retrievedNames.get(0), "First name should be 'x'"); |
| 67 | + assertEquals("y", retrievedNames.get(1), "Second name should be 'y'"); |
| 68 | + assertEquals("z", retrievedNames.get(2), "Third name should be 'z'"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testGetTypes() { |
| 73 | + Predicate pred = new Predicate(); |
| 74 | + List<String> varNames = List.of("x"); |
| 75 | + List<String> varTypes = List.of("int"); |
| 76 | + |
| 77 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 78 | + List<CtTypeReference<?>> types = alias.getTypes(); |
| 79 | + |
| 80 | + assertNotNull(types, "Types should not be null"); |
| 81 | + assertEquals(1, types.size(), "Should have 1 type"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testGetClonedPredicate() { |
| 86 | + Predicate pred = Predicate.createVar("x"); |
| 87 | + List<String> varNames = List.of(); |
| 88 | + List<String> varTypes = List.of(); |
| 89 | + |
| 90 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 91 | + Predicate cloned = alias.getClonedPredicate(); |
| 92 | + |
| 93 | + assertNotNull(cloned, "Cloned predicate should not be null"); |
| 94 | + assertEquals(pred.toString(), cloned.toString(), "Cloned predicate should match original"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testGetNewExpression() { |
| 99 | + Predicate pred = Predicate.createVar("x"); |
| 100 | + List<String> varNames = List.of("x"); |
| 101 | + List<String> varTypes = List.of("int"); |
| 102 | + |
| 103 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 104 | + List<String> newNames = List.of("y"); |
| 105 | + Expression newExpr = alias.getNewExpression(newNames); |
| 106 | + |
| 107 | + assertNotNull(newExpr, "New expression should not be null"); |
| 108 | + assertEquals("y", newExpr.toString(), "Expression should have substituted variable"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testGetNewVariables() { |
| 113 | + Predicate pred = new Predicate(); |
| 114 | + List<String> varNames = List.of("x", "y"); |
| 115 | + List<String> varTypes = List.of("int", "int"); |
| 116 | + |
| 117 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 118 | + List<String> newVars = alias.getNewVariables(context); |
| 119 | + |
| 120 | + assertEquals(2, newVars.size(), "Should have 2 new variables"); |
| 121 | + assertTrue(newVars.get(0).contains("x"), "First variable should contain 'x'"); |
| 122 | + assertTrue(newVars.get(1).contains("y"), "Second variable should contain 'y'"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testGetTypesWithNames() { |
| 127 | + Predicate pred = new Predicate(); |
| 128 | + List<String> varNames = List.of("x", "y"); |
| 129 | + List<String> varTypes = List.of("int", "String"); |
| 130 | + |
| 131 | + AliasWrapper alias = new AliasWrapper("myAlias", pred, varNames, varTypes); |
| 132 | + List<String> newNames = List.of("a", "b"); |
| 133 | + Map<String, CtTypeReference<?>> typesMap = alias.getTypes(newNames); |
| 134 | + |
| 135 | + assertEquals(2, typesMap.size(), "Map should have 2 entries"); |
| 136 | + assertTrue(typesMap.containsKey("a"), "Map should contain 'a'"); |
| 137 | + assertTrue(typesMap.containsKey("b"), "Map should contain 'b'"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testWithNoVariables() { |
| 142 | + Predicate pred = Predicate.createLit("true", "boolean"); |
| 143 | + List<String> varNames = List.of(); |
| 144 | + List<String> varTypes = List.of(); |
| 145 | + |
| 146 | + AliasWrapper alias = new AliasWrapper("constantAlias", pred, varNames, varTypes); |
| 147 | + |
| 148 | + assertEquals(0, alias.getVarNames().size(), "Should have no variables"); |
| 149 | + assertEquals(0, alias.getTypes().size(), "Should have no types"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void testWithMultipleVariables() { |
| 154 | + Predicate pred = Predicate.createVar("x"); |
| 155 | + List<String> varNames = List.of("x", "y", "z"); |
| 156 | + List<String> varTypes = List.of("int", "double", "boolean"); |
| 157 | + |
| 158 | + AliasWrapper alias = new AliasWrapper("multiVarAlias", pred, varNames, varTypes); |
| 159 | + |
| 160 | + assertEquals(3, alias.getVarNames().size(), "Should have 3 variables"); |
| 161 | + assertEquals(3, alias.getTypes().size(), "Should have 3 types"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testSubstitution() { |
| 166 | + // Create predicate: x + y |
| 167 | + Predicate x = Predicate.createVar("x"); |
| 168 | + Predicate y = Predicate.createVar("y"); |
| 169 | + Predicate pred = Predicate.createOperation(x, "+", y); |
| 170 | + |
| 171 | + List<String> varNames = List.of("x", "y"); |
| 172 | + List<String> varTypes = List.of("int", "int"); |
| 173 | + |
| 174 | + AliasWrapper alias = new AliasWrapper("addAlias", pred, varNames, varTypes); |
| 175 | + List<String> newNames = List.of("a", "b"); |
| 176 | + Expression newExpr = alias.getNewExpression(newNames); |
| 177 | + |
| 178 | + String exprStr = newExpr.toString(); |
| 179 | + assertTrue(exprStr.contains("a"), "Expression should contain 'a'"); |
| 180 | + assertTrue(exprStr.contains("b"), "Expression should contain 'b'"); |
| 181 | + assertFalse(exprStr.contains("x"), "Expression should not contain 'x'"); |
| 182 | + assertFalse(exprStr.contains("y"), "Expression should not contain 'y'"); |
| 183 | + } |
| 184 | +} |
0 commit comments