Skip to content

Commit

Permalink
SONAR-23160 Improve exception message in DefaultSymbol.newReference (…
Browse files Browse the repository at this point in the history
…#10823)
  • Loading branch information
martin-strecker-sonarsource authored and sonartech committed Sep 26, 2024
1 parent 845f2ee commit 1ecf4a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public NewSymbol newReference(int startLine, int startLineOffset, int endLine, i
@Override
public NewSymbol newReference(TextRange range) {
requireNonNull(range, "Provided range is null");
checkArgument(!declaration.overlap(range), "Overlapping symbol declaration and reference for symbol at %s", declaration);
checkArgument(!declaration.overlap(range),
"Overlapping symbol declaration and reference for symbol declared at %s and referenced at %s in file %s",
declaration,
range,
inputFile);
references.add(range);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.batch.sensor.symbol.NewSymbol;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

public class DefaultSymbolTableTest {
Expand All @@ -51,7 +53,7 @@ public void setUpSampleSymbols() {
symbolTableBuilder
.newSymbol(1, 0, 1, 10)
.newReference(2, 10, 2, 15)
.newReference(1, 16, 1, 20);
.newReference(1, 16, 1, 20);

symbolTableBuilder
.newSymbol(1, 12, 1, 15)
Expand All @@ -62,6 +64,18 @@ public void setUpSampleSymbols() {
referencesPerSymbol = symbolTableBuilder.getReferencesBySymbol();
}

@Test
public void fail_on_reference_overlaps_declaration() {
NewSymbol symbol = new DefaultSymbolTable(mock(SensorStorage.class))
.onFile(INPUT_FILE)
.newSymbol(1, 0, 1, 10);

assertThatThrownBy(() -> symbol.newReference(1, 3, 1, 12))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Overlapping symbol declaration and reference for symbol declared at Range[from [line=1, lineOffset=0] to [line=1, lineOffset=10]] and referenced at Range[from [line=1, lineOffset=3] to [line=1, lineOffset=12]] in file src/Foo.java");
}

@Test
public void should_register_symbols() {
assertThat(referencesPerSymbol).hasSize(2);
Expand Down

0 comments on commit 1ecf4a4

Please sign in to comment.