|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "math" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_ParseInt(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + target ottl.StringGetter[any] |
| 21 | + base ottl.IntGetter[any] |
| 22 | + expected any |
| 23 | + err bool |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "string in base 10", |
| 27 | + target: &ottl.StandardStringGetter[any]{ |
| 28 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 29 | + return "123456789", nil |
| 30 | + }, |
| 31 | + }, |
| 32 | + base: &ottl.StandardIntGetter[any]{ |
| 33 | + Getter: func(context.Context, any) (any, error) { |
| 34 | + return int64(10), nil |
| 35 | + }, |
| 36 | + }, |
| 37 | + expected: int64(123456789), |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "string in base 2", |
| 41 | + target: &ottl.StandardStringGetter[any]{ |
| 42 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 43 | + return "01010101", nil |
| 44 | + }, |
| 45 | + }, |
| 46 | + base: &ottl.StandardIntGetter[any]{ |
| 47 | + Getter: func(context.Context, any) (any, error) { |
| 48 | + return int64(2), nil |
| 49 | + }, |
| 50 | + }, |
| 51 | + expected: int64(85), |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "an out of range string", |
| 55 | + target: &ottl.StandardStringGetter[any]{ |
| 56 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 57 | + return fmt.Sprintf("%d", uint64(math.MaxUint64)), nil |
| 58 | + }, |
| 59 | + }, |
| 60 | + base: &ottl.StandardIntGetter[any]{ |
| 61 | + Getter: func(context.Context, any) (any, error) { |
| 62 | + return int64(10), nil |
| 63 | + }, |
| 64 | + }, |
| 65 | + expected: nil, |
| 66 | + err: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "string in base 16", |
| 70 | + target: &ottl.StandardStringGetter[any]{ |
| 71 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 72 | + return "AF", nil |
| 73 | + }, |
| 74 | + }, |
| 75 | + base: &ottl.StandardIntGetter[any]{ |
| 76 | + Getter: func(context.Context, any) (any, error) { |
| 77 | + return int64(16), nil |
| 78 | + }, |
| 79 | + }, |
| 80 | + expected: int64(175), |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "string in base 16 with 0x prefix", |
| 84 | + target: &ottl.StandardStringGetter[any]{ |
| 85 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 86 | + return "0XAF", nil |
| 87 | + }, |
| 88 | + }, |
| 89 | + base: &ottl.StandardIntGetter[any]{ |
| 90 | + Getter: func(context.Context, any) (any, error) { |
| 91 | + return int64(0), nil |
| 92 | + }, |
| 93 | + }, |
| 94 | + expected: int64(175), |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "not a number string", |
| 98 | + target: &ottl.StandardStringGetter[any]{ |
| 99 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 100 | + return "test", nil |
| 101 | + }, |
| 102 | + }, |
| 103 | + base: &ottl.StandardIntGetter[any]{ |
| 104 | + Getter: func(context.Context, any) (any, error) { |
| 105 | + return int64(10), nil |
| 106 | + }, |
| 107 | + }, |
| 108 | + expected: nil, |
| 109 | + err: true, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "empty string", |
| 113 | + target: &ottl.StandardStringGetter[any]{ |
| 114 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 115 | + return "", nil |
| 116 | + }, |
| 117 | + }, |
| 118 | + base: &ottl.StandardIntGetter[any]{ |
| 119 | + Getter: func(context.Context, any) (any, error) { |
| 120 | + return int64(10), nil |
| 121 | + }, |
| 122 | + }, |
| 123 | + expected: nil, |
| 124 | + err: true, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "negative base value", |
| 128 | + target: &ottl.StandardStringGetter[any]{ |
| 129 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 130 | + return "12345", nil |
| 131 | + }, |
| 132 | + }, |
| 133 | + base: &ottl.StandardIntGetter[any]{ |
| 134 | + Getter: func(context.Context, any) (any, error) { |
| 135 | + return int64(-10), nil |
| 136 | + }, |
| 137 | + }, |
| 138 | + expected: nil, |
| 139 | + err: true, |
| 140 | + }, |
| 141 | + } |
| 142 | + for _, tt := range tests { |
| 143 | + t.Run(tt.name, func(t *testing.T) { |
| 144 | + exprFunc := parseIntFunc[any](tt.target, tt.base) |
| 145 | + result, err := exprFunc(nil, nil) |
| 146 | + if tt.err { |
| 147 | + assert.Error(t, err) |
| 148 | + } else { |
| 149 | + assert.NoError(t, err) |
| 150 | + } |
| 151 | + assert.Equal(t, tt.expected, result) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments