-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_test.go
70 lines (65 loc) · 1.54 KB
/
cast_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
package configurationmanager
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCastAndTryAssignValue(t *testing.T) {
RunTests(t, &[]Test{
{
name: "cast string to interface{} (string) should return string",
run: func(t *testing.T) {
from := "test"
var to interface{} = ""
CastAndAssignValue(from, &to)
assert.Equal(t, "test", to)
},
},
{
name: "cast string to time.duration should return time.duration",
run: func(t *testing.T) {
from := "1"
var to time.Duration
CastAndAssignValue(from, &to)
assert.Equal(t, time.Nanosecond, to)
},
},
{
name: "cast string to integer should return integer",
run: func(t *testing.T) {
from := "1"
var to int
CastAndAssignValue(from, &to)
assert.Equal(t, 1, to)
},
},
{
name: "cast string to integer pointer should return integer pointer",
run: func(t *testing.T) {
from := pointer("1")
var to *int
CastAndAssignValue(&from, &to)
assert.Equal(t, 1, *to)
},
},
{
name: "cast string to nested integer pointer should return integer pointer",
run: func(t *testing.T) {
from := "1"
var to ******int
CastAndAssignValue(&from, &to)
assert.Equal(t, 1, ******to)
},
},
{
name: "cast string to timestamp should return timestamp",
run: func(t *testing.T) {
from := time.Now()
var to time.Time
CastAndAssignValue(from.Format(time.RFC3339Nano), &to)
assert.True(t, time.Duration(math.Abs(float64(to.Sub(from)))) < time.Millisecond)
},
},
})
}