value_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "github.com/stretchr/testify/assert"
  19. "go.opentelemetry.io/otel/attribute"
  20. )
  21. func TestValue(t *testing.T) {
  22. k := attribute.Key("test")
  23. for _, testcase := range []struct {
  24. name string
  25. value attribute.Value
  26. wantType attribute.Type
  27. wantValue interface{}
  28. }{
  29. {
  30. name: "Key.Bool() correctly returns keys's internal bool value",
  31. value: k.Bool(true).Value,
  32. wantType: attribute.BOOL,
  33. wantValue: true,
  34. },
  35. {
  36. name: "Key.BoolSlice() correctly returns keys's internal []bool value",
  37. value: k.BoolSlice([]bool{true, false, true}).Value,
  38. wantType: attribute.BOOLSLICE,
  39. wantValue: []bool{true, false, true},
  40. },
  41. {
  42. name: "Key.Int64() correctly returns keys's internal int64 value",
  43. value: k.Int64(42).Value,
  44. wantType: attribute.INT64,
  45. wantValue: int64(42),
  46. },
  47. {
  48. name: "Key.Int64Slice() correctly returns keys's internal []int64 value",
  49. value: k.Int64Slice([]int64{42, -3, 12}).Value,
  50. wantType: attribute.INT64SLICE,
  51. wantValue: []int64{42, -3, 12},
  52. },
  53. {
  54. name: "Key.Int() correctly returns keys's internal signed integral value",
  55. value: k.Int(42).Value,
  56. wantType: attribute.INT64,
  57. wantValue: int64(42),
  58. },
  59. {
  60. name: "Key.IntSlice() correctly returns keys's internal []int64 value",
  61. value: k.IntSlice([]int{42, -3, 12}).Value,
  62. wantType: attribute.INT64SLICE,
  63. wantValue: []int64{42, -3, 12},
  64. },
  65. {
  66. name: "Key.Float64() correctly returns keys's internal float64 value",
  67. value: k.Float64(42.1).Value,
  68. wantType: attribute.FLOAT64,
  69. wantValue: 42.1,
  70. },
  71. {
  72. name: "Key.Float64Slice() correctly returns keys's internal []float64 value",
  73. value: k.Float64Slice([]float64{42, -3, 12}).Value,
  74. wantType: attribute.FLOAT64SLICE,
  75. wantValue: []float64{42, -3, 12},
  76. },
  77. {
  78. name: "Key.String() correctly returns keys's internal string value",
  79. value: k.String("foo").Value,
  80. wantType: attribute.STRING,
  81. wantValue: "foo",
  82. },
  83. {
  84. name: "Key.StringSlice() correctly returns keys's internal []string value",
  85. value: k.StringSlice([]string{"forty-two", "negative three", "twelve"}).Value,
  86. wantType: attribute.STRINGSLICE,
  87. wantValue: []string{"forty-two", "negative three", "twelve"},
  88. },
  89. } {
  90. t.Logf("Running test case %s", testcase.name)
  91. if testcase.value.Type() != testcase.wantType {
  92. t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
  93. }
  94. if testcase.wantType == attribute.INVALID {
  95. continue
  96. }
  97. got := testcase.value.AsInterface()
  98. if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
  99. t.Errorf("+got, -want: %s", diff)
  100. }
  101. }
  102. }
  103. func TestSetComparability(t *testing.T) {
  104. pairs := [][2]attribute.KeyValue{
  105. {
  106. attribute.Bool("Bool", true),
  107. attribute.Bool("Bool", true),
  108. },
  109. {
  110. attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
  111. attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
  112. },
  113. {
  114. attribute.Int("Int", 34),
  115. attribute.Int("Int", 34),
  116. },
  117. {
  118. attribute.IntSlice("IntSlice", []int{312, 1, -2}),
  119. attribute.IntSlice("IntSlice", []int{312, 1, -2}),
  120. },
  121. {
  122. attribute.Int64("Int64", 98),
  123. attribute.Int64("Int64", 98),
  124. },
  125. {
  126. attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
  127. attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
  128. },
  129. {
  130. attribute.Float64("Float64", 19.09),
  131. attribute.Float64("Float64", 19.09),
  132. },
  133. {
  134. attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
  135. attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
  136. },
  137. {
  138. attribute.String("String", "string value"),
  139. attribute.String("String", "string value"),
  140. },
  141. {
  142. attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
  143. attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
  144. },
  145. }
  146. for _, p := range pairs {
  147. s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
  148. m := map[attribute.Set]struct{}{s0: {}}
  149. _, ok := m[s1]
  150. assert.Truef(t, ok, "%s not comparable", p[0].Value.Type())
  151. }
  152. }
  153. func TestAsSlice(t *testing.T) {
  154. bs1 := []bool{true, false, true}
  155. kv := attribute.BoolSlice("BoolSlice", bs1)
  156. bs2 := kv.Value.AsBoolSlice()
  157. assert.Equal(t, bs1, bs2)
  158. i64s1 := []int64{12, 1298, -219, 2}
  159. kv = attribute.Int64Slice("Int64Slice", i64s1)
  160. i64s2 := kv.Value.AsInt64Slice()
  161. assert.Equal(t, i64s1, i64s2)
  162. is1 := []int{12, 1298, -219, 2}
  163. kv = attribute.IntSlice("IntSlice", is1)
  164. i64s2 = kv.Value.AsInt64Slice()
  165. assert.Equal(t, i64s1, i64s2)
  166. fs1 := []float64{12398.1, -37.1713873737, 3}
  167. kv = attribute.Float64Slice("Float64Slice", fs1)
  168. fs2 := kv.Value.AsFloat64Slice()
  169. assert.Equal(t, fs1, fs2)
  170. ss1 := []string{"one", "two", "three"}
  171. kv = attribute.StringSlice("StringSlice", ss1)
  172. ss2 := kv.Value.AsStringSlice()
  173. assert.Equal(t, ss1, ss2)
  174. }