Conditions | 6 |
Total Lines | 52 |
Code Lines | 36 |
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 images_test |
||
111 | func TestConvertToWebpWithQuality(t *testing.T) { |
||
112 | images.StartVips() |
||
113 | |||
114 | type args struct { |
||
115 | name string |
||
116 | quality int |
||
117 | } |
||
118 | |||
119 | tests := []struct { |
||
120 | name string |
||
121 | args args |
||
122 | want []byte |
||
123 | want1 vips.ImageMetadata |
||
124 | wantErr bool |
||
125 | }{ |
||
126 | { |
||
127 | name: "ConvertToWebpWithQuality", |
||
128 | args: args{ |
||
129 | name: "test/1.jpg", |
||
130 | quality: 80, |
||
131 | }, |
||
132 | wantErr: false, |
||
133 | }, |
||
134 | { |
||
135 | name: "ConvertToWebpWithQuality", |
||
136 | args: args{ |
||
137 | name: "test/2.png", |
||
138 | quality: 80, |
||
139 | }, |
||
140 | wantErr: false, |
||
141 | }, |
||
142 | { |
||
143 | name: "ConvertToWebpWithWrongQuality", |
||
144 | args: args{ |
||
145 | name: "test/1.jpg", |
||
146 | quality: 0, |
||
147 | }, |
||
148 | wantErr: true, |
||
149 | }, |
||
150 | } |
||
151 | |||
152 | for _, tt := range tests { |
||
153 | t.Run(tt.name, func(t *testing.T) { |
||
154 | _, got1, err := images.ConvertToWebpWithQuality(tt.args.name, tt.args.quality) |
||
155 | if (err != nil) != tt.wantErr { |
||
156 | t.Errorf("ConvertToWebp() error = %v, wantErr %v", err, tt.wantErr) |
||
157 | return |
||
158 | } |
||
159 | // Check the metadata formats |
||
160 | if !tt.wantErr { |
||
161 | if got1.Format.FileExt() != ".webp" { |
||
162 | t.Errorf("ConvertToWebp() got1 = %v, want %v", got1.Format.FileExt(), ".webp") |
||
163 | } |
||
168 |