fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

Expand tests for match merging (#892)

Follow up to https://github.com/sourcegraph/zoekt/pull/888, where I forgot to improve the test coverage.

+54 -6
+54 -6
index_test.go
··· 1556 1556 } 1557 1557 1558 1558 func TestMergeMatches(t *testing.T) { 1559 - content := []byte("blablabla") 1560 - b := testIndexBuilder(t, nil, 1561 - Document{Name: "f1", Content: content}) 1562 - 1563 - t.Run("LineMatches", func(t *testing.T) { 1559 + t.Run("LineMatches, adjacent matches", func(t *testing.T) { 1560 + b := testIndexBuilder(t, nil, 1561 + Document{Name: "f1", Content: []byte("blablabla")}) 1564 1562 sres := searchForTest(t, b, 1565 1563 &query.Substring{Pattern: "bla"}) 1564 + 1566 1565 if len(sres.Files) != 1 || len(sres.Files[0].LineMatches) != 1 { 1567 1566 t.Fatalf("got %v, want 1 match", sres.Files) 1568 1567 } 1568 + 1569 + if len(sres.Files[0].LineMatches[0].LineFragments) != 3 { 1570 + t.Fatalf("got %v, want 3 fragments", sres.Files[0].LineMatches[0].LineFragments) 1571 + } 1569 1572 }) 1570 1573 1571 - t.Run("ChunkMatches", func(t *testing.T) { 1574 + t.Run("LineMatches, overlapping matches", func(t *testing.T) { 1575 + b := testIndexBuilder(t, nil, 1576 + Document{Name: "f1", Content: []byte("hellogoodbye")}) 1577 + sres := searchForTest(t, b, 1578 + &query.And{Children: []query.Q{ 1579 + &query.Substring{Pattern: "hello"}, 1580 + &query.Substring{Pattern: "logood"}, 1581 + }}) 1582 + 1583 + if len(sres.Files) != 1 || len(sres.Files[0].LineMatches) != 1 { 1584 + t.Fatalf("got %v, want 1 match", sres.Files) 1585 + } 1586 + 1587 + lineFragments := sres.Files[0].LineMatches[0].LineFragments 1588 + if len(lineFragments) != 1 || lineFragments[0].MatchLength != len("hello") { 1589 + t.Fatalf("got %v, want single line fragment 'hello'", lineFragments) 1590 + } 1591 + }) 1592 + 1593 + t.Run("ChunkMatches, no overlap", func(t *testing.T) { 1594 + b := testIndexBuilder(t, nil, 1595 + Document{Name: "f1", Content: []byte("blablabla")}) 1596 + 1572 1597 sres := searchForTest(t, b, 1573 1598 &query.Substring{Pattern: "bla"}, 1574 1599 chunkOpts, 1575 1600 ) 1576 1601 if len(sres.Files) != 1 || len(sres.Files[0].ChunkMatches) != 1 { 1577 1602 t.Fatalf("got %v, want 1 match", sres.Files) 1603 + } 1604 + 1605 + if len(sres.Files[0].ChunkMatches[0].Ranges) != 3 { 1606 + t.Fatalf("got %v, want 3 ranges", sres.Files[0].ChunkMatches[0].Ranges) 1607 + } 1608 + }) 1609 + 1610 + t.Run("ChunkMatches, overlapping matches", func(t *testing.T) { 1611 + b := testIndexBuilder(t, nil, 1612 + Document{Name: "f1", Content: []byte("hellogoodbye")}) 1613 + sres := searchForTest(t, b, 1614 + &query.And{Children: []query.Q{ 1615 + &query.Substring{Pattern: "hello"}, 1616 + &query.Substring{Pattern: "logood"}, 1617 + }}, chunkOpts) 1618 + 1619 + if len(sres.Files) != 1 || len(sres.Files[0].ChunkMatches) != 1 { 1620 + t.Fatalf("got %v, want 1 chunk match", sres.Files) 1621 + } 1622 + 1623 + ranges := sres.Files[0].ChunkMatches[0].Ranges 1624 + if len(ranges) != 1 || ranges[0].Start.ByteOffset != 0 || ranges[0].End.ByteOffset != 5 { 1625 + t.Fatalf("got %v, want single chunk range 'hello'", ranges) 1578 1626 } 1579 1627 }) 1580 1628 }