-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstring_test.go
More file actions
112 lines (94 loc) · 3.37 KB
/
string_test.go
File metadata and controls
112 lines (94 loc) · 3.37 KB
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
package convert_test
import (
"testing"
"time"
"github.com/Eun/go-convert/internal/testhelpers"
)
type SomeStructWithStringFunc struct{}
func (SomeStructWithStringFunc) String() string {
return "Hello World"
}
type SomeStructWithStringFuncPtr struct{}
func (*SomeStructWithStringErrFuncPtr) String() string {
return "Hello World"
}
type SomeStructWithStringErrFunc struct{}
func (SomeStructWithStringErrFunc) String() (string, error) {
return "Hello World", nil
}
type SomeStructWithStringErrFuncPtr struct{}
func (*SomeStructWithStringFuncPtr) String() (string, error) {
return "Hello World", nil
}
func TestString(t *testing.T) {
beginningOfTime := time.Unix(0, 0).UTC()
tests := []testhelpers.TestCase{
// nil
{nil, "", "", "", nil},
// string
{"Hello World", "", "Hello World", "", nil},
// bool
{true, "", "true", "", nil},
// int
{6, "", "6", "", nil},
// int8
{int8(6), "", "6", "", nil},
// int16
{int16(6), "", "6", "", nil},
// int32
{int32(6), "", "6", "", nil},
// int64
{int64(6), "", "6", "", nil},
// uint
{uint(6), "", "6", "", nil},
// uint8
{uint8(6), "", "6", "", nil},
// uint16
{uint16(6), "", "6", "", nil},
// uint32
{uint32(6), "", "6", "", nil},
// uint64
{uint64(6), "", "6", "", nil},
// float32
{float32(6), "", "6.000000", "", nil},
// float32 with format
{float32(6), "%06.2f", "006.00", "", nil},
// float32 with invalid format
{float32(6), "06.2f", "6.000000", "", nil},
// float64
{float64(6), "", "6.000000", "", nil},
// float64 with format
{float64(6), "%06.2f", "006.00", "", nil},
// float64 with invalid format
{float64(6), "06.2f", "6.000000", "", nil},
// slice
{[]int{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]int8{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]int16{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]int32{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]int64{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]uint{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]uint8{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]uint16{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]uint32{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]uint64{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]byte{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]rune{'H', 'e', 'l', 'l', 'o'}, "", "Hello", "", nil},
{[]string{"H", "e", "l", "l", "o"}, "", "", "unable to convert []string to string: no recipe", nil},
// struct
{struct{}{}, "", "", "unable to convert struct {} to string: struct {} has no String() function", nil},
{SomeStructWithStringFunc{}, "Hello World", "Hello World", "", nil},
{&SomeStructWithStringFunc{}, "Hello World", "Hello World", "", nil},
{SomeStructWithStringFuncPtr{}, "Hello World", "Hello World", "", nil},
{&SomeStructWithStringFuncPtr{}, "Hello World", "Hello World", "", nil},
{SomeStructWithStringErrFunc{}, "Hello World", "Hello World", "", nil},
{&SomeStructWithStringErrFunc{}, "Hello World", "Hello World", "", nil},
{SomeStructWithStringErrFuncPtr{}, "Hello World", "Hello World", "", nil},
{&SomeStructWithStringErrFuncPtr{}, "Hello World", "Hello World", "", nil},
{beginningOfTime, "", "1970-01-01 00:00:00 +0000 UTC", "", nil},
{&beginningOfTime, "", "1970-01-01 00:00:00 +0000 UTC", "", nil},
}
for i, test := range tests {
testhelpers.RunTest(t, test, i)
}
}