| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package otel |
| 6 | |
| 7 | import ( |
| 8 | "errors" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/tools/internal/event/keys" |
| 12 | ) |
| 13 | |
| 14 | func TestLabelToAttribute_String(t *testing.T) { |
| 15 | key := keys.NewString("mykey", "") |
| 16 | l := key.Of("myvalue") |
| 17 | |
| 18 | attr, ok := labelToAttribute(l) |
| 19 | if !ok { |
| 20 | t.Fatal("expected ok=true") |
| 21 | } |
| 22 | if attr.Key != "mykey" { |
| 23 | t.Errorf("expected key 'mykey', got %q", attr.Key) |
| 24 | } |
| 25 | if attr.Value.StringValue == nil || *attr.Value.StringValue != "myvalue" { |
| 26 | t.Errorf("expected stringValue 'myvalue', got %v", attr.Value.StringValue) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestLabelToAttribute_Int(t *testing.T) { |
| 31 | key := keys.NewInt("count", "") |
| 32 | l := key.Of(42) |
| 33 | |
| 34 | attr, ok := labelToAttribute(l) |
| 35 | if !ok { |
| 36 | t.Fatal("expected ok=true") |
| 37 | } |
| 38 | if attr.Key != "count" { |
| 39 | t.Errorf("expected key 'count', got %q", attr.Key) |
| 40 | } |
| 41 | if attr.Value.IntValue == nil || *attr.Value.IntValue != 42 { |
| 42 | t.Errorf("expected intValue 42, got %v", attr.Value.IntValue) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestLabelToAttribute_Float(t *testing.T) { |
| 47 | key := keys.NewFloat("ratio", "") |
| 48 | l := key.Of(3.14) |
| 49 | |
| 50 | attr, ok := labelToAttribute(l) |
| 51 | if !ok { |
| 52 | t.Fatal("expected ok=true") |
| 53 | } |
| 54 | if attr.Key != "ratio" { |
| 55 | t.Errorf("expected key 'ratio', got %q", attr.Key) |
| 56 | } |
| 57 | if attr.Value.DoubleValue == nil || *attr.Value.DoubleValue != 3.14 { |
| 58 | t.Errorf("expected doubleValue 3.14, got %v", attr.Value.DoubleValue) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestLabelToAttribute_Error(t *testing.T) { |
| 63 | key := keys.NewError("err", "") |
| 64 | l := key.Of(errors.New("something failed")) |
| 65 | |
| 66 | attr, ok := labelToAttribute(l) |
| 67 | if !ok { |
| 68 | t.Fatal("expected ok=true") |
| 69 | } |
| 70 | if attr.Key != "err" { |
| 71 | t.Errorf("expected key 'err', got %q", attr.Key) |
| 72 | } |
| 73 | if attr.Value.StringValue == nil || *attr.Value.StringValue != "something failed" { |
| 74 | t.Errorf("expected stringValue 'something failed', got %v", attr.Value.StringValue) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestLabelToAttribute_NilError(t *testing.T) { |
| 79 | key := keys.NewError("err", "") |
| 80 | l := key.Of(nil) |
| 81 | |
| 82 | _, ok := labelToAttribute(l) |
| 83 | if ok { |
| 84 | t.Error("expected ok=false for nil error") |
| 85 | } |
| 86 | } |