...

Source file src/edge-infra.dev/third_party/gopherage/pkg/cov/junit/calculation/calculation_test.go

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

     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 calculation
    18  
    19  import (
    20  	"testing"
    21  
    22  	"golang.org/x/tools/cover"
    23  )
    24  
    25  func TestCovList(t *testing.T) {
    26  	profiles := []*cover.Profile{
    27  		{FileName: "a", Mode: "count", Blocks: []cover.ProfileBlock{
    28  			{NumStmt: 5, Count: 1},
    29  			{NumStmt: 100, Count: 2},
    30  			{NumStmt: 2018},
    31  		},
    32  		},
    33  		{FileName: "b", Mode: "count", Blocks: []cover.ProfileBlock{
    34  			{NumStmt: 59, Count: 1},
    35  			{NumStmt: 1500, Count: 2},
    36  			{NumStmt: 2000},
    37  		},
    38  		},
    39  		{FileName: "a/c", Mode: "count", Blocks: []cover.ProfileBlock{}},
    40  	}
    41  	covList := ProduceCovList(profiles)
    42  
    43  	if len(covList.Group) == 0 {
    44  		t.Fatalf("covlist is empty\n")
    45  	}
    46  
    47  	testCases := []struct {
    48  		covExpected Coverage
    49  		covActual   Coverage
    50  	}{
    51  		{Coverage{Name: "a", NumCoveredStmts: 105, NumAllStmts: 2123},
    52  			covList.Group[0]},
    53  		{Coverage{Name: "b", NumCoveredStmts: 1559, NumAllStmts: 3559},
    54  			covList.Group[1]},
    55  		{Coverage{Name: "a/c"},
    56  			covList.Group[2]},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		if tc.covExpected != tc.covActual {
    61  			t.Fatalf("File level summarized coverage data does does match expectation: "+
    62  				"expected = %v; actual = %v", tc.covExpected, tc.covActual)
    63  		}
    64  	}
    65  
    66  	expected := float32(1664) / float32(5682)
    67  	if expected != covList.Ratio() {
    68  		t.Fatalf("Overall summarized coverage data does does match expectation: "+
    69  			"expected = %v; actual = %v", expected, covList.Ratio())
    70  	}
    71  }
    72  

View as plain text