forked from git-chglog/git-chglog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
110 lines (90 loc) · 1.84 KB
/
utils_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
package chglog
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDotGet(t *testing.T) {
assert := assert.New(t)
now := time.Now()
type Nest struct {
Str string
Int int
Time time.Time
}
type Sample struct {
Str string
Int int
Date time.Time
Nest Nest
}
sample := Sample{
Str: "sample_string",
Int: 12,
Date: now,
Nest: Nest{
Str: "nest_string",
Int: 34,
Time: now,
},
}
var val interface{}
var ok bool
// .Str
val, ok = dotGet(&sample, "Str")
assert.True(ok)
assert.Equal(val, "sample_string")
// Lowercase
val, ok = dotGet(&sample, "str")
assert.True(ok)
assert.Equal(val, "sample_string")
// Int
val, ok = dotGet(&sample, "Int")
assert.True(ok)
assert.Equal(val, 12)
// Time
val, ok = dotGet(&sample, "Date")
assert.True(ok)
assert.Equal(val, now)
// Nest
val, ok = dotGet(&sample, "Nest.Str")
assert.True(ok)
assert.Equal(val, "nest_string")
val, ok = dotGet(&sample, "Nest.Int")
assert.True(ok)
assert.Equal(val, 34)
val, ok = dotGet(&sample, "Nest.Time")
assert.True(ok)
assert.Equal(val, now)
val, ok = dotGet(&sample, "nest.int")
assert.True(ok)
assert.Equal(val, 34)
// Notfound
val, ok = dotGet(&sample, "not.found")
assert.False(ok)
assert.Nil(val)
}
func TestCompare(t *testing.T) {
assert := assert.New(t)
type sample struct {
a interface{}
op string
b interface{}
expected bool
}
table := []sample{
{0, "<", 1, true},
{0, ">", 1, false},
{1, ">", 0, true},
{1, "<", 0, false},
{"a", "<", "b", true},
{"a", ">", "b", false},
{time.Unix(1518018017, 0), "<", time.Unix(1518018043, 0), true},
{time.Unix(1518018017, 0), ">", time.Unix(1518018043, 0), false},
}
for _, sa := range table {
actual, err := compare(sa.a, sa.op, sa.b)
assert.Nil(err)
assert.Equal(sa.expected, actual)
}
}