2022-08-09 18:46:36 +00:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSource(t *testing.T) {
|
|
|
|
module := "foo.bar"
|
|
|
|
|
|
|
|
assert := is.New(t)
|
|
|
|
|
|
|
|
_, err := Source(noImports, module)
|
|
|
|
assert.True(errors.Is(err, ErrNoImports)) // Should get error for no imports
|
|
|
|
|
|
|
|
_, err = Source(fakeImports, module)
|
|
|
|
assert.True(errors.Is(err, ErrNoImports)) // Should get error for no "real" imports
|
|
|
|
|
|
|
|
formatted, err := Source(before, module)
|
|
|
|
assert.NoErr(err) // Should be able to format before block
|
|
|
|
assert.True(bytes.Equal(formatted, after)) // Formatted should match after block
|
2023-01-10 17:35:59 +00:00
|
|
|
|
|
|
|
_, err = Source(singleImport, module)
|
|
|
|
assert.NoErr(err) // Should not get an error for single import
|
2022-08-09 18:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
noImports = []byte(`package main
|
|
|
|
|
|
|
|
func main() {}`)
|
|
|
|
fakeImports = []byte(`package main
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
s := "import \"fmt\""
|
|
|
|
_ = s
|
|
|
|
}`)
|
2023-01-10 17:35:59 +00:00
|
|
|
singleImport = []byte(`package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {}`)
|
2022-08-09 18:46:36 +00:00
|
|
|
before = []byte(`package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"foo.bar/baz"
|
|
|
|
"net/http"
|
|
|
|
"github.com/peterbourgon/ff/v3"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"github.com/matryer/is"
|
|
|
|
"foo.bar/bux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {}`)
|
|
|
|
after = []byte(`package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"foo.bar/baz"
|
|
|
|
"foo.bar/bux"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
|
|
|
"github.com/peterbourgon/ff/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {}`)
|
|
|
|
)
|