Conditions | 9 |
Total Lines | 53 |
Code Lines | 40 |
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 |
||
39 | func TestNewClient(t *testing.T) { |
||
40 | type args struct { |
||
41 | ctx context.Context |
||
42 | config *Config |
||
43 | bc httpClient |
||
44 | bu string |
||
45 | } |
||
46 | |||
47 | c, err := NewConfig(true, "dummy") |
||
48 | if err != nil { |
||
49 | t.Fatal(err) |
||
50 | } |
||
51 | |||
52 | tests := []struct { |
||
53 | name string |
||
54 | wantErr bool |
||
55 | err error |
||
56 | args args |
||
57 | }{ |
||
58 | { |
||
59 | name: "client is build successfully", |
||
60 | wantErr: false, |
||
61 | err: nil, |
||
62 | args: args{ |
||
63 | ctx: nil, |
||
64 | config: c, |
||
65 | bc: nil, |
||
66 | bu: "", |
||
67 | }, |
||
68 | }, |
||
69 | { |
||
70 | name: "client fails if config is nil", |
||
71 | wantErr: true, |
||
72 | err: ErrMissingConfig, |
||
73 | args: args{ |
||
74 | ctx: nil, |
||
75 | config: nil, |
||
76 | bc: nil, |
||
77 | bu: "", |
||
78 | }, |
||
79 | }, |
||
80 | } |
||
81 | |||
82 | for _, tt := range tests { |
||
83 | t.Run(tt.name, func(t *testing.T) { |
||
84 | got, err := NewClient(tt.args.ctx, tt.args.config, tt.args.bc, tt.args.bu) |
||
85 | if tt.wantErr && err != nil { |
||
86 | if !reflect.DeepEqual(err, tt.err) { |
||
87 | t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err) |
||
88 | } |
||
89 | } else { |
||
90 | if !reflect.DeepEqual(tt.err, nil) || got == nil { |
||
91 | t.Errorf("missmatched expectation, got: %v, want: %v", err, tt.err) |
||
92 | } |
||
159 |