| Conditions | 7 |
| Total Lines | 60 |
| Code Lines | 45 |
| 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 devto |
||
| 8 | func TestNewConfig(t *testing.T) { |
||
| 9 | type args struct { |
||
| 10 | p bool |
||
| 11 | k string |
||
| 12 | } |
||
| 13 | tests := []struct { |
||
| 14 | name string |
||
| 15 | a args |
||
| 16 | want Config |
||
| 17 | wantErr bool |
||
| 18 | err error |
||
| 19 | }{ |
||
| 20 | { |
||
| 21 | name: "new config call is successful for public endpoints", |
||
| 22 | a: args{ |
||
| 23 | p: false, |
||
| 24 | k: "", |
||
| 25 | }, |
||
| 26 | want: Config{ |
||
| 27 | InsecureOnly: true, |
||
| 28 | APIKey: "", |
||
| 29 | }, |
||
| 30 | wantErr: false, |
||
| 31 | err: nil, |
||
| 32 | }, |
||
| 33 | { |
||
| 34 | name: "new config call is successful for protected endpoints", |
||
| 35 | a: args{ |
||
| 36 | p: true, |
||
| 37 | k: "dummy-api-key", |
||
| 38 | }, |
||
| 39 | want: Config{ |
||
| 40 | InsecureOnly: false, |
||
| 41 | APIKey: "dummy-api-key", |
||
| 42 | }, |
||
| 43 | wantErr: false, |
||
| 44 | err: nil, |
||
| 45 | }, |
||
| 46 | { |
||
| 47 | name: "new config call fails for protected access without a key", |
||
| 48 | a: args{ |
||
| 49 | p: true, |
||
| 50 | k: "", |
||
| 51 | }, |
||
| 52 | want: Config{}, |
||
| 53 | wantErr: true, |
||
| 54 | err: ErrMissingRequiredParameter, |
||
| 55 | }, |
||
| 56 | } |
||
| 57 | |||
| 58 | for _, tt := range tests { |
||
| 59 | t.Run(tt.name, func(t *testing.T) { |
||
| 60 | got, err := NewConfig(tt.a.p, tt.a.k) |
||
| 61 | if tt.wantErr && err != nil { |
||
| 62 | if !reflect.DeepEqual(err, tt.err) { |
||
| 63 | t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err) |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | if !reflect.DeepEqual(tt.want, *got) { |
||
| 67 | t.Errorf("missmatched expectation, got: %v, want: %v", got, tt.want) |
||
| 68 | } |
||
| 73 |