key_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package attribute_test
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/stretchr/testify/require"
  19. "go.opentelemetry.io/otel/attribute"
  20. )
  21. func TestDefined(t *testing.T) {
  22. for _, testcase := range []struct {
  23. name string
  24. k attribute.Key
  25. want bool
  26. }{
  27. {
  28. name: "Key.Defined() returns true when len(v.Name) != 0",
  29. k: attribute.Key("foo"),
  30. want: true,
  31. },
  32. {
  33. name: "Key.Defined() returns false when len(v.Name) == 0",
  34. k: attribute.Key(""),
  35. want: false,
  36. },
  37. } {
  38. t.Run(testcase.name, func(t *testing.T) {
  39. //func (k attribute.Key) Defined() bool {
  40. have := testcase.k.Defined()
  41. if have != testcase.want {
  42. t.Errorf("Want: %v, but have: %v", testcase.want, have)
  43. }
  44. })
  45. }
  46. }
  47. func TestJSONValue(t *testing.T) {
  48. var kvs interface{} = [2]attribute.KeyValue{
  49. attribute.String("A", "B"),
  50. attribute.Int64("C", 1),
  51. }
  52. data, err := json.Marshal(kvs)
  53. require.NoError(t, err)
  54. require.Equal(t,
  55. `[{"Key":"A","Value":{"Type":"STRING","Value":"B"}},{"Key":"C","Value":{"Type":"INT64","Value":1}}]`,
  56. string(data))
  57. }
  58. func TestEmit(t *testing.T) {
  59. for _, testcase := range []struct {
  60. name string
  61. v attribute.Value
  62. want string
  63. }{
  64. {
  65. name: `test Key.Emit() can emit a string representing self.BOOL`,
  66. v: attribute.BoolValue(true),
  67. want: "true",
  68. },
  69. {
  70. name: `test Key.Emit() can emit a string representing self.INT64`,
  71. v: attribute.Int64Value(42),
  72. want: "42",
  73. },
  74. {
  75. name: `test Key.Emit() can emit a string representing self.FLOAT64`,
  76. v: attribute.Float64Value(42.1),
  77. want: "42.1",
  78. },
  79. {
  80. name: `test Key.Emit() can emit a string representing self.STRING`,
  81. v: attribute.StringValue("foo"),
  82. want: "foo",
  83. },
  84. } {
  85. t.Run(testcase.name, func(t *testing.T) {
  86. //proto: func (v attribute.Value) Emit() string {
  87. have := testcase.v.Emit()
  88. if have != testcase.want {
  89. t.Errorf("Want: %s, but have: %s", testcase.want, have)
  90. }
  91. })
  92. }
  93. }