Source file src/go/types/methodlist_test.go

     1  // Copyright 2022 The Go Authors. 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 types
     6  
     7  import (
     8  	"go/token"
     9  	"testing"
    10  )
    11  
    12  func TestLazyMethodList(t *testing.T) {
    13  	l := newLazyMethodList(2)
    14  
    15  	if got := l.Len(); got != 2 {
    16  		t.Fatalf("Len() = %d, want 2", got)
    17  	}
    18  
    19  	f0 := NewFunc(token.NoPos, nil, "f0", nil)
    20  	f1 := NewFunc(token.NoPos, nil, "f1", nil)
    21  
    22  	// Verify that methodList.At is idempotent, by calling it repeatedly with a
    23  	// resolve func that returns different pointer values (f0 or f1).
    24  	steps := []struct {
    25  		index   int
    26  		resolve *Func // the *Func returned by the resolver
    27  		want    *Func // the actual *Func returned by methodList.At
    28  	}{
    29  		{0, f0, f0},
    30  		{0, f1, f0},
    31  		{1, f1, f1},
    32  		{1, f0, f1},
    33  	}
    34  
    35  	for i, step := range steps {
    36  		got := l.At(step.index, func() *Func { return step.resolve })
    37  		if got != step.want {
    38  			t.Errorf("step %d: At(%d, ...) = %s, want %s", i, step.index, got.Name(), step.want.Name())
    39  		}
    40  	}
    41  }
    42  

View as plain text