|
| 1 | +package utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +import dtos.diagnostics.LJDiagnosticDTO; |
| 14 | +import dtos.diagnostics.SourcePositionDTO; |
| 15 | +import dtos.errors.*; |
| 16 | +import dtos.warnings.*; |
| 17 | +import liquidjava.diagnostics.TranslationTable; |
| 18 | +import liquidjava.diagnostics.errors.*; |
| 19 | +import liquidjava.diagnostics.warnings.*; |
| 20 | +import liquidjava.processor.VCImplication; |
| 21 | +import liquidjava.processor.context.PlacementInCode; |
| 22 | +import liquidjava.rj_language.Predicate; |
| 23 | +import liquidjava.rj_language.ast.LiteralBoolean; |
| 24 | +import liquidjava.rj_language.opt.VCSimplificationResult; |
| 25 | +import spoon.Launcher; |
| 26 | +import spoon.reflect.cu.SourcePosition; |
| 27 | +import spoon.reflect.declaration.CtField; |
| 28 | + |
| 29 | +class DiagnosticConverterTest { |
| 30 | + @TempDir |
| 31 | + Path workspace; |
| 32 | + |
| 33 | + private CtField<?> field; |
| 34 | + private SourcePosition position; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void createSourcePosition() throws Exception { |
| 38 | + Path file = workspace.resolve("Example.java"); |
| 39 | + Files.writeString(file, "class Example {\n int value = 0;\n}\n"); |
| 40 | + Launcher launcher = new Launcher(); |
| 41 | + launcher.getEnvironment().setNoClasspath(true); |
| 42 | + launcher.addInputResource(file.toString()); |
| 43 | + launcher.buildModel(); |
| 44 | + field = launcher.getFactory().Class().get("Example").getField("value"); |
| 45 | + position = field.getPosition(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void preservesCommonDiagnosticFields() throws Exception { |
| 50 | + CustomError error = new CustomError("verification failed", position); |
| 51 | + error.setHint("check the refinement"); |
| 52 | + LJDiagnosticDTO dto = (LJDiagnosticDTO) DiagnosticConverter.convertToDTO(error); |
| 53 | + assertEquals("error", dto.category); |
| 54 | + assertEquals("custom-error", dto.type); |
| 55 | + assertEquals("Error", dto.title); |
| 56 | + assertEquals("verification failed", dto.message); |
| 57 | + assertEquals("check the refinement", dto.hint); |
| 58 | + assertEquals(workspace.resolve("Example.java").toRealPath().toString(), dto.file); |
| 59 | + assertEquals(new SourcePositionDTO(dto.file, 1, 8, 1, 18), dto.position); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void convertsIllegalConstructorTransitionToAnError() { |
| 64 | + LJDiagnosticDTO dto = (LJDiagnosticDTO) DiagnosticConverter.convertToDTO( |
| 65 | + new IllegalConstructorTransitionError(position)); |
| 66 | + assertEquals("error", dto.category); |
| 67 | + assertEquals("illegal-constructor-transition-error", dto.type); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void convertsCustomWarningWithoutTreatingItAsAnError() { |
| 72 | + LJDiagnosticDTO dto = (LJDiagnosticDTO) DiagnosticConverter.convertToDTO(new CustomWarning("custom warning")); |
| 73 | + assertEquals("warning", dto.category); |
| 74 | + assertEquals("custom-warning", dto.type); |
| 75 | + assertEquals("custom warning", dto.message); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void preservesErrorSpecificDetails() { |
| 80 | + SyntaxErrorDTO syntax = (SyntaxErrorDTO) DiagnosticConverter.convertToDTO(new SyntaxError("invalid syntax", "_ >")); |
| 81 | + assertEquals("error", syntax.category); |
| 82 | + assertEquals("syntax-error", syntax.type); |
| 83 | + assertEquals("_ >", syntax.refinement); |
| 84 | + assertNull(syntax.file); |
| 85 | + assertNull(syntax.position); |
| 86 | + assertTrue(syntax.translationTable.isEmpty()); |
| 87 | + |
| 88 | + InvalidRefinementErrorDTO invalid = (InvalidRefinementErrorDTO) DiagnosticConverter.convertToDTO( |
| 89 | + new InvalidRefinementError(position, "not boolean", "42")); |
| 90 | + assertEquals("error", invalid.category); |
| 91 | + assertEquals("invalid-refinement-error", invalid.type); |
| 92 | + assertEquals("42", invalid.refinement); |
| 93 | + |
| 94 | + NotFoundErrorDTO missing = (NotFoundErrorDTO) DiagnosticConverter.convertToDTO( |
| 95 | + new NotFoundError(position, "missing", NotFoundError.Kind.GHOST, List.of())); |
| 96 | + assertEquals("error", missing.category); |
| 97 | + assertEquals("not-found-error", missing.type); |
| 98 | + assertEquals("missing", missing.name); |
| 99 | + assertEquals("Ghost", missing.kind); |
| 100 | + |
| 101 | + StateConflictErrorDTO conflict = (StateConflictErrorDTO) DiagnosticConverter.convertToDTO( |
| 102 | + new StateConflictError(position, new LiteralBoolean(false), null)); |
| 103 | + assertEquals("error", conflict.category); |
| 104 | + assertEquals("state-conflict-error", conflict.type); |
| 105 | + assertEquals("false", conflict.state); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void preservesWarningSpecificDetailsAndOverloadHint() { |
| 110 | + ExternalClassNotFoundWarningDTO missingClass = (ExternalClassNotFoundWarningDTO) DiagnosticConverter.convertToDTO( |
| 111 | + new ExternalClassNotFoundWarning(position, "missing class", "example.External")); |
| 112 | + assertEquals("warning", missingClass.category); |
| 113 | + assertEquals("external-class-not-found-warning", missingClass.type); |
| 114 | + assertEquals("example.External", missingClass.className); |
| 115 | + |
| 116 | + ExternalMethodNotFoundWarningDTO missingMethod = (ExternalMethodNotFoundWarningDTO) DiagnosticConverter.convertToDTO( |
| 117 | + new ExternalMethodNotFoundWarning(position, "missing method", "run()", "example.External", |
| 118 | + new String[] { "run(int)", "run(String)" })); |
| 119 | + assertEquals("warning", missingMethod.category); |
| 120 | + assertEquals("external-method-not-found-warning", missingMethod.type); |
| 121 | + assertEquals("run()", missingMethod.signature); |
| 122 | + assertEquals("example.External", missingMethod.className); |
| 123 | + assertArrayEquals(new String[] { "run(int)", "run(String)" }, missingMethod.overloads); |
| 124 | + assertEquals("Available overloads:\n run(int)\n run(String)", missingMethod.hint); |
| 125 | + |
| 126 | + UnsatisfiableRefinementWarningDTO unsatisfiable = (UnsatisfiableRefinementWarningDTO) DiagnosticConverter.convertToDTO( |
| 127 | + new UnsatisfiableRefinementWarning(position, "_ > 0 && _ < 0")); |
| 128 | + assertEquals("warning", unsatisfiable.category); |
| 129 | + assertEquals("unsatisfiable-refinement-warning", unsatisfiable.type); |
| 130 | + assertEquals("_ > 0 && _ < 0", unsatisfiable.refinement); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void preservesRefinementDetailsAndSimplificationHistory() { |
| 135 | + VCSimplificationResult origin = new VCSimplificationResult(new VCImplication(new Predicate())); |
| 136 | + VCSimplificationResult found = new VCSimplificationResult( |
| 137 | + new VCImplication(new Predicate(new LiteralBoolean(false))), origin, "constant folding"); |
| 138 | + RefinementErrorDTO dto = (RefinementErrorDTO) DiagnosticConverter.convertToDTO( |
| 139 | + new RefinementError(position, position, new Predicate(), found, null, null, "expected true")); |
| 140 | + assertEquals("error", dto.category); |
| 141 | + assertEquals("refinement-error", dto.type); |
| 142 | + assertEquals("true", dto.expected); |
| 143 | + assertEquals("expected true", dto.customMessage); |
| 144 | + assertEquals(dto.position, dto.declarationPosition); |
| 145 | + assertEquals("false", dto.found.implication().predicate()); |
| 146 | + assertEquals("constant folding", dto.found.simplification()); |
| 147 | + assertEquals("true", dto.found.origin().implication().predicate()); |
| 148 | + assertNull(dto.found.origin().origin()); |
| 149 | + assertNull(dto.found.origin().simplification()); |
| 150 | + assertTrue(dto.counterexample.assignments().isEmpty()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void preservesStateRefinementDetailsWithoutDeclarationFile() { |
| 155 | + StateRefinementErrorDTO dto = (StateRefinementErrorDTO) DiagnosticConverter.convertToDTO( |
| 156 | + new StateRefinementError(position, null, new Predicate(new LiteralBoolean(false)), |
| 157 | + new VCSimplificationResult(new VCImplication(new Predicate())), null, "expected closed")); |
| 158 | + assertEquals("error", dto.category); |
| 159 | + assertEquals("state-refinement-error", dto.type); |
| 160 | + assertEquals("false", dto.expected); |
| 161 | + assertEquals("true", dto.found.implication().predicate()); |
| 162 | + assertEquals("expected closed", dto.customMessage); |
| 163 | + assertNull(dto.declarationPosition); |
| 164 | + assertNull(dto.stateMachine); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void convertsTranslationTablePlacementsAndDisplayNames() { |
| 169 | + TranslationTable table = new TranslationTable(); |
| 170 | + table.put("#value_12", PlacementInCode.createPlacement(field)); |
| 171 | + ArgumentMismatchErrorDTO dto = (ArgumentMismatchErrorDTO) DiagnosticConverter.convertToDTO( |
| 172 | + new ArgumentMismatchError("wrong arguments", position, table)); |
| 173 | + assertEquals("error", dto.category); |
| 174 | + assertEquals("argument-mismatch-error", dto.type); |
| 175 | + assertEquals(1, dto.translationTable.size()); |
| 176 | + assertFalse(dto.translationTable.containsKey("#value_12")); |
| 177 | + assertEquals("int value = 0;", dto.translationTable.get("value¹²").text()); |
| 178 | + assertEquals(dto.position, dto.translationTable.get("value¹²").position()); |
| 179 | + } |
| 180 | +} |
0 commit comments