github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/aggregate_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  	"reflect"
    21  	"testing"
    22  
    23  	"golang.org/x/tools/cover"
    24  	"k8s.io/test-infra/gopherage/pkg/cov"
    25  )
    26  
    27  func TestAggregateProfilesSingleProfile(t *testing.T) {
    28  	p := [][]*cover.Profile{
    29  		{
    30  			{
    31  				FileName: "a.go",
    32  				Mode:     "count",
    33  				Blocks: []cover.ProfileBlock{
    34  					{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    35  					{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 0},
    36  				},
    37  			},
    38  			{
    39  				FileName: "b.go",
    40  				Mode:     "count",
    41  				Blocks: []cover.ProfileBlock{
    42  					{StartLine: 3, StartCol: 2, EndLine: 7, EndCol: 2, NumStmt: 3, Count: 1},
    43  				},
    44  			},
    45  		},
    46  	}
    47  
    48  	aggregate, err := cov.AggregateProfiles(p)
    49  	if err != nil {
    50  		t.Fatalf("AggregateProfiles failed: %v", err)
    51  	}
    52  
    53  	expected := []*cover.Profile{
    54  		{
    55  			FileName: "a.go",
    56  			Mode:     "count",
    57  			Blocks: []cover.ProfileBlock{
    58  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 1},
    59  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 0},
    60  			},
    61  		},
    62  		{
    63  			FileName: "b.go",
    64  			Mode:     "count",
    65  			Blocks: []cover.ProfileBlock{
    66  				{StartLine: 3, StartCol: 2, EndLine: 7, EndCol: 2, NumStmt: 3, Count: 1},
    67  			},
    68  		},
    69  	}
    70  
    71  	if !reflect.DeepEqual(aggregate, expected) {
    72  		t.Fatal("aggregate profile incorrect")
    73  	}
    74  }
    75  
    76  func TestAggregateProfilesOverlapping(t *testing.T) {
    77  	p := [][]*cover.Profile{
    78  		{
    79  			{
    80  				FileName: "a.go",
    81  				Mode:     "count",
    82  				Blocks: []cover.ProfileBlock{
    83  					{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    84  					{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 0},
    85  					{StartLine: 14, StartCol: 3, EndLine: 19, EndCol: 4, NumStmt: 6, Count: 0},
    86  				},
    87  			},
    88  		},
    89  		{
    90  			{
    91  				FileName: "a.go",
    92  				Mode:     "count",
    93  				Blocks: []cover.ProfileBlock{
    94  					{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 0},
    95  					{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 4},
    96  					{StartLine: 14, StartCol: 3, EndLine: 19, EndCol: 4, NumStmt: 6, Count: 0},
    97  				},
    98  			},
    99  		},
   100  	}
   101  
   102  	aggregate, err := cov.AggregateProfiles(p)
   103  	if err != nil {
   104  		t.Fatalf("AggregateProfiles failed: %v", err)
   105  	}
   106  
   107  	expected := []*cover.Profile{
   108  		{
   109  			FileName: "a.go",
   110  			Mode:     "count",
   111  			Blocks: []cover.ProfileBlock{
   112  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 1},
   113  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 1},
   114  				{StartLine: 14, StartCol: 3, EndLine: 19, EndCol: 4, NumStmt: 6, Count: 0},
   115  			},
   116  		},
   117  	}
   118  
   119  	if !reflect.DeepEqual(aggregate, expected) {
   120  		t.Fatal("aggregate profile incorrect")
   121  	}
   122  }