| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internaltest |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
var key, value = "test", "true" |
| 12 |
|
| 13 |
func TestTextMapCarrierKeys(t *testing.T) { |
| 14 |
tmc := NewTextMapCarrier(map[string]string{key: value}) |
| 15 |
expected, actual := []string{key}, tmc.Keys() |
| 16 |
if !reflect.DeepEqual(actual, expected) { |
| 17 |
t.Errorf("expected tmc.Keys() to be %v but it was %v", expected, actual) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestTextMapCarrierGet(t *testing.T) { |
| 22 |
tmc := NewTextMapCarrier(map[string]string{key: value}) |
| 23 |
tmc.GotN(t, 0) |
| 24 |
if got := tmc.Get("empty"); got != "" { |
| 25 |
t.Errorf("TextMapCarrier.Get returned %q for an empty key", got) |
| 26 |
} |
| 27 |
tmc.GotKey(t, "empty") |
| 28 |
tmc.GotN(t, 1) |
| 29 |
if got := tmc.Get(key); got != value { |
| 30 |
t.Errorf("TextMapCarrier.Get(%q) returned %q, want %q", key, got, value) |
| 31 |
} |
| 32 |
tmc.GotKey(t, key) |
| 33 |
tmc.GotN(t, 2) |
| 34 |
} |
| 35 |
|
| 36 |
func TestTextMapCarrierSet(t *testing.T) { |
| 37 |
tmc := NewTextMapCarrier(nil) |
| 38 |
tmc.SetN(t, 0) |
| 39 |
tmc.Set(key, value) |
| 40 |
if got, ok := tmc.data[key]; !ok { |
| 41 |
t.Errorf("TextMapCarrier.Set(%q,%q) failed to store pair", key, value) |
| 42 |
} else if got != value { |
| 43 |
t.Errorf("TextMapCarrier.Set(%q,%q) stored (%q,%q), not (%q,%q)", key, value, key, got, key, value) |
| 44 |
} |
| 45 |
tmc.SetKeyValue(t, key, value) |
| 46 |
tmc.SetN(t, 1) |
| 47 |
} |
| 48 |
|
| 49 |
func TestTextMapCarrierReset(t *testing.T) { |
| 50 |
tmc := NewTextMapCarrier(map[string]string{key: value}) |
| 51 |
tmc.GotN(t, 0) |
| 52 |
tmc.SetN(t, 0) |
| 53 |
tmc.Reset(nil) |
| 54 |
tmc.GotN(t, 0) |
| 55 |
tmc.SetN(t, 0) |
| 56 |
if got := tmc.Get(key); got != "" { |
| 57 |
t.Error("TextMapCarrier.Reset() failed to clear initial data") |
| 58 |
} |
| 59 |
tmc.GotN(t, 1) |
| 60 |
tmc.GotKey(t, key) |
| 61 |
tmc.Set(key, value) |
| 62 |
tmc.SetKeyValue(t, key, value) |
| 63 |
tmc.SetN(t, 1) |
| 64 |
tmc.Reset(nil) |
| 65 |
tmc.GotN(t, 0) |
| 66 |
tmc.SetN(t, 0) |
| 67 |
if got := tmc.Get(key); got != "" { |
| 68 |
t.Error("TextMapCarrier.Reset() failed to clear data") |
| 69 |
} |
| 70 |
} |