Skip to content

Commit b4eaa92

Browse files
eamonnmcmanusnick-someone
authored andcommitted
In onLine(n), produce a sensible exception message if n is out of range. Currently you get an ArrayIndexOutOfBoundsException.
RELNOTES=Better exception in onLine(n) when n is out of range. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=304030110
1 parent b59a137 commit b4eaa92

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>junit</groupId>
5252
<artifactId>junit</artifactId>
53-
<version>4.12</version>
53+
<version>4.13</version>
5454
</dependency>
5555
<dependency>
5656
<groupId>com.google.truth</groupId>

src/main/java/com/google/testing/compile/CompilationSubject.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,12 @@ ImmutableList<String> linesInFile() {
466466

467467
/** Lists the line at a line number (1-based). */
468468
String listLine(long lineNumber) {
469-
return lineNumber == Diagnostic.NOPOS
470-
? "(no associated line)"
471-
: String.format("%4d: %s", lineNumber, linesInFile().get((int) (lineNumber - 1)));
469+
if (lineNumber == Diagnostic.NOPOS) {
470+
return "(no associated line)";
471+
}
472+
checkArgument(lineNumber > 0 && lineNumber <= linesInFile().size(),
473+
"Invalid line number %s; number of lines is only %s", lineNumber, linesInFile().size());
474+
return String.format("%4d: %s", lineNumber, linesInFile().get((int) (lineNumber - 1)));
472475
}
473476
}
474477

src/test/java/com/google/testing/compile/CompilationSubjectTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import static java.nio.charset.StandardCharsets.UTF_8;
2525
import static java.util.stream.Collectors.joining;
2626
import static javax.tools.StandardLocation.CLASS_OUTPUT;
27+
import static org.junit.Assert.assertThrows;
2728
import static org.junit.Assert.fail;
2829

2930
import com.google.common.collect.ImmutableList;
3031
import com.google.common.io.ByteSource;
3132
import com.google.common.truth.ExpectFailure;
3233
import com.google.common.truth.Truth;
34+
import java.io.BufferedReader;
35+
import java.io.IOException;
3336
import java.util.regex.Pattern;
3437
import java.util.stream.Stream;
3538
import javax.tools.Diagnostic;
@@ -347,6 +350,22 @@ public void hadWarningContainingInFileOnLine_wrongLine() {
347350
assertThat(expected.getMessage()).contains("" + actualErrorLine);
348351
}
349352

353+
@Test
354+
public void hadWarningContainingInFileOnLine_lineTooBig() throws IOException {
355+
long lineCount = new BufferedReader(sourceFile.openReader(false)).lines().count();
356+
IllegalArgumentException exception =
357+
assertThrows(
358+
IllegalArgumentException.class,
359+
() ->
360+
assertThat(compilerWithWarning().compile(sourceFile))
361+
.hadWarningContainingMatch("this is a+ message")
362+
.inFile(sourceFile)
363+
.onLine(100));
364+
assertThat(exception)
365+
.hasMessageThat()
366+
.isEqualTo("Invalid line number 100; number of lines is only " + lineCount);
367+
}
368+
350369
/* TODO(dpb): Negative cases for onLineContaining for
351370
* (warning, error, note) x
352371
* (containing(String), containingMatch(String), containingMatch(Pattern)). */

0 commit comments

Comments
 (0)