github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/codegen/importer_test.go (about)

     1  package codegen
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGetPkgImportPathAndExpose(t *testing.T) {
    10  	cases := []struct {
    11  		full       string
    12  		importPath string
    13  		expose     string
    14  	}{
    15  		{
    16  			"github.com/stretchr/testify/assert",
    17  			"github.com/stretchr/testify/assert",
    18  			"",
    19  		},
    20  		{
    21  			"golib/tes.Test",
    22  			"golib/tes",
    23  			"Test",
    24  		},
    25  		{
    26  			"github.com/stretchr/testify/assert.Equal",
    27  			"github.com/stretchr/testify/assert",
    28  			"Equal",
    29  		},
    30  		{
    31  			"git.chinawayltd.com/stretchr/testify/assert.Equal",
    32  			"git.chinawayltd.com/stretchr/testify/assert",
    33  			"Equal",
    34  		},
    35  		{
    36  			"gopkg.in/yaml.v2.Marshal",
    37  			"gopkg.in/yaml.v2",
    38  			"Marshal",
    39  		},
    40  	}
    41  
    42  	for _, c := range cases {
    43  		importPath, expose := getPkgImportPathAndExpose(c.full)
    44  		assert.Equal(t, c.importPath, importPath)
    45  		assert.Equal(t, c.expose, expose)
    46  	}
    47  }
    48  
    49  func TestImports(t *testing.T) {
    50  	tt := assert.New(t)
    51  	imports := Importer{}
    52  
    53  	tt.Equal(`[]string{"1", "2"}`, imports.Sdump([]string{"1", "2"}))
    54  	tt.Equal(`[]interface {}{"1", nil}`, imports.Sdump([]interface{}{"1", nil}))
    55  	tt.Equal(`map[string]int{"1": 2}`, imports.Sdump(map[string]int{"1": 2}))
    56  }