Browse Source

moved filter tests into their own test file which matches the implementation file, filter.go. (#356)

lvng 7 years ago
parent
commit
94bbd8ee36
2 changed files with 115 additions and 96 deletions
  1. 115
    0
      profile/filter_test.go
  2. 0
    96
      profile/profile_test.go

+ 115
- 0
profile/filter_test.go View File

@@ -0,0 +1,115 @@
1
+// Copyright 2014 Google Inc. All Rights Reserved.
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+//     http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package profile
16
+
17
+import (
18
+	"regexp"
19
+	"testing"
20
+)
21
+
22
+func TestFilter(t *testing.T) {
23
+	// Perform several forms of filtering on the test profile.
24
+
25
+	type filterTestcase struct {
26
+		focus, ignore, hide, show *regexp.Regexp
27
+		fm, im, hm, hnm           bool
28
+	}
29
+
30
+	for tx, tc := range []filterTestcase{
31
+		{
32
+			fm: true, // nil focus matches every sample
33
+		},
34
+		{
35
+			focus: regexp.MustCompile("notfound"),
36
+		},
37
+		{
38
+			ignore: regexp.MustCompile("foo.c"),
39
+			fm:     true,
40
+			im:     true,
41
+		},
42
+		{
43
+			hide: regexp.MustCompile("lib.so"),
44
+			fm:   true,
45
+			hm:   true,
46
+		},
47
+		{
48
+			show: regexp.MustCompile("foo.c"),
49
+			fm:   true,
50
+			hnm:  true,
51
+		},
52
+		{
53
+			show: regexp.MustCompile("notfound"),
54
+			fm:   true,
55
+		},
56
+	} {
57
+		prof := *testProfile1.Copy()
58
+		gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
59
+		if gf != tc.fm {
60
+			t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
61
+		}
62
+		if gi != tc.im {
63
+			t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
64
+		}
65
+		if gh != tc.hm {
66
+			t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
67
+		}
68
+		if gnh != tc.hnm {
69
+			t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
70
+		}
71
+	}
72
+}
73
+
74
+func TestTagFilter(t *testing.T) {
75
+	// Perform several forms of tag filtering on the test profile.
76
+
77
+	type filterTestcase struct {
78
+		include, exclude *regexp.Regexp
79
+		im, em           bool
80
+		count            int
81
+	}
82
+
83
+	countTags := func(p *Profile) map[string]bool {
84
+		tags := make(map[string]bool)
85
+
86
+		for _, s := range p.Sample {
87
+			for l := range s.Label {
88
+				tags[l] = true
89
+			}
90
+			for l := range s.NumLabel {
91
+				tags[l] = true
92
+			}
93
+		}
94
+		return tags
95
+	}
96
+
97
+	for tx, tc := range []filterTestcase{
98
+		{nil, nil, true, false, 3},
99
+		{regexp.MustCompile("notfound"), nil, false, false, 0},
100
+		{regexp.MustCompile("key1"), nil, true, false, 1},
101
+		{nil, regexp.MustCompile("key[12]"), true, true, 1},
102
+	} {
103
+		prof := testProfile1.Copy()
104
+		gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
105
+		if gim != tc.im {
106
+			t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
107
+		}
108
+		if gem != tc.em {
109
+			t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
110
+		}
111
+		if tags := countTags(prof); len(tags) != tc.count {
112
+			t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
113
+		}
114
+	}
115
+}

+ 0
- 96
profile/profile_test.go View File

@@ -20,7 +20,6 @@ import (
20 20
 	"io/ioutil"
21 21
 	"path/filepath"
22 22
 	"reflect"
23
-	"regexp"
24 23
 	"strings"
25 24
 	"sync"
26 25
 	"testing"
@@ -902,101 +901,6 @@ func TestNormalizeIncompatibleProfiles(t *testing.T) {
902 901
 	}
903 902
 }
904 903
 
905
-func TestFilter(t *testing.T) {
906
-	// Perform several forms of filtering on the test profile.
907
-
908
-	type filterTestcase struct {
909
-		focus, ignore, hide, show *regexp.Regexp
910
-		fm, im, hm, hnm           bool
911
-	}
912
-
913
-	for tx, tc := range []filterTestcase{
914
-		{
915
-			fm: true, // nil focus matches every sample
916
-		},
917
-		{
918
-			focus: regexp.MustCompile("notfound"),
919
-		},
920
-		{
921
-			ignore: regexp.MustCompile("foo.c"),
922
-			fm:     true,
923
-			im:     true,
924
-		},
925
-		{
926
-			hide: regexp.MustCompile("lib.so"),
927
-			fm:   true,
928
-			hm:   true,
929
-		},
930
-		{
931
-			show: regexp.MustCompile("foo.c"),
932
-			fm:   true,
933
-			hnm:  true,
934
-		},
935
-		{
936
-			show: regexp.MustCompile("notfound"),
937
-			fm:   true,
938
-		},
939
-	} {
940
-		prof := *testProfile1.Copy()
941
-		gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
942
-		if gf != tc.fm {
943
-			t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
944
-		}
945
-		if gi != tc.im {
946
-			t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
947
-		}
948
-		if gh != tc.hm {
949
-			t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
950
-		}
951
-		if gnh != tc.hnm {
952
-			t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
953
-		}
954
-	}
955
-}
956
-
957
-func TestTagFilter(t *testing.T) {
958
-	// Perform several forms of tag filtering on the test profile.
959
-
960
-	type filterTestcase struct {
961
-		include, exclude *regexp.Regexp
962
-		im, em           bool
963
-		count            int
964
-	}
965
-
966
-	countTags := func(p *Profile) map[string]bool {
967
-		tags := make(map[string]bool)
968
-
969
-		for _, s := range p.Sample {
970
-			for l := range s.Label {
971
-				tags[l] = true
972
-			}
973
-			for l := range s.NumLabel {
974
-				tags[l] = true
975
-			}
976
-		}
977
-		return tags
978
-	}
979
-
980
-	for tx, tc := range []filterTestcase{
981
-		{nil, nil, true, false, 3},
982
-		{regexp.MustCompile("notfound"), nil, false, false, 0},
983
-		{regexp.MustCompile("key1"), nil, true, false, 1},
984
-		{nil, regexp.MustCompile("key[12]"), true, true, 1},
985
-	} {
986
-		prof := testProfile1.Copy()
987
-		gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
988
-		if gim != tc.im {
989
-			t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
990
-		}
991
-		if gem != tc.em {
992
-			t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
993
-		}
994
-		if tags := countTags(prof); len(tags) != tc.count {
995
-			t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
996
-		}
997
-	}
998
-}
999
-
1000 904
 // locationHash constructs a string to use as a hashkey for a sample, based on its locations
1001 905
 func locationHash(s *Sample) string {
1002 906
 	var tb string