github.com/youminxue/odin@v0.0.0-20230216022911-c2c8b05d3a41/cmd/name_test.go (about)

     1  package cmd_test
     2  
     3  import (
     4  	"github.com/youminxue/odin/cmd"
     5  	"github.com/youminxue/odin/toolkit/astutils"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestNameCmd(t *testing.T) {
    12  	dir := testDir + "/namecmd"
    13  	receiver := NewMockSvc(dir)
    14  	receiver.Init()
    15  	defer os.RemoveAll(dir)
    16  	err := os.Chdir(dir)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	_, _, err = ExecuteCommandC(cmd.GetRootCmd(), []string{"name", "-f", filepath.Join(dir, "dto", "dto.go"), "-o"}...)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  }
    25  
    26  func TestGetImportPath(t *testing.T) {
    27  	dir := testDir + "/importpath"
    28  	receiver := NewMockSvc(dir)
    29  	receiver.Init()
    30  	defer os.RemoveAll(dir)
    31  	err := os.Chdir(dir)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	type args struct {
    36  		file string
    37  	}
    38  	tests := []struct {
    39  		name string
    40  		args args
    41  		want string
    42  	}{
    43  		{
    44  			name: "1",
    45  			args: args{
    46  				file: filepath.Join(dir, "/entity"),
    47  			},
    48  			want: "importpath/entity",
    49  		},
    50  	}
    51  	for _, tt := range tests {
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			if got := astutils.GetImportPath(tt.args.file); got != tt.want {
    54  				t.Errorf("GetImportPath() = %v, want %v", got, tt.want)
    55  			}
    56  		})
    57  	}
    58  }