fork of go-gitdiff with jj support
at v0.2.0 2.2 kB view raw
1package gitdiff 2 3import ( 4 "bytes" 5 "fmt" 6 "io" 7 "testing" 8) 9 10func TestLineReaderAt(t *testing.T) { 11 tests := map[string]struct { 12 InputLines int 13 Offset int64 14 Count int 15 Err bool 16 EOF bool 17 EOFCount int 18 }{ 19 "readLines": { 20 InputLines: 32, 21 Offset: 0, 22 Count: 4, 23 }, 24 "readLinesOffset": { 25 InputLines: 32, 26 Offset: 8, 27 Count: 4, 28 }, 29 "readLinesLargeOffset": { 30 InputLines: 8192, 31 Offset: 4096, 32 Count: 64, 33 }, 34 "readSingleLine": { 35 InputLines: 4, 36 Offset: 2, 37 Count: 1, 38 }, 39 "readZeroLines": { 40 InputLines: 4, 41 Offset: 2, 42 Count: 0, 43 }, 44 "readThroughEOF": { 45 InputLines: 16, 46 Offset: 12, 47 Count: 8, 48 EOF: true, 49 EOFCount: 4, 50 }, 51 "emptyInput": { 52 InputLines: 0, 53 Offset: 0, 54 Count: 2, 55 EOF: true, 56 EOFCount: 0, 57 }, 58 "offsetAfterEOF": { 59 InputLines: 8, 60 Offset: 10, 61 Count: 2, 62 EOF: true, 63 EOFCount: 0, 64 }, 65 "offsetNegative": { 66 InputLines: 8, 67 Offset: -1, 68 Count: 2, 69 Err: true, 70 }, 71 } 72 73 const lineTemplate = "generated test line %d\n" 74 75 for name, test := range tests { 76 t.Run(name, func(t *testing.T) { 77 var input bytes.Buffer 78 for i := 0; i < test.InputLines; i++ { 79 fmt.Fprintf(&input, lineTemplate, i) 80 } 81 82 output := make([][]byte, test.Count) 83 for i := 0; i < test.Count; i++ { 84 output[i] = []byte(fmt.Sprintf(lineTemplate, test.Offset+int64(i))) 85 } 86 87 r := &lineReaderAt{r: bytes.NewReader(input.Bytes())} 88 lines := make([][]byte, test.Count) 89 90 n, err := r.ReadLinesAt(lines, test.Offset) 91 if test.Err { 92 if err == nil { 93 t.Fatal("expected error reading lines, but got nil") 94 } 95 return 96 } 97 if err != nil && (!test.EOF || err != io.EOF) { 98 t.Fatalf("unexpected error reading lines: %v", err) 99 } 100 101 count := test.Count 102 if test.EOF { 103 count = test.EOFCount 104 } 105 106 if n != count { 107 t.Fatalf("incorrect number of lines read: expected %d, actual %d", count, n) 108 } 109 for i := 0; i < n; i++ { 110 if !bytes.Equal(output[i], lines[i]) { 111 t.Errorf("incorrect content in line %d:\nexpected: %q\nactual: %q", i, output[i], lines[i]) 112 } 113 } 114 }) 115 } 116}