| Conditions | 7 |
| Total Lines | 96 |
| Code Lines | 69 |
| 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 domain_test |
||
| 118 | func TestCreateDeck_Validate(t *testing.T) { |
||
| 119 | testCases := []struct { |
||
| 120 | createDeck *domain.CreateDeck |
||
| 121 | name string |
||
| 122 | shouldErr bool |
||
| 123 | }{ |
||
| 124 | { |
||
| 125 | name: "Valid CreateDeck", |
||
| 126 | createDeck: &domain.CreateDeck{ |
||
| 127 | Name: "Test Name", |
||
| 128 | Description: "Test Description", |
||
| 129 | Lang: "EN", |
||
| 130 | Banner: "https://test.com/banner.png", |
||
| 131 | }, |
||
| 132 | shouldErr: false, |
||
| 133 | }, |
||
| 134 | { |
||
| 135 | name: "No Name CreateDeck", |
||
| 136 | createDeck: &domain.CreateDeck{ |
||
| 137 | Name: "", |
||
| 138 | Description: "Test Description", |
||
| 139 | Lang: "EN", |
||
| 140 | Banner: "https://test.com/banner.png", |
||
| 141 | }, |
||
| 142 | shouldErr: true, |
||
| 143 | }, |
||
| 144 | { |
||
| 145 | name: "No Description CreateDeck", |
||
| 146 | createDeck: &domain.CreateDeck{ |
||
| 147 | Name: "Test Name", |
||
| 148 | Description: "", |
||
| 149 | Lang: "EN", |
||
| 150 | Banner: "https://test.com/banner.png", |
||
| 151 | }, |
||
| 152 | shouldErr: true, |
||
| 153 | }, |
||
| 154 | { |
||
| 155 | name: "No Lang CreateDeck", |
||
| 156 | createDeck: &domain.CreateDeck{ |
||
| 157 | Name: "Test Name", |
||
| 158 | Description: "Test Description", |
||
| 159 | Lang: "", |
||
| 160 | Banner: "https://test.com/banner.png", |
||
| 161 | }, |
||
| 162 | shouldErr: true, |
||
| 163 | }, |
||
| 164 | { |
||
| 165 | name: "No Banner CreateDeck", |
||
| 166 | createDeck: &domain.CreateDeck{ |
||
| 167 | Name: "Test Name", |
||
| 168 | Description: "Test Description", |
||
| 169 | Lang: "EN", |
||
| 170 | Banner: "", |
||
| 171 | }, |
||
| 172 | shouldErr: true, |
||
| 173 | }, |
||
| 174 | { |
||
| 175 | name: "Invalid Lang CreateDeck", |
||
| 176 | createDeck: &domain.CreateDeck{ |
||
| 177 | Name: "Test Name", |
||
| 178 | Description: "Test Description", |
||
| 179 | Lang: "ENGLISH", |
||
| 180 | Banner: "https://test.com/banner.png", |
||
| 181 | }, |
||
| 182 | shouldErr: true, |
||
| 183 | }, |
||
| 184 | { |
||
| 185 | name: "Invalid Banner CreateDeck", |
||
| 186 | createDeck: &domain.CreateDeck{ |
||
| 187 | Name: "Test Name", |
||
| 188 | Description: "Test Description", |
||
| 189 | Lang: "EN", |
||
| 190 | Banner: "test.com/banner.png", |
||
| 191 | }, |
||
| 192 | shouldErr: true, |
||
| 193 | }, |
||
| 194 | { |
||
| 195 | name: "Invalid Language CreateDeck", |
||
| 196 | createDeck: &domain.CreateDeck{ |
||
| 197 | Name: "Test Name", |
||
| 198 | Description: "Test Description", |
||
| 199 | Lang: "INVALID", |
||
| 200 | Banner: "https://test.com/banner.png", |
||
| 201 | }, |
||
| 202 | shouldErr: true, |
||
| 203 | }, |
||
| 204 | } |
||
| 205 | |||
| 206 | for _, tc := range testCases { |
||
| 207 | t.Run(tc.name, func(t *testing.T) { |
||
| 208 | err := tc.createDeck.Validate() |
||
| 209 | if tc.shouldErr && err == nil { |
||
| 210 | t.Errorf("Expected error, but got nil") |
||
| 211 | } |
||
| 212 | if !tc.shouldErr && err != nil { |
||
| 213 | t.Errorf("Expected no error, but got %v", err) |
||
| 214 | } |
||
| 218 |