Conditions | 3 |
Total Lines | 103 |
Code Lines | 76 |
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 |
||
16 | func TestIntegerGenerator_GenerateDataBySchema_GivenSchemaAndRandomValue_ExpectedValue(t *testing.T) { |
||
17 | min := 10.0 |
||
18 | max := 100.0 |
||
19 | |||
20 | tests := []struct { |
||
21 | name string |
||
22 | schema *openapi3.Schema |
||
23 | randomValue int64 |
||
24 | expectedMaxValue int64 |
||
25 | expectedValue int64 |
||
26 | }{ |
||
27 | { |
||
28 | "no params, min random value", |
||
29 | openapi3.NewSchema(), |
||
30 | 0, |
||
31 | testDefaultMaxInt + 1, |
||
32 | 0, |
||
33 | }, |
||
34 | { |
||
35 | "no params, max random value", |
||
36 | openapi3.NewSchema(), |
||
37 | testDefaultMaxInt, |
||
38 | testDefaultMaxInt + 1, |
||
39 | testDefaultMaxInt, |
||
40 | }, |
||
41 | { |
||
42 | "given range, min random value", |
||
43 | &openapi3.Schema{ |
||
44 | Min: &min, |
||
45 | Max: &max, |
||
46 | }, |
||
47 | 0, |
||
48 | 91, |
||
49 | 10, |
||
50 | }, |
||
51 | { |
||
52 | "given range, max random value", |
||
53 | &openapi3.Schema{ |
||
54 | Min: &min, |
||
55 | Max: &max, |
||
56 | }, |
||
57 | 90, |
||
58 | 91, |
||
59 | 100, |
||
60 | }, |
||
61 | { |
||
62 | "given exclusive range, min random value", |
||
63 | &openapi3.Schema{ |
||
64 | Min: &min, |
||
65 | Max: &max, |
||
66 | ExclusiveMin: true, |
||
67 | ExclusiveMax: true, |
||
68 | }, |
||
69 | 0, |
||
70 | 89, |
||
71 | 11, |
||
72 | }, |
||
73 | { |
||
74 | "given exclusive range, max random value", |
||
75 | &openapi3.Schema{ |
||
76 | Min: &min, |
||
77 | Max: &max, |
||
78 | ExclusiveMin: true, |
||
79 | ExclusiveMax: true, |
||
80 | }, |
||
81 | 88, |
||
82 | 89, |
||
83 | 99, |
||
84 | }, |
||
85 | { |
||
86 | "32bit format, min random value", |
||
87 | &openapi3.Schema{ |
||
88 | Format: "int32", |
||
89 | }, |
||
90 | 0, |
||
91 | testDefaultMaxInt + 1, |
||
92 | 0, |
||
93 | }, |
||
94 | { |
||
95 | "multiple of, random value", |
||
96 | &openapi3.Schema{ |
||
97 | MultipleOf: &min, |
||
98 | }, |
||
99 | 17, |
||
100 | testDefaultMaxInt + 1, |
||
101 | 10, |
||
102 | }, |
||
103 | } |
||
104 | for _, test := range tests { |
||
105 | t.Run(test.name, func(t *testing.T) { |
||
106 | randomMock := &mockRandomGenerator{} |
||
107 | integerGeneratorInstance := &integerGenerator{ |
||
108 | random: randomMock, |
||
109 | defaultMinimum: testDefaultMinInt, |
||
110 | defaultMaximum: testDefaultMaxInt, |
||
111 | } |
||
112 | randomMock.On("Int63n", test.expectedMaxValue).Return(test.randomValue).Once() |
||
113 | |||
114 | data, err := integerGeneratorInstance.GenerateDataBySchema(context.Background(), test.schema) |
||
115 | |||
116 | randomMock.AssertExpectations(t) |
||
117 | assert.NoError(t, err) |
||
118 | assert.Equal(t, test.expectedValue, data) |
||
119 | }) |
||
150 |