File: util_test.go

package info (click to toggle)
golang-github-issue9-assert 0.0~git20170908.0.ceac1aa-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (232 lines) | stat: -rw-r--r-- 4,461 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2014 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package assert

import (
	"testing"
)

func TestIsEqual(t *testing.T) {
	eq := func(v1, v2 interface{}) {
		if !IsEqual(v1, v2) {
			t.Errorf("eq:[%v]!=[%v]", v1, v2)
		}
	}

	neq := func(v1, v2 interface{}) {
		if IsEqual(v1, v2) {
			t.Errorf("eq:[%v]==[%v]", v1, v2)
		}
	}

	eq([]byte("abc"), "abc")
	eq("abc", []byte("abc"))

	eq([]byte("中文abc"), "中文abc")
	eq("中文abc", []byte("中文abc"))

	eq([]rune("中文abc"), "中文abc")
	eq("中文abc", []rune("中文abc"))

	eq(5, 5.0)
	eq(int8(5), 5)
	eq(5, int8(5))
	eq(float64(5), int8(5))
	eq([]int{1, 2, 3}, []int{1, 2, 3})
	eq([]int{1, 2, 3}, []int8{1, 2, 3})
	eq([]float32{1, 2.0, 3}, []int8{1, 2, 3})
	eq([]float32{1, 2.0, 3}, []float64{1, 2, 3})

	// 比较两个元素类型可相互转换的数组
	eq(
		[][]int{
			[]int{1, 2},
			[]int{3, 4},
		},
		[][]int8{
			[]int8{1, 2},
			[]int8{3, 4},
		},
	)

	// 比较两个元素类型可转换的map
	eq(
		[]map[int]int{
			map[int]int{1: 1, 2: 2},
			map[int]int{3: 3, 4: 4},
		},
		[]map[int]int8{
			map[int]int8{1: 1, 2: 2},
			map[int]int8{3: 3, 4: 4},
		},
	)
	eq(map[string]int{"1": 1, "2": 2}, map[string]int8{"1": 1, "2": 2})

	// 比较两个元素类型可转换的map
	eq(
		map[int]string{
			1: "1",
			2: "2",
		},
		map[int][]byte{
			1: []byte("1"),
			2: []byte("2"),
		},
	)

	// array 对比
	eq([2]int{1, 2}, [2]int{1, 2})
	eq([2]int{9, 3}, [2]int8{9, 3})
	eq([2]int8{1, 4}, [2]int{1, 4})
	eq([2]int{1, 5}, []int8{1, 5})

	neq(map[int]int{1: 1, 2: 2}, map[int8]int{1: 1, 2: 2})
	neq([]int{1, 2, 3}, []int{3, 2, 1})
	neq("5", 5)
	neq(true, "true")
	neq(true, 1)
	neq(true, "1")
	// 判断包含不同键名的两个map
	neq(map[int]int{1: 1, 2: 2}, map[int]int{5: 5, 6: 6})
}

func TestIsEmpty(t *testing.T) {
	if IsEmpty([]string{""}) {
		t.Error("IsEmpty([]string{\"\"})")
	}

	if !IsEmpty([]string{}) {
		t.Error("IsEmpty([]string{})")
	}

	if !IsEmpty([]int{}) {
		t.Error("IsEmpty([]int{})")
	}

	if !IsEmpty(map[string]int{}) {
		t.Error("IsEmpty(map[string]int{})")
	}

	if !IsEmpty(0) {
		t.Error("IsEmpty(0)")
	}

	if !IsEmpty(uint64(0)) {
		t.Error("IsEmpty(uint64(0))")
	}

	if !IsEmpty(0.0) {
		t.Error("IsEmpty(0.0)")
	}

	if !IsEmpty("") {
		t.Error("IsEmpty(``)")
	}

	if IsEmpty("  ") {
		t.Error("IsEmpty(\"  \")")
	}
}

func TestIsNil(t *testing.T) {
	if !IsNil(nil) {
		t.Error("IsNil(nil)")
	}

	var v1 []int
	if !IsNil(v1) {
		t.Error("IsNil(v1)")
	}

	var v2 map[string]string
	if !IsNil(v2) {
		t.Error("IsNil(v2)")
	}
}

func TestHasPanic(t *testing.T) {
	f1 := func() {
		panic("panic")
	}

	if has, _ := HasPanic(f1); !has {
		t.Error("f1未发生panic")
	}

	f2 := func() {
		f1()
	}

	if has, msg := HasPanic(f2); !has {
		t.Error("f2未发生panic")
	} else if msg != "panic" {
		t.Errorf("f2发生了panic,但返回信息不正确,应为[panic],但其实返回了%v", msg)
	}

	f3 := func() {
		defer func() {
			if msg := recover(); msg != nil {
				t.Logf("TestHasPanic.f3 recover msg:[%v]", msg)
			}
		}()

		f1()
	}

	if has, msg := HasPanic(f3); has {
		t.Errorf("f3发生了panic,其信息为:[%v]", msg)
	}

	f4 := func() {
		//todo
	}

	if has, msg := HasPanic(f4); has {
		t.Errorf("f4发生panic,其信息为[%v]", msg)
	}
}

func TestIsContains(t *testing.T) {
	fn := func(result bool, container, item interface{}) {
		if result != IsContains(container, item) {
			t.Errorf("%v == (IsContains%v, %v)出错\n", result, container, item)
		}
	}

	fn(false, nil, nil)

	fn(true, "abc", "a")
	fn(true, "abc", 'a')       // string vs byte
	fn(true, "abc", rune('a')) // string vs rune
	fn(true, "abc", "c")
	fn(true, "abc", "bc")

	fn(true, "中文a", "中")
	fn(true, "中文a", "a")
	fn(true, "中文a", '中')

	fn(true, []int{1, 2, 3}, 1)
	fn(true, []int{1, 2, 3}, int8(3))
	fn(true, []int{1, 2, 4}, []int{1, 2})
	fn(true, []interface{}{[]int{1, 2}, 5, 6}, []int8{1, 2})
	fn(true, []interface{}{[]int{1, 2}, 5, 6}, 5)

	fn(true, map[string]int{"1": 1, "2": 2}, map[string]int8{"1": 1})
	fn(true,
		map[string][]int{
			"1": []int{1, 2, 3},
			"2": []int{4, 5, 6},
		},
		map[string][]int8{
			"1": []int8{1, 2, 3},
			"2": []int8{4, 5, 6},
		},
	)

	fn(false, map[string]int{}, nil)
	fn(false, map[string]int{"1": 1, "2": 2}, map[string]int8{})
	fn(false, []int{1, 2, 3}, nil)
	fn(false, []int{1, 2, 3}, []int8{1, 3})
}