Skip to content

Commit

Permalink
update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
camdencheek committed Apr 9, 2024
1 parent c833d31 commit 386244e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 53 deletions.
16 changes: 5 additions & 11 deletions contentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin
m := ms[0]
num := p.newlines().atOffset(m.byteOffset)
lineStart := int(p.newlines().lineStart(num))
lineEnd := int(p.newlines().lineEnd(num, true))
lineEnd := int(p.newlines().lineEnd(num))

var lineCands []*candidateMatch

endMatch := m.byteOffset + m.byteMatchSz

for len(ms) > 0 {
m := ms[0]
if int(m.byteOffset) <= lineEnd {
if int(m.byteOffset) < lineEnd {
endMatch = m.byteOffset + m.byteMatchSz
lineCands = append(lineCands, m)
ms = ms[1:]
Expand All @@ -252,7 +252,7 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin
// crosses a line boundary. Prevent confusion by
// taking lines until we pass the last match
for lineEnd < len(data) && endMatch > uint32(lineEnd) {
next := bytes.IndexByte(data[lineEnd+1:], '\n')
next := bytes.IndexByte(data[lineEnd:], '\n')
if next == -1 {
lineEnd = len(data)
} else {
Expand Down Expand Up @@ -487,20 +487,14 @@ func (nls newlines) lineStart(lineNumber int) uint32 {
// last byte of the line. lineNumber is 1-based. If lineNumber is out
// of range of the lines in the file, the return value will be clamped to
// [0,fileSize].
//
// If trimNewline is set, the offset exclude any line-terminating newline.
// Note: this does _not_ trim any carriage returns and should probably not be
// used by default. It only exists for backwards compatibility.
func (nls newlines) lineEnd(lineNumber int, trimNewline bool) uint32 {
func (nls newlines) lineEnd(lineNumber int) uint32 {
// nls.locs[0] + 1 is the start of the 2nd line of data.
endIdx := lineNumber - 1

if endIdx < 0 {
return 0
} else if endIdx >= len(nls.locs) {
return nls.fileSize
} else if trimNewline {
return nls.locs[endIdx]
} else {
return nls.locs[endIdx] + 1
}
Expand All @@ -514,7 +508,7 @@ func (nls newlines) getLines(data []byte, low, high int) []byte {
}

lowStart := nls.lineStart(low)
highEnd := nls.lineEnd(high-1, false)
highEnd := nls.lineEnd(high - 1)

return data[lowStart:highEnd]
}
Expand Down
4 changes: 2 additions & 2 deletions contentprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestAtOffset(t *testing.T) {
if gotLineStart := nls.lineStart(gotLineNumber); gotLineStart != tt.lineStart {
t.Fatalf("expected line start %d, got %d", tt.lineStart, gotLineStart)
}
if gotLineEnd := nls.lineEnd(gotLineNumber, false); gotLineEnd != tt.lineEnd {
if gotLineEnd := nls.lineEnd(gotLineNumber); gotLineEnd != tt.lineEnd {
t.Fatalf("expected line end %d, got %d", tt.lineEnd, gotLineEnd)
}
})
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestLineBounds(t *testing.T) {
if gotStart != tt.start {
t.Fatalf("expected line start %d, got %d", tt.start, gotStart)
}
gotEnd := nls.lineEnd(tt.lineNumber, false)
gotEnd := nls.lineEnd(tt.lineNumber)
if gotEnd != tt.end {
t.Fatalf("expected line end %d, got %d", tt.end, gotEnd)
}
Expand Down
12 changes: 6 additions & 6 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (s *memSeeker) Size() (uint32, error) {
func TestNewlines(t *testing.T) {
b := testIndexBuilder(t, nil,
Document{Name: "filename", Content: []byte("line1\nline2\nbla")})
// ---------------------------------------------012345-678901-234
// -------------------------------------------012345-678901-234

t.Run("LineMatches", func(t *testing.T) {
sres := searchForTest(t, b, &query.Substring{Pattern: "ne2"})
Expand All @@ -216,15 +216,15 @@ func TestNewlines(t *testing.T) {
LineOffset: 2,
MatchLength: 3,
}},
Line: []byte("line2"),
Line: []byte("line2\n"),
LineStart: 6,
LineEnd: 11,
LineEnd: 12,
LineNumber: 2,
}},
}}

if !reflect.DeepEqual(matches, want) {
t.Errorf("got %v, want %v", matches, want)
if diff := cmp.Diff(matches, want); diff != "" {
t.Fatal(diff)
}
})

Expand Down Expand Up @@ -269,7 +269,7 @@ func TestQueryNewlines(t *testing.T) {
}
m := matches[0]
if len(m.LineMatches) != 2 {
t.Fatalf("got %d line matches, want exactly two", len(m.LineMatches))
t.Fatalf("got %d line matches, want exactly two %#v", len(m.LineMatches), m.LineMatches)
}
})

Expand Down
2 changes: 1 addition & 1 deletion matchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc
}
prev = line
byteStart := int(cp.newlines().lineStart(line))
byteEnd := int(cp.newlines().lineEnd(line, false))
byteEnd := int(cp.newlines().lineEnd(line))
lines = append(lines, lineRange{byteStart, byteEnd})
}

Expand Down
Binary file added testdata/backcompat/new_v16.00000.zoekt
Binary file not shown.
16 changes: 8 additions & 8 deletions testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "ZnVuYyBtYWluKCkgew==",
"Line": "ZnVuYyBtYWluKCkgewo=",
"LineStart": 69,
"LineEnd": 82,
"LineEnd": 83,
"LineNumber": 10,
"Before": null,
"After": null,
Expand Down Expand Up @@ -39,9 +39,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "cGFja2FnZSBtYWlu",
"Line": "cGFja2FnZSBtYWluCg==",
"LineStart": 0,
"LineEnd": 12,
"LineEnd": 13,
"LineNumber": 1,
"Before": null,
"After": null,
Expand Down Expand Up @@ -69,9 +69,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "CW51bSAgICAgPSA1",
"Line": "CW51bSAgICAgPSA1Cg==",
"LineStart": 34,
"LineEnd": 46,
"LineEnd": 47,
"LineNumber": 6,
"Before": null,
"After": null,
Expand Down Expand Up @@ -104,9 +104,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "CW1lc3NhZ2UgPSAiaGVsbG8i",
"Line": "CW1lc3NhZ2UgPSAiaGVsbG8iCg==",
"LineStart": 47,
"LineEnd": 65,
"LineEnd": 66,
"LineNumber": 7,
"Before": null,
"After": null,
Expand Down
16 changes: 8 additions & 8 deletions testdata/golden/TestReadSearch/ctagsrepo_v17.00000.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "ZnVuYyBtYWluKCkgew==",
"Line": "ZnVuYyBtYWluKCkgewo=",
"LineStart": 69,
"LineEnd": 82,
"LineEnd": 83,
"LineNumber": 10,
"Before": null,
"After": null,
Expand Down Expand Up @@ -39,9 +39,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "cGFja2FnZSBtYWlu",
"Line": "cGFja2FnZSBtYWluCg==",
"LineStart": 0,
"LineEnd": 12,
"LineEnd": 13,
"LineNumber": 1,
"Before": null,
"After": null,
Expand Down Expand Up @@ -69,9 +69,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "CW51bSAgICAgPSA1",
"Line": "CW51bSAgICAgPSA1Cg==",
"LineStart": 34,
"LineEnd": 46,
"LineEnd": 47,
"LineNumber": 6,
"Before": null,
"After": null,
Expand Down Expand Up @@ -104,9 +104,9 @@
"Language": "go",
"LineMatches": [
{
"Line": "CW1lc3NhZ2UgPSAiaGVsbG8i",
"Line": "CW1lc3NhZ2UgPSAiaGVsbG8iCg==",
"LineStart": 47,
"LineEnd": 65,
"LineEnd": 66,
"LineNumber": 7,
"Before": null,
"After": null,
Expand Down
8 changes: 4 additions & 4 deletions testdata/golden/TestReadSearch/repo17_v17.00000.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "ZnVuYyBtYWluKCkgew==",
"Line": "ZnVuYyBtYWluKCkgewo=",
"LineStart": 69,
"LineEnd": 82,
"LineEnd": 83,
"LineNumber": 10,
"Before": null,
"After": null,
Expand Down Expand Up @@ -39,9 +39,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "cGFja2FnZSBtYWlu",
"Line": "cGFja2FnZSBtYWluCg==",
"LineStart": 0,
"LineEnd": 12,
"LineEnd": 13,
"LineNumber": 1,
"Before": null,
"After": null,
Expand Down
8 changes: 4 additions & 4 deletions testdata/golden/TestReadSearch/repo2_v16.00000.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "ZnVuYyBtYWluKCkgew==",
"Line": "ZnVuYyBtYWluKCkgewo=",
"LineStart": 33,
"LineEnd": 46,
"LineEnd": 47,
"LineNumber": 7,
"Before": null,
"After": null,
Expand Down Expand Up @@ -39,9 +39,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "cGFja2FnZSBtYWlu",
"Line": "cGFja2FnZSBtYWluCg==",
"LineStart": 0,
"LineEnd": 12,
"LineEnd": 13,
"LineNumber": 1,
"Before": null,
"After": null,
Expand Down
8 changes: 4 additions & 4 deletions testdata/golden/TestReadSearch/repo_v16.00000.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "ZnVuYyBtYWluKCkgew==",
"Line": "ZnVuYyBtYWluKCkgewo=",
"LineStart": 69,
"LineEnd": 82,
"LineEnd": 83,
"LineNumber": 10,
"Before": null,
"After": null,
Expand Down Expand Up @@ -39,9 +39,9 @@
"Language": "Go",
"LineMatches": [
{
"Line": "cGFja2FnZSBtYWlu",
"Line": "cGFja2FnZSBtYWluCg==",
"LineStart": 0,
"LineEnd": 12,
"LineEnd": 13,
"LineNumber": 1,
"Before": null,
"After": null,
Expand Down
12 changes: 7 additions & 5 deletions web/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "f",
Match: "our",
Post: "th",
Post: "th\n",
},
},
},
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "f",
Match: "our",
Post: "th",
Post: "th\n",
},
},
Before: "second snippet\nthird thing\n",
Expand All @@ -453,7 +453,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "",
Match: "one",
Post: " line",
Post: " line\n",
},
},
After: "second snippet\nthird thing\n",
Expand Down Expand Up @@ -516,7 +516,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "",
Match: "one",
Post: " line",
Post: " line\n",
},
},
After: "second snippet\nthird thing\nfourth\nfifth block\nsixth example\nseventh",
Expand All @@ -537,6 +537,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "\t",
Match: "trois",
Post: "\n",
},
},
Before: "un \n \n",
Expand All @@ -558,7 +559,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "to carry ",
Match: "water",
Post: " in the no later bla",
Post: " in the no later bla\n",
},
},
Before: "\n\n\n\n",
Expand All @@ -580,6 +581,7 @@ func TestContextLines(t *testing.T) {
{
Pre: "",
Match: "pastures",
Post: "\n",
},
},
Before: "green\n",
Expand Down

0 comments on commit 386244e

Please sign in to comment.