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