this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move text parsing tests to a new test files

These are about to get even longer, so it makes sense to separate them.
Also refactor the remaining parser tests to remove some duplication.

+335 -347
+20 -347
gitdiff/parser_test.go
··· 3 3 import ( 4 4 "bufio" 5 5 "io" 6 - "reflect" 7 6 "strings" 8 7 "testing" 9 8 ) ··· 13 12 14 13 t.Run("read", func(t *testing.T) { 15 14 p := newTestParser(content, false) 16 - if err := p.Next(); err != nil { 17 - t.Fatalf("error advancing parser: %v", err) 18 - } 19 - if p.lineno != 1 { 20 - t.Fatalf("incorrect line number: expected %d, actual: %d", 1, p.lineno) 21 - } 22 - 23 - line := p.Line(0) 24 - if line != "the first line\n" { 25 - t.Fatalf("incorrect first line: %s", line) 26 - } 27 15 28 - if err := p.Next(); err != nil { 29 - t.Fatalf("error advancing parser: %v", err) 30 - } 31 - if p.lineno != 2 { 32 - t.Fatalf("incorrect line number: expected %d, actual: %d", 2, p.lineno) 33 - } 34 - 35 - line = p.Line(0) 36 - if line != "the second line\n" { 37 - t.Fatalf("incorrect second line: %s", line) 38 - } 16 + for i, expected := range []string{ 17 + "the first line\n", 18 + "the second line\n", 19 + "the third line\n", 20 + } { 21 + if err := p.Next(); err != nil { 22 + t.Fatalf("error advancing parser after line %d: %v", i, err) 23 + } 24 + if p.lineno != int64(i+1) { 25 + t.Fatalf("incorrect line number: expected %d, actual: %d", i+1, p.lineno) 26 + } 39 27 40 - if err := p.Next(); err != nil { 41 - t.Fatalf("error advancing parser: %v", err) 42 - } 43 - if p.lineno != 3 { 44 - t.Fatalf("incorrect line number: expected %d, actual: %d", 3, p.lineno) 45 - } 46 - 47 - line = p.Line(0) 48 - if line != "the third line\n" { 49 - t.Fatalf("incorrect third line: %s", line) 28 + line := p.Line(0) 29 + if line != expected { 30 + t.Fatalf("incorrect line %d: expected %q, was %q", i+1, expected, line) 31 + } 50 32 } 51 33 52 34 // reading after the last line should return EOF 53 35 if err := p.Next(); err != io.EOF { 54 - t.Fatalf("expected EOF, but got: %v", err) 36 + t.Fatalf("expected EOF after end, but got: %v", err) 55 37 } 56 38 if p.lineno != 4 { 57 39 t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno) ··· 59 41 60 42 // reading again returns EOF again and does not advance the line 61 43 if err := p.Next(); err != io.EOF { 62 - t.Fatalf("expected EOF, but got: %v", err) 44 + t.Fatalf("expected EOF after end, but got: %v", err) 63 45 } 64 46 if p.lineno != 4 { 65 47 t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno) ··· 78 60 } 79 61 80 62 if err := p.Next(); err != nil { 81 - t.Fatalf("error advancing parser: %v", err) 63 + t.Fatalf("error advancing parser after peek: %v", err) 82 64 } 83 65 84 66 line = p.Line(0) 85 67 if line != "the second line\n" { 86 - t.Fatalf("incorrect line: %s", line) 68 + t.Fatalf("incorrect read line: %s", line) 87 69 } 88 70 }) 89 71 90 72 t.Run("emptyInput", func(t *testing.T) { 91 73 p := newTestParser("", false) 92 74 if err := p.Next(); err != io.EOF { 93 - t.Fatalf("expected EOF, but got: %v", err) 75 + t.Fatalf("expected EOF on first Next(), but got: %v", err) 94 76 } 95 77 }) 96 78 } ··· 149 131 150 132 if test.EndLine != p.Line(0) { 151 133 t.Errorf("incorrect position after parsing\nexpected: %q\nactual: %q", test.EndLine, p.Line(0)) 152 - } 153 - }) 154 - } 155 - } 156 - 157 - func TestParseTextFragmentHeader(t *testing.T) { 158 - tests := map[string]struct { 159 - Input string 160 - Output *Fragment 161 - Err bool 162 - }{ 163 - "shortest": { 164 - Input: "@@ -1 +1 @@\n", 165 - Output: &Fragment{ 166 - OldPosition: 1, 167 - OldLines: 1, 168 - NewPosition: 1, 169 - NewLines: 1, 170 - }, 171 - }, 172 - "standard": { 173 - Input: "@@ -21,5 +28,9 @@\n", 174 - Output: &Fragment{ 175 - OldPosition: 21, 176 - OldLines: 5, 177 - NewPosition: 28, 178 - NewLines: 9, 179 - }, 180 - }, 181 - "trailingComment": { 182 - Input: "@@ -21,5 +28,9 @@ func test(n int) {\n", 183 - Output: &Fragment{ 184 - Comment: "func test(n int) {", 185 - OldPosition: 21, 186 - OldLines: 5, 187 - NewPosition: 28, 188 - NewLines: 9, 189 - }, 190 - }, 191 - "incomplete": { 192 - Input: "@@ -12,3 +2\n", 193 - Err: true, 194 - }, 195 - "badNumbers": { 196 - Input: "@@ -1a,2b +3c,4d @@\n", 197 - Err: true, 198 - }, 199 - } 200 - 201 - for name, test := range tests { 202 - t.Run(name, func(t *testing.T) { 203 - p := newTestParser(test.Input, true) 204 - 205 - frag, err := p.ParseTextFragmentHeader() 206 - if test.Err { 207 - if err == nil { 208 - t.Fatalf("expected error parsing header, but got nil") 209 - } 210 - return 211 - } 212 - if err != nil { 213 - t.Fatalf("error parsing header: %v", err) 214 - } 215 - 216 - if !reflect.DeepEqual(test.Output, frag) { 217 - t.Errorf("incorrect fragment\nexpected: %+v\nactual: %+v", test.Output, frag) 218 - } 219 - }) 220 - } 221 - } 222 - 223 - func TestParseTextChunk(t *testing.T) { 224 - tests := map[string]struct { 225 - Input string 226 - Fragment Fragment 227 - 228 - Output *Fragment 229 - Err bool 230 - }{ 231 - "addWithContext": { 232 - Input: ` context line 233 - +new line 1 234 - +new line 2 235 - context line 236 - `, 237 - Fragment: Fragment{ 238 - OldLines: 2, 239 - NewLines: 4, 240 - }, 241 - Output: &Fragment{ 242 - OldLines: 2, 243 - NewLines: 4, 244 - Lines: []FragmentLine{ 245 - {OpContext, "context line\n"}, 246 - {OpAdd, "new line 1\n"}, 247 - {OpAdd, "new line 2\n"}, 248 - {OpContext, "context line\n"}, 249 - }, 250 - LinesAdded: 2, 251 - LeadingContext: 1, 252 - TrailingContext: 1, 253 - }, 254 - }, 255 - "deleteWithContext": { 256 - Input: ` context line 257 - -old line 1 258 - -old line 2 259 - context line 260 - `, 261 - Fragment: Fragment{ 262 - OldLines: 4, 263 - NewLines: 2, 264 - }, 265 - Output: &Fragment{ 266 - OldLines: 4, 267 - NewLines: 2, 268 - Lines: []FragmentLine{ 269 - {OpContext, "context line\n"}, 270 - {OpDelete, "old line 1\n"}, 271 - {OpDelete, "old line 2\n"}, 272 - {OpContext, "context line\n"}, 273 - }, 274 - LinesDeleted: 2, 275 - LeadingContext: 1, 276 - TrailingContext: 1, 277 - }, 278 - }, 279 - "replaceWithContext": { 280 - Input: ` context line 281 - -old line 1 282 - +new line 1 283 - context line 284 - `, 285 - Fragment: Fragment{ 286 - OldLines: 3, 287 - NewLines: 3, 288 - }, 289 - Output: &Fragment{ 290 - OldLines: 3, 291 - NewLines: 3, 292 - Lines: []FragmentLine{ 293 - {OpContext, "context line\n"}, 294 - {OpDelete, "old line 1\n"}, 295 - {OpAdd, "new line 1\n"}, 296 - {OpContext, "context line\n"}, 297 - }, 298 - LinesDeleted: 1, 299 - LinesAdded: 1, 300 - LeadingContext: 1, 301 - TrailingContext: 1, 302 - }, 303 - }, 304 - "deleteFinalNewline": { 305 - Input: ` context line 306 - -old line 1 307 - +new line 1 308 - \ No newline at end of file 309 - `, 310 - Fragment: Fragment{ 311 - OldLines: 2, 312 - NewLines: 2, 313 - }, 314 - Output: &Fragment{ 315 - OldLines: 2, 316 - NewLines: 2, 317 - Lines: []FragmentLine{ 318 - {OpContext, "context line\n"}, 319 - {OpDelete, "old line 1\n"}, 320 - {OpAdd, "new line 1"}, 321 - }, 322 - LinesDeleted: 1, 323 - LinesAdded: 1, 324 - LeadingContext: 1, 325 - }, 326 - }, 327 - "addFinalNewline": { 328 - Input: ` context line 329 - -old line 1 330 - \ No newline at end of file 331 - +new line 1 332 - `, 333 - Fragment: Fragment{ 334 - OldLines: 2, 335 - NewLines: 2, 336 - }, 337 - Output: &Fragment{ 338 - OldLines: 2, 339 - NewLines: 2, 340 - Lines: []FragmentLine{ 341 - {OpContext, "context line\n"}, 342 - {OpDelete, "old line 1"}, 343 - {OpAdd, "new line 1\n"}, 344 - }, 345 - LinesDeleted: 1, 346 - LinesAdded: 1, 347 - LeadingContext: 1, 348 - }, 349 - }, 350 - "addAll": { 351 - Input: `+new line 1 352 - +new line 2 353 - +new line 3 354 - `, 355 - Fragment: Fragment{ 356 - OldLines: 0, 357 - NewLines: 3, 358 - }, 359 - Output: &Fragment{ 360 - OldLines: 0, 361 - NewLines: 3, 362 - Lines: []FragmentLine{ 363 - {OpAdd, "new line 1\n"}, 364 - {OpAdd, "new line 2\n"}, 365 - {OpAdd, "new line 3\n"}, 366 - }, 367 - LinesAdded: 3, 368 - }, 369 - }, 370 - "deleteAll": { 371 - Input: `-old line 1 372 - -old line 2 373 - -old line 3 374 - `, 375 - Fragment: Fragment{ 376 - OldLines: 3, 377 - NewLines: 0, 378 - }, 379 - Output: &Fragment{ 380 - OldLines: 3, 381 - NewLines: 0, 382 - Lines: []FragmentLine{ 383 - {OpDelete, "old line 1\n"}, 384 - {OpDelete, "old line 2\n"}, 385 - {OpDelete, "old line 3\n"}, 386 - }, 387 - LinesDeleted: 3, 388 - }, 389 - }, 390 - "emptyContextLine": { 391 - Input: ` context line 392 - 393 - +new line 394 - context line 395 - `, 396 - Fragment: Fragment{ 397 - OldLines: 3, 398 - NewLines: 4, 399 - }, 400 - Output: &Fragment{ 401 - OldLines: 3, 402 - NewLines: 4, 403 - Lines: []FragmentLine{ 404 - {OpContext, "context line\n"}, 405 - {OpContext, "\n"}, 406 - {OpAdd, "new line\n"}, 407 - {OpContext, "context line\n"}, 408 - }, 409 - LinesAdded: 1, 410 - LeadingContext: 2, 411 - TrailingContext: 1, 412 - }, 413 - }, 414 - "emptyChunk": { 415 - Input: "", 416 - Err: true, 417 - }, 418 - "invalidOperation": { 419 - Input: ` context line 420 - ?wat line 421 - context line 422 - `, 423 - Fragment: Fragment{ 424 - OldLines: 3, 425 - NewLines: 3, 426 - }, 427 - Err: true, 428 - }, 429 - "unbalancedHeader": { 430 - Input: ` context line 431 - -old line 1 432 - +new line 1 433 - context line 434 - `, 435 - Fragment: Fragment{ 436 - OldLines: 2, 437 - NewLines: 5, 438 - }, 439 - Err: true, 440 - }, 441 - } 442 - 443 - for name, test := range tests { 444 - t.Run(name, func(t *testing.T) { 445 - p := newTestParser(test.Input, true) 446 - 447 - frag := test.Fragment 448 - err := p.ParseTextChunk(&frag) 449 - if test.Err { 450 - if err == nil { 451 - t.Fatalf("expected error parsing text chunk, but got nil") 452 - } 453 - return 454 - } 455 - if err != nil { 456 - t.Fatalf("error parsing text chunk: %v", err) 457 - } 458 - 459 - if !reflect.DeepEqual(test.Output, &frag) { 460 - t.Errorf("incorrect fragment\nexpected: %+v\nactual: %+v", test.Output, &frag) 461 134 } 462 135 }) 463 136 }
+315
gitdiff/parser_text_test.go
··· 1 + package gitdiff 2 + 3 + import ( 4 + "reflect" 5 + "testing" 6 + ) 7 + 8 + func TestParseTextFragmentHeader(t *testing.T) { 9 + tests := map[string]struct { 10 + Input string 11 + Output *Fragment 12 + Err bool 13 + }{ 14 + "shortest": { 15 + Input: "@@ -1 +1 @@\n", 16 + Output: &Fragment{ 17 + OldPosition: 1, 18 + OldLines: 1, 19 + NewPosition: 1, 20 + NewLines: 1, 21 + }, 22 + }, 23 + "standard": { 24 + Input: "@@ -21,5 +28,9 @@\n", 25 + Output: &Fragment{ 26 + OldPosition: 21, 27 + OldLines: 5, 28 + NewPosition: 28, 29 + NewLines: 9, 30 + }, 31 + }, 32 + "trailingComment": { 33 + Input: "@@ -21,5 +28,9 @@ func test(n int) {\n", 34 + Output: &Fragment{ 35 + Comment: "func test(n int) {", 36 + OldPosition: 21, 37 + OldLines: 5, 38 + NewPosition: 28, 39 + NewLines: 9, 40 + }, 41 + }, 42 + "incomplete": { 43 + Input: "@@ -12,3 +2\n", 44 + Err: true, 45 + }, 46 + "badNumbers": { 47 + Input: "@@ -1a,2b +3c,4d @@\n", 48 + Err: true, 49 + }, 50 + } 51 + 52 + for name, test := range tests { 53 + t.Run(name, func(t *testing.T) { 54 + p := newTestParser(test.Input, true) 55 + 56 + frag, err := p.ParseTextFragmentHeader() 57 + if test.Err { 58 + if err == nil { 59 + t.Fatalf("expected error parsing header, but got nil") 60 + } 61 + return 62 + } 63 + if err != nil { 64 + t.Fatalf("error parsing header: %v", err) 65 + } 66 + 67 + if !reflect.DeepEqual(test.Output, frag) { 68 + t.Errorf("incorrect fragment\nexpected: %+v\nactual: %+v", test.Output, frag) 69 + } 70 + }) 71 + } 72 + } 73 + 74 + func TestParseTextChunk(t *testing.T) { 75 + tests := map[string]struct { 76 + Input string 77 + Fragment Fragment 78 + 79 + Output *Fragment 80 + Err bool 81 + }{ 82 + "addWithContext": { 83 + Input: ` context line 84 + +new line 1 85 + +new line 2 86 + context line 87 + `, 88 + Fragment: Fragment{ 89 + OldLines: 2, 90 + NewLines: 4, 91 + }, 92 + Output: &Fragment{ 93 + OldLines: 2, 94 + NewLines: 4, 95 + Lines: []FragmentLine{ 96 + {OpContext, "context line\n"}, 97 + {OpAdd, "new line 1\n"}, 98 + {OpAdd, "new line 2\n"}, 99 + {OpContext, "context line\n"}, 100 + }, 101 + LinesAdded: 2, 102 + LeadingContext: 1, 103 + TrailingContext: 1, 104 + }, 105 + }, 106 + "deleteWithContext": { 107 + Input: ` context line 108 + -old line 1 109 + -old line 2 110 + context line 111 + `, 112 + Fragment: Fragment{ 113 + OldLines: 4, 114 + NewLines: 2, 115 + }, 116 + Output: &Fragment{ 117 + OldLines: 4, 118 + NewLines: 2, 119 + Lines: []FragmentLine{ 120 + {OpContext, "context line\n"}, 121 + {OpDelete, "old line 1\n"}, 122 + {OpDelete, "old line 2\n"}, 123 + {OpContext, "context line\n"}, 124 + }, 125 + LinesDeleted: 2, 126 + LeadingContext: 1, 127 + TrailingContext: 1, 128 + }, 129 + }, 130 + "replaceWithContext": { 131 + Input: ` context line 132 + -old line 1 133 + +new line 1 134 + context line 135 + `, 136 + Fragment: Fragment{ 137 + OldLines: 3, 138 + NewLines: 3, 139 + }, 140 + Output: &Fragment{ 141 + OldLines: 3, 142 + NewLines: 3, 143 + Lines: []FragmentLine{ 144 + {OpContext, "context line\n"}, 145 + {OpDelete, "old line 1\n"}, 146 + {OpAdd, "new line 1\n"}, 147 + {OpContext, "context line\n"}, 148 + }, 149 + LinesDeleted: 1, 150 + LinesAdded: 1, 151 + LeadingContext: 1, 152 + TrailingContext: 1, 153 + }, 154 + }, 155 + "deleteFinalNewline": { 156 + Input: ` context line 157 + -old line 1 158 + +new line 1 159 + \ No newline at end of file 160 + `, 161 + Fragment: Fragment{ 162 + OldLines: 2, 163 + NewLines: 2, 164 + }, 165 + Output: &Fragment{ 166 + OldLines: 2, 167 + NewLines: 2, 168 + Lines: []FragmentLine{ 169 + {OpContext, "context line\n"}, 170 + {OpDelete, "old line 1\n"}, 171 + {OpAdd, "new line 1"}, 172 + }, 173 + LinesDeleted: 1, 174 + LinesAdded: 1, 175 + LeadingContext: 1, 176 + }, 177 + }, 178 + "addFinalNewline": { 179 + Input: ` context line 180 + -old line 1 181 + \ No newline at end of file 182 + +new line 1 183 + `, 184 + Fragment: Fragment{ 185 + OldLines: 2, 186 + NewLines: 2, 187 + }, 188 + Output: &Fragment{ 189 + OldLines: 2, 190 + NewLines: 2, 191 + Lines: []FragmentLine{ 192 + {OpContext, "context line\n"}, 193 + {OpDelete, "old line 1"}, 194 + {OpAdd, "new line 1\n"}, 195 + }, 196 + LinesDeleted: 1, 197 + LinesAdded: 1, 198 + LeadingContext: 1, 199 + }, 200 + }, 201 + "addAll": { 202 + Input: `+new line 1 203 + +new line 2 204 + +new line 3 205 + `, 206 + Fragment: Fragment{ 207 + OldLines: 0, 208 + NewLines: 3, 209 + }, 210 + Output: &Fragment{ 211 + OldLines: 0, 212 + NewLines: 3, 213 + Lines: []FragmentLine{ 214 + {OpAdd, "new line 1\n"}, 215 + {OpAdd, "new line 2\n"}, 216 + {OpAdd, "new line 3\n"}, 217 + }, 218 + LinesAdded: 3, 219 + }, 220 + }, 221 + "deleteAll": { 222 + Input: `-old line 1 223 + -old line 2 224 + -old line 3 225 + `, 226 + Fragment: Fragment{ 227 + OldLines: 3, 228 + NewLines: 0, 229 + }, 230 + Output: &Fragment{ 231 + OldLines: 3, 232 + NewLines: 0, 233 + Lines: []FragmentLine{ 234 + {OpDelete, "old line 1\n"}, 235 + {OpDelete, "old line 2\n"}, 236 + {OpDelete, "old line 3\n"}, 237 + }, 238 + LinesDeleted: 3, 239 + }, 240 + }, 241 + "emptyContextLine": { 242 + Input: ` context line 243 + 244 + +new line 245 + context line 246 + `, 247 + Fragment: Fragment{ 248 + OldLines: 3, 249 + NewLines: 4, 250 + }, 251 + Output: &Fragment{ 252 + OldLines: 3, 253 + NewLines: 4, 254 + Lines: []FragmentLine{ 255 + {OpContext, "context line\n"}, 256 + {OpContext, "\n"}, 257 + {OpAdd, "new line\n"}, 258 + {OpContext, "context line\n"}, 259 + }, 260 + LinesAdded: 1, 261 + LeadingContext: 2, 262 + TrailingContext: 1, 263 + }, 264 + }, 265 + "emptyChunk": { 266 + Input: "", 267 + Err: true, 268 + }, 269 + "invalidOperation": { 270 + Input: ` context line 271 + ?wat line 272 + context line 273 + `, 274 + Fragment: Fragment{ 275 + OldLines: 3, 276 + NewLines: 3, 277 + }, 278 + Err: true, 279 + }, 280 + "unbalancedHeader": { 281 + Input: ` context line 282 + -old line 1 283 + +new line 1 284 + context line 285 + `, 286 + Fragment: Fragment{ 287 + OldLines: 2, 288 + NewLines: 5, 289 + }, 290 + Err: true, 291 + }, 292 + } 293 + 294 + for name, test := range tests { 295 + t.Run(name, func(t *testing.T) { 296 + p := newTestParser(test.Input, true) 297 + 298 + frag := test.Fragment 299 + err := p.ParseTextChunk(&frag) 300 + if test.Err { 301 + if err == nil { 302 + t.Fatalf("expected error parsing text chunk, but got nil") 303 + } 304 + return 305 + } 306 + if err != nil { 307 + t.Fatalf("error parsing text chunk: %v", err) 308 + } 309 + 310 + if !reflect.DeepEqual(test.Output, &frag) { 311 + t.Errorf("incorrect fragment\nexpected: %+v\nactual: %+v", test.Output, &frag) 312 + } 313 + }) 314 + } 315 + }