github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/typesutil/signature_test.go (about)

     1  package typesutil
     2  
     3  import (
     4  	"go/token"
     5  	"go/types"
     6  	"testing"
     7  )
     8  
     9  func TestSignature_RequiredParams(t *testing.T) {
    10  	tests := []struct {
    11  		descr string
    12  		sig   *types.Signature
    13  		want  int
    14  	}{{
    15  		descr: "regular signature",
    16  		sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(
    17  			types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
    18  			types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
    19  			types.NewVar(token.NoPos, nil, "c", types.NewSlice(types.Typ[types.String])),
    20  		), nil, false),
    21  		want: 3,
    22  	}, {
    23  		descr: "variadic signature",
    24  		sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(
    25  			types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
    26  			types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
    27  			types.NewVar(token.NoPos, nil, "c", types.NewSlice(types.Typ[types.String])),
    28  		), nil, true /*variadic*/),
    29  		want: 2,
    30  	}}
    31  
    32  	for _, test := range tests {
    33  		t.Run(test.descr, func(t *testing.T) {
    34  			sig := Signature{Sig: test.sig}
    35  			got := sig.RequiredParams()
    36  			if got != test.want {
    37  				t.Errorf("Got: {%s}.RequiredParams() = %d. Want: %d.", test.sig, got, test.want)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func TestSignature_VariadicType(t *testing.T) {
    44  	tests := []struct {
    45  		descr string
    46  		sig   *types.Signature
    47  		want  types.Type
    48  	}{{
    49  		descr: "regular signature",
    50  		sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(
    51  			types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
    52  			types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
    53  			types.NewVar(token.NoPos, nil, "c", types.NewSlice(types.Typ[types.String])),
    54  		), nil, false),
    55  		want: nil,
    56  	}, {
    57  		descr: "variadic signature",
    58  		sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(
    59  			types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
    60  			types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
    61  			types.NewVar(token.NoPos, nil, "c", types.NewSlice(types.Typ[types.String])),
    62  		), nil, true /*variadic*/),
    63  		want: types.NewSlice(types.Typ[types.String]),
    64  	}}
    65  
    66  	for _, test := range tests {
    67  		t.Run(test.descr, func(t *testing.T) {
    68  			sig := Signature{Sig: test.sig}
    69  			got := sig.VariadicType()
    70  			if !types.Identical(got, test.want) {
    71  				t.Errorf("Got: {%s}.VariadicType() = %v. Want: %v.", test.sig, got, test.want)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestSignature_Param(t *testing.T) {
    78  	sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(
    79  		types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
    80  		types.NewVar(token.NoPos, nil, "b", types.Typ[types.Byte]),
    81  		types.NewVar(token.NoPos, nil, "c", types.NewSlice(types.Typ[types.String])),
    82  	), nil, true /*variadic*/)
    83  
    84  	tests := []struct {
    85  		descr    string
    86  		param    int
    87  		ellipsis bool
    88  		want     types.Type
    89  	}{{
    90  		descr: "required param",
    91  		param: 1,
    92  		want:  types.Typ[types.Byte],
    93  	}, {
    94  		descr: "variadic param",
    95  		param: 2,
    96  		want:  types.Typ[types.String],
    97  	}, {
    98  		descr: "variadic param repeated",
    99  		param: 3,
   100  		want:  types.Typ[types.String],
   101  	}, {
   102  		descr:    "variadic param with ellipsis",
   103  		param:    2,
   104  		ellipsis: true,
   105  		want:     types.NewSlice(types.Typ[types.String]),
   106  	}}
   107  
   108  	for _, test := range tests {
   109  		t.Run(test.descr, func(t *testing.T) {
   110  			sig := Signature{Sig: sig}
   111  			got := sig.Param(test.param, test.ellipsis)
   112  			if !types.Identical(got, test.want) {
   113  				t.Errorf("Got: {%s}.Param(%v, %v) = %v. Want: %v.", sig, test.param, test.ellipsis, got, test.want)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func TestSignature_HasXResults(t *testing.T) {
   120  	tests := []struct {
   121  		descr           string
   122  		sig             *types.Signature
   123  		hasResults      bool
   124  		hasNamedResults bool
   125  	}{{
   126  		descr:           "no results",
   127  		sig:             types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(), false),
   128  		hasResults:      false,
   129  		hasNamedResults: false,
   130  	}, {
   131  		descr: "anonymous result",
   132  		sig: types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(
   133  			types.NewVar(token.NoPos, nil, "", types.Typ[types.String]),
   134  		), false),
   135  		hasResults:      true,
   136  		hasNamedResults: false,
   137  	}, {
   138  		descr: "named result",
   139  		sig: types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(
   140  			types.NewVar(token.NoPos, nil, "s", types.Typ[types.String]),
   141  		), false),
   142  		hasResults:      true,
   143  		hasNamedResults: true,
   144  	}, {
   145  		descr: "underscore named result",
   146  		sig: types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(
   147  			types.NewVar(token.NoPos, nil, "_", types.Typ[types.String]),
   148  		), false),
   149  		hasResults:      true,
   150  		hasNamedResults: true,
   151  	}}
   152  
   153  	for _, test := range tests {
   154  		t.Run(test.descr, func(t *testing.T) {
   155  			sig := Signature{Sig: test.sig}
   156  			gotHasResults := sig.HasResults()
   157  			if gotHasResults != test.hasResults {
   158  				t.Errorf("Got: {%s}.HasResults() = %v. Want: %v.", test.sig, gotHasResults, test.hasResults)
   159  			}
   160  			gotHasNamedResults := sig.HasNamedResults()
   161  			if gotHasNamedResults != test.hasNamedResults {
   162  				t.Errorf("Got: {%s}.HasResults() = %v. Want: %v.", test.sig, gotHasNamedResults, test.hasNamedResults)
   163  			}
   164  		})
   165  	}
   166  }