### Question:
TextRangeCalculator { static TextRange calculate(RuleViolation pmdViolation, InputFile inputFile) { final int startLine = calculateBeginLine(pmdViolation); final int endLine = calculateEndLine(pmdViolation); final TextPointer startPointer = inputFile.selectLine(startLine).start(); final TextPointer endPointer = inputFile.selectLine(endLine).end(); return inputFile.newRange(startPointer, endPointer); }    }### Answer:
@Test void whenBeginLineIsNegativeThenRangeStartsAndEndsAtEndLine() { final int beginLine = -2; final int endLine = 1; final RuleViolation mockedViolation = mock(RuleViolation.class); when(mockedViolation.getBeginLine()).thenReturn(beginLine); when(mockedViolation.getEndLine()).thenReturn(endLine); final TextRange result = TextRangeCalculator.calculate(mockedViolation, testInput); assertThat(result).isNotNull(); assertThat(result.start()) .isNotNull() .hasFieldOrPropertyWithValue("line", endLine); assertThat(result.end()) .isNotNull() .hasFieldOrPropertyWithValue("line", endLine); }
@Test void whenEndLineIsNegativeThenRangeStartsAndEndsAtBeginLine() { final int beginLine = 2; final int endLine = -1; final RuleViolation mockedViolation = mock(RuleViolation.class); when(mockedViolation.getBeginLine()).thenReturn(beginLine); when(mockedViolation.getEndLine()).thenReturn(endLine); final TextRange result = TextRangeCalculator.calculate(mockedViolation, testInput); assertThat(result).isNotNull(); assertThat(result.start()) .isNotNull() .hasFieldOrPropertyWithValue("line", beginLine); assertThat(result.end()) .isNotNull() .hasFieldOrPropertyWithValue("line", beginLine); }
@Test void testCalculateTextRange() { final int beginLine = 1; final int endLine = 3; final RuleViolation mockedViolation = mock(RuleViolation.class); when(mockedViolation.getBeginLine()).thenReturn(beginLine); when(mockedViolation.getEndLine()).thenReturn(endLine); final TextRange result = TextRangeCalculator.calculate(mockedViolation, testInput); assertThat(result).isNotNull(); assertThat(result.start()) .isNotNull() .hasFieldOrPropertyWithValue("line", beginLine); assertThat(result.end()) .isNotNull() .hasFieldOrPropertyWithValue("line", endLine); }
@Test void whenEndLineIsGreaterThanBeginLineThenLineNumbersAreFlipped() { final int beginLine = 3; final int endLine = 1; final RuleViolation mockedViolation = mock(RuleViolation.class); when(mockedViolation.getBeginLine()).thenReturn(beginLine); when(mockedViolation.getEndLine()).thenReturn(endLine); final TextRange result = TextRangeCalculator.calculate(mockedViolation, testInput); assertThat(result).isNotNull(); assertThat(result.start()) .isNotNull() .hasFieldOrPropertyWithValue("line", endLine); assertThat(result.end()) .isNotNull() .hasFieldOrPropertyWithValue("line", beginLine); }
@Test void whenEndLineEqualsBeginLineThenRangeStartAndEndsAtSameLine() { final int line = 1; final RuleViolation mockedViolation = mock(RuleViolation.class); when(mockedViolation.getBeginLine()).thenReturn(line); when(mockedViolation.getEndLine()).thenReturn(line); final TextRange result = TextRangeCalculator.calculate(mockedViolation, testInput); assertThat(result).isNotNull(); assertThat(result.start()) .isNotNull() .hasFieldOrPropertyWithValue("line", line); assertThat(result.end()) .isNotNull() .hasFieldOrPropertyWithValue("line", line); }