1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
package sentry
import (
"reflect"
"testing"
)
var input = [][]byte{
[]byte("line 1"),
[]byte("line 2"),
[]byte("line 3"),
[]byte("line 4"),
[]byte("line 5"),
}
func assertContextLines(t *testing.T, gotLines, wantLines [][]byte, gotReadLines, wantReadLines int) {
t.Helper()
if !reflect.DeepEqual(gotLines, wantLines) {
t.Errorf("incorrect context lines returned. got %s, want %s", gotLines, wantLines)
}
if gotReadLines != wantReadLines {
t.Errorf("incorrect context lines count returned. got %d, want %d", gotReadLines, wantReadLines)
}
}
func TestCalculateContextLinesSingleLine(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 2, 0)
wantLines, wantReadLines := [][]byte{
[]byte("line 2"),
}, 0
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesNegativeLine(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, -2, 0)
var wantLines [][]byte
var wantReadLines int
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesNegativeContext(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 2, -2)
wantLines, wantReadLines := [][]byte{
[]byte("line 2"),
}, 0
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesOverflowLine(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 10, 0)
var wantLines [][]byte
var wantReadLines int
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesWholeFile(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 3, 2)
wantLines, wantReadLines := [][]byte{
[]byte("line 1"),
[]byte("line 2"),
[]byte("line 3"),
[]byte("line 4"),
[]byte("line 5"),
}, 2
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesOverflowContextAtTheTop(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 2, 3)
wantLines, wantReadLines := [][]byte{
[]byte("line 1"),
[]byte("line 2"),
[]byte("line 3"),
[]byte("line 4"),
[]byte("line 5"),
}, 1
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesOverflowContextAtTheBottom(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 5, 3)
wantLines, wantReadLines := [][]byte{
[]byte("line 2"),
[]byte("line 3"),
[]byte("line 4"),
[]byte("line 5"),
}, 3
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestCalculateContextLinesOverflowContextBothSides(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.calculateContextLines(input, 2, 10)
wantLines, wantReadLines := [][]byte{
[]byte("line 1"),
[]byte("line 2"),
[]byte("line 3"),
[]byte("line 4"),
[]byte("line 5"),
}, 1
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
}
func TestReadContextLinesNonExistingInput(t *testing.T) {
sr := newSourceReader()
gotLines, gotReadLines := sr.readContextLines("non_existing.go", 2, 10)
var wantLines [][]byte
var wantReadLines int
assertContextLines(t, gotLines, wantLines, gotReadLines, wantReadLines)
assertEqual(t, sr.cache["non_existing.go"], wantLines)
}
|