github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/diff_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 TestDiffProfilesBasicDiff(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  	result, err := cov.DiffProfiles(a, b)
    49  	if err != nil {
    50  		t.Fatalf("DiffProfiles 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: 4},
    59  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 0},
    60  			},
    61  		},
    62  	}
    63  
    64  	if !reflect.DeepEqual(result, expected) {
    65  		t.Fatal("diffed profile incorrect")
    66  	}
    67  }
    68  
    69  func TestDiffProfilesWrongFileName(t *testing.T) {
    70  	a := []*cover.Profile{
    71  		{
    72  			FileName: "a.go",
    73  			Mode:     "count",
    74  			Blocks: []cover.ProfileBlock{
    75  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
    76  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    77  			},
    78  		},
    79  	}
    80  	b := []*cover.Profile{
    81  		{
    82  			FileName: "b.go",
    83  			Mode:     "count",
    84  			Blocks: []cover.ProfileBlock{
    85  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
    86  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
    87  			},
    88  		},
    89  	}
    90  	if _, err := cov.DiffProfiles(a, b); err == nil {
    91  		t.Fatal("expected DiffProfiles to fail when diffing mismatched files")
    92  	}
    93  }
    94  
    95  func TestDiffProfilesWrongFileCount(t *testing.T) {
    96  	a := []*cover.Profile{
    97  		{
    98  			FileName: "a.go",
    99  			Mode:     "count",
   100  			Blocks: []cover.ProfileBlock{
   101  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   102  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   103  			},
   104  		},
   105  		{
   106  			FileName: "b.go",
   107  			Mode:     "count",
   108  			Blocks: []cover.ProfileBlock{
   109  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
   110  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   111  			},
   112  		},
   113  	}
   114  	b := []*cover.Profile{
   115  		{
   116  			FileName: "a.go",
   117  			Mode:     "count",
   118  			Blocks: []cover.ProfileBlock{
   119  				{StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
   120  				{StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
   121  			},
   122  		},
   123  	}
   124  	if _, err := cov.DiffProfiles(a, b); err == nil {
   125  		t.Fatal("expected DiffProfiles to fail when diffing mismatched profiles")
   126  	}
   127  }