github.com/searKing/golang/go@v1.2.117/ast/ast_test.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ast_test
     6  
     7  import (
     8  	"go/ast"
     9  	"reflect"
    10  	"strconv"
    11  	"strings"
    12  	"testing"
    13  
    14  	ast_ "github.com/searKing/golang/go/ast"
    15  )
    16  
    17  // TestIndirect tests the Indirect function in Go source code.
    18  func TestIndirect(t *testing.T) {
    19  	tests := []struct {
    20  		name       string
    21  		inputExpr  ast.Expr
    22  		inputDepth int
    23  		wantExpr   ast.Expr
    24  		wantDepth  int
    25  	}{
    26  		{"star", &ast.StarExpr{}, 0, nil, 1},
    27  		{"paren", &ast.ParenExpr{}, 0, &ast.ParenExpr{}, 0},
    28  		{"star-star", &ast.StarExpr{X: &ast.StarExpr{}}, 0, nil, 2},
    29  		{"star-star-paren", &ast.StarExpr{X: &ast.StarExpr{X: &ast.ParenExpr{}}}, 0, &ast.ParenExpr{}, 2},
    30  		{"star-paren", &ast.StarExpr{X: &ast.ParenExpr{}}, 0, &ast.ParenExpr{}, 1},
    31  		{"selector", &ast.SelectorExpr{}, 0, &ast.SelectorExpr{}, 0},
    32  		{"index", &ast.IndexExpr{}, 0, &ast.IndexExpr{}, 0},
    33  		{"slice", &ast.SliceExpr{}, 0, &ast.SliceExpr{}, 0},
    34  		{"call", &ast.CallExpr{}, 0, &ast.CallExpr{}, 0},
    35  		{"unary", &ast.UnaryExpr{}, 0, &ast.UnaryExpr{}, 0},
    36  		{"binary", &ast.BinaryExpr{}, 0, &ast.BinaryExpr{}, 0},
    37  		{"basicLit", &ast.BasicLit{}, 0, &ast.BasicLit{}, 0},
    38  	}
    39  
    40  	for i, tt := range tests {
    41  		t.Run(strings.Join([]string{strconv.Itoa(i), tt.name}, ":"), func(t *testing.T) {
    42  			gotExpr, gotDepth := ast_.Indirect(tt.inputExpr, tt.inputDepth)
    43  			if !reflect.DeepEqual(gotExpr, tt.wantExpr) || gotDepth != tt.wantDepth {
    44  				t.Errorf("Indirect(%v, %v) = (%v, %v), want (%v,%v)", tt.inputExpr, 0, gotExpr, gotDepth, tt.wantExpr, tt.wantDepth)
    45  			}
    46  		})
    47  	}
    48  }