Skip to content

Commit

Permalink
Add a quick unit test for PostprocessGoFile error management on disco…
Browse files Browse the repository at this point in the history
…vered imports
  • Loading branch information
K-Phoen committed Aug 1, 2023
1 parent f0dba70 commit 9eeef56
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions encoding/gocode/postprocessing_test.go
@@ -0,0 +1,49 @@
package gocode

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPostprocessGoFile_withDiscoveredImport(t *testing.T) {
req := require.New(t)
input := GenGoFile{
Path: "file.go",
In: []byte(`package thema
func Hello(name string) string {
return fmt.Sprintf("Hello %w", name)
}`),
IgnoreDiscoveredImports: false,
}

_, err := PostprocessGoFile(input)
req.Error(err)
req.ErrorContains(err, "goimports added the following import statements")
}

func TestPostprocessGoFile_withIgnoredDiscoveredImport(t *testing.T) {
req := require.New(t)
input := GenGoFile{
Path: "file.go",
In: []byte(`package thema
func Hello(name string) string {
return fmt.Sprintf("Hello %w", name)
}`),
IgnoreDiscoveredImports: true,
}

output, err := PostprocessGoFile(input)
req.NoError(err)

req.Equal(`package thema
import "fmt"
func Hello(name string) string {
return fmt.Sprintf("Hello %w", name)
}
`, string(output))
}

0 comments on commit 9eeef56

Please sign in to comment.