Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move filter tests into their own test file. #356

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions profile/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package profile

import (
"regexp"
"testing"
)

func TestFilter(t *testing.T) {
// Perform several forms of filtering on the test profile.

type filterTestcase struct {
focus, ignore, hide, show *regexp.Regexp
fm, im, hm, hnm bool
}

for tx, tc := range []filterTestcase{
{
fm: true, // nil focus matches every sample
},
{
focus: regexp.MustCompile("notfound"),
},
{
ignore: regexp.MustCompile("foo.c"),
fm: true,
im: true,
},
{
hide: regexp.MustCompile("lib.so"),
fm: true,
hm: true,
},
{
show: regexp.MustCompile("foo.c"),
fm: true,
hnm: true,
},
{
show: regexp.MustCompile("notfound"),
fm: true,
},
} {
prof := *testProfile1.Copy()
gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
if gf != tc.fm {
t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
}
if gi != tc.im {
t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
}
if gh != tc.hm {
t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
}
if gnh != tc.hnm {
t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
}
}
}

func TestTagFilter(t *testing.T) {
// Perform several forms of tag filtering on the test profile.

type filterTestcase struct {
include, exclude *regexp.Regexp
im, em bool
count int
}

countTags := func(p *Profile) map[string]bool {
tags := make(map[string]bool)

for _, s := range p.Sample {
for l := range s.Label {
tags[l] = true
}
for l := range s.NumLabel {
tags[l] = true
}
}
return tags
}

for tx, tc := range []filterTestcase{
{nil, nil, true, false, 3},
{regexp.MustCompile("notfound"), nil, false, false, 0},
{regexp.MustCompile("key1"), nil, true, false, 1},
{nil, regexp.MustCompile("key[12]"), true, true, 1},
} {
prof := testProfile1.Copy()
gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
if gim != tc.im {
t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
}
if gem != tc.em {
t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
}
if tags := countTags(prof); len(tags) != tc.count {
t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
}
}
}
96 changes: 0 additions & 96 deletions profile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"io/ioutil"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -902,101 +901,6 @@ func TestNormalizeIncompatibleProfiles(t *testing.T) {
}
}

func TestFilter(t *testing.T) {
// Perform several forms of filtering on the test profile.

type filterTestcase struct {
focus, ignore, hide, show *regexp.Regexp
fm, im, hm, hnm bool
}

for tx, tc := range []filterTestcase{
{
fm: true, // nil focus matches every sample
},
{
focus: regexp.MustCompile("notfound"),
},
{
ignore: regexp.MustCompile("foo.c"),
fm: true,
im: true,
},
{
hide: regexp.MustCompile("lib.so"),
fm: true,
hm: true,
},
{
show: regexp.MustCompile("foo.c"),
fm: true,
hnm: true,
},
{
show: regexp.MustCompile("notfound"),
fm: true,
},
} {
prof := *testProfile1.Copy()
gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
if gf != tc.fm {
t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
}
if gi != tc.im {
t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
}
if gh != tc.hm {
t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
}
if gnh != tc.hnm {
t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
}
}
}

func TestTagFilter(t *testing.T) {
// Perform several forms of tag filtering on the test profile.

type filterTestcase struct {
include, exclude *regexp.Regexp
im, em bool
count int
}

countTags := func(p *Profile) map[string]bool {
tags := make(map[string]bool)

for _, s := range p.Sample {
for l := range s.Label {
tags[l] = true
}
for l := range s.NumLabel {
tags[l] = true
}
}
return tags
}

for tx, tc := range []filterTestcase{
{nil, nil, true, false, 3},
{regexp.MustCompile("notfound"), nil, false, false, 0},
{regexp.MustCompile("key1"), nil, true, false, 1},
{nil, regexp.MustCompile("key[12]"), true, true, 1},
} {
prof := testProfile1.Copy()
gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
if gim != tc.im {
t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
}
if gem != tc.em {
t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
}
if tags := countTags(prof); len(tags) != tc.count {
t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
}
}
}

// locationHash constructs a string to use as a hashkey for a sample, based on its locations
func locationHash(s *Sample) string {
var tb string
Expand Down