|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 13 | + |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_TrimPrefix(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + target any |
| 21 | + prefix ottl.StringGetter[any] |
| 22 | + expected string |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "has prefix true", |
| 26 | + target: "hello world", |
| 27 | + prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "hello ", nil }}, |
| 28 | + expected: "world", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "has prefix false", |
| 32 | + target: "hello world", |
| 33 | + prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return " world", nil }}, |
| 34 | + expected: "hello world", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "target pcommon.Value", |
| 38 | + target: pcommon.NewValueStr("hello world"), |
| 39 | + prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "hello", nil }}, |
| 40 | + expected: " world", |
| 41 | + }, |
| 42 | + } |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + factory := NewTrimPrefixFactory[any]() |
| 46 | + exprFunc, err := factory.CreateFunction( |
| 47 | + ottl.FunctionContext{}, |
| 48 | + &TrimPrefixArguments[any]{ |
| 49 | + Target: ottl.StandardStringGetter[any]{ |
| 50 | + Getter: func(context.Context, any) (any, error) { |
| 51 | + return tt.target, nil |
| 52 | + }, |
| 53 | + }, |
| 54 | + Prefix: tt.prefix, |
| 55 | + }) |
| 56 | + assert.NoError(t, err) |
| 57 | + result, err := exprFunc(t.Context(), nil) |
| 58 | + require.NoError(t, err) |
| 59 | + assert.Equal(t, tt.expected, result) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func Test_TrimPrefix_Error(t *testing.T) { |
| 65 | + target := &ottl.StandardStringGetter[any]{ |
| 66 | + Getter: func(context.Context, any) (any, error) { |
| 67 | + return true, nil |
| 68 | + }, |
| 69 | + } |
| 70 | + prefix := &ottl.StandardStringGetter[any]{ |
| 71 | + Getter: func(context.Context, any) (any, error) { |
| 72 | + return "test", nil |
| 73 | + }, |
| 74 | + } |
| 75 | + exprFunc := trimPrefix[any](target, prefix) |
| 76 | + _, err := exprFunc(t.Context(), nil) |
| 77 | + require.Error(t, err) |
| 78 | +} |
| 79 | + |
| 80 | +func Test_TrimPrefix_Error_prefix(t *testing.T) { |
| 81 | + target := &ottl.StandardStringGetter[any]{ |
| 82 | + Getter: func(context.Context, any) (any, error) { |
| 83 | + return true, nil |
| 84 | + }, |
| 85 | + } |
| 86 | + prefix := &ottl.StandardStringGetter[any]{ |
| 87 | + Getter: func(context.Context, any) (any, error) { |
| 88 | + return true, nil |
| 89 | + }, |
| 90 | + } |
| 91 | + exprFunc := trimPrefix[any](target, prefix) |
| 92 | + _, err := exprFunc(t.Context(), nil) |
| 93 | + require.Error(t, err) |
| 94 | +} |
0 commit comments