github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/filter_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cov_test
    18  
    19  import (
    20  	"golang.org/x/tools/cover"
    21  	"k8s.io/test-infra/gopherage/pkg/cov"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestFilterProfilePathsInclude(t *testing.T) {
    27  	a := []*cover.Profile{
    28  		{
    29  			FileName: "a.go",
    30  			Mode:     "count",
    31  			Blocks: []cover.ProfileBlock{
    32  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    33  			},
    34  		},
    35  		{
    36  			FileName: "b.go",
    37  			Mode:     "count",
    38  			Blocks: []cover.ProfileBlock{
    39  				{StartLine: 2, StartCol: 15, EndLine: 6, EndCol: 14, NumStmt: 5, Count: 4},
    40  			},
    41  		},
    42  		{
    43  			FileName: "c.go",
    44  			Mode:     "count",
    45  			Blocks: []cover.ProfileBlock{
    46  				{StartLine: 3, StartCol: 16, EndLine: 7, EndCol: 15, NumStmt: 6, Count: 5},
    47  			},
    48  		},
    49  	}
    50  
    51  	r, err := cov.FilterProfilePaths(a, []string{"a", "b"}, true)
    52  	if err != nil {
    53  		t.Fatalf("error filtering profile: %v", err)
    54  	}
    55  
    56  	expected := []*cover.Profile{
    57  		{
    58  			FileName: "a.go",
    59  			Mode:     "count",
    60  			Blocks: []cover.ProfileBlock{
    61  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    62  			},
    63  		},
    64  		{
    65  			FileName: "b.go",
    66  			Mode:     "count",
    67  			Blocks: []cover.ProfileBlock{
    68  				{StartLine: 2, StartCol: 15, EndLine: 6, EndCol: 14, NumStmt: 5, Count: 4},
    69  			},
    70  		},
    71  	}
    72  
    73  	if !reflect.DeepEqual(r, expected) {
    74  		t.Fatalf("filtered profile incorrect.")
    75  	}
    76  }
    77  
    78  func TestFilterProfilePathsExclude(t *testing.T) {
    79  	a := []*cover.Profile{
    80  		{
    81  			FileName: "a.go",
    82  			Mode:     "count",
    83  			Blocks: []cover.ProfileBlock{
    84  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    85  			},
    86  		},
    87  		{
    88  			FileName: "b.go",
    89  			Mode:     "count",
    90  			Blocks: []cover.ProfileBlock{
    91  				{StartLine: 2, StartCol: 15, EndLine: 6, EndCol: 14, NumStmt: 5, Count: 4},
    92  			},
    93  		},
    94  		{
    95  			FileName: "c.go",
    96  			Mode:     "count",
    97  			Blocks: []cover.ProfileBlock{
    98  				{StartLine: 3, StartCol: 16, EndLine: 7, EndCol: 15, NumStmt: 6, Count: 5},
    99  			},
   100  		},
   101  	}
   102  
   103  	r, err := cov.FilterProfilePaths(a, []string{"a", "b"}, false)
   104  	if err != nil {
   105  		t.Fatalf("error filtering profile: %v", err)
   106  	}
   107  
   108  	expected := []*cover.Profile{
   109  		{
   110  			FileName: "c.go",
   111  			Mode:     "count",
   112  			Blocks: []cover.ProfileBlock{
   113  				{StartLine: 3, StartCol: 16, EndLine: 7, EndCol: 15, NumStmt: 6, Count: 5},
   114  			},
   115  		},
   116  	}
   117  
   118  	if !reflect.DeepEqual(r, expected) {
   119  		t.Fatalf("filtered profile incorrect.")
   120  	}
   121  }
   122  
   123  func TestFilterProfilePathsBadRegex(t *testing.T) {
   124  	a := []*cover.Profile{
   125  		{
   126  			FileName: "a.go",
   127  			Mode:     "count",
   128  			Blocks: []cover.ProfileBlock{
   129  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   130  			},
   131  		},
   132  	}
   133  	_, err := cov.FilterProfilePaths(a, []string{"("}, false)
   134  	if err == nil {
   135  		t.Fatalf("expected error when filtering profile, but didn't get one.")
   136  	}
   137  }