Passed
Pull Request — master (#66)
by Hayrullah
56s
created

internal.TestSortDescBy   A

Complexity

Conditions 5

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
dl 0
loc 8
rs 9.3333
c 0
b 0
f 0
nop 1
1
package internal
2
3
import (
4
	"fmt"
5
	"reflect"
6
	"testing"
7
)
8
9
func TestSort(t *testing.T) {
10
	testData := getSortTestData()
11
	for key, test := range testData {
12
		a, erra := test.output, test.err
13
		b, errb := Sort(test.inputValue)
14
		if erra == nil {
15
			if !reflect.DeepEqual(a, b) || errb != nil {
16
				t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error())
17
			}
18
		}
19
	}
20
}
21
22
func ExampleSort() {
23
24
	data := []int64{100, 30, -100, -5}
25
	// Input: [100 30 -100 -5]
26
	newData, _ := Sort(data)
27
	fmt.Println(newData)
28
	// Output: [-100 -5 30 100]
29
}
30
31
func TestSortBy(t *testing.T) {
32
	testData := getSortByTestData()
33
	for key, test := range testData {
34
		a, erra := test.output, test.err
35
		b, errb := SortBy(test.inputValue, test.property)
36
		if erra == nil {
37
			if !reflect.DeepEqual(a, b) || errb != nil {
38
				t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error())
39
			}
40
		}
41
	}
42
}
43
44
func ExampleSortBy() {
45
	data := []user{
46
		{
47
			name: "Micheal",
48
			age:  27,
49
			location: location{
50
				city: "New York",
51
			},
52
		},
53
		{
54
			name: "Joe",
55
			age:  30,
56
			location: location{
57
				city: "Detroit",
58
			},
59
		},
60
		{
61
			name: "Olivia",
62
			age:  42,
63
			location: location{
64
				city: "New York",
65
			},
66
		},
67
		{
68
			name: "Kevin",
69
			age:  10,
70
			location: location{
71
				city: "Boston",
72
			},
73
		},
74
	}
75
	// Input: [{Micheal 27 {New York}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Kevin 10 {Boston}}]
76
	newData, _ := SortBy(data, "location.city")
77
	fmt.Println(newData)
78
	// Output: [{Kevin 10 {Boston}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Micheal 27 {New York}}]
79
}
80
81
func TestSortDesc(t *testing.T) {
82
	testData := getSortDescTestData()
83
	for key, test := range testData {
84
		a, erra := test.output, test.err
85
		b, errb := SortDesc(test.inputValue)
86
		if erra == nil {
87
			if !reflect.DeepEqual(a, b) || errb != nil {
88
				t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error())
89
			}
90
		}
91
	}
92
}
93
94
func ExampleSortDesc() {
95
96
	data := []int64{-100, -5, 30, 100}
97
	// Input: [-100 -5 30 100]
98
	newData, _ := SortDesc(data)
99
	fmt.Println(newData)
100
	// Output: [100 30 -5 -100]
101
}
102
103
func TestSortDescBy(t *testing.T) {
104
	testData := getSortDescByTestData()
105
	for key, test := range testData {
106
		a, erra := test.output, test.err
107
		b, errb := SortDescBy(test.inputValue, test.property)
108
		if erra == nil {
109
			if !reflect.DeepEqual(a, b) || errb != nil {
110
				t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error())
111
			}
112
		}
113
	}
114
}
115
116
func ExampleSortDescBy() {
117
	data := []user{
118
		{
119
			name: "Micheal",
120
			age:  27,
121
			location: location{
122
				city: "New York",
123
			},
124
		},
125
		{
126
			name: "Joe",
127
			age:  30,
128
			location: location{
129
				city: "Detroit",
130
			},
131
		},
132
		{
133
			name: "Olivia",
134
			age:  42,
135
			location: location{
136
				city: "New York",
137
			},
138
		},
139
		{
140
			name: "Kevin",
141
			age:  10,
142
			location: location{
143
				city: "Boston",
144
			},
145
		},
146
	}
147
	// Input: [{Micheal 27 {New York}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Kevin 10 {Boston}}]
148
	newData, _ := SortDescBy(data, "location.city")
149
	fmt.Println(newData)
150
	// Output: [{Micheal 27 {New York}} {Olivia 42 {New York}} {Joe 30 {Detroit}} {Kevin 10 {Boston}}]
151
}
152
153
func getSortTestData() map[string]struct {
154
	inputValue interface{}
155
	output     interface{}
156
	property   string
157
	err        error
158
} {
159
160
	testData := map[string]struct {
161
		inputValue interface{}
162
		output     interface{}
163
		property   string
164
		err        error
165
	}{
166
		"sort_numbers": {
167
			inputValue: []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55},
168
			output:     []int64{-100, -5, 5, 11, 30, 33, 55, 100, 1000},
169
			property:   "",
170
			err:        nil,
171
		},
172
	}
173
	return testData
174
}
175
176
func getSortDescTestData() map[string]struct {
177
	inputValue interface{}
178
	output     interface{}
179
	property   string
180
	err        error
181
} {
182
183
	testData := map[string]struct {
184
		inputValue interface{}
185
		output     interface{}
186
		property   string
187
		err        error
188
	}{
189
		"sort_numbers": {
190
			inputValue: []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55},
191
			output:     []int64{1000, 100, 55, 33, 30, 11, 5, -5, -100},
192
			property:   "",
193
			err:        nil,
194
		},
195
	}
196
	return testData
197
}
198
199
func getSortByTestData() map[string]struct {
200
	inputValue interface{}
201
	output     interface{}
202
	property   string
203
	err        error
204
} {
205
206
	testData := map[string]struct {
207
		inputValue interface{}
208
		output     interface{}
209
		property   string
210
		err        error
211
	}{
212
		"sort_users": {
213
			inputValue: []user{
214
				{
215
					name: "Micheal",
216
					age:  27,
217
					location: location{
218
						city: "New York",
219
					},
220
				},
221
				{
222
					name: "Joe",
223
					age:  30,
224
					location: location{
225
						city: "Detroit",
226
					},
227
				},
228
				{
229
					name: "Olivia",
230
					age:  42,
231
					location: location{
232
						city: "New York",
233
					},
234
				},
235
				{
236
					name: "Kevin",
237
					age:  10,
238
					location: location{
239
						city: "Boston",
240
					},
241
				},
242
			},
243
			output: []user{
244
				{
245
					name: "Kevin",
246
					age:  10,
247
					location: location{
248
						city: "Boston",
249
					},
250
				},
251
				{
252
					name: "Joe",
253
					age:  30,
254
					location: location{
255
						city: "Detroit",
256
					},
257
				},
258
				{
259
					name: "Olivia",
260
					age:  42,
261
					location: location{
262
						city: "New York",
263
					},
264
				},
265
				{
266
					name: "Micheal",
267
					age:  27,
268
					location: location{
269
						city: "New York",
270
					},
271
				},
272
			},
273
			property: "location.city",
274
			err:      nil,
275
		},
276
	}
277
	return testData
278
}
279
280
func getSortDescByTestData() map[string]struct {
281
	inputValue interface{}
282
	output     interface{}
283
	property   string
284
	err        error
285
} {
286
287
	testData := map[string]struct {
288
		inputValue interface{}
289
		output     interface{}
290
		property   string
291
		err        error
292
	}{
293
		"sort_users": {
294
			inputValue: []user{
295
				{
296
					name: "Micheal",
297
					age:  27,
298
					location: location{
299
						city: "New York",
300
					},
301
				},
302
				{
303
					name: "Joe",
304
					age:  30,
305
					location: location{
306
						city: "Detroit",
307
					},
308
				},
309
				{
310
					name: "Olivia",
311
					age:  42,
312
					location: location{
313
						city: "New York",
314
					},
315
				},
316
				{
317
					name: "Kevin",
318
					age:  10,
319
					location: location{
320
						city: "Boston",
321
					},
322
				},
323
			},
324
			output: []user{
325
				{
326
					name: "Olivia",
327
					age:  42,
328
					location: location{
329
						city: "New York",
330
					},
331
				},
332
				{
333
					name: "Joe",
334
					age:  30,
335
					location: location{
336
						city: "Detroit",
337
					},
338
				},
339
				{
340
					name: "Micheal",
341
					age:  27,
342
					location: location{
343
						city: "New York",
344
					},
345
				},
346
				{
347
					name: "Kevin",
348
					age:  10,
349
					location: location{
350
						city: "Boston",
351
					},
352
				},
353
			},
354
			property: "age",
355
			err:      nil,
356
		},
357
	}
358
	return testData
359
}
360