fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

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

use testIndexBuilder helper in index_test

+51 -99
+51 -99
index_test.go
··· 43 43 } 44 44 45 45 func testIndexBuilder(t *testing.T, repo *Repository, docs ...Document) *IndexBuilder { 46 + t.Helper() 47 + 46 48 b, err := NewIndexBuilder(repo) 47 49 b.contentBloom.bits = b.contentBloom.bits[:bloomSizeTest] 48 50 b.nameBloom.bits = b.nameBloom.bits[:bloomSizeTest] ··· 280 282 } 281 283 282 284 func TestAndSearch(t *testing.T) { 283 - b, err := NewIndexBuilder(nil) 284 - if err != nil { 285 - t.Fatalf("NewIndexBuilder: %v", err) 286 - } 287 - 288 - b.AddFile("f1", []byte("x banana y")) 289 - b.AddFile("f2", []byte("x apple y")) 290 - b.AddFile("f3", []byte("x banana apple y")) 291 - // ---------------------0123456789012345 285 + b := testIndexBuilder(t, nil, 286 + Document{Name: "f1", Content: []byte("x banana y")}, 287 + Document{Name: "f2", Content: []byte("x apple y")}, 288 + Document{Name: "f3", Content: []byte("x banana apple y")}) 289 + // -------------------------------------0123456789012345 292 290 sres := searchForTest(t, b, query.NewAnd( 293 291 &query.Substring{ 294 292 Pattern: "banana", ··· 322 320 } 323 321 324 322 func TestAndNegateSearch(t *testing.T) { 325 - b, err := NewIndexBuilder(nil) 326 - if err != nil { 327 - t.Fatalf("NewIndexBuilder: %v", err) 328 - } 329 - 330 - b.AddFile("f1", []byte("x banana y")) 331 - b.AddFile("f4", []byte("x banana apple y")) 332 - // ---------------------0123456789012345 323 + b := testIndexBuilder(t, nil, 324 + Document{Name: "f1", Content: []byte("x banana y")}, 325 + Document{Name: "f4", Content: []byte("x banana apple y")}) 326 + // -------------------------------------0123456789012345 333 327 sres := searchForTest(t, b, query.NewAnd( 334 328 &query.Substring{ 335 329 Pattern: "banana", ··· 352 346 } 353 347 354 348 func TestNegativeMatchesOnlyShortcut(t *testing.T) { 355 - b, err := NewIndexBuilder(nil) 356 - if err != nil { 357 - t.Fatalf("NewIndexBuilder: %v", err) 358 - } 359 - 360 - b.AddFile("f1", []byte("x banana y")) 361 - b.AddFile("f2", []byte("x appelmoes y")) 362 - b.AddFile("f3", []byte("x appelmoes y")) 363 - b.AddFile("f3", []byte("x appelmoes y")) 349 + b := testIndexBuilder(t, nil, 350 + Document{Name: "f1", Content: []byte("x banana y")}, 351 + Document{Name: "f2", Content: []byte("x appelmoes y")}, 352 + Document{Name: "f3", Content: []byte("x appelmoes y")}, 353 + Document{Name: "f3", Content: []byte("x appelmoes y")}) 364 354 365 355 sres := searchForTest(t, b, query.NewAnd( 366 356 &query.Substring{ ··· 376 366 } 377 367 378 368 func TestFileSearch(t *testing.T) { 379 - b, err := NewIndexBuilder(nil) 380 - if err != nil { 381 - t.Fatalf("NewIndexBuilder: %v", err) 382 - } 383 - 384 - b.AddFile("banzana", []byte("x orange y")) 385 - // --------0123456 386 - b.AddFile("banana", []byte("x apple y")) 387 - // --------789012 369 + b := testIndexBuilder(t, nil, 370 + Document{Name: "banzana", Content: []byte("x orange y")}, 371 + // -------------0123456 372 + Document{Name: "banana", Content: []byte("x apple y")}, 373 + // -------------789012 374 + ) 388 375 sres := searchForTest(t, b, &query.Substring{ 389 376 Pattern: "anan", 390 377 FileName: true, ··· 412 399 } 413 400 414 401 func TestFileCase(t *testing.T) { 415 - b, err := NewIndexBuilder(nil) 416 - if err != nil { 417 - t.Fatalf("NewIndexBuilder: %v", err) 418 - } 419 - 420 - b.AddFile("BANANA", []byte("x orange y")) 402 + b := testIndexBuilder(t, nil, 403 + Document{Name: "BANANA", Content: []byte("x orange y")}) 421 404 sres := searchForTest(t, b, &query.Substring{ 422 405 Pattern: "banana", 423 406 FileName: true, ··· 430 413 } 431 414 432 415 func TestFileRegexpSearchBruteForce(t *testing.T) { 433 - b, err := NewIndexBuilder(nil) 434 - if err != nil { 435 - t.Fatalf("NewIndexBuilder: %v", err) 436 - } 437 - 438 - b.AddFile("banzana", []byte("x orange y")) 439 - // --------------------------0123456879 440 - b.AddFile("banana", []byte("x apple y")) 416 + b := testIndexBuilder(t, nil, 417 + Document{Name: "banzana", Content: []byte("x orange y")}, 418 + // ----------------------------------------0123456879 419 + Document{Name: "banana", Content: []byte("x apple y")}) 441 420 sres := searchForTest(t, b, &query.Regexp{ 442 421 Regexp: mustParseRE("[qn][zx]"), 443 422 FileName: true, ··· 450 429 } 451 430 452 431 func TestFileRegexpSearchShortString(t *testing.T) { 453 - b, err := NewIndexBuilder(nil) 454 - if err != nil { 455 - t.Fatalf("NewIndexBuilder: %v", err) 456 - } 457 - 458 - b.AddFile("banana.py", []byte("x orange y")) 432 + b := testIndexBuilder(t, nil, 433 + Document{Name: "banana.py", Content: []byte("x orange y")}) 459 434 sres := searchForTest(t, b, &query.Regexp{ 460 435 Regexp: mustParseRE("ana.py"), 461 436 FileName: true, ··· 468 443 } 469 444 470 445 func TestFileSubstringSearchBruteForce(t *testing.T) { 471 - b, err := NewIndexBuilder(nil) 472 - if err != nil { 473 - t.Fatalf("NewIndexBuilder: %v", err) 474 - } 475 - 476 - b.AddFile("BANZANA", []byte("x orange y")) 477 - b.AddFile("banana", []byte("x apple y")) 446 + b := testIndexBuilder(t, nil, 447 + Document{Name: "BANZANA", Content: []byte("x orange y")}, 448 + Document{Name: "banana", Content: []byte("x apple y")}) 478 449 479 450 q := &query.Substring{ 480 451 Pattern: "z", ··· 488 459 } 489 460 490 461 func TestFileSubstringSearchBruteForceEnd(t *testing.T) { 491 - b, err := NewIndexBuilder(nil) 492 - if err != nil { 493 - t.Fatalf("NewIndexBuilder: %v", err) 494 - } 495 - 496 - b.AddFile("BANZANA", []byte("x orange y")) 497 - b.AddFile("bananaq", []byte("x apple y")) 462 + b := testIndexBuilder(t, nil, 463 + Document{Name: "BANZANA", Content: []byte("x orange y")}, 464 + Document{Name: "bananaq", Content: []byte("x apple y")}) 498 465 499 466 q := &query.Substring{ 500 467 Pattern: "q", ··· 508 475 } 509 476 510 477 func TestSearchMatchAll(t *testing.T) { 511 - b, err := NewIndexBuilder(nil) 512 - if err != nil { 513 - t.Fatalf("NewIndexBuilder: %v", err) 514 - } 515 - 516 - b.AddFile("banzana", []byte("x orange y")) 517 - // --------------------------0123456879 518 - b.AddFile("banana", []byte("x apple y")) 478 + b := testIndexBuilder(t, nil, 479 + Document{Name: "banzana", Content: []byte("x orange y")}, 480 + // ----------------------------------------0123456879 481 + Document{Name: "banana", Content: []byte("x apple y")}) 519 482 sres := searchForTest(t, b, &query.Const{Value: true}) 520 483 521 484 matches := sres.Files ··· 525 488 } 526 489 527 490 func TestSearchNewline(t *testing.T) { 528 - b, err := NewIndexBuilder(nil) 529 - if err != nil { 530 - t.Fatalf("NewIndexBuilder: %v", err) 531 - } 532 - 533 - b.AddFile("banzana", []byte("abcd\ndefg")) 491 + b := testIndexBuilder(t, nil, 492 + Document{Name: "banzana", Content: []byte("abcd\ndefg")}) 534 493 sres := searchForTest(t, b, &query.Substring{Pattern: "d\nd"}) 535 494 536 495 // Just check that we don't crash. ··· 542 501 } 543 502 544 503 func TestSearchMatchAllRegexp(t *testing.T) { 545 - b, err := NewIndexBuilder(nil) 546 - if err != nil { 547 - t.Fatalf("NewIndexBuilder: %v", err) 548 - } 549 - 550 - b.AddFile("banzana", []byte("abcd")) 551 - // --------------------------0123456879 552 - b.AddFile("banana", []byte("pqrs")) 504 + b := testIndexBuilder(t, nil, 505 + Document{Name: "banzana", Content: []byte("abcd")}, 506 + // ----------------------------------------0123456879 507 + Document{Name: "banana", Content: []byte("pqrs")}) 553 508 sres := searchForTest(t, b, &query.Regexp{Regexp: mustParseRE(".")}) 554 509 555 510 matches := sres.Files ··· 562 517 } 563 518 564 519 func TestFileRestriction(t *testing.T) { 565 - b, err := NewIndexBuilder(nil) 566 - if err != nil { 567 - t.Fatalf("NewIndexBuilder: %v", err) 568 - } 569 520 570 - b.AddFile("banana1", []byte("x orange y")) 571 - // --------------------------0123456879 572 - b.AddFile("banana2", []byte("x apple y")) 573 - b.AddFile("orange", []byte("x apple y")) 521 + b := testIndexBuilder(t, nil, 522 + Document{Name: "banana1", Content: []byte("x orange y")}, 523 + // ----------------------------------------0123456879 524 + Document{Name: "banana2", Content: []byte("x apple y")}, 525 + Document{Name: "orange", Content: []byte("x apple y")}) 574 526 sres := searchForTest(t, b, query.NewAnd( 575 527 &query.Substring{ 576 528 Pattern: "banana",