| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package configtelemetry |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
_ encoding.TextMarshaler = (*Level)(nil) |
| 16 |
_ encoding.TextUnmarshaler = (*Level)(nil) |
| 17 |
) |
| 18 |
|
| 19 |
func TestUnmarshalText(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
str []string |
| 22 |
level Level |
| 23 |
err bool |
| 24 |
}{ |
| 25 |
{ |
| 26 |
str: []string{"", "other_string"}, |
| 27 |
level: LevelNone, |
| 28 |
err: true, |
| 29 |
}, |
| 30 |
{ |
| 31 |
str: []string{"none", "None", "NONE"}, |
| 32 |
level: LevelNone, |
| 33 |
}, |
| 34 |
{ |
| 35 |
str: []string{"basic", "Basic", "BASIC"}, |
| 36 |
level: LevelBasic, |
| 37 |
}, |
| 38 |
{ |
| 39 |
str: []string{"normal", "Normal", "NORMAL"}, |
| 40 |
level: LevelNormal, |
| 41 |
}, |
| 42 |
{ |
| 43 |
str: []string{"detailed", "Detailed", "DETAILED"}, |
| 44 |
level: LevelDetailed, |
| 45 |
}, |
| 46 |
} |
| 47 |
|
| 48 |
for _, test := range tests { |
| 49 |
for _, str := range test.str { |
| 50 |
t.Run(str, func(t *testing.T) { |
| 51 |
var lvl Level |
| 52 |
err := lvl.UnmarshalText([]byte(str)) |
| 53 |
if test.err { |
| 54 |
assert.Error(t, err) |
| 55 |
} else { |
| 56 |
require.NoError(t, err) |
| 57 |
assert.Equal(t, test.level, lvl) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestUnmarshalTextNilLevel(t *testing.T) { |
| 65 |
lvl := (*Level)(nil) |
| 66 |
assert.Error(t, lvl.UnmarshalText([]byte(levelNormalStr))) |
| 67 |
} |
| 68 |
|
| 69 |
func TestLevelStringMarshal(t *testing.T) { |
| 70 |
tests := []struct { |
| 71 |
str string |
| 72 |
level Level |
| 73 |
err bool |
| 74 |
}{ |
| 75 |
{ |
| 76 |
str: "", |
| 77 |
level: Level(-10), |
| 78 |
}, |
| 79 |
{ |
| 80 |
str: levelNoneStr, |
| 81 |
level: LevelNone, |
| 82 |
}, |
| 83 |
{ |
| 84 |
str: levelBasicStr, |
| 85 |
level: LevelBasic, |
| 86 |
}, |
| 87 |
{ |
| 88 |
str: levelNormalStr, |
| 89 |
level: LevelNormal, |
| 90 |
}, |
| 91 |
{ |
| 92 |
str: levelDetailedStr, |
| 93 |
level: LevelDetailed, |
| 94 |
}, |
| 95 |
} |
| 96 |
for _, tt := range tests { |
| 97 |
t.Run(tt.str, func(t *testing.T) { |
| 98 |
assert.Equal(t, tt.str, tt.level.String()) |
| 99 |
got, err := tt.level.MarshalText() |
| 100 |
require.NoError(t, err) |
| 101 |
assert.Equal(t, tt.str, string(got)) |
| 102 |
}) |
| 103 |
} |
| 104 |
} |