|
| 1 | + |
| 2 | +package dotty.tools |
| 3 | +package dotc.util |
| 4 | + |
| 5 | +import Spans.* |
| 6 | + |
| 7 | +import org.junit.Assert.{assertThrows as _, *} |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +class SourceFileTests: |
| 11 | + @Test def `i15209 source column handles tabs`: Unit = |
| 12 | + val text = "\ta\n \tb\n \t c\n" |
| 13 | + val f = SourceFile.virtual("batch", text) |
| 14 | + assertEquals(8, f.column(text.indexOf('a'))) |
| 15 | + assertEquals(8, f.column(text.indexOf('b'))) |
| 16 | + assertEquals(9, f.column(text.indexOf('c'))) |
| 17 | + |
| 18 | + def lineContentOf(code: String, offset: Int) = SourceFile.virtual("batch", code).lineContent(offset) |
| 19 | + |
| 20 | + @Test def si8205_lineToString: Unit = |
| 21 | + assertEquals("", lineContentOf("", 0)) |
| 22 | + assertEquals("abc", lineContentOf("abc", 0)) |
| 23 | + assertEquals("abc", lineContentOf("abc", 3)) |
| 24 | + assertEquals("code no newline", lineContentOf("code no newline", 1)) |
| 25 | + assertEquals("\n", lineContentOf("\n", 0)) |
| 26 | + assertEquals("abc\n", lineContentOf("abc\ndef", 0)) |
| 27 | + assertEquals("abc\n", lineContentOf("abc\ndef", 3)) |
| 28 | + assertEquals("def", lineContentOf("abc\ndef", 4)) |
| 29 | + assertEquals("def", lineContentOf("abc\ndef", 6)) |
| 30 | + assertEquals("def\n", lineContentOf("abc\ndef\n", 7)) |
| 31 | + |
| 32 | + @Test def CRisEOL: Unit = |
| 33 | + assertEquals("\r", lineContentOf("\r", 0)) |
| 34 | + assertEquals("abc\r", lineContentOf("abc\rdef", 0)) |
| 35 | + assertEquals("abc\r", lineContentOf("abc\rdef", 3)) |
| 36 | + assertEquals("def", lineContentOf("abc\rdef", 4)) |
| 37 | + assertEquals("def", lineContentOf("abc\rdef", 6)) |
| 38 | + assertEquals("def\r", lineContentOf("abc\rdef\r", 7)) |
| 39 | + |
| 40 | + @Test def CRNLisEOL(): Unit = |
| 41 | + assertEquals("\r\n", lineContentOf("\r\n", 0)) |
| 42 | + assertEquals("abc\r\n", lineContentOf("abc\r\ndef", 0)) |
| 43 | + assertEquals("abc\r\n", lineContentOf("abc\r\ndef", 3)) |
| 44 | + assertEquals("abc\r\n", lineContentOf("abc\r\ndef", 4)) |
| 45 | + assertEquals("def", lineContentOf("abc\r\ndef", 5)) |
| 46 | + assertEquals("def", lineContentOf("abc\r\ndef", 7)) |
| 47 | + assertEquals("def", lineContentOf("abc\r\ndef", 8)) |
| 48 | + assertEquals("def\r\n", lineContentOf("abc\r\ndef\r\n", 9)) |
| 49 | + |
| 50 | + @Test def `t9885 lineToOffset throws on bad line`: Unit = |
| 51 | + val text = "a\nb\nc\n" |
| 52 | + val f = SourceFile.virtual("batch", text) |
| 53 | + // was: EOL is line terminator, not line separator, so there is not an empty 4th line |
| 54 | + val splitsville = text.split("\n") |
| 55 | + assertEquals(List(0, 2, 4, 6), (0 to splitsville.nn.length).toList.map(f.lineToOffset)) |
| 56 | + assertThrows[IndexOutOfBoundsException] { |
| 57 | + f.lineToOffset(4) // was: 3 in Scala 2 (no empty line), 5 in Scala 3 (sentinel induces off-by-one) |
| 58 | + } |
| 59 | + |
| 60 | + // Position and SourceFile used to count differently |
| 61 | + val p = SourcePosition(f, Span(text.length - 1)) |
| 62 | + val q = SourcePosition(f, Span(f.lineToOffset(p.line - 1))) |
| 63 | + assertEquals(2, p.line) |
| 64 | + assertEquals(p.line - 1, q.line) |
| 65 | + assertEquals(p.column, q.column + 1) |
| 66 | + assertEquals(f.startOfLine(p.span.start), SourcePosition(f, Span(f.lineToOffset(p.line))).span.start) |
| 67 | + |
| 68 | + @Test def `t9885 lineToOffset ignores lack of final EOL`: Unit = |
| 69 | + val text = "a\nb\nc" |
| 70 | + val f = SourceFile.virtual("batch", text) |
| 71 | + assertThrows[IndexOutOfBoundsException] { |
| 72 | + f.lineToOffset(3) |
| 73 | + } |
| 74 | + assertEquals(4, f.lineToOffset(2)) |
| 75 | + assertEquals(2, f.offsetToLine(text.length)) |
| 76 | + |
| 77 | + @Test def `t11572 offsetToLine throws on bad offset`: Unit = |
| 78 | + val text = "a\nb\nc\n" |
| 79 | + val f = SourceFile.virtual("batch", text) |
| 80 | + /* current code requires offsets untethered from source |
| 81 | + assertThrows[IndexOutOfBoundsException] { |
| 82 | + f.offsetToLine(-1) // was: -1 |
| 83 | + } |
| 84 | + assertThrows[IndexOutOfBoundsException] { |
| 85 | + f.offsetToLine(7) // was: 3 |
| 86 | + } |
| 87 | + */ |
| 88 | + assertEquals(0, f.offsetToLine(0)) |
| 89 | + assertEquals(0, f.offsetToLine(1)) |
| 90 | + assertEquals(1, f.offsetToLine(2)) |
| 91 | + assertEquals(2, f.offsetToLine(4)) |
| 92 | + assertEquals(2, f.offsetToLine(5)) |
| 93 | + assertEquals(3, f.offsetToLine(6)) |
| 94 | + |
| 95 | + @Test def `t11572b offsetToLine throws on bad offset`: Unit = |
| 96 | + val text = "a\nb\nc\nd" |
| 97 | + val f = SourceFile.virtual("batch", text) |
| 98 | + /* |
| 99 | + assertThrows[IndexOutOfBoundsException] { |
| 100 | + f.offsetToLine(-1) |
| 101 | + } |
| 102 | + assertThrows[IndexOutOfBoundsException] { |
| 103 | + f.offsetToLine(8) |
| 104 | + } |
| 105 | + */ |
| 106 | + assertEquals(0, f.offsetToLine(0)) |
| 107 | + assertEquals(0, f.offsetToLine(1)) |
| 108 | + assertEquals(1, f.offsetToLine(2)) |
| 109 | + assertEquals(2, f.offsetToLine(4)) |
| 110 | + assertEquals(2, f.offsetToLine(5)) |
| 111 | + assertEquals(3, f.offsetToLine(6)) |
| 112 | + assertEquals(3, f.offsetToLine(7)) |
0 commit comments