github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/merge_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 TestMergeProfilesSimilar(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  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    34  			},
    35  		},
    36  	}
    37  	b := []*cover.Profile{
    38  		{
    39  			FileName: "a.go",
    40  			Mode:     "count",
    41  			Blocks: []cover.ProfileBlock{
    42  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
    43  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    44  			},
    45  		},
    46  	}
    47  
    48  	expected := []*cover.Profile{
    49  		{
    50  			FileName: "a.go",
    51  			Mode:     "count",
    52  			Blocks: []cover.ProfileBlock{
    53  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 10},
    54  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 4},
    55  			},
    56  		},
    57  	}
    58  
    59  	result, err := cov.MergeProfiles(a, b)
    60  	if err != nil {
    61  		t.Fatalf("error merging profiles: %v", err)
    62  	}
    63  	if !reflect.DeepEqual(result, expected) {
    64  		t.Fatal("merged profile incorrect")
    65  	}
    66  }
    67  
    68  func TestMergeProfilesDisjoint(t *testing.T) {
    69  	a := []*cover.Profile{
    70  		{
    71  			FileName: "a.go",
    72  			Mode:     "count",
    73  			Blocks: []cover.ProfileBlock{
    74  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    75  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    76  			},
    77  		},
    78  	}
    79  	b := []*cover.Profile{
    80  		{
    81  			FileName: "b.go",
    82  			Mode:     "count",
    83  			Blocks: []cover.ProfileBlock{
    84  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
    85  			},
    86  		},
    87  	}
    88  
    89  	expected := []*cover.Profile{
    90  		{
    91  			FileName: "a.go",
    92  			Mode:     "count",
    93  			Blocks: []cover.ProfileBlock{
    94  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    95  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    96  			},
    97  		},
    98  		{
    99  			FileName: "b.go",
   100  			Mode:     "count",
   101  			Blocks: []cover.ProfileBlock{
   102  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
   103  			},
   104  		},
   105  	}
   106  
   107  	result, err := cov.MergeProfiles(a, b)
   108  	if err != nil {
   109  		t.Fatalf("error merging profiles: %v", err)
   110  	}
   111  	if !reflect.DeepEqual(result, expected) {
   112  		t.Fatal("merged profile incorrect")
   113  	}
   114  }
   115  
   116  func TestMergeProfilesOverlapping(t *testing.T) {
   117  	a := []*cover.Profile{
   118  		{
   119  			FileName: "bar.go",
   120  			Mode:     "count",
   121  			Blocks: []cover.ProfileBlock{
   122  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 17},
   123  			},
   124  		},
   125  		{
   126  			FileName: "foo.go",
   127  			Mode:     "count",
   128  			Blocks: []cover.ProfileBlock{
   129  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   130  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   131  			},
   132  		},
   133  	}
   134  	b := []*cover.Profile{
   135  		{
   136  			FileName: "bar.go",
   137  			Mode:     "count",
   138  			Blocks: []cover.ProfileBlock{
   139  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 8},
   140  			},
   141  		},
   142  		{
   143  			FileName: "baz.go",
   144  			Mode:     "count",
   145  			Blocks: []cover.ProfileBlock{
   146  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
   147  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   148  			},
   149  		},
   150  	}
   151  
   152  	expected := []*cover.Profile{
   153  		{
   154  			FileName: "bar.go",
   155  			Mode:     "count",
   156  			Blocks: []cover.ProfileBlock{
   157  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 25},
   158  			},
   159  		},
   160  		{
   161  			FileName: "baz.go",
   162  			Mode:     "count",
   163  			Blocks: []cover.ProfileBlock{
   164  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
   165  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   166  			},
   167  		},
   168  		{
   169  			FileName: "foo.go",
   170  			Mode:     "count",
   171  			Blocks: []cover.ProfileBlock{
   172  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   173  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   174  			},
   175  		},
   176  	}
   177  
   178  	result, err := cov.MergeProfiles(a, b)
   179  	if err != nil {
   180  		t.Fatalf("error merging profiles: %v", err)
   181  	}
   182  	if !reflect.DeepEqual(result, expected) {
   183  		t.Fatal("merged profile incorrect")
   184  	}
   185  }
   186  
   187  func TestMergeMultipleProfiles(t *testing.T) {
   188  	a := []*cover.Profile{
   189  		{
   190  			FileName: "foo.go",
   191  			Mode:     "count",
   192  			Blocks: []cover.ProfileBlock{
   193  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 3},
   194  			},
   195  		},
   196  	}
   197  	b := []*cover.Profile{
   198  		{
   199  			FileName: "foo.go",
   200  			Mode:     "count",
   201  			Blocks: []cover.ProfileBlock{
   202  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 5},
   203  			},
   204  		},
   205  	}
   206  	c := []*cover.Profile{
   207  		{
   208  			FileName: "foo.go",
   209  			Mode:     "count",
   210  			Blocks: []cover.ProfileBlock{
   211  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 8},
   212  			},
   213  		},
   214  	}
   215  
   216  	expected := []*cover.Profile{
   217  		{
   218  			FileName: "foo.go",
   219  			Mode:     "count",
   220  			Blocks: []cover.ProfileBlock{
   221  				{StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 16},
   222  			},
   223  		},
   224  	}
   225  
   226  	result, err := cov.MergeMultipleProfiles([][]*cover.Profile{a, b, c})
   227  	if err != nil {
   228  		t.Fatalf("error merging profiles: %v", err)
   229  	}
   230  	if !reflect.DeepEqual(result, expected) {
   231  		t.Fatal("merged profile incorrect", result)
   232  	}
   233  }
   234  
   235  func TestMergeProfilesEmpty(t *testing.T) {
   236  	_, err := cov.MergeMultipleProfiles([][]*cover.Profile{})
   237  	if err == nil {
   238  		t.Fatal("expected merging zero profiles to fail")
   239  	}
   240  }
   241  
   242  func TestMergeProfilesConflictingFileContents(t *testing.T) {
   243  	a := []*cover.Profile{
   244  		{
   245  			FileName: "a.go",
   246  			Mode:     "count",
   247  			Blocks: []cover.ProfileBlock{
   248  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   249  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   250  			},
   251  		},
   252  	}
   253  	b := []*cover.Profile{
   254  		{
   255  			FileName: "a.go",
   256  			Mode:     "count",
   257  			Blocks: []cover.ProfileBlock{
   258  				{StartLine: 2, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
   259  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   260  			},
   261  		},
   262  	}
   263  
   264  	if result, err := cov.MergeProfiles(a, b); err == nil {
   265  		t.Fatalf("expected merging conflicting profiles to fail: %+v", result[0])
   266  	}
   267  }