| Conditions | 3 |
| Total Lines | 58 |
| Code Lines | 47 |
| 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 data |
||
| 9 | func TestRandomArrayLengthGenerator_GenerateLength_GivenRange_ExpectedLengths(t *testing.T) { |
||
| 10 | tests := []struct { |
||
| 11 | name string |
||
| 12 | min uint64 |
||
| 13 | max uint64 |
||
| 14 | randomValue int |
||
| 15 | expectedMaxValue int |
||
| 16 | expectedLength int |
||
| 17 | expectedMinLength int |
||
| 18 | }{ |
||
| 19 | { |
||
| 20 | "empty values, random value equals to min", |
||
| 21 | 0, |
||
| 22 | 0, |
||
| 23 | 0, |
||
| 24 | defaultMaxItems + 1, |
||
| 25 | 0, |
||
| 26 | 0, |
||
| 27 | }, |
||
| 28 | { |
||
| 29 | "empty values, random value equals to max", |
||
| 30 | 0, |
||
| 31 | 0, |
||
| 32 | defaultMaxItems, |
||
| 33 | defaultMaxItems + 1, |
||
| 34 | defaultMaxItems, |
||
| 35 | 0, |
||
| 36 | }, |
||
| 37 | { |
||
| 38 | "given range, random value equals to min", |
||
| 39 | 10, |
||
| 40 | 100, |
||
| 41 | 0, |
||
| 42 | 91, |
||
| 43 | 10, |
||
| 44 | 10, |
||
| 45 | }, |
||
| 46 | { |
||
| 47 | "given range, random value equals to max", |
||
| 48 | 10, |
||
| 49 | 100, |
||
| 50 | 90, |
||
| 51 | 91, |
||
| 52 | 100, |
||
| 53 | 10, |
||
| 54 | }, |
||
| 55 | } |
||
| 56 | for _, test := range tests { |
||
| 57 | t.Run(test.name, func(t *testing.T) { |
||
| 58 | randomMock := &mockRandomGenerator{} |
||
| 59 | generator := &randomArrayLengthGenerator{random: randomMock} |
||
| 60 | randomMock.On("Intn", test.expectedMaxValue).Return(test.randomValue).Once() |
||
| 61 | |||
| 62 | length, minLength := generator.GenerateLength(test.min, test.max) |
||
| 63 | |||
| 64 | randomMock.AssertExpectations(t) |
||
| 65 | assert.Equal(t, uint64(test.expectedLength), length) |
||
| 66 | assert.Equal(t, uint64(test.expectedMinLength), minLength) |
||
| 67 | }) |
||
| 79 |