Skip to content

Commit e10f995

Browse files
committed
ci: test code 작성 완료
1 parent ba6455c commit e10f995

File tree

4 files changed

+341
-0
lines changed

4 files changed

+341
-0
lines changed

default_test.go

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package safe
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestTimeOrDefault(t *testing.T) {
10+
var val *time.Time
11+
12+
tVal1 := time.Now().Add(time.Hour)
13+
def := func() time.Time {
14+
return tVal1
15+
}
16+
17+
assert.Equal(t, TimeOrDefault(val, def), tVal1)
18+
19+
val = new(time.Time)
20+
assert.Equal(t, TimeOrDefault(val, def), *val)
21+
}
22+
23+
func TestStrOrDefault(t *testing.T) {
24+
var str *string
25+
def := func() string {
26+
return "hello world"
27+
}
28+
29+
assert.Equal(t, StrOrDefault(str, def), "hello world")
30+
31+
str = new(string)
32+
assert.Equal(t, StrOrDefault(str, def), "")
33+
}
34+
35+
func TestIOrDefault(t *testing.T) {
36+
var i *int
37+
def := func() int {
38+
return 101010101
39+
}
40+
41+
assert.Equal(t, IOrDefault(i, def), 101010101)
42+
43+
i = new(int)
44+
assert.Equal(t, IOrDefault(i, def), 0)
45+
}
46+
47+
func TestI8OrDefault(t *testing.T) {
48+
var i8 *int8
49+
def := func() int8 {
50+
return 101
51+
}
52+
53+
assert.Equal(t, I8OrDefault(i8, def), int8(101))
54+
55+
i8 = new(int8)
56+
assert.Equal(t, I8OrDefault(i8, def), int8(0))
57+
}
58+
59+
func TestI16OrDefault(t *testing.T) {
60+
var i16 *int16
61+
def := func() int16 {
62+
return 10101
63+
}
64+
65+
assert.Equal(t, I16OrDefault(i16, def), int16(10101))
66+
67+
i16 = new(int16)
68+
assert.Equal(t, I16OrDefault(i16, def), int16(0))
69+
}
70+
71+
func TestI32OrDefault(t *testing.T) {
72+
var i32 *int32
73+
def := func() int32 {
74+
return 1010101
75+
}
76+
77+
assert.Equal(t, I32OrDefault(i32, def), int32(1010101))
78+
79+
i32 = new(int32)
80+
assert.Equal(t, I32OrDefault(i32, def), int32(0))
81+
}
82+
83+
func TestI64OrDefault(t *testing.T) {
84+
var i64 *int64
85+
def := func() int64 {
86+
return 1010101
87+
}
88+
89+
assert.Equal(t, I64OrDefault(i64, def), int64(1010101))
90+
91+
i64 = new(int64)
92+
assert.Equal(t, I64OrDefault(i64, def), int64(0))
93+
}
94+
95+
func TestUiOrDefault(t *testing.T) {
96+
var ui *uint
97+
def := func() uint {
98+
return 1101010101
99+
}
100+
101+
assert.Equal(t, UiOrDefault(ui, def), uint(1101010101))
102+
103+
ui = new(uint)
104+
assert.Equal(t, UiOrDefault(ui, def), uint(0))
105+
}
106+
107+
func TestUi8OrDefault(t *testing.T) {
108+
var ui8 *uint8
109+
def := func() uint8 {
110+
return 110
111+
}
112+
113+
assert.Equal(t, Ui8OrDefault(ui8, def), uint8(110))
114+
115+
ui8 = new(uint8)
116+
assert.Equal(t, Ui8OrDefault(ui8, def), uint8(0))
117+
}
118+
119+
func TestUi16OrDefault(t *testing.T) {
120+
var ui16 *uint16
121+
def := func() uint16 {
122+
return 11010
123+
}
124+
125+
assert.Equal(t, Ui16OrDefault(ui16, def), uint16(11010))
126+
127+
ui16 = new(uint16)
128+
assert.Equal(t, Ui16OrDefault(ui16, def), uint16(0))
129+
}
130+
131+
func TestUi32OrDefault(t *testing.T) {
132+
var ui32 *uint32
133+
def := func() uint32 {
134+
return 11010101
135+
}
136+
137+
assert.Equal(t, Ui32OrDefault(ui32, def), uint32(11010101))
138+
139+
ui32 = new(uint32)
140+
assert.Equal(t, Ui32OrDefault(ui32, def), uint32(0))
141+
}
142+
143+
func TestUi64OrDefault(t *testing.T) {
144+
var ui64 *uint64
145+
def := func() uint64 {
146+
return 11010101010
147+
}
148+
149+
assert.Equal(t, Ui64OrDefault(ui64, def), uint64(11010101010))
150+
151+
ui64 = new(uint64)
152+
assert.Equal(t, Ui64OrDefault(ui64, def), uint64(0))
153+
}
154+
155+
func TestF32OrDefault(t *testing.T) {
156+
var f32 *float32
157+
def := func() float32 {
158+
return 1.123
159+
}
160+
161+
assert.Equal(t, F32OrDefault(f32, def), float32(1.123))
162+
163+
f32 = new(float32)
164+
assert.Equal(t, F32OrDefault(f32, def), float32(0))
165+
}
166+
167+
func TestF64OrDefault(t *testing.T) {
168+
var f64 *float64
169+
def := func() float64 {
170+
return 3.141592
171+
}
172+
173+
assert.Equal(t, F64OrDefault(f64, def), 3.141592)
174+
175+
f64 = new(float64)
176+
assert.Equal(t, F64OrDefault(f64, def), float64(0))
177+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/unsafe-risk/go-safe
22

33
go 1.17
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.0 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/objx v0.1.0 // indirect
9+
github.com/stretchr/testify v1.7.1 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
8+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

or_zero_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package safe
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"math/rand"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestTimeOrNow(t *testing.T) {
11+
var val *time.Time
12+
assert.Equal(t, TimeOrNow(val), time.Now())
13+
14+
val = new(time.Time)
15+
assert.NotEqual(t, TimeOrNow(val), time.Now())
16+
}
17+
18+
func TestStrOrZero(t *testing.T) {
19+
var str *string
20+
assert.Zero(t, StrOrZero(str))
21+
22+
str = new(string)
23+
*str = "hello world"
24+
assert.NotZero(t, StrOrZero(str))
25+
}
26+
27+
func TestIOrZero(t *testing.T) {
28+
rand.Seed(time.Now().UnixNano())
29+
var i *int
30+
assert.Zero(t, IOrZero(i))
31+
32+
i = new(int)
33+
*i = rand.Int()
34+
assert.NotZero(t, IOrZero(i))
35+
}
36+
37+
func TestInt8OrZero(t *testing.T) {
38+
rand.Seed(time.Now().UnixNano())
39+
var i8 *int8
40+
assert.Zero(t, I8OrZero(i8))
41+
42+
i8 = new(int8)
43+
*i8 = int8(rand.Int())
44+
assert.NotZero(t, I8OrZero(i8))
45+
}
46+
47+
func TestInt16OrZero(t *testing.T) {
48+
rand.Seed(time.Now().UnixNano())
49+
var i16 *int16
50+
assert.Zero(t, I16OrZero(i16))
51+
52+
i16 = new(int16)
53+
*i16 = int16(rand.Int())
54+
assert.NotZero(t, I16OrZero(i16))
55+
}
56+
57+
func TestInt32OrZero(t *testing.T) {
58+
rand.Seed(time.Now().UnixNano())
59+
var i32 *int32
60+
assert.Zero(t, I32OrZero(i32))
61+
62+
i32 = new(int32)
63+
*i32 = rand.Int31()
64+
assert.NotZero(t, I32OrZero(i32))
65+
}
66+
67+
func TestInt64OrZero(t *testing.T) {
68+
rand.Seed(time.Now().UnixNano())
69+
var i64 *int64
70+
assert.Zero(t, I64OrZero(i64))
71+
72+
i64 = new(int64)
73+
*i64 = rand.Int63()
74+
assert.NotZero(t, I64OrZero(i64))
75+
}
76+
77+
func TestUintOrZero(t *testing.T) {
78+
rand.Seed(time.Now().UnixNano())
79+
var ui *uint
80+
assert.Zero(t, UiOrZero(ui))
81+
82+
ui = new(uint)
83+
*ui = uint(rand.Uint64())
84+
assert.NotZero(t, UiOrZero(ui))
85+
}
86+
87+
func TestUint8OrZero(t *testing.T) {
88+
rand.Seed(time.Now().UnixNano())
89+
var ui8 *uint8
90+
assert.Zero(t, Ui8OrZero(ui8))
91+
92+
ui8 = new(uint8)
93+
*ui8 = uint8(rand.Uint64())
94+
assert.NotZero(t, Ui8OrZero(ui8))
95+
}
96+
97+
func TestUint16OrZero(t *testing.T) {
98+
rand.Seed(time.Now().UnixNano())
99+
var ui16 *uint16
100+
assert.Zero(t, Ui16OrZero(ui16))
101+
102+
ui16 = new(uint16)
103+
*ui16 = uint16(rand.Uint64())
104+
assert.NotZero(t, Ui16OrZero(ui16))
105+
}
106+
107+
func TestUint32OrZero(t *testing.T) {
108+
rand.Seed(time.Now().UnixNano())
109+
var ui32 *uint32
110+
assert.Zero(t, Ui32OrZero(ui32))
111+
112+
ui32 = new(uint32)
113+
*ui32 = rand.Uint32()
114+
assert.NotZero(t, Ui32OrZero(ui32))
115+
}
116+
117+
func TestUint64OrZero(t *testing.T) {
118+
rand.Seed(time.Now().UnixNano())
119+
var ui64 *uint64
120+
assert.Zero(t, Ui64OrZero(ui64))
121+
122+
ui64 = new(uint64)
123+
*ui64 = rand.Uint64()
124+
assert.NotZero(t, Ui64OrZero(ui64))
125+
}
126+
127+
func TestFloat32OrZero(t *testing.T) {
128+
rand.Seed(time.Now().UnixNano())
129+
var f32 *float32
130+
assert.Zero(t, F32OrZero(f32))
131+
132+
f32 = new(float32)
133+
*f32 = rand.Float32()
134+
assert.NotZero(t, F32OrZero(f32))
135+
}
136+
137+
func TestFloat64OrZero(t *testing.T) {
138+
rand.Seed(time.Now().UnixNano())
139+
var f64 *float64
140+
assert.Zero(t, F64OrZero(f64))
141+
142+
f64 = new(float64)
143+
*f64 = rand.Float64()
144+
assert.NotZero(t, F64OrZero(f64))
145+
}

0 commit comments

Comments
 (0)