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