github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/internal/typeparams/map_test.go (about)

     1  package typeparams
     2  
     3  import (
     4  	"go/token"
     5  	"go/types"
     6  	"testing"
     7  )
     8  
     9  func TestInstanceMap(t *testing.T) {
    10  	i1 := Instance{
    11  		Object: types.NewTypeName(token.NoPos, nil, "i1", nil),
    12  		TArgs: []types.Type{
    13  			types.Typ[types.Int],
    14  			types.Typ[types.Int8],
    15  		},
    16  	}
    17  	i1clone := Instance{
    18  		Object: i1.Object,
    19  		TArgs: []types.Type{
    20  			types.Typ[types.Int],
    21  			types.Typ[types.Int8],
    22  		},
    23  	}
    24  
    25  	i2 := Instance{
    26  		Object: types.NewTypeName(token.NoPos, nil, "i2", nil), // Different pointer.
    27  		TArgs: []types.Type{
    28  			types.Typ[types.Int],
    29  			types.Typ[types.Int8],
    30  		},
    31  	}
    32  	i3 := Instance{
    33  		Object: i1.Object,
    34  		TArgs:  []types.Type{types.Typ[types.String]}, // Different type args.
    35  	}
    36  
    37  	_ = i1
    38  	_ = i1clone
    39  	_ = i3
    40  	_ = i2
    41  
    42  	m := InstanceMap[string]{}
    43  
    44  	// Check operations on a missing key.
    45  	t.Run("empty", func(t *testing.T) {
    46  		if got := m.Has(i1); got {
    47  			t.Errorf("Got: empty map contains %s. Want: empty map contains nothing.", i1)
    48  		}
    49  		if got := m.Get(i1); got != "" {
    50  			t.Errorf("Got: getting missing key returned %q. Want: zero value.", got)
    51  		}
    52  		if got := m.Len(); got != 0 {
    53  			t.Errorf("Got: empty map length %d. Want: 0.", got)
    54  		}
    55  		if got := m.Set(i1, "abc"); got != "" {
    56  			t.Errorf("Got: setting a new key returned old value %q. Want: zero value", got)
    57  		}
    58  		if got := m.Len(); got != 1 {
    59  			t.Errorf("Got: map length %d. Want: 1.", got)
    60  		}
    61  	})
    62  
    63  	// Check operations on the existing key.
    64  	t.Run("first key", func(t *testing.T) {
    65  		if got := m.Set(i1, "def"); got != "abc" {
    66  			t.Errorf(`Got: setting an existing key returned old value %q. Want: "abc".`, got)
    67  		}
    68  		if got := m.Len(); got != 1 {
    69  			t.Errorf("Got: map length %d. Want: 1.", got)
    70  		}
    71  		if got := m.Has(i1); !got {
    72  			t.Errorf("Got: set map key is reported as missing. Want: key present.")
    73  		}
    74  		if got := m.Get(i1); got != "def" {
    75  			t.Errorf(`Got: getting set key returned %q. Want: "def"`, got)
    76  		}
    77  		if got := m.Get(i1clone); got != "def" {
    78  			t.Errorf(`Got: getting set key returned %q. Want: "def"`, got)
    79  		}
    80  	})
    81  
    82  	// Check for key collisions.
    83  	t.Run("different object", func(t *testing.T) {
    84  		if got := m.Has(i2); got {
    85  			t.Errorf("Got: a new key %q is reported as present. Want: not present.", i2)
    86  		}
    87  		if got := m.Set(i2, "123"); got != "" {
    88  			t.Errorf("Got: a new key %q overrode an old value %q. Want: zero value.", i2, got)
    89  		}
    90  		if got := m.Get(i2); got != "123" {
    91  			t.Errorf(`Got: getting set key %q returned: %q. Want: "123"`, i2, got)
    92  		}
    93  		if got := m.Len(); got != 2 {
    94  			t.Errorf("Got: map length %d. Want: 2.", got)
    95  		}
    96  	})
    97  	t.Run("different tArgs", func(t *testing.T) {
    98  		if got := m.Has(i3); got {
    99  			t.Errorf("Got: a new key %q is reported as present. Want: not present.", i3)
   100  		}
   101  		if got := m.Set(i3, "456"); got != "" {
   102  			t.Errorf("Got: a new key %q overrode an old value %q. Want: zero value.", i3, got)
   103  		}
   104  		if got := m.Get(i3); got != "456" {
   105  			t.Errorf(`Got: getting set key %q returned: %q. Want: "456"`, i3, got)
   106  		}
   107  		if got := m.Len(); got != 3 {
   108  			t.Errorf("Got: map length %d. Want: 3.", got)
   109  		}
   110  	})
   111  }