-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
identifiable_test.go
75 lines (68 loc) · 1.52 KB
/
identifiable_test.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package component
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMarshalText(t *testing.T) {
id := NewIDWithName("test", "name")
got, err := id.MarshalText()
assert.NoError(t, err)
assert.Equal(t, id.String(), string(got))
}
func TestUnmarshalText(t *testing.T) {
var testCases = []struct {
idStr string
expectedErr bool
expectedID ID
}{
{
idStr: "valid_type",
expectedID: ID{typeVal: "valid_type", nameVal: ""},
},
{
idStr: "valid_type/valid_name",
expectedID: ID{typeVal: "valid_type", nameVal: "valid_name"},
},
{
idStr: " valid_type / valid_name ",
expectedID: ID{typeVal: "valid_type", nameVal: "valid_name"},
},
{
idStr: "/valid_name",
expectedErr: true,
},
{
idStr: " /valid_name",
expectedErr: true,
},
{
idStr: "valid_type/",
expectedErr: true,
},
{
idStr: "valid_type/ ",
expectedErr: true,
},
{
idStr: " ",
expectedErr: true,
},
}
for _, test := range testCases {
t.Run(test.idStr, func(t *testing.T) {
id := ID{}
err := id.UnmarshalText([]byte(test.idStr))
if test.expectedErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, test.expectedID, id)
assert.Equal(t, test.expectedID.Type(), id.Type())
assert.Equal(t, test.expectedID.Name(), id.Name())
assert.Equal(t, test.expectedID.String(), id.String())
})
}
}