Conditions | 3 |
Total Lines | 107 |
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 |
||
30 | func TestCoordinatingMediaGenerator_GenerateData_UseExamplesOptionAndGivenExample_ExpectedData(t *testing.T) { |
||
31 | const randomData = "randomData" |
||
32 | const exampleData = "exampleData" |
||
33 | |||
34 | schemaGeneratorMock := &mockSchemaGenerator{} |
||
35 | mediaGenerator := &coordinatingMediaGenerator{ |
||
36 | schemaGenerator: schemaGeneratorMock, |
||
37 | } |
||
38 | schema := openapi3.NewSchema() |
||
39 | mediaType := &openapi3.MediaType{ |
||
40 | Schema: openapi3.NewSchemaRef("", schema), |
||
41 | } |
||
42 | schemaGeneratorMock.On("GenerateDataBySchema", mock.Anything, schema).Return(randomData, nil) |
||
43 | |||
44 | tests := []struct { |
||
45 | name string |
||
46 | useExamples UseExamplesEnum |
||
47 | example interface{} |
||
48 | examples map[string]*openapi3.ExampleRef |
||
49 | expectedData interface{} |
||
50 | }{ |
||
51 | { |
||
52 | "use examples disabled, no example, no examples", |
||
53 | No, |
||
54 | nil, |
||
55 | nil, |
||
56 | randomData, |
||
57 | }, |
||
58 | { |
||
59 | "use examples disabled, given example, no examples", |
||
60 | No, |
||
61 | exampleData, |
||
62 | nil, |
||
63 | randomData, |
||
64 | }, |
||
65 | { |
||
66 | "use examples disabled, no example, given examples", |
||
67 | No, |
||
68 | nil, |
||
69 | map[string]*openapi3.ExampleRef{ |
||
70 | "example": { |
||
71 | Value: &openapi3.Example{Value: exampleData}, |
||
72 | }, |
||
73 | }, |
||
74 | randomData, |
||
75 | }, |
||
76 | { |
||
77 | "use examples if present, no example, no examples", |
||
78 | IfPresent, |
||
79 | nil, |
||
80 | nil, |
||
81 | randomData, |
||
82 | }, |
||
83 | { |
||
84 | "use examples if present, given example, no examples", |
||
85 | IfPresent, |
||
86 | exampleData, |
||
87 | nil, |
||
88 | exampleData, |
||
89 | }, |
||
90 | { |
||
91 | "use examples if present, no example, given examples", |
||
92 | IfPresent, |
||
93 | nil, |
||
94 | map[string]*openapi3.ExampleRef{ |
||
95 | "example": { |
||
96 | Value: &openapi3.Example{Value: exampleData}, |
||
97 | }, |
||
98 | }, |
||
99 | exampleData, |
||
100 | }, |
||
101 | { |
||
102 | "use examples exclusively, no example, no examples", |
||
103 | Exclusively, |
||
104 | nil, |
||
105 | nil, |
||
106 | nil, |
||
107 | }, |
||
108 | { |
||
109 | "use examples exclusively, given example, no examples", |
||
110 | Exclusively, |
||
111 | exampleData, |
||
112 | nil, |
||
113 | exampleData, |
||
114 | }, |
||
115 | { |
||
116 | "use examples exclusively, no example, given examples", |
||
117 | Exclusively, |
||
118 | nil, |
||
119 | map[string]*openapi3.ExampleRef{ |
||
120 | "example": { |
||
121 | Value: &openapi3.Example{Value: exampleData}, |
||
122 | }, |
||
123 | }, |
||
124 | exampleData, |
||
125 | }, |
||
126 | } |
||
127 | for _, test := range tests { |
||
128 | t.Run(test.name, func(t *testing.T) { |
||
129 | mediaGenerator.useExamples = test.useExamples |
||
130 | mediaType.Example = test.example |
||
131 | mediaType.Examples = test.examples |
||
132 | |||
133 | data, err := mediaGenerator.GenerateData(context.Background(), mediaType) |
||
134 | |||
135 | assert.NoError(t, err) |
||
136 | assert.Equal(t, test.expectedData, data) |
||
137 | }) |
||
140 |