aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/go.opentelemetry.io/otel/attribute/iterator_test.go
blob: a40432aaacc0b18f2b4211d7f9304f71875a3c3a (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package attribute_test

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/otel/attribute"
)

func TestIterator(t *testing.T) {
	one := attribute.String("one", "1")
	two := attribute.Int("two", 2)
	lbl := attribute.NewSet(one, two)
	iter := lbl.Iter()
	require.Equal(t, 2, iter.Len())

	require.True(t, iter.Next())
	require.Equal(t, one, iter.Attribute())
	idx, attr := iter.IndexedAttribute()
	require.Equal(t, 0, idx)
	require.Equal(t, one, attr)
	require.Equal(t, 2, iter.Len())

	require.True(t, iter.Next())
	require.Equal(t, two, iter.Attribute())
	idx, attr = iter.IndexedAttribute()
	require.Equal(t, 1, idx)
	require.Equal(t, two, attr)
	require.Equal(t, 2, iter.Len())

	require.False(t, iter.Next())
	require.Equal(t, 2, iter.Len())
}

func TestEmptyIterator(t *testing.T) {
	lbl := attribute.NewSet()
	iter := lbl.Iter()
	require.Equal(t, 0, iter.Len())
	require.False(t, iter.Next())
}

func TestMergedIterator(t *testing.T) {
	type inputs struct {
		name   string
		keys1  []string
		keys2  []string
		expect []string
	}

	makeAttributes := func(keys []string, num int) (result []attribute.KeyValue) {
		for _, k := range keys {
			result = append(result, attribute.Int(k, num))
		}
		return
	}

	for _, input := range []inputs{
		{
			name:   "one overlap",
			keys1:  []string{"A", "B"},
			keys2:  []string{"B", "C"},
			expect: []string{"A/1", "B/1", "C/2"},
		},
		{
			name:   "reversed one overlap",
			keys1:  []string{"B", "A"},
			keys2:  []string{"C", "B"},
			expect: []string{"A/1", "B/1", "C/2"},
		},
		{
			name:   "one empty",
			keys1:  nil,
			keys2:  []string{"C", "B"},
			expect: []string{"B/2", "C/2"},
		},
		{
			name:   "two empty",
			keys1:  []string{"C", "B"},
			keys2:  nil,
			expect: []string{"B/1", "C/1"},
		},
		{
			name:   "no overlap both",
			keys1:  []string{"C"},
			keys2:  []string{"B"},
			expect: []string{"B/2", "C/1"},
		},
		{
			name:   "one empty single two",
			keys1:  nil,
			keys2:  []string{"B"},
			expect: []string{"B/2"},
		},
		{
			name:   "two empty single one",
			keys1:  []string{"A"},
			keys2:  nil,
			expect: []string{"A/1"},
		},
		{
			name:   "all empty",
			keys1:  nil,
			keys2:  nil,
			expect: nil,
		},
		{
			name:   "full overlap",
			keys1:  []string{"A", "B", "C", "D"},
			keys2:  []string{"A", "B", "C", "D"},
			expect: []string{"A/1", "B/1", "C/1", "D/1"},
		},
	} {
		t.Run(input.name, func(t *testing.T) {
			attr1 := makeAttributes(input.keys1, 1)
			attr2 := makeAttributes(input.keys2, 2)

			set1 := attribute.NewSet(attr1...)
			set2 := attribute.NewSet(attr2...)

			merge := attribute.NewMergeIterator(&set1, &set2)

			var result []string

			for merge.Next() {
				attr := merge.Attribute()
				result = append(result, fmt.Sprint(attr.Key, "/", attr.Value.Emit()))
			}

			require.Equal(t, input.expect, result)
		})
	}
}