Skip to content

Commit

Permalink
bindings/go: Fix bug in RangeText function (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
varungandhi-src authored May 3, 2024
1 parent dc6f8c6 commit 6495bfb
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bindings/go/scip/source_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (d *SourceFile) String() string {
// RangeText returns the substring of the source file contents that enclose the provided range.
func (d *SourceFile) RangeText(position Range) string {
result := strings.Builder{}
for line := position.Start.Line; line < position.End.Line; line++ {
for line := position.Start.Line; line <= position.End.Line; line++ {
start := position.Start.Character
if line > position.Start.Line {
result.WriteString("\n")
Expand Down
42 changes: 42 additions & 0 deletions bindings/go/scip/source_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package scip

import (
"fmt"
"testing"

"github.com/hexops/autogold/v2"
)

const testFileContents = `Mary had a little lamb,
Its fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go.
`

func TestRangeText(t *testing.T) {
// Update expect values with `go test ./bindings/go/scip -update`
testCases := []struct {
range_ []int32
expect autogold.Value
}{
{range_: []int32{0, 0, 0, 0}, expect: autogold.Expect("")},
{range_: []int32{0, 0, 0, 8}, expect: autogold.Expect("Mary had")},
{range_: []int32{0, 0, 1, 0}, expect: autogold.Expect("Mary had a little lamb,\n")},
{range_: []int32{0, 0, 1, 10}, expect: autogold.Expect("Mary had a little lamb,\nIts fleece")},
{range_: []int32{0, 0, 4, 0}, expect: autogold.Expect(`Mary had a little lamb,
Its fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go.
`)},
}

sourceFile := NewSourceFile("", "", testFileContents)

for _, testCase := range testCases {
r := testCase.range_
range_ := Range{Start: Position{Line: r[0], Character: r[1]}, End: Position{Line: r[2], Character: r[3]}}
t.Run(fmt.Sprintf("%v", range_), func(t *testing.T) {
testCase.expect.Equal(t, sourceFile.RangeText(range_))
})
}
}
23 changes: 14 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/bufbuild/buf v1.25.0
github.com/google/go-cmp v0.5.9
github.com/google/gofuzz v1.1.0
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/gotextdiff v1.0.3
github.com/hhatto/gocloc v0.4.2
github.com/k0kubun/pp/v3 v3.1.0
Expand All @@ -15,7 +16,7 @@ require (
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220511160847-5a43d3ea24eb
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
golang.org/x/tools v0.11.0
golang.org/x/tools v0.12.0
google.golang.org/protobuf v1.31.0
)

Expand All @@ -41,6 +42,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/protoc-gen-validate v0.3.0-java // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/go-chi/chi/v5 v5.0.10 // indirect
Expand All @@ -54,6 +56,7 @@ require (
github.com/google/go-containerregistry v0.15.2 // indirect
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hexops/valast v1.4.4 // indirect
github.com/huandu/xstrings v1.0.0 // indirect
github.com/imdario/mergo v0.3.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -63,22 +66,23 @@ require (
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mwitkow/go-proto-validators v0.0.0-20180403085117-0950a7990007 // indirect
github.com/nightlyone/lockfile v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pseudomuto/protokit v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand All @@ -94,13 +98,14 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
)
Loading

0 comments on commit 6495bfb

Please sign in to comment.