Skip to content

Commit

Permalink
Add unit tests for IsBool converter
Browse files Browse the repository at this point in the history
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
Dennis40816 committed Nov 12, 2023
1 parent 2c4135b commit 703b390
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_bool_test.go
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)
}

0 comments on commit 703b390

Please sign in to comment.