github.com/animeshon/gqlgen@v0.13.1-0.20210304133704-3a770431bb6d/internal/rewrite/rewriter_test.go (about)

     1  package rewrite
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestRewriter(t *testing.T) {
    12  	t.Run("default", func(t *testing.T) {
    13  		r, err := New("testdata")
    14  		require.NoError(t, err)
    15  
    16  		body := r.GetMethodBody("Foo", "Method")
    17  		require.Equal(t, `
    18  	// leading comment
    19  
    20  	// field comment
    21  	m.Field++
    22  
    23  	// trailing comment
    24  `, strings.Replace(body, "\r\n", "\n", -1))
    25  
    26  		imps := r.ExistingImports("testdata/example.go")
    27  		require.Len(t, imps, 2)
    28  		assert.Equal(t, []Import{
    29  			{
    30  				Alias:      "",
    31  				ImportPath: "fmt",
    32  			},
    33  			{
    34  				Alias:      "lol",
    35  				ImportPath: "bytes",
    36  			},
    37  		}, imps)
    38  
    39  	})
    40  
    41  	t.Run("out of scope dir", func(t *testing.T) {
    42  		_, err := New("../../../out-of-gomod/package")
    43  		require.Error(t, err)
    44  	})
    45  }