github.com/pulumi/terraform@v1.4.0/pkg/addrs/map_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestMap(t *testing.T) {
     8  	variableName := InputVariable{Name: "name"}
     9  	localHello := LocalValue{Name: "hello"}
    10  	pathModule := PathAttr{Name: "module"}
    11  	moduleBeep := ModuleCall{Name: "beep"}
    12  	eachKey := ForEachAttr{Name: "key"} // intentionally not in the map
    13  
    14  	m := MakeMap(
    15  		MakeMapElem[Referenceable](variableName, "Aisling"),
    16  	)
    17  
    18  	m.Put(localHello, "hello")
    19  	m.Put(pathModule, "boop")
    20  	m.Put(moduleBeep, "unrealistic")
    21  
    22  	keySet := m.Keys()
    23  	if want := variableName; !m.Has(want) {
    24  		t.Errorf("map does not include %s", want)
    25  	}
    26  	if want := variableName; !keySet.Has(want) {
    27  		t.Errorf("key set does not include %s", want)
    28  	}
    29  	if want := localHello; !m.Has(want) {
    30  		t.Errorf("map does not include %s", want)
    31  	}
    32  	if want := localHello; !keySet.Has(want) {
    33  		t.Errorf("key set does not include %s", want)
    34  	}
    35  	if want := pathModule; !keySet.Has(want) {
    36  		t.Errorf("key set does not include %s", want)
    37  	}
    38  	if want := moduleBeep; !keySet.Has(want) {
    39  		t.Errorf("key set does not include %s", want)
    40  	}
    41  	if doNotWant := eachKey; m.Has(doNotWant) {
    42  		t.Errorf("map includes rogue element %s", doNotWant)
    43  	}
    44  	if doNotWant := eachKey; keySet.Has(doNotWant) {
    45  		t.Errorf("key set includes rogue element %s", doNotWant)
    46  	}
    47  
    48  	if got, want := m.Get(variableName), "Aisling"; got != want {
    49  		t.Errorf("unexpected value %q for %s; want %q", got, variableName, want)
    50  	}
    51  	if got, want := m.Get(localHello), "hello"; got != want {
    52  		t.Errorf("unexpected value %q for %s; want %q", got, localHello, want)
    53  	}
    54  	if got, want := m.Get(pathModule), "boop"; got != want {
    55  		t.Errorf("unexpected value %q for %s; want %q", got, pathModule, want)
    56  	}
    57  	if got, want := m.Get(moduleBeep), "unrealistic"; got != want {
    58  		t.Errorf("unexpected value %q for %s; want %q", got, moduleBeep, want)
    59  	}
    60  	if got, want := m.Get(eachKey), ""; got != want {
    61  		// eachKey isn't in the map, so Get returns the zero value of string
    62  		t.Errorf("unexpected value %q for %s; want %q", got, eachKey, want)
    63  	}
    64  
    65  	if v, ok := m.GetOk(variableName); v != "Aisling" || !ok {
    66  		t.Errorf("GetOk for %q returned incorrect result (%q, %#v)", variableName, v, ok)
    67  	}
    68  	if v, ok := m.GetOk(eachKey); v != "" || ok {
    69  		t.Errorf("GetOk for %q returned incorrect result (%q, %#v)", eachKey, v, ok)
    70  	}
    71  
    72  	m.Remove(moduleBeep)
    73  	if doNotWant := moduleBeep; m.Has(doNotWant) {
    74  		t.Errorf("map still includes %s after removing it", doNotWant)
    75  	}
    76  	if want := moduleBeep; !keySet.Has(want) {
    77  		t.Errorf("key set no longer includes %s after removing it from the map; key set is supposed to be a snapshot at the time of call", want)
    78  	}
    79  	keySet = m.Keys()
    80  	if doNotWant := moduleBeep; keySet.Has(doNotWant) {
    81  		t.Errorf("key set still includes %s after a second call after removing it from the map", doNotWant)
    82  	}
    83  }