| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pcommon |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/base64" |
| 8 |
"math" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/internal/testutil" |
| 15 |
"go.opentelemetry.io/collector/pdata/internal" |
| 16 |
) |
| 17 |
|
| 18 |
func TestValue(t *testing.T) { |
| 19 |
v := NewValueStr("abc") |
| 20 |
assert.Equal(t, ValueTypeStr, v.Type()) |
| 21 |
assert.Equal(t, "abc", v.Str()) |
| 22 |
|
| 23 |
v = NewValueInt(123) |
| 24 |
assert.Equal(t, ValueTypeInt, v.Type()) |
| 25 |
assert.EqualValues(t, 123, v.Int()) |
| 26 |
|
| 27 |
v = NewValueDouble(3.4) |
| 28 |
assert.Equal(t, ValueTypeDouble, v.Type()) |
| 29 |
assert.InDelta(t, 3.4, v.Double(), 0.01) |
| 30 |
|
| 31 |
v = NewValueBool(true) |
| 32 |
assert.Equal(t, ValueTypeBool, v.Type()) |
| 33 |
assert.True(t, v.Bool()) |
| 34 |
|
| 35 |
v = NewValueBytes() |
| 36 |
assert.Equal(t, ValueTypeBytes, v.Type()) |
| 37 |
|
| 38 |
v = NewValueEmpty() |
| 39 |
assert.Equal(t, ValueTypeEmpty, v.Type()) |
| 40 |
|
| 41 |
v = NewValueMap() |
| 42 |
assert.Equal(t, ValueTypeMap, v.Type()) |
| 43 |
|
| 44 |
v = NewValueSlice() |
| 45 |
assert.Equal(t, ValueTypeSlice, v.Type()) |
| 46 |
} |
| 47 |
|
| 48 |
func TestValueReadOnly(t *testing.T) { |
| 49 |
state := internal.NewState() |
| 50 |
state.MarkReadOnly() |
| 51 |
v := newValue(&internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "v"}}, state) |
| 52 |
|
| 53 |
assert.Equal(t, ValueTypeStr, v.Type()) |
| 54 |
assert.Equal(t, "v", v.Str()) |
| 55 |
assert.EqualValues(t, 0, v.Int()) |
| 56 |
assert.InDelta(t, 0, v.Double(), 0.01) |
| 57 |
assert.False(t, v.Bool()) |
| 58 |
assert.Equal(t, ByteSlice{}, v.Bytes()) |
| 59 |
assert.Equal(t, Map{}, v.Map()) |
| 60 |
assert.Equal(t, Slice{}, v.Slice()) |
| 61 |
|
| 62 |
assert.Equal(t, "v", v.AsString()) |
| 63 |
|
| 64 |
assert.Panics(t, func() { v.SetStr("abc") }) |
| 65 |
assert.Panics(t, func() { v.SetInt(123) }) |
| 66 |
assert.Panics(t, func() { v.SetDouble(3.4) }) |
| 67 |
assert.Panics(t, func() { v.SetBool(true) }) |
| 68 |
assert.Panics(t, func() { v.SetEmptyBytes() }) |
| 69 |
assert.Panics(t, func() { v.SetEmptyMap() }) |
| 70 |
assert.Panics(t, func() { v.SetEmptySlice() }) |
| 71 |
|
| 72 |
v2 := NewValueEmpty() |
| 73 |
v.CopyTo(v2) |
| 74 |
assert.Equal(t, v.AsRaw(), v2.AsRaw()) |
| 75 |
assert.Panics(t, func() { v2.CopyTo(v) }) |
| 76 |
} |
| 77 |
|
| 78 |
func TestValueType(t *testing.T) { |
| 79 |
assert.Equal(t, "Empty", ValueTypeEmpty.String()) |
| 80 |
assert.Equal(t, "Str", ValueTypeStr.String()) |
| 81 |
assert.Equal(t, "Bool", ValueTypeBool.String()) |
| 82 |
assert.Equal(t, "Int", ValueTypeInt.String()) |
| 83 |
assert.Equal(t, "Double", ValueTypeDouble.String()) |
| 84 |
assert.Equal(t, "Map", ValueTypeMap.String()) |
| 85 |
assert.Equal(t, "Slice", ValueTypeSlice.String()) |
| 86 |
assert.Equal(t, "Bytes", ValueTypeBytes.String()) |
| 87 |
assert.Empty(t, ValueType(100).String()) |
| 88 |
} |
| 89 |
|
| 90 |
func TestValueMap(t *testing.T) { |
| 91 |
m1 := NewValueMap() |
| 92 |
assert.Equal(t, ValueTypeMap, m1.Type()) |
| 93 |
assert.Equal(t, NewMap(), m1.Map()) |
| 94 |
assert.Equal(t, 0, m1.Map().Len()) |
| 95 |
|
| 96 |
m1.Map().PutDouble("double_key", 123) |
| 97 |
assert.Equal(t, 1, m1.Map().Len()) |
| 98 |
got, exists := m1.Map().Get("double_key") |
| 99 |
assert.True(t, exists) |
| 100 |
assert.Equal(t, NewValueDouble(123), got) |
| 101 |
|
| 102 |
// Create a second map. |
| 103 |
m2 := m1.Map().PutEmptyMap("child_map") |
| 104 |
assert.Equal(t, 0, m2.Len()) |
| 105 |
|
| 106 |
// Modify the source map that was inserted. |
| 107 |
m2.PutStr("key_in_child", "somestr") |
| 108 |
assert.Equal(t, 1, m2.Len()) |
| 109 |
got, exists = m2.Get("key_in_child") |
| 110 |
assert.True(t, exists) |
| 111 |
assert.Equal(t, NewValueStr("somestr"), got) |
| 112 |
|
| 113 |
// Insert the second map as a child. This should perform a deep copy. |
| 114 |
assert.Equal(t, 2, m1.Map().Len()) |
| 115 |
got, exists = m1.Map().Get("double_key") |
| 116 |
assert.True(t, exists) |
| 117 |
assert.Equal(t, NewValueDouble(123), got) |
| 118 |
got, exists = m1.Map().Get("child_map") |
| 119 |
assert.True(t, exists) |
| 120 |
assert.Equal(t, m2, got.Map()) |
| 121 |
|
| 122 |
// Modify the source map m2 that was inserted into m1. |
| 123 |
m2.PutStr("key_in_child", "somestr2") |
| 124 |
assert.Equal(t, 1, m2.Len()) |
| 125 |
got, exists = m2.Get("key_in_child") |
| 126 |
assert.True(t, exists) |
| 127 |
assert.Equal(t, NewValueStr("somestr2"), got) |
| 128 |
|
| 129 |
// The child map inside m1 should be modified. |
| 130 |
childMap, childMapExists := m1.Map().Get("child_map") |
| 131 |
require.True(t, childMapExists) |
| 132 |
got, exists = childMap.Map().Get("key_in_child") |
| 133 |
require.True(t, exists) |
| 134 |
assert.Equal(t, NewValueStr("somestr2"), got) |
| 135 |
|
| 136 |
// Now modify the inserted map (not the source) |
| 137 |
childMap.Map().PutStr("key_in_child", "somestr3") |
| 138 |
assert.Equal(t, 1, childMap.Map().Len()) |
| 139 |
got, exists = childMap.Map().Get("key_in_child") |
| 140 |
require.True(t, exists) |
| 141 |
assert.Equal(t, NewValueStr("somestr3"), got) |
| 142 |
|
| 143 |
// The source child map should be modified. |
| 144 |
got, exists = m2.Get("key_in_child") |
| 145 |
require.True(t, exists) |
| 146 |
assert.Equal(t, NewValueStr("somestr3"), got) |
| 147 |
|
| 148 |
removed := m1.Map().Remove("double_key") |
| 149 |
assert.True(t, removed) |
| 150 |
assert.Equal(t, 1, m1.Map().Len()) |
| 151 |
_, exists = m1.Map().Get("double_key") |
| 152 |
assert.False(t, exists) |
| 153 |
|
| 154 |
removed = m1.Map().Remove("child_map") |
| 155 |
assert.True(t, removed) |
| 156 |
assert.Equal(t, 0, m1.Map().Len()) |
| 157 |
_, exists = m1.Map().Get("child_map") |
| 158 |
assert.False(t, exists) |
| 159 |
|
| 160 |
// Test nil KvlistValue case for MapWrapper() func. |
| 161 |
orig := &internal.AnyValue{Value: &internal.AnyValue_KvlistValue{KvlistValue: nil}} |
| 162 |
m1 = newValue(orig, internal.NewState()) |
| 163 |
assert.Equal(t, Map{}, m1.Map()) |
| 164 |
} |
| 165 |
|
| 166 |
func TestValueSlice(t *testing.T) { |
| 167 |
a1 := NewValueSlice() |
| 168 |
assert.Equal(t, ValueTypeSlice, a1.Type()) |
| 169 |
assert.Equal(t, NewSlice(), a1.Slice()) |
| 170 |
assert.Equal(t, 0, a1.Slice().Len()) |
| 171 |
|
| 172 |
a1.Slice().AppendEmpty().SetDouble(123) |
| 173 |
assert.Equal(t, 1, a1.Slice().Len()) |
| 174 |
assert.Equal(t, NewValueDouble(123), a1.Slice().At(0)) |
| 175 |
// Create a second array. |
| 176 |
a2 := NewValueSlice() |
| 177 |
assert.Equal(t, 0, a2.Slice().Len()) |
| 178 |
|
| 179 |
a2.Slice().AppendEmpty().SetStr("somestr") |
| 180 |
assert.Equal(t, 1, a2.Slice().Len()) |
| 181 |
assert.Equal(t, NewValueStr("somestr"), a2.Slice().At(0)) |
| 182 |
|
| 183 |
// Insert the second array as a child. |
| 184 |
a2.CopyTo(a1.Slice().AppendEmpty()) |
| 185 |
assert.Equal(t, 2, a1.Slice().Len()) |
| 186 |
assert.Equal(t, NewValueDouble(123), a1.Slice().At(0)) |
| 187 |
assert.Equal(t, a2, a1.Slice().At(1)) |
| 188 |
|
| 189 |
// Check that the array was correctly inserted. |
| 190 |
childArray := a1.Slice().At(1) |
| 191 |
assert.Equal(t, ValueTypeSlice, childArray.Type()) |
| 192 |
assert.Equal(t, 1, childArray.Slice().Len()) |
| 193 |
|
| 194 |
v := childArray.Slice().At(0) |
| 195 |
assert.Equal(t, ValueTypeStr, v.Type()) |
| 196 |
assert.Equal(t, "somestr", v.Str()) |
| 197 |
|
| 198 |
// Test nil values case for Slice() func. |
| 199 |
a1 = newValue(&internal.AnyValue{Value: &internal.AnyValue_ArrayValue{ArrayValue: nil}}, internal.NewState()) |
| 200 |
assert.Equal(t, newSlice(nil, nil), a1.Slice()) |
| 201 |
} |
| 202 |
|
| 203 |
func TestNilOrigSetValue(t *testing.T) { |
| 204 |
av := NewValueEmpty() |
| 205 |
av.SetStr("abc") |
| 206 |
assert.Equal(t, "abc", av.Str()) |
| 207 |
|
| 208 |
av = NewValueEmpty() |
| 209 |
av.SetInt(123) |
| 210 |
assert.EqualValues(t, 123, av.Int()) |
| 211 |
|
| 212 |
av = NewValueEmpty() |
| 213 |
av.SetBool(true) |
| 214 |
assert.True(t, av.Bool()) |
| 215 |
|
| 216 |
av = NewValueEmpty() |
| 217 |
av.SetDouble(1.23) |
| 218 |
assert.InDelta(t, 1.23, av.Double(), 0.01) |
| 219 |
|
| 220 |
av = NewValueEmpty() |
| 221 |
av.SetEmptyBytes().FromRaw([]byte{1, 2, 3}) |
| 222 |
assert.Equal(t, []byte{1, 2, 3}, av.Bytes().AsRaw()) |
| 223 |
|
| 224 |
av = NewValueEmpty() |
| 225 |
require.NoError(t, av.SetEmptyMap().FromRaw(map[string]any{"k": "v"})) |
| 226 |
assert.Equal(t, map[string]any{"k": "v"}, av.Map().AsRaw()) |
| 227 |
|
| 228 |
av = NewValueEmpty() |
| 229 |
require.NoError(t, av.SetEmptySlice().FromRaw([]any{int64(1), "val"})) |
| 230 |
assert.Equal(t, []any{int64(1), "val"}, av.Slice().AsRaw()) |
| 231 |
} |
| 232 |
|
| 233 |
func TestValue_MoveTo(t *testing.T) { |
| 234 |
src := NewValueMap() |
| 235 |
src.Map().PutStr("key", "value") |
| 236 |
|
| 237 |
dest := NewValueEmpty() |
| 238 |
assert.True(t, dest.Equal(NewValueEmpty())) |
| 239 |
|
| 240 |
src.MoveTo(dest) |
| 241 |
assert.True(t, src.Equal(NewValueEmpty())) |
| 242 |
|
| 243 |
expected := NewValueMap() |
| 244 |
expected.Map().PutStr("key", "value") |
| 245 |
assert.True(t, dest.Equal(expected)) |
| 246 |
|
| 247 |
dest.MoveTo(dest) |
| 248 |
assert.True(t, dest.Equal(expected)) |
| 249 |
} |
| 250 |
|
| 251 |
func TestValue_CopyTo(t *testing.T) { |
| 252 |
dest := NewValueEmpty() |
| 253 |
orig := internal.GenTestAnyValue() |
| 254 |
newValue(orig, internal.NewState()).CopyTo(dest) |
| 255 |
assert.Equal(t, internal.GenTestAnyValue(), dest.getOrig()) |
| 256 |
} |
| 257 |
|
| 258 |
func TestSliceWithNilValues(t *testing.T) { |
| 259 |
origWithNil := []internal.AnyValue{ |
| 260 |
{}, |
| 261 |
{Value: &internal.AnyValue_StringValue{StringValue: "test_value"}}, |
| 262 |
} |
| 263 |
sm := newSlice(&origWithNil, internal.NewState()) |
| 264 |
|
| 265 |
val := sm.At(0) |
| 266 |
assert.Equal(t, ValueTypeEmpty, val.Type()) |
| 267 |
assert.Empty(t, val.Str()) |
| 268 |
|
| 269 |
val = sm.At(1) |
| 270 |
assert.Equal(t, ValueTypeStr, val.Type()) |
| 271 |
assert.Equal(t, "test_value", val.Str()) |
| 272 |
|
| 273 |
sm.AppendEmpty().SetStr("other_value") |
| 274 |
val = sm.At(2) |
| 275 |
assert.Equal(t, ValueTypeStr, val.Type()) |
| 276 |
assert.Equal(t, "other_value", val.Str()) |
| 277 |
} |
| 278 |
|
| 279 |
func TestValueAsString(t *testing.T) { |
| 280 |
tests := []struct { |
| 281 |
name string |
| 282 |
input Value |
| 283 |
expected string |
| 284 |
}{ |
| 285 |
{ |
| 286 |
name: "string", |
| 287 |
input: NewValueStr("string value"), |
| 288 |
expected: "string value", |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "int64", |
| 292 |
input: NewValueInt(42), |
| 293 |
expected: "42", |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "float64", |
| 297 |
input: NewValueDouble(1.61803399), |
| 298 |
expected: "1.61803399", |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "small float64", |
| 302 |
input: NewValueDouble(.000000009), |
| 303 |
expected: "9e-9", |
| 304 |
}, |
| 305 |
{ |
| 306 |
name: "bad float64", |
| 307 |
input: NewValueDouble(math.Inf(1)), |
| 308 |
expected: "json: unsupported value: +Inf", |
| 309 |
}, |
| 310 |
{ |
| 311 |
name: "boolean", |
| 312 |
input: NewValueBool(true), |
| 313 |
expected: "true", |
| 314 |
}, |
| 315 |
{ |
| 316 |
name: "empty_map", |
| 317 |
input: NewValueMap(), |
| 318 |
expected: "{}", |
| 319 |
}, |
| 320 |
{ |
| 321 |
name: "simple_map", |
| 322 |
input: generateTestValueMap(), |
| 323 |
expected: "{\"arrKey\":[\"strOne\",\"strTwo\"],\"boolKey\":false,\"floatKey\":18.6,\"intKey\":7,\"mapKey\":{\"keyOne\":\"valOne\",\"keyTwo\":\"valTwo\"},\"nullKey\":null,\"strKey\":\"strVal\"}", |
| 324 |
}, |
| 325 |
{ |
| 326 |
name: "empty_array", |
| 327 |
input: NewValueSlice(), |
| 328 |
expected: "[]", |
| 329 |
}, |
| 330 |
{ |
| 331 |
name: "simple_array", |
| 332 |
input: generateTestValueSlice(), |
| 333 |
expected: "[\"strVal\",7,18.6,false,null]", |
| 334 |
}, |
| 335 |
{ |
| 336 |
name: "empty", |
| 337 |
input: NewValueEmpty(), |
| 338 |
expected: "", |
| 339 |
}, |
| 340 |
{ |
| 341 |
name: "bytes", |
| 342 |
input: generateTestValueBytes(), |
| 343 |
expected: base64.StdEncoding.EncodeToString([]byte("String bytes")), |
| 344 |
}, |
| 345 |
} |
| 346 |
for _, tt := range tests { |
| 347 |
t.Run(tt.name, func(t *testing.T) { |
| 348 |
actual := tt.input.AsString() |
| 349 |
assert.Equal(t, tt.expected, actual) |
| 350 |
}) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func TestValueAsRaw(t *testing.T) { |
| 355 |
tests := []struct { |
| 356 |
name string |
| 357 |
input Value |
| 358 |
expected any |
| 359 |
}{ |
| 360 |
{ |
| 361 |
name: "string", |
| 362 |
input: NewValueStr("value"), |
| 363 |
expected: "value", |
| 364 |
}, |
| 365 |
{ |
| 366 |
name: "int", |
| 367 |
input: NewValueInt(11), |
| 368 |
expected: int64(11), |
| 369 |
}, |
| 370 |
{ |
| 371 |
name: "double", |
| 372 |
input: NewValueDouble(1.2), |
| 373 |
expected: 1.2, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "bytes", |
| 377 |
input: generateTestValueBytes(), |
| 378 |
expected: []byte("String bytes"), |
| 379 |
}, |
| 380 |
{ |
| 381 |
name: "empty", |
| 382 |
input: NewValueEmpty(), |
| 383 |
expected: nil, |
| 384 |
}, |
| 385 |
{ |
| 386 |
name: "slice", |
| 387 |
input: generateTestValueSlice(), |
| 388 |
expected: []any{"strVal", int64(7), 18.6, false, nil}, |
| 389 |
}, |
| 390 |
{ |
| 391 |
name: "map", |
| 392 |
input: generateTestValueMap(), |
| 393 |
expected: map[string]any{ |
| 394 |
"mapKey": map[string]any{"keyOne": "valOne", "keyTwo": "valTwo"}, |
| 395 |
"nullKey": nil, |
| 396 |
"strKey": "strVal", |
| 397 |
"arrKey": []any{"strOne", "strTwo"}, |
| 398 |
"boolKey": false, |
| 399 |
"floatKey": 18.6, |
| 400 |
"intKey": int64(7), |
| 401 |
}, |
| 402 |
}, |
| 403 |
} |
| 404 |
for _, tt := range tests { |
| 405 |
t.Run(tt.name, func(t *testing.T) { |
| 406 |
actual := tt.input.AsRaw() |
| 407 |
assert.Equal(t, tt.expected, actual) |
| 408 |
}) |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
func TestNewValueFromRaw(t *testing.T) { |
| 413 |
tests := []struct { |
| 414 |
name string |
| 415 |
input any |
| 416 |
expected Value |
| 417 |
}{ |
| 418 |
{ |
| 419 |
name: "nil", |
| 420 |
input: nil, |
| 421 |
expected: NewValueEmpty(), |
| 422 |
}, |
| 423 |
{ |
| 424 |
name: "string", |
| 425 |
input: "text", |
| 426 |
expected: NewValueStr("text"), |
| 427 |
}, |
| 428 |
{ |
| 429 |
name: "int", |
| 430 |
input: 123, |
| 431 |
expected: NewValueInt(int64(123)), |
| 432 |
}, |
| 433 |
{ |
| 434 |
name: "int8", |
| 435 |
input: int8(12), |
| 436 |
expected: NewValueInt(int64(12)), |
| 437 |
}, |
| 438 |
{ |
| 439 |
name: "int16", |
| 440 |
input: int16(23), |
| 441 |
expected: NewValueInt(int64(23)), |
| 442 |
}, |
| 443 |
{ |
| 444 |
name: "int32", |
| 445 |
input: int32(34), |
| 446 |
expected: NewValueInt(int64(34)), |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "int64", |
| 450 |
input: int64(45), |
| 451 |
expected: NewValueInt(45), |
| 452 |
}, |
| 453 |
{ |
| 454 |
name: "uint", |
| 455 |
input: uint(56), |
| 456 |
expected: NewValueInt(int64(56)), |
| 457 |
}, |
| 458 |
{ |
| 459 |
name: "uint8", |
| 460 |
input: uint8(67), |
| 461 |
expected: NewValueInt(int64(67)), |
| 462 |
}, |
| 463 |
{ |
| 464 |
name: "uint16", |
| 465 |
input: uint16(78), |
| 466 |
expected: NewValueInt(int64(78)), |
| 467 |
}, |
| 468 |
{ |
| 469 |
name: "uint32", |
| 470 |
input: uint32(89), |
| 471 |
expected: NewValueInt(int64(89)), |
| 472 |
}, |
| 473 |
{ |
| 474 |
name: "uint64", |
| 475 |
input: uint64(90), |
| 476 |
expected: NewValueInt(int64(90)), |
| 477 |
}, |
| 478 |
{ |
| 479 |
name: "float32", |
| 480 |
input: float32(1.234), |
| 481 |
expected: NewValueDouble(float64(float32(1.234))), |
| 482 |
}, |
| 483 |
{ |
| 484 |
name: "float64", |
| 485 |
input: float64(2.345), |
| 486 |
expected: NewValueDouble(float64(2.345)), |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "bool", |
| 490 |
input: true, |
| 491 |
expected: NewValueBool(true), |
| 492 |
}, |
| 493 |
{ |
| 494 |
name: "bytes", |
| 495 |
input: []byte{1, 2, 3}, |
| 496 |
expected: func() Value { |
| 497 |
m := NewValueBytes() |
| 498 |
m.Bytes().FromRaw([]byte{1, 2, 3}) |
| 499 |
return m |
| 500 |
}(), |
| 501 |
}, |
| 502 |
{ |
| 503 |
name: "map", |
| 504 |
input: map[string]any{ |
| 505 |
"k": "v", |
| 506 |
}, |
| 507 |
expected: func() Value { |
| 508 |
m := NewValueMap() |
| 509 |
assert.NoError(t, m.Map().FromRaw(map[string]any{"k": "v"})) |
| 510 |
return m |
| 511 |
}(), |
| 512 |
}, |
| 513 |
{ |
| 514 |
name: "empty map", |
| 515 |
input: map[string]any{}, |
| 516 |
expected: func() Value { |
| 517 |
m := NewValueMap() |
| 518 |
assert.NoError(t, m.Map().FromRaw(map[string]any{})) |
| 519 |
return m |
| 520 |
}(), |
| 521 |
}, |
| 522 |
{ |
| 523 |
name: "slice", |
| 524 |
input: []any{"v1", "v2"}, |
| 525 |
expected: (func() Value { |
| 526 |
s := NewValueSlice() |
| 527 |
assert.NoError(t, s.Slice().FromRaw([]any{"v1", "v2"})) |
| 528 |
return s |
| 529 |
})(), |
| 530 |
}, |
| 531 |
{ |
| 532 |
name: "empty slice", |
| 533 |
input: []any{}, |
| 534 |
expected: (func() Value { |
| 535 |
s := NewValueSlice() |
| 536 |
assert.NoError(t, s.Slice().FromRaw([]any{})) |
| 537 |
return s |
| 538 |
})(), |
| 539 |
}, |
| 540 |
} |
| 541 |
for _, tt := range tests { |
| 542 |
t.Run(tt.name, func(t *testing.T) { |
| 543 |
actual := NewValueEmpty() |
| 544 |
require.NoError(t, actual.FromRaw(tt.input)) |
| 545 |
assert.Equal(t, tt.expected, actual) |
| 546 |
}) |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
func TestNewValueFromRawInvalid(t *testing.T) { |
| 551 |
actual := NewValueEmpty() |
| 552 |
assert.EqualError(t, actual.FromRaw(ValueTypeDouble), "<Invalid value type pcommon.ValueType>") |
| 553 |
} |
| 554 |
|
| 555 |
func TestInvalidValue(t *testing.T) { |
| 556 |
v := Value{} |
| 557 |
assert.False(t, v.Bool()) |
| 558 |
assert.Equal(t, int64(0), v.Int()) |
| 559 |
assert.InDelta(t, float64(0), v.Double(), 0.01) |
| 560 |
assert.Empty(t, v.Str()) |
| 561 |
assert.Equal(t, ByteSlice{}, v.Bytes()) |
| 562 |
assert.Equal(t, Map{}, v.Map()) |
| 563 |
assert.Equal(t, Slice{}, v.Slice()) |
| 564 |
assert.Panics(t, func() { v.AsString() }) |
| 565 |
assert.Panics(t, func() { v.AsRaw() }) |
| 566 |
assert.Panics(t, func() { _ = v.FromRaw(1) }) |
| 567 |
assert.Panics(t, func() { v.Type() }) |
| 568 |
assert.Panics(t, func() { v.SetStr("") }) |
| 569 |
assert.Panics(t, func() { v.SetInt(0) }) |
| 570 |
assert.Panics(t, func() { v.SetDouble(0) }) |
| 571 |
assert.Panics(t, func() { v.SetBool(false) }) |
| 572 |
assert.Panics(t, func() { v.SetEmptyBytes() }) |
| 573 |
assert.Panics(t, func() { v.SetEmptyMap() }) |
| 574 |
assert.Panics(t, func() { v.SetEmptySlice() }) |
| 575 |
nv := NewValueEmpty() |
| 576 |
v.CopyTo(nv) |
| 577 |
assert.Nil(t, nv.getOrig().Value) |
| 578 |
} |
| 579 |
|
| 580 |
func TestValueEqual(t *testing.T) { |
| 581 |
for _, tt := range []struct { |
| 582 |
name string |
| 583 |
value Value |
| 584 |
comparison Value |
| 585 |
expected bool |
| 586 |
}{ |
| 587 |
{ |
| 588 |
name: "different types", |
| 589 |
value: NewValueEmpty(), |
| 590 |
comparison: NewValueStr("test"), |
| 591 |
expected: false, |
| 592 |
}, |
| 593 |
{ |
| 594 |
name: "same empty", |
| 595 |
value: NewValueEmpty(), |
| 596 |
comparison: NewValueEmpty(), |
| 597 |
expected: true, |
| 598 |
}, |
| 599 |
{ |
| 600 |
name: "same strings", |
| 601 |
value: NewValueStr("test"), |
| 602 |
comparison: NewValueStr("test"), |
| 603 |
expected: true, |
| 604 |
}, |
| 605 |
{ |
| 606 |
name: "different strings", |
| 607 |
value: NewValueStr("test"), |
| 608 |
comparison: NewValueStr("non-test"), |
| 609 |
expected: false, |
| 610 |
}, |
| 611 |
{ |
| 612 |
name: "same booleans", |
| 613 |
value: NewValueBool(true), |
| 614 |
comparison: NewValueBool(true), |
| 615 |
expected: true, |
| 616 |
}, |
| 617 |
{ |
| 618 |
name: "different booleans", |
| 619 |
value: NewValueBool(true), |
| 620 |
comparison: NewValueBool(false), |
| 621 |
expected: false, |
| 622 |
}, |
| 623 |
{ |
| 624 |
name: "same int", |
| 625 |
value: NewValueInt(42), |
| 626 |
comparison: NewValueInt(42), |
| 627 |
expected: true, |
| 628 |
}, |
| 629 |
{ |
| 630 |
name: "different ints", |
| 631 |
value: NewValueInt(42), |
| 632 |
comparison: NewValueInt(1701), |
| 633 |
expected: false, |
| 634 |
}, |
| 635 |
{ |
| 636 |
name: "same double", |
| 637 |
value: NewValueDouble(13.37), |
| 638 |
comparison: NewValueDouble(13.37), |
| 639 |
expected: true, |
| 640 |
}, |
| 641 |
{ |
| 642 |
name: "different doubles", |
| 643 |
value: NewValueDouble(13.37), |
| 644 |
comparison: NewValueDouble(17.01), |
| 645 |
expected: false, |
| 646 |
}, |
| 647 |
{ |
| 648 |
name: "same byte slice", |
| 649 |
value: func() Value { |
| 650 |
m := NewValueBytes() |
| 651 |
m.Bytes().FromRaw([]byte{1, 3, 3, 7}) |
| 652 |
return m |
| 653 |
}(), |
| 654 |
comparison: func() Value { |
| 655 |
m := NewValueBytes() |
| 656 |
m.Bytes().FromRaw([]byte{1, 3, 3, 7}) |
| 657 |
return m |
| 658 |
}(), |
| 659 |
expected: true, |
| 660 |
}, |
| 661 |
{ |
| 662 |
name: "different byte slice", |
| 663 |
value: func() Value { |
| 664 |
m := NewValueBytes() |
| 665 |
m.Bytes().FromRaw([]byte{1, 3, 3, 7}) |
| 666 |
return m |
| 667 |
}(), |
| 668 |
comparison: func() Value { |
| 669 |
m := NewValueBytes() |
| 670 |
m.Bytes().FromRaw([]byte{1, 7, 0, 1}) |
| 671 |
return m |
| 672 |
}(), |
| 673 |
expected: false, |
| 674 |
}, |
| 675 |
{ |
| 676 |
name: "same slice", |
| 677 |
value: func() Value { |
| 678 |
m := NewValueSlice() |
| 679 |
require.NoError(t, m.Slice().FromRaw([]any{1337})) |
| 680 |
return m |
| 681 |
}(), |
| 682 |
comparison: func() Value { |
| 683 |
m := NewValueSlice() |
| 684 |
require.NoError(t, m.Slice().FromRaw([]any{1337})) |
| 685 |
return m |
| 686 |
}(), |
| 687 |
expected: true, |
| 688 |
}, |
| 689 |
{ |
| 690 |
name: "different slice", |
| 691 |
value: func() Value { |
| 692 |
m := NewValueSlice() |
| 693 |
require.NoError(t, m.Slice().FromRaw([]any{1337})) |
| 694 |
return m |
| 695 |
}(), |
| 696 |
comparison: func() Value { |
| 697 |
m := NewValueSlice() |
| 698 |
require.NoError(t, m.Slice().FromRaw([]any{1701})) |
| 699 |
return m |
| 700 |
}(), |
| 701 |
expected: false, |
| 702 |
}, |
| 703 |
{ |
| 704 |
name: "same map", |
| 705 |
value: func() Value { |
| 706 |
m := NewValueMap() |
| 707 |
m.Map().PutStr("hello", "world") |
| 708 |
return m |
| 709 |
}(), |
| 710 |
comparison: func() Value { |
| 711 |
m := NewValueMap() |
| 712 |
m.Map().PutStr("hello", "world") |
| 713 |
return m |
| 714 |
}(), |
| 715 |
expected: true, |
| 716 |
}, |
| 717 |
{ |
| 718 |
name: "different maps", |
| 719 |
value: func() Value { |
| 720 |
m := NewValueMap() |
| 721 |
m.Map().PutStr("hello", "world") |
| 722 |
return m |
| 723 |
}(), |
| 724 |
comparison: func() Value { |
| 725 |
m := NewValueMap() |
| 726 |
m.Map().PutStr("bonjour", "monde") |
| 727 |
return m |
| 728 |
}(), |
| 729 |
expected: false, |
| 730 |
}, |
| 731 |
} { |
| 732 |
t.Run(tt.name, func(t *testing.T) { |
| 733 |
assert.Equal(t, tt.expected, tt.value.Equal(tt.comparison)) |
| 734 |
}) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
func BenchmarkValueEqual(b *testing.B) { |
| 739 |
testutil.SkipMemoryBench(b) |
| 740 |
|
| 741 |
for _, bb := range []struct { |
| 742 |
name string |
| 743 |
value Value |
| 744 |
comparison Value |
| 745 |
}{ |
| 746 |
{ |
| 747 |
name: "nil", |
| 748 |
value: NewValueEmpty(), |
| 749 |
comparison: NewValueEmpty(), |
| 750 |
}, |
| 751 |
{ |
| 752 |
name: "strings", |
| 753 |
value: NewValueStr("test"), |
| 754 |
comparison: NewValueStr("test"), |
| 755 |
}, |
| 756 |
{ |
| 757 |
name: "booleans", |
| 758 |
value: NewValueBool(true), |
| 759 |
comparison: NewValueBool(true), |
| 760 |
}, |
| 761 |
{ |
| 762 |
name: "ints", |
| 763 |
value: NewValueInt(42), |
| 764 |
comparison: NewValueInt(42), |
| 765 |
}, |
| 766 |
{ |
| 767 |
name: "doubles", |
| 768 |
value: NewValueDouble(13.37), |
| 769 |
comparison: NewValueDouble(13.37), |
| 770 |
}, |
| 771 |
{ |
| 772 |
name: "byte slices", |
| 773 |
value: func() Value { |
| 774 |
m := NewValueBytes() |
| 775 |
m.Bytes().FromRaw([]byte{1, 3, 3, 7}) |
| 776 |
return m |
| 777 |
}(), |
| 778 |
comparison: func() Value { |
| 779 |
m := NewValueBytes() |
| 780 |
m.Bytes().FromRaw([]byte{1, 3, 3, 7}) |
| 781 |
return m |
| 782 |
}(), |
| 783 |
}, |
| 784 |
{ |
| 785 |
name: "slices", |
| 786 |
value: func() Value { |
| 787 |
m := NewValueSlice() |
| 788 |
require.NoError(b, m.Slice().FromRaw([]any{1337})) |
| 789 |
return m |
| 790 |
}(), |
| 791 |
comparison: func() Value { |
| 792 |
m := NewValueSlice() |
| 793 |
require.NoError(b, m.Slice().FromRaw([]any{1337})) |
| 794 |
return m |
| 795 |
}(), |
| 796 |
}, |
| 797 |
{ |
| 798 |
name: "maps", |
| 799 |
value: func() Value { |
| 800 |
m := NewValueMap() |
| 801 |
m.Map().PutStr("hello", "world") |
| 802 |
return m |
| 803 |
}(), |
| 804 |
comparison: func() Value { |
| 805 |
m := NewValueMap() |
| 806 |
m.Map().PutStr("hello", "world") |
| 807 |
return m |
| 808 |
}(), |
| 809 |
}, |
| 810 |
} { |
| 811 |
b.Run(bb.name, func(b *testing.B) { |
| 812 |
b.ResetTimer() |
| 813 |
b.ReportAllocs() |
| 814 |
|
| 815 |
for b.Loop() { |
| 816 |
_ = bb.value.Equal(bb.comparison) |
| 817 |
} |
| 818 |
}) |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
func generateTestValueMap() Value { |
| 823 |
ret := NewValueMap() |
| 824 |
attrMap := ret.Map() |
| 825 |
attrMap.PutStr("strKey", "strVal") |
| 826 |
attrMap.PutInt("intKey", 7) |
| 827 |
attrMap.PutDouble("floatKey", 18.6) |
| 828 |
attrMap.PutBool("boolKey", false) |
| 829 |
attrMap.PutEmpty("nullKey") |
| 830 |
|
| 831 |
m := attrMap.PutEmptyMap("mapKey") |
| 832 |
m.PutStr("keyOne", "valOne") |
| 833 |
m.PutStr("keyTwo", "valTwo") |
| 834 |
|
| 835 |
s := attrMap.PutEmptySlice("arrKey") |
| 836 |
s.AppendEmpty().SetStr("strOne") |
| 837 |
s.AppendEmpty().SetStr("strTwo") |
| 838 |
|
| 839 |
return ret |
| 840 |
} |
| 841 |
|
| 842 |
func generateTestValueSlice() Value { |
| 843 |
ret := NewValueSlice() |
| 844 |
attrArr := ret.Slice() |
| 845 |
attrArr.AppendEmpty().SetStr("strVal") |
| 846 |
attrArr.AppendEmpty().SetInt(7) |
| 847 |
attrArr.AppendEmpty().SetDouble(18.6) |
| 848 |
attrArr.AppendEmpty().SetBool(false) |
| 849 |
attrArr.AppendEmpty() |
| 850 |
return ret |
| 851 |
} |
| 852 |
|
| 853 |
func generateTestValueBytes() Value { |
| 854 |
v := NewValueBytes() |
| 855 |
v.Bytes().FromRaw([]byte("String bytes")) |
| 856 |
return v |
| 857 |
} |