| Conditions | 8 |
| Total Lines | 57 |
| Code Lines | 43 |
| 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 |
||
| 10 | func TestNewClient(t *testing.T) { |
||
| 11 | type args struct { |
||
| 12 | ctx context.Context |
||
| 13 | config *Config |
||
| 14 | bc httpClient |
||
| 15 | } |
||
| 16 | |||
| 17 | c, err := NewConfig(true, "dummy") |
||
| 18 | if err != nil { |
||
| 19 | t.Fatal(err) |
||
| 20 | } |
||
| 21 | |||
| 22 | tests := []struct { |
||
| 23 | name string |
||
| 24 | want Client |
||
| 25 | wantErr bool |
||
| 26 | err error |
||
| 27 | args args |
||
| 28 | }{ |
||
| 29 | { |
||
| 30 | name: "client is build sucessfully", |
||
| 31 | want: Client{ |
||
| 32 | Context: context.Background(), |
||
| 33 | HTTPClient: http.DefaultClient, |
||
| 34 | Config: c, |
||
| 35 | }, |
||
| 36 | wantErr: false, |
||
| 37 | err: nil, |
||
| 38 | args: args{ |
||
| 39 | ctx: nil, |
||
| 40 | config: c, |
||
| 41 | bc: nil, |
||
| 42 | }, |
||
| 43 | }, |
||
| 44 | { |
||
| 45 | name: "client fails if config is nil", |
||
| 46 | want: Client{}, |
||
| 47 | wantErr: true, |
||
| 48 | err: ErrMissingConfig, |
||
| 49 | args: args{ |
||
| 50 | ctx: nil, |
||
| 51 | config: nil, |
||
| 52 | bc: nil, |
||
| 53 | }, |
||
| 54 | }, |
||
| 55 | } |
||
| 56 | |||
| 57 | for _, tt := range tests { |
||
| 58 | t.Run(tt.name, func(t *testing.T) { |
||
| 59 | got, err := NewClient(tt.args.ctx, tt.args.config, tt.args.bc) |
||
| 60 | if tt.wantErr && err != nil { |
||
| 61 | if !reflect.DeepEqual(err, tt.err) { |
||
| 62 | t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err) |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | if !reflect.DeepEqual(tt.want, *got) { |
||
| 66 | t.Errorf("missmatched expectation, got: %v, want: %v", got, tt.want) |
||
| 67 | } |
||
| 72 |