summaryrefslogtreecommitdiff
path: root/coll/index_test.go
blob: 876a9cce79d54bb8f45e5c37719a845e71d1c2ee (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
package coll

import (
	"testing"

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

func TestIndex(t *testing.T) {
	out, err := Index(map[string]interface{}{
		"foo": "bar", "baz": "qux",
	}, "foo")
	assert.NoError(t, err)
	assert.Equal(t, "bar", out)

	out, err = Index(map[string]interface{}{
		"foo": "bar", "baz": "qux", "quux": "corge",
	}, "foo", 2)
	assert.NoError(t, err)
	assert.Equal(t, byte('r'), out)

	out, err = Index([]interface{}{"foo", "bar", "baz"}, 2)
	assert.NoError(t, err)
	assert.Equal(t, "baz", out)

	out, err = Index([]interface{}{"foo", "bar", "baz"}, 2, 2)
	assert.NoError(t, err)
	assert.Equal(t, byte('z'), out)

	// error cases
	out, err = Index([]interface{}{"foo", "bar", "baz"}, 0, 1, 2)
	assert.Error(t, err)
	assert.Nil(t, out)

	out, err = Index(nil, 0)
	assert.Error(t, err)
	assert.Nil(t, out)

	out, err = Index("foo", nil)
	assert.Error(t, err)
	assert.Nil(t, out)

	out, err = Index(map[interface{}]string{nil: "foo", 2: "bar"}, "baz")
	assert.Error(t, err)
	assert.Nil(t, out)

	out, err = Index([]int{}, 0)
	assert.Error(t, err)
	assert.Nil(t, out)
}