-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
113 lines (93 loc) · 2.95 KB
/
util_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package peas
import (
"github.com/procyon-projects/goo"
"github.com/stretchr/testify/assert"
"testing"
)
type testInterface interface {
testMethod()
}
type baseTestStruct struct {
}
type testStruct2 struct {
}
type testStruct struct {
baseTestStruct
}
func (testStruct) InitializePea() error {
return nil
}
func (testStruct) testMethod() {
}
func newStructFunction() testStruct {
return testStruct{}
}
func newStructFunctionWithParameters(str string, i int, obj interface{}) testStruct {
return testStruct{}
}
func newStructFunctionWithMoreReturnValuesThanOne() (testStruct, error) {
return testStruct{}, nil
}
func TestCreateInstance_WhenIsInvokedWithFunctionNotHavingAnyParameters(t *testing.T) {
instance, err := CreateInstance(goo.GetType(newStructFunction), nil)
assert.NotNil(t, instance)
assert.Nil(t, err)
}
func TestCreateInstance_WhenIsInvokedWithFunctionHavingParameters(t *testing.T) {
args := make([]interface{}, 0)
args = append(args, "test-arg")
args = append(args, 0)
args = append(args, testStruct{})
instance, err := CreateInstance(goo.GetType(newStructFunctionWithParameters), args)
assert.NotNil(t, instance)
assert.Nil(t, err)
}
func TestCreateInstance_WhenIsInvokedWithFunctionReturningMoreValuesThanOne(t *testing.T) {
args := make([]interface{}, 0)
args = append(args, "test-arg")
instance, err := CreateInstance(goo.GetType(newStructFunctionWithMoreReturnValuesThanOne), nil)
assert.Nil(t, instance)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "it only supports the construction functions with one return parameter")
}
func TestCreateInstance_WhenIsInvokedWithStructAndArgs(t *testing.T) {
args := make([]interface{}, 0)
args = append(args, "test-arg")
instance, err := CreateInstance(goo.GetType(testStruct{}), args)
assert.Nil(t, instance)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "struct type does not support args")
}
func TestCreateInstance_WhenIsInvokedWithNonStructAndFunction(t *testing.T) {
instance, err := CreateInstance(goo.GetType("test"), nil)
assert.Nil(t, instance)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "you can only pass Struct or Func types")
}
func TestGetStringMapKeys_WhenIsInvokedWithNil(t *testing.T) {
assert.Nil(t, getStringMapKeys(nil))
}
func TestGetStringMapKeys_WhenIsInvokedWithMapHavingNonStringKeyType(t *testing.T) {
assert.Panics(t, func() {
getStringMapKeys(new(map[interface{}]interface{}))
})
}
func TestGetStringMapKeys_WhenIsInvokedWithNonMapType(t *testing.T) {
assert.Panics(t, func() {
getStringMapKeys("test")
})
}
func TestGetStringMapKeys_WhenIsInvokedWithMapHavingStringKeyType(t *testing.T) {
testMap := make(map[string]interface{})
testMap["key1"] = nil
testMap["key2"] = nil
testMap["key3"] = nil
assert.NotPanics(t, func() {
keys := getStringMapKeys(testMap)
assert.Equal(t, 3, len(keys))
assert.Contains(t, keys, "key1")
assert.Contains(t, keys, "key2")
assert.Contains(t, keys, "key3")
assert.NotContains(t, keys, "key4")
})
}