|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Weburz/crisp/internal/parser" |
| 8 | +) |
| 9 | + |
| 10 | +// TestCheckMessageType tests the checkMessageType function. |
| 11 | +func TestCheckMessageType(t *testing.T) { |
| 12 | + testCases := []struct { |
| 13 | + name string |
| 14 | + messageType string |
| 15 | + expectedError string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "Valid Type - feat", |
| 19 | + messageType: "feat", |
| 20 | + expectedError: "", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "Invalid Type - Uppercase", |
| 24 | + messageType: "Feat", |
| 25 | + expectedError: `invalid commit message casing, "Feat" should be "feat"`, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + for _, tc := range testCases { |
| 30 | + t.Run(tc.name, func(t *testing.T) { |
| 31 | + err := checkMessageType(&tc.messageType) |
| 32 | + if tc.expectedError == "" { |
| 33 | + if err != nil { |
| 34 | + t.Errorf("expected no error, got %v", err) |
| 35 | + } |
| 36 | + } else { |
| 37 | + if err == nil || err.Error() != tc.expectedError { |
| 38 | + t.Errorf("expected error %q, got %q", tc.expectedError, err) |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestCheckMessageScope tests the checkMessageScope function. |
| 46 | +func TestCheckMessageScope(t *testing.T) { |
| 47 | + testCases := []struct { |
| 48 | + name string |
| 49 | + scope string |
| 50 | + expectedError string |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "Valid Scope - Lowercase", |
| 54 | + scope: "user", |
| 55 | + expectedError: "", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Valid Scope - Empty", |
| 59 | + scope: "", |
| 60 | + expectedError: "", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "Invalid Scope - Uppercase", |
| 64 | + scope: "User", |
| 65 | + expectedError: `invalid commit message scope casing, "User" should be "user"`, // Expected error message |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tc := range testCases { |
| 70 | + t.Run(tc.name, func(t *testing.T) { |
| 71 | + err := checkMessageScope(&tc.scope) |
| 72 | + if tc.expectedError == "" { |
| 73 | + if err != nil { |
| 74 | + t.Errorf("expected no error, got %v", err) |
| 75 | + } |
| 76 | + } else { |
| 77 | + if err == nil || err.Error() != tc.expectedError { |
| 78 | + t.Errorf("expected error %q, got %q", tc.expectedError, err) |
| 79 | + } |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestCheckMessageSubject tests the checkMessageSubject function. |
| 86 | +func TestCheckMessageSubject(t *testing.T) { |
| 87 | + testCases := []struct { |
| 88 | + name string |
| 89 | + subject string |
| 90 | + expectedError string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "Valid Subject - Lowercase", |
| 94 | + subject: "add a new feature", |
| 95 | + expectedError: "", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Invalid Subject - Uppercase", |
| 99 | + subject: "Add a new feature", |
| 100 | + expectedError: "commit message subject should be lowercased & not end with a period(.)", |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tc := range testCases { |
| 105 | + t.Run(tc.name, func(t *testing.T) { |
| 106 | + err := checkMessageSubject(&tc.subject) |
| 107 | + if tc.expectedError == "" { |
| 108 | + if err != nil { |
| 109 | + t.Errorf("expected no error, got %v", err) |
| 110 | + } |
| 111 | + } else { |
| 112 | + if err == nil || err.Error() != tc.expectedError { |
| 113 | + t.Errorf("expected error %q, got %q", tc.expectedError, err) |
| 114 | + } |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestCheckMessageLength tests the checkMessageLength function. |
| 121 | +func TestCheckMessageLength(t *testing.T) { |
| 122 | + testCases := []struct { |
| 123 | + name string |
| 124 | + message string |
| 125 | + expectedError string |
| 126 | + }{ |
| 127 | + { |
| 128 | + name: "Valid Length", |
| 129 | + message: "feat: add a new feature", |
| 130 | + expectedError: "", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "Invalid Length", |
| 134 | + message: "Add a new feature that makes the application run faster and more efficiently", |
| 135 | + expectedError: fmt.Sprintf("commit message exceeds 50 characters, current length: %d", |
| 136 | + len("Add a new feature that makes the application run faster and more efficiently")), |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tc := range testCases { |
| 141 | + t.Run(tc.name, func(t *testing.T) { |
| 142 | + err := checkMessageLength(&tc.message) |
| 143 | + if tc.expectedError == "" { |
| 144 | + if err != nil { |
| 145 | + t.Errorf("expected no error, got %v", err) |
| 146 | + } |
| 147 | + } else { |
| 148 | + if err == nil || err.Error() != tc.expectedError { |
| 149 | + t.Errorf("expected error %q, got %q", tc.expectedError, err) |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// TestValidateMessage tests the ValidateMessage function. |
| 157 | +func TestValidateMessage(t *testing.T) { |
| 158 | + testCases := []struct { |
| 159 | + name string |
| 160 | + message parser.Message |
| 161 | + expectedError string |
| 162 | + expectedMessage string |
| 163 | + }{ |
| 164 | + { |
| 165 | + name: "Valid Message", |
| 166 | + message: parser.Message{ |
| 167 | + Type: "feat", |
| 168 | + Scope: "user", |
| 169 | + Description: "add a new feature", |
| 170 | + }, |
| 171 | + expectedError: "", |
| 172 | + expectedMessage: "valid commit message", |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "Invalid Message - Invalid Type", |
| 176 | + message: parser.Message{ |
| 177 | + Type: "Invalid", |
| 178 | + Scope: "user", |
| 179 | + Description: "add a new feature", |
| 180 | + }, |
| 181 | + expectedError: `invalid commit message type: Invalid`, |
| 182 | + expectedMessage: "", |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "Invalid Message - Invalid Scope", |
| 186 | + message: parser.Message{ |
| 187 | + Type: "feat", |
| 188 | + Scope: "User", |
| 189 | + Description: "add a new feature", |
| 190 | + }, |
| 191 | + expectedError: `invalid commit message scope casing, "User" should be "user"`, |
| 192 | + expectedMessage: "", |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "Invalid Message - Invalid Subject", |
| 196 | + message: parser.Message{ |
| 197 | + Type: "feat", |
| 198 | + Scope: "user", |
| 199 | + Description: "Add a new feature", |
| 200 | + }, |
| 201 | + expectedError: "commit message subject should be lowercased & not end with a period(.)", |
| 202 | + expectedMessage: "", |
| 203 | + }, |
| 204 | + { |
| 205 | + name: "Invalid Message - Exceeds Length", |
| 206 | + message: parser.Message{ |
| 207 | + Type: "feat", |
| 208 | + Scope: "user", |
| 209 | + Description: "add a new feature that makes the application run faster and more efficiently", // More than 50 chars |
| 210 | + }, |
| 211 | + expectedError: fmt.Sprintf("commit message exceeds 50 characters, current length: %d", |
| 212 | + len("Add a new feature that makes the application run faster and more efficiently")), |
| 213 | + expectedMessage: "", |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "Valid Message - Empty Scope", |
| 217 | + message: parser.Message{ |
| 218 | + Type: "feat", |
| 219 | + Scope: "", |
| 220 | + Description: "add a new feature", |
| 221 | + }, |
| 222 | + expectedError: "", |
| 223 | + expectedMessage: "valid commit message", |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tc := range testCases { |
| 228 | + t.Run(tc.name, func(t *testing.T) { |
| 229 | + messageResult, err := ValidateMessage(&tc.message) |
| 230 | + |
| 231 | + if tc.expectedError == "" && err != nil { |
| 232 | + t.Errorf("expected no error, got %v", err) |
| 233 | + } else if err != nil && err.Error() != tc.expectedError { |
| 234 | + t.Errorf("expected error %q, got %q", tc.expectedError, err.Error()) |
| 235 | + } |
| 236 | + |
| 237 | + if messageResult != tc.expectedMessage { |
| 238 | + t.Errorf("expected message %q, got %q", tc.expectedMessage, messageResult) |
| 239 | + } |
| 240 | + }) |
| 241 | + } |
| 242 | +} |
0 commit comments