...

Source file src/edge-infra.dev/third_party/gopherage/pkg/cov/merge_test.go

Documentation: edge-infra.dev/third_party/gopherage/pkg/cov

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

View as plain text