Przeglądaj źródła

Add testcase for tagshow/taghide

Raul Silvera 8 lat temu
rodzic
commit
ed301b091e
1 zmienionych plików z 43 dodań i 0 usunięć
  1. 43
    0
      profile/profile_test.go

+ 43
- 0
profile/profile_test.go Wyświetl plik

@@ -507,6 +507,49 @@ func TestFilter(t *testing.T) {
507 507
 	}
508 508
 }
509 509
 
510
+func TestTagFilter(t *testing.T) {
511
+     // Perform several forms of tag filtering on the test profile.
512
+
513
+	type filterTestcase struct {
514
+		include, exclude *regexp.Regexp
515
+		im, em bool
516
+		count int
517
+	}
518
+
519
+	countTags := func (p *Profile) map[string]bool {
520
+		  tags := make(map[string]bool)
521
+
522
+		  for _, s := range p.Sample {
523
+		      for l := range s.Label {
524
+		      	  tags[l] = true
525
+		      }		      
526
+		      for l := range s.NumLabel {
527
+		      	  tags[l] = true
528
+		      }		      
529
+		  }
530
+		  return tags
531
+	}	
532
+	
533
+	for tx, tc := range []filterTestcase{
534
+		{nil, nil, true, false, 3},
535
+		{regexp.MustCompile("notfound"), nil, false, false, 0},
536
+		{regexp.MustCompile("key1"), nil, true, false, 1},
537
+		{nil, regexp.MustCompile("key[12]"), true, true, 1},
538
+	} {
539
+		prof := testProfile.Copy()
540
+		gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
541
+		if  gim != tc.im {
542
+			t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
543
+		}		
544
+		if  gem != tc.em {
545
+			t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
546
+		}		
547
+		if  tags := countTags(prof) ; len(tags) != tc.count {
548
+			t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
549
+		}		
550
+	}
551
+}
552
+
510 553
 // locationHash constructs a string to use as a hashkey for a sample, based on its locations
511 554
 func locationHash(s *Sample) string {
512 555
 	var tb string