github.imxd.top/hashicorp/consul@v1.4.5/agent/metadata/server_internal_test.go (about) 1 package metadata 2 3 import ( 4 "testing" 5 ) 6 7 func TestServer_Key_Equal(t *testing.T) { 8 tests := []struct { 9 name string 10 k1 *Key 11 k2 *Key 12 equal bool 13 }{ 14 { 15 name: "Key equality", 16 k1: &Key{ 17 name: "s1", 18 }, 19 k2: &Key{ 20 name: "s1", 21 }, 22 equal: true, 23 }, 24 { 25 name: "Key Inequality", 26 k1: &Key{ 27 name: "s1", 28 }, 29 k2: &Key{ 30 name: "s2", 31 }, 32 equal: false, 33 }, 34 } 35 36 for _, test := range tests { 37 if test.k1.Equal(test.k2) != test.equal { 38 t.Errorf("Expected a %v result from test %s", test.equal, test.name) 39 } 40 41 // Test Key to make sure it actually works as a key 42 m := make(map[Key]bool) 43 m[*test.k1] = true 44 if _, found := m[*test.k2]; found != test.equal { 45 t.Errorf("Expected a %v result from map test %s", test.equal, test.name) 46 } 47 } 48 } 49 50 func TestServer_Key(t *testing.T) { 51 tests := []struct { 52 name string 53 sd *Server 54 k *Key 55 equal bool 56 }{ 57 { 58 name: "Key equality", 59 sd: &Server{ 60 Name: "s1", 61 }, 62 k: &Key{ 63 name: "s1", 64 }, 65 equal: true, 66 }, 67 { 68 name: "Key inequality", 69 sd: &Server{ 70 Name: "s1", 71 }, 72 k: &Key{ 73 name: "s2", 74 }, 75 equal: false, 76 }, 77 } 78 79 for _, test := range tests { 80 if test.k.Equal(test.sd.Key()) != test.equal { 81 t.Errorf("Expected a %v result from test %s", test.equal, test.name) 82 } 83 } 84 }