74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
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
|
|
|
|
_, err = Source(singleImport, module)
|
|
assert.NoErr(err) // Should not get an error for single import
|
|
}
|
|
|
|
var (
|
|
noImports = []byte(`package main
|
|
|
|
func main() {}`)
|
|
fakeImports = []byte(`package main
|
|
|
|
func main() {
|
|
s := "import \"fmt\""
|
|
_ = s
|
|
}`)
|
|
singleImport = []byte(`package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {}`)
|
|
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() {}`)
|
|
)
|