e_ExpectedLengths   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 58
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 47
nop 1
dl 0
loc 58
rs 8.7345
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 data
2
3
import (
4
	"testing"
5
6
	"github.com/stretchr/testify/assert"
7
)
8
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
		})
68
	}
69
}
70
71
func TestRandomArrayLengthGenerator_GenerateLength_MaxLessThanMin_MinValueReturned(t *testing.T) {
72
	generator := &randomArrayLengthGenerator{}
73
74
	length, minLength := generator.GenerateLength(100, 0)
75
76
	assert.Equal(t, uint64(100), length)
77
	assert.Equal(t, uint64(100), minLength)
78
}
79