-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces a new test file, func_is_bool_test.go, within the pkg/ottl/ottlfuncs directory. The test file contains comprehensive unit tests for the IsBool converter, ensuring its accuracy in type validation for various input value scenarios. Specifically, the tests cover: - Native boolean values - pcommon.ValueTypeBool objects - Incorrect types such as integers and slices - Error handling for type errors
- Loading branch information
1 parent
2c4135b
commit 703b390
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_IsBool(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
expected bool | ||
}{ | ||
{ | ||
name: "bool", | ||
value: true, | ||
expected: true, | ||
}, | ||
{ | ||
name: "ValueTypeBool", | ||
value: pcommon.NewValueBool(false), | ||
expected: true, | ||
}, | ||
{ | ||
name: "not bool", | ||
value: 1, | ||
expected: false, | ||
}, | ||
{ | ||
name: "ValueTypeSlice", | ||
value: pcommon.NewValueSlice(), | ||
expected: false, | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc := isBool[any](&ottl.StandardBoolGetter[any]{ | ||
Getter: func(context.Context, interface{}) (interface{}, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
// nolint:errorlint | ||
func Test_IsBool_Error(t *testing.T) { | ||
exprFunc := isBool[any](&ottl.StandardBoolGetter[any]{ | ||
Getter: func(context.Context, interface{}) (interface{}, error) { | ||
return nil, ottl.TypeError("") | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.Equal(t, false, result) | ||
assert.Error(t, err) | ||
_, ok := err.(ottl.TypeError) | ||
assert.False(t, ok) | ||
} |