gotil_test.TestSortBy   B
last analyzed

Complexity

Conditions 2

Size

Total Lines 66
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 40
nop 1
dl 0
loc 66
rs 8.92
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package gotil_test
2
3
import (
4
	"fmt"
5
	"reflect"
6
	"testing"
7
8
	"github.com/gotilty/gotil"
9
)
10
11
type user struct {
12
	name     string
13
	age      int
14
	location location
15
}
16
17
type location struct {
18
	city string
19
}
20
21
func TestSort(t *testing.T) {
22
23
	a := "10"
24
	r, _ := gotil.ToInt8(a)
25
	fmt.Println(r)
26
	// input := []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55}
27
	// expected := []int64{-100, -5, 5, 11, 30, 33, 55, 100, 1000}
28
	// result := gotil.Sort(input)
29
	// if !reflect.DeepEqual(expected, result) {
30
	// 	t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result)
31
	// }
32
}
33
34
func ExampleSort() {
35
	data := []int64{100, 30, -100, -5}
36
	// Input: [100 30 -100 -5]
37
	newData := gotil.Sort(data)
38
	fmt.Println(newData)
39
	// Output: [-100 -5 30 100]
40
}
41
42
func TestSortBy(t *testing.T) {
43
	// Input: [{Micheal 27 {New York}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Kevin 10 {Boston}}]
44
	input := []user{
45
		{
46
			name: "Micheal",
47
			age:  27,
48
			location: location{
49
				city: "New York",
50
			},
51
		},
52
		{
53
			name: "Joe",
54
			age:  30,
55
			location: location{
56
				city: "Detroit",
57
			},
58
		},
59
		{
60
			name: "Olivia",
61
			age:  42,
62
			location: location{
63
				city: "New York",
64
			},
65
		},
66
		{
67
			name: "Kevin",
68
			age:  10,
69
			location: location{
70
				city: "Boston",
71
			},
72
		},
73
	}
74
	expected := []user{
75
		{
76
			name: "Kevin",
77
			age:  10,
78
			location: location{
79
				city: "Boston",
80
			},
81
		},
82
83
		{
84
			name: "Joe",
85
			age:  30,
86
			location: location{
87
				city: "Detroit",
88
			},
89
		},
90
		{
91
			name: "Olivia",
92
			age:  42,
93
			location: location{
94
				city: "New York",
95
			},
96
		},
97
		{
98
			name: "Micheal",
99
			age:  27,
100
			location: location{
101
				city: "New York",
102
			},
103
		},
104
	}
105
	result := gotil.SortBy(input, "location.city")
106
	if !reflect.DeepEqual(expected, result) {
107
		t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input, expected, result)
108
	}
109
}
110
111
func TestSortDesc(t *testing.T) {
112
	input := []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55}
113
	expected := []int64{1000, 100, 55, 33, 30, 11, 5, -5, -100}
114
	result := gotil.SortDesc(input)
115
	if !reflect.DeepEqual(expected, result) {
116
		t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result)
117
	}
118
}
119
120
func ExampleSortDesc() {
121
122
	data := []int64{-100, -5, 30, 100}
123
	// Input: [-100 -5 30 100]
124
	newData := gotil.SortDesc(data)
125
	fmt.Println(newData)
126
	// Output: [100 30 -5 -100]
127
}
128
129
func TestSortDescBy(t *testing.T) {
130
	input := []user{
131
		{
132
			name: "Micheal",
133
			age:  27,
134
			location: location{
135
				city: "New York",
136
			},
137
		},
138
		{
139
			name: "Joe",
140
			age:  30,
141
			location: location{
142
				city: "Detroit",
143
			},
144
		},
145
		{
146
			name: "Olivia",
147
			age:  42,
148
			location: location{
149
				city: "New York",
150
			},
151
		},
152
		{
153
			name: "Kevin",
154
			age:  10,
155
			location: location{
156
				city: "Boston",
157
			},
158
		},
159
	}
160
	expected := []user{
161
		{
162
			name: "Olivia",
163
			age:  42,
164
			location: location{
165
				city: "New York",
166
			},
167
		},
168
		{
169
			name: "Joe",
170
			age:  30,
171
			location: location{
172
				city: "Detroit",
173
			},
174
		},
175
		{
176
			name: "Micheal",
177
			age:  27,
178
			location: location{
179
				city: "New York",
180
			},
181
		},
182
		{
183
			name: "Kevin",
184
			age:  10,
185
			location: location{
186
				city: "Boston",
187
			},
188
		},
189
	}
190
	result := gotil.SortDescBy(input, "age")
191
	if !reflect.DeepEqual(expected, result) {
192
		t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input, expected, result)
193
	}
194
}
195