summaryrefslogtreecommitdiff
path: root/internal/funcs/conv_test.go
blob: 2fd23eaa17dfab1c3459bd081c5aac55c9f34f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package funcs

import (
	"context"
	"fmt"
	"strconv"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCreateConvFuncs(t *testing.T) {
	t.Parallel()

	for i := range 10 {
		// Run this a bunch to catch race conditions
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			t.Parallel()

			ctx := context.Background()
			fmap := CreateConvFuncs(ctx)
			actual := fmap["conv"].(func() any)

			assert.Equal(t, ctx, actual().(*ConvFuncs).ctx)
		})
	}
}

func TestDefault(t *testing.T) {
	t.Parallel()

	s := struct{}{}
	c := &ConvFuncs{}
	def := "DEFAULT"
	data := []struct {
		val   any
		empty bool
	}{
		{0, true},
		{1, false},
		{nil, true},
		{"", true},
		{"foo", false},
		{[]string{}, true},
		{[]string{"foo"}, false},
		{[]string{""}, false},
		{c, false},
		{s, false},
	}

	for _, d := range data {
		t.Run(fmt.Sprintf("%T/%#v empty==%v", d.val, d.val, d.empty), func(t *testing.T) {
			t.Parallel()

			if d.empty {
				assert.Equal(t, def, c.Default(def, d.val))
			} else {
				assert.Equal(t, d.val, c.Default(def, d.val))
			}
		})
	}
}