-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes_test.go
More file actions
81 lines (73 loc) · 2.38 KB
/
Copy pathattributes_test.go
File metadata and controls
81 lines (73 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// attributes_test.go
package cinc
import (
"encoding/json"
"testing"
)
func TestAttributes_DigAndGet(t *testing.T) {
a := Attributes{"kernel": map[string]any{"machine": "x86_64", "bits": float64(64)}}
if v, ok := a.Dig("kernel", "machine"); !ok || v != "x86_64" {
t.Fatalf("Dig = %v %v", v, ok)
}
if got := a.GetString("kernel", "machine"); got != "x86_64" {
t.Fatalf("GetString = %q", got)
}
if _, ok := a.Dig("kernel", "missing"); ok {
t.Fatal("expected miss")
}
}
func TestAttributes_Dig_EmptyPath(t *testing.T) {
a := Attributes{"x": 1}
if v, ok := a.Dig(); ok || v != nil {
t.Fatalf("Dig() with no args = (%v, %v), want (nil, false)", v, ok)
}
}
func TestAttributes_Dig_NonMapIntermediate(t *testing.T) {
// "kernel.machine" is a string, so digging further must return (nil, false)
// rather than panicking on the wrong type assertion.
a := Attributes{"kernel": map[string]any{"machine": "x86_64"}}
if v, ok := a.Dig("kernel", "machine", "deeper"); ok || v != nil {
t.Fatalf("Dig into non-map = (%v, %v), want (nil, false)", v, ok)
}
}
func TestAttributes_Dig_MissingTopLevel(t *testing.T) {
a := Attributes{"kernel": map[string]any{}}
if v, ok := a.Dig("absent"); ok || v != nil {
t.Fatalf("Dig(absent) = (%v, %v), want (nil, false)", v, ok)
}
}
func TestAttributes_GetString_NonString(t *testing.T) {
// Non-string values must yield "" (not a panic, not a stringification).
a := Attributes{"port": float64(8080), "enabled": true, "name": "web"}
if got := a.GetString("port"); got != "" {
t.Errorf("GetString(port) = %q, want \"\"", got)
}
if got := a.GetString("enabled"); got != "" {
t.Errorf("GetString(enabled) = %q, want \"\"", got)
}
if got := a.GetString("name"); got != "web" {
t.Errorf("GetString(name) = %q, want web", got)
}
}
func TestAttributes_GetString_Missing(t *testing.T) {
if got := (Attributes{}).GetString("nope"); got != "" {
t.Errorf("GetString on missing key = %q, want \"\"", got)
}
}
func TestAttributes_RoundTripPreservesUnknown(t *testing.T) {
src := []byte(`{"a":1,"nested":{"keep":true},"x":["y"]}`)
var a Attributes
if err := json.Unmarshal(src, &a); err != nil {
t.Fatal(err)
}
out, err := json.Marshal(a)
if err != nil {
t.Fatal(err)
}
var got, want map[string]any
json.Unmarshal(out, &got)
json.Unmarshal(src, &want)
if len(got) != len(want) || got["a"] != want["a"] {
t.Fatalf("round trip lost data: %s", out)
}
}