File: indirect_test.go

package info (click to toggle)
golang-github-spf13-cast 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 300 kB
  • sloc: makefile: 34
file content (66 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download | duplicates (2)
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
63
64
65
66
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package cast

import (
	"testing"

	qt "github.com/frankban/quicktest"
)

func TestIndirect(t *testing.T) {
	c := qt.New(t)

	x := 13
	y := &x
	z := &y

	var ptr *string
	var ptrptr **string

	var i any

	var n = int(13)
	var i2 any = n

	var i3 any = &n

	var b *bool
	var i4 any = b

	testCases := []struct {
		input      any
		expected   any
		expectedOk bool
	}{
		{x, 13, false},
		{y, 13, true},
		{z, 13, true},
		{ptr, nil, true},
		{ptrptr, nil, true},
		{i, nil, false},
		{&i, nil, true},
		{i2, 13, false},
		{&i2, 13, true},
		{i3, 13, true},
		{&i3, 13, true},
		{i4, nil, true},
		{&i4, nil, true},
		{nil, nil, false},
	}

	for _, testCase := range testCases {
		// TODO: remove after minimum Go version is >=1.22
		testCase := testCase

		t.Run("", func(t *testing.T) {
			v, ok := indirect(testCase.input)

			c.Assert(v, qt.Equals, testCase.expected)
			c.Assert(ok, qt.Equals, testCase.expectedOk)
		})
	}
}