Conditions | 5 |
Total Lines | 125 |
Code Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | package generic |
||
94 | func TestNewIterable(t *testing.T) { |
||
95 | tests := []struct { |
||
96 | name string |
||
97 | value interface{} |
||
98 | expectedKey string |
||
99 | expectedIsIndex bool |
||
100 | expectedIndex int |
||
101 | expectedValue interface{} |
||
102 | }{ |
||
103 | { |
||
104 | name: "array of strings", |
||
105 | value: [...]string{"value"}, |
||
106 | expectedKey: "0", |
||
107 | expectedIsIndex: true, |
||
108 | expectedIndex: 0, |
||
109 | expectedValue: "value", |
||
110 | }, |
||
111 | { |
||
112 | name: "array of string pointers", |
||
113 | value: [...]*string{stringPointer("value")}, |
||
114 | expectedKey: "0", |
||
115 | expectedIsIndex: true, |
||
116 | expectedIndex: 0, |
||
117 | expectedValue: "value", |
||
118 | }, |
||
119 | { |
||
120 | name: "array of string pointers with nil", |
||
121 | value: [...]*string{nil}, |
||
122 | expectedKey: "0", |
||
123 | expectedIsIndex: true, |
||
124 | expectedIndex: 0, |
||
125 | expectedValue: nil, |
||
126 | }, |
||
127 | { |
||
128 | name: "slice of strings", |
||
129 | value: []string{"value"}, |
||
130 | expectedKey: "0", |
||
131 | expectedIsIndex: true, |
||
132 | expectedIndex: 0, |
||
133 | expectedValue: "value", |
||
134 | }, |
||
135 | { |
||
136 | name: "slice of string pointers", |
||
137 | value: []*string{stringPointer("value")}, |
||
138 | expectedKey: "0", |
||
139 | expectedIsIndex: true, |
||
140 | expectedIndex: 0, |
||
141 | expectedValue: "value", |
||
142 | }, |
||
143 | { |
||
144 | name: "slice of string pointers with nil", |
||
145 | value: []*string{nil}, |
||
146 | expectedKey: "0", |
||
147 | expectedIsIndex: true, |
||
148 | expectedIndex: 0, |
||
149 | expectedValue: nil, |
||
150 | }, |
||
151 | { |
||
152 | name: "map of strings", |
||
153 | value: map[string]string{"key": "value"}, |
||
154 | expectedKey: "key", |
||
155 | expectedIsIndex: false, |
||
156 | expectedIndex: -1, |
||
157 | expectedValue: "value", |
||
158 | }, |
||
159 | { |
||
160 | name: "map of string pointers", |
||
161 | value: map[string]*string{"key": stringPointer("value")}, |
||
162 | expectedKey: "key", |
||
163 | expectedIsIndex: false, |
||
164 | expectedIndex: -1, |
||
165 | expectedValue: "value", |
||
166 | }, |
||
167 | { |
||
168 | name: "map of string pointers with nil", |
||
169 | value: map[string]*string{"key": nil}, |
||
170 | expectedKey: "key", |
||
171 | expectedIsIndex: false, |
||
172 | expectedIndex: -1, |
||
173 | expectedValue: nil, |
||
174 | }, |
||
175 | { |
||
176 | name: "map of string pointers with pointer key", |
||
177 | value: map[*string]*string{stringPointer("key"): nil}, |
||
178 | expectedKey: "key", |
||
179 | expectedIsIndex: false, |
||
180 | expectedIndex: -1, |
||
181 | expectedValue: nil, |
||
182 | }, |
||
183 | { |
||
184 | name: "map of string pointers with nil pointer key", |
||
185 | value: map[*string]*string{nil: nil}, |
||
186 | expectedKey: "", |
||
187 | expectedIsIndex: false, |
||
188 | expectedIndex: -1, |
||
189 | expectedValue: nil, |
||
190 | }, |
||
191 | { |
||
192 | name: "map of string pointers with struct key", |
||
193 | value: map[mapKey]string{{Key: "key"}: "value"}, |
||
194 | expectedKey: "<generic.mapKey Value>", |
||
195 | expectedIsIndex: false, |
||
196 | expectedIndex: -1, |
||
197 | expectedValue: "value", |
||
198 | }, |
||
199 | } |
||
200 | for _, test := range tests { |
||
201 | t.Run(test.name, func(t *testing.T) { |
||
202 | iterable, err := NewIterable(test.value) |
||
203 | |||
204 | if assert.NoError(t, err) { |
||
205 | assert.Equal(t, 1, iterable.Count()) |
||
206 | assert.False(t, iterable.IsNil()) |
||
207 | i := 0 |
||
208 | err = iterable.Iterate(func(key Key, value interface{}) error { |
||
209 | assert.Equal(t, test.expectedKey, key.String()) |
||
210 | assert.Equal(t, test.expectedIsIndex, key.IsIndex()) |
||
211 | assert.Equal(t, test.expectedIndex, key.Index()) |
||
212 | assert.Equal(t, test.expectedValue, value) |
||
213 | i++ |
||
214 | |||
215 | return nil |
||
216 | }) |
||
217 | assert.NoError(t, err) |
||
218 | assert.Equal(t, 1, i) |
||
219 | } |
||
223 |