| Conditions | 8 |
| Total Lines | 56 |
| 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 |
||
| 98 | func TestClient_NewRequest(t *testing.T) { |
||
| 99 | setup() |
||
| 100 | defer teardown() |
||
| 101 | type args struct { |
||
| 102 | m string |
||
| 103 | uri string |
||
| 104 | body io.Reader |
||
| 105 | } |
||
| 106 | tests := []struct { |
||
| 107 | name string |
||
| 108 | args args |
||
| 109 | wantErr bool |
||
| 110 | err error |
||
| 111 | }{ |
||
| 112 | { |
||
| 113 | "new request is created", |
||
| 114 | args{ |
||
| 115 | m: "", |
||
| 116 | uri: "", |
||
| 117 | body: nil, |
||
| 118 | }, |
||
| 119 | false, |
||
| 120 | nil, |
||
| 121 | }, |
||
| 122 | { |
||
| 123 | "new fails for invalid url", |
||
| 124 | args{ |
||
| 125 | m: "", |
||
| 126 | uri: " http://localhost", |
||
| 127 | body: nil, |
||
| 128 | }, |
||
| 129 | true, |
||
| 130 | errors.New("parse http://localhost: first path segment in URL cannot contain colon"), |
||
| 131 | }, |
||
| 132 | { |
||
| 133 | "new fails for invalid url", |
||
| 134 | args{ |
||
| 135 | m: "\\\\\\\\\\\\\\", |
||
| 136 | uri: "", |
||
| 137 | body: nil, |
||
| 138 | }, |
||
| 139 | true, |
||
| 140 | fmt.Errorf("net/http: invalid method %q", "\\\\\\\\\\\\\\"), |
||
| 141 | }, |
||
| 142 | } |
||
| 143 | |||
| 144 | for _, tt := range tests { |
||
| 145 | t.Run(tt.name, func(t *testing.T) { |
||
| 146 | got, err := testClientPro.NewRequest(tt.args.m, tt.args.uri, tt.args.body) |
||
| 147 | if tt.wantErr && err != nil { |
||
| 148 | if !reflect.DeepEqual(err.Error(), tt.err.Error()) { |
||
| 149 | t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err) |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | if !reflect.DeepEqual(tt.err, nil) || got == nil { |
||
| 153 | t.Errorf("missmatched expectation, got: %v, want: %v", err, tt.err) |
||
| 154 | } |
||
| 159 |