github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/robots/coverage/diff/view_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 diff
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/test-infra/gopherage/pkg/cov/junit/calculation"
    23  )
    24  
    25  func TestMakeTable(t *testing.T) {
    26  	type args struct {
    27  		baseCovList       *calculation.CoverageList
    28  		newCovList        *calculation.CoverageList
    29  		jobName           string
    30  		coverageThreshold float32
    31  	}
    32  	tests := []struct {
    33  		name              string
    34  		args              args
    35  		wantRes           string
    36  		wantIsCoverageLow bool
    37  	}{
    38  		{
    39  			name: "A",
    40  			args: args{
    41  				baseCovList: &calculation.CoverageList{
    42  					Group: []calculation.Coverage{
    43  						{Name: "a", NumCoveredStmts: 10, NumAllStmts: 100},
    44  						{Name: "a2", NumCoveredStmts: 12, NumAllStmts: 100},
    45  						{Name: "c", NumCoveredStmts: 20, NumAllStmts: 100},
    46  						{Name: "d", NumCoveredStmts: 30, NumAllStmts: 100},
    47  					},
    48  				},
    49  				newCovList: &calculation.CoverageList{
    50  					Group: []calculation.Coverage{
    51  						{Name: "a", NumCoveredStmts: 5, NumAllStmts: 100},
    52  						{Name: "b", NumCoveredStmts: 10, NumAllStmts: 100},
    53  						{Name: "c", NumCoveredStmts: 20, NumAllStmts: 100},
    54  						{Name: "d", NumCoveredStmts: 40, NumAllStmts: 100},
    55  					},
    56  				},
    57  				jobName:           "example-coverage-test",
    58  				coverageThreshold: 30,
    59  			},
    60  			wantRes: "a | 10.0% | 5.0% | -5.0\n" +
    61  				"b | Does not exist | 10.0% | \n" +
    62  				"d | 30.0% | 40.0% | 10.0",
    63  			wantIsCoverageLow: true,
    64  		},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			gotRes, gotIsCoverageLow := makeTable(tt.args.baseCovList, tt.args.newCovList, tt.args.coverageThreshold)
    69  			if gotRes != tt.wantRes {
    70  				t.Errorf("makeTable() gotRes = %v, want %v", gotRes, tt.wantRes)
    71  			}
    72  			if gotIsCoverageLow != tt.wantIsCoverageLow {
    73  				t.Errorf("makeTable() gotIsCoverageLow = %v, want %v", gotIsCoverageLow, tt.wantIsCoverageLow)
    74  			}
    75  		})
    76  	}
    77  }