Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Jun 9, 2023
1 parent 03d0e3a commit 4b4e7d0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
84 changes: 84 additions & 0 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,18 @@ func Test_StandardStringGetter(t *testing.T) {
}
}

func Test_StandardStringGetter_WrappedError(t *testing.T) {
getter := StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardStringLikeGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -810,6 +822,18 @@ func Test_StandardStringLikeGetter(t *testing.T) {
}
}

func Test_StandardStringLikeGetter_WrappedError(t *testing.T) {
getter := StandardStringLikeGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardFloatGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -874,6 +898,18 @@ func Test_StandardFloatGetter(t *testing.T) {
}
}

func Test_StandardFloatGetter_WrappedError(t *testing.T) {
getter := StandardFloatGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardFloatLikeGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1038,6 +1074,18 @@ func Test_StandardFloatLikeGetter(t *testing.T) {
}
}

func Test_StandardFloatLikeGetter_WrappedError(t *testing.T) {
getter := StandardFloatLikeGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardIntGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1102,6 +1150,18 @@ func Test_StandardIntGetter(t *testing.T) {
}
}

func Test_StandardIntGetter_WrappedError(t *testing.T) {
getter := StandardIntGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardIntLikeGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1266,6 +1326,18 @@ func Test_StandardIntLikeGetter(t *testing.T) {
}
}

func Test_StandardIntLikeGetter_WrappedError(t *testing.T) {
getter := StandardIntLikeGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}

func Test_StandardPMapGetter(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1339,3 +1411,15 @@ func Test_StandardPMapGetter(t *testing.T) {
})
}
}

func Test_StandardPMapGetter_WrappedError(t *testing.T) {
getter := StandardPMapGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", TypeError(""))
},
}
_, err := getter.Get(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(TypeError)
assert.False(t, ok)
}
13 changes: 13 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ottlfuncs

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,3 +59,15 @@ func Test_IsMap(t *testing.T) {
})
}
}

func Test_IsMap_Error(t *testing.T) {
exprFunc := isMap[any](&ottl.StandardPMapGetter[any]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", ottl.TypeError(""))
},
})
_, err := exprFunc(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(ottl.TypeError)
assert.False(t, ok)
}
4 changes: 2 additions & 2 deletions pkg/ottl/ottlfuncs/func_is_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func createIsStringFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments)
return nil, fmt.Errorf("IsStringFactory args must be of type *IsStringArguments[K]")
}

return isStringFunc(args.Target), nil
return isString(args.Target), nil
}

// nolint:errorlint
func isStringFunc[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
func isString[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (interface{}, error) {
_, err := target.Get(ctx, tCtx)
// Use type assertion because we don't want to check wrapped errors
Expand Down
15 changes: 14 additions & 1 deletion pkg/ottl/ottlfuncs/func_is_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ottlfuncs

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,7 +48,7 @@ func Test_IsString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := isStringFunc[any](&ottl.StandardStringGetter[any]{
exprFunc := isString[any](&ottl.StandardStringGetter[any]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return tt.value, nil
},
Expand All @@ -58,3 +59,15 @@ func Test_IsString(t *testing.T) {
})
}
}

func Test_IsString_Error(t *testing.T) {
exprFunc := isString[any](&ottl.StandardStringGetter[any]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return nil, fmt.Errorf("not a TypeError: %w", ottl.TypeError(""))
},
})
_, err := exprFunc(context.Background(), nil)
assert.Error(t, err)
_, ok := err.(ottl.TypeError)
assert.False(t, ok)
}

0 comments on commit 4b4e7d0

Please sign in to comment.