github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/pkgx/pkgx_z_unit_test.go (about)

     1  package pkgx_test
     2  
     3  import (
     4  	"fmt"
     5  	"go/ast"
     6  	"go/parser"
     7  	"go/token"
     8  	"go/types"
     9  	"os"
    10  	"path"
    11  	"path/filepath"
    12  	"runtime"
    13  	"strconv"
    14  	"strings"
    15  	"testing"
    16  
    17  	. "github.com/onsi/gomega"
    18  
    19  	"github.com/machinefi/w3bstream/pkg/depends/x/misc/must"
    20  	. "github.com/machinefi/w3bstream/pkg/depends/x/pkgx"
    21  )
    22  
    23  func TestImportPathAndExpose(t *testing.T) {
    24  	cases := []struct {
    25  		imported string
    26  		expose   string
    27  		s        string
    28  	}{
    29  		{"", "B", "B"},
    30  		{"testing", "B", "testing.B"},
    31  		{"a.b.c.d/c", "B", "a.b.c.d/c.B"},
    32  		{"e", "B", "a.b.c.d/vendor/e.B"},
    33  	}
    34  
    35  	for i, c := range cases {
    36  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    37  			imported, expose := ImportPathAndExpose(c.s)
    38  			NewWithT(t).Expect(imported).To(Equal(c.imported))
    39  			NewWithT(t).Expect(expose).To(Equal(c.expose))
    40  		})
    41  	}
    42  }
    43  
    44  var root = "./__tests__"
    45  
    46  func TestCommentScanner(t *testing.T) {
    47  	fset := token.NewFileSet()
    48  
    49  	fpth, err := filepath.Abs(path.Join(root, "comments.go"))
    50  	if err != nil {
    51  		t.Error(err)
    52  		return
    53  	}
    54  
    55  	fast, err := parser.ParseFile(fset, fpth, nil, parser.ParseComments)
    56  	if err != nil {
    57  		t.Error(err)
    58  		return
    59  	}
    60  
    61  	ast.Inspect(fast, func(node ast.Node) bool {
    62  		comments := strings.Split(NewCommentScanner(fset, fast).CommentsOf(node), "\n")
    63  		NewWithT(t).Expect(3 >= len(comments)).To(BeTrue())
    64  		return true
    65  	})
    66  }
    67  
    68  func TestPkgComments(t *testing.T) {
    69  	cwd, err := os.Getwd()
    70  	NewWithT(t).Expect(err).To(BeNil())
    71  	pkg, err := LoadFrom(path.Join(cwd, root))
    72  	NewWithT(t).Expect(err).To(BeNil())
    73  	NewWithT(t).Expect(pkg.Imports()).NotTo(BeEmpty())
    74  
    75  	for _, v := range []struct {
    76  		object types.Object // identifier object
    77  		expect string       // expect identifier's comment
    78  	}{
    79  		{pkg.TypeName("Date"), "type Date"},
    80  		{pkg.Var("test"), "var"},
    81  		{pkg.Const("A"), "a\n\nA"},
    82  		{pkg.Func("Print"), "func Print"},
    83  	} {
    84  		NewWithT(t).
    85  			Expect(pkg.CommentsOf(pkg.IdentOf(v.object))).
    86  			To(Equal(v.expect))
    87  	}
    88  }
    89  
    90  func TestPkgFuncReturns(t *testing.T) {
    91  	cwd, err := os.Getwd()
    92  	NewWithT(t).Expect(err).To(BeNil())
    93  	pkg, err := LoadFrom(path.Join(cwd, root))
    94  	NewWithT(t).Expect(err).To(BeNil())
    95  	NewWithT(t).Expect(pkg.Imports()).NotTo(BeEmpty())
    96  
    97  	var pkgid string
    98  
    99  	{
   100  		_, current, _, _ := runtime.Caller(0)
   101  		dir := filepath.Join(filepath.Dir(current), "./__tests__")
   102  		pkgid = must.String(PkgIdByPath(dir))
   103  	}
   104  
   105  	var cases = []struct {
   106  		FuncName string
   107  		Results  [][]string
   108  	}{
   109  		{
   110  			"FuncSingleReturn",
   111  			[][]string{{"untyped int(2)"}},
   112  		},
   113  		{
   114  			"FuncSelectExprReturn",
   115  			[][]string{{"string"}},
   116  		},
   117  		{
   118  			"FuncWillCall",
   119  			[][]string{
   120  				{"interface{}"},
   121  				{strings.Join([]string{pkgid, "String"}, ".")},
   122  			},
   123  		},
   124  		{
   125  			"FuncReturnWithCallDirectly",
   126  			[][]string{
   127  				{"interface{}"},
   128  				{strings.Join([]string{pkgid, "String"}, ".")},
   129  			},
   130  		},
   131  		{
   132  			"FuncWithNamedReturn",
   133  			[][]string{
   134  				{"interface{}"},
   135  				{strings.Join([]string{pkgid, "String"}, ".")},
   136  			},
   137  		},
   138  		{
   139  			"FuncSingleNamedReturnByAssign",
   140  			[][]string{
   141  				{`untyped string("1")`},
   142  				{strings.Join([]string{pkgid, `String("2")`}, ".")},
   143  			},
   144  		},
   145  		{
   146  			"FuncWithSwitch",
   147  			[][]string{
   148  				{
   149  					`untyped string("a1")`,
   150  					`untyped string("a2")`,
   151  					`untyped string("a3")`,
   152  				},
   153  				{
   154  					strings.Join([]string{pkgid, `String("b1")`}, "."),
   155  					strings.Join([]string{pkgid, `String("b2")`}, "."),
   156  					strings.Join([]string{pkgid, `String("b3")`}, "."),
   157  				},
   158  			},
   159  		},
   160  	}
   161  	for _, c := range cases {
   162  		t.Run(c.FuncName, func(t *testing.T) {
   163  			values, n := pkg.FuncResultsOf(pkg.Func(c.FuncName))
   164  			NewWithT(t).Expect(values).To(HaveLen(n))
   165  			NewWithT(t).Expect(c.Results).To(Equal(PrintValues(pkg.Fset, values)))
   166  		})
   167  	}
   168  }
   169  
   170  func PrintValues(fs *token.FileSet, res map[int][]TypeAndValueExpr) [][]string {
   171  	if res == nil {
   172  		return [][]string{}
   173  	}
   174  	ret := make([][]string, len(res))
   175  	for i := range ret {
   176  		tve := res[i]
   177  		ret[i] = make([]string, len(tve))
   178  		for j, v := range tve {
   179  			fmt.Println(v.Type, v.Value)
   180  			if v.Value == nil {
   181  				ret[i][j] = v.Type.String()
   182  			} else {
   183  				ret[i][j] = fmt.Sprintf("%s(%s)", v.Type, v.Value)
   184  			}
   185  		}
   186  	}
   187  	return ret
   188  }
   189  
   190  func PrintAstInfo(t *testing.T) {
   191  	fset := token.NewFileSet()
   192  	fpth, err := filepath.Abs(path.Join(root, "ast.go"))
   193  	if err != nil {
   194  		t.Error(err)
   195  		return
   196  	}
   197  	fast, err := parser.ParseFile(fset, fpth, nil, parser.AllErrors)
   198  	if err != nil {
   199  		t.Error(err)
   200  		return
   201  	}
   202  	_ = ast.Print(fset, fast)
   203  }