github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/util_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 "bytes" 21 "golang.org/x/tools/cover" 22 "k8s.io/test-infra/gopherage/pkg/cov" 23 "testing" 24 ) 25 26 func TestDumpProfileOneFile(t *testing.T) { 27 a := []*cover.Profile{ 28 { 29 FileName: "foo.go", 30 Mode: "count", 31 Blocks: []cover.ProfileBlock{ 32 {StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 3}, 33 {StartLine: 22, StartCol: 5, EndLine: 28, EndCol: 2, NumStmt: 5, Count: 2}, 34 }, 35 }, 36 } 37 38 expected := `mode: count 39 foo.go:1.3,20.1 10 3 40 foo.go:22.5,28.2 5 2 41 ` 42 43 var buffer bytes.Buffer 44 if err := cov.DumpProfile(a, &buffer); err != nil { 45 t.Fatalf("DumpProfile failed: %v", err) 46 } 47 48 if buffer.String() != expected { 49 t.Errorf("bad result.\n\nexpected:\n%s\nactual:\n%s\n", expected, buffer.String()) 50 } 51 } 52 53 func TestDumpProfileMultipleFiles(t *testing.T) { 54 a := []*cover.Profile{ 55 { 56 FileName: "bar.go", 57 Mode: "atomic", 58 Blocks: []cover.ProfileBlock{ 59 {StartLine: 5, StartCol: 1, EndLine: 16, EndCol: 7, NumStmt: 7, Count: 0}, 60 }, 61 }, 62 { 63 FileName: "foo.go", 64 Mode: "atomic", 65 Blocks: []cover.ProfileBlock{ 66 {StartLine: 1, StartCol: 3, EndLine: 20, EndCol: 1, NumStmt: 10, Count: 3}, 67 {StartLine: 22, StartCol: 5, EndLine: 28, EndCol: 2, NumStmt: 5, Count: 2}, 68 }, 69 }, 70 } 71 72 expected := `mode: atomic 73 bar.go:5.1,16.7 7 0 74 foo.go:1.3,20.1 10 3 75 foo.go:22.5,28.2 5 2 76 ` 77 var buffer bytes.Buffer 78 if err := cov.DumpProfile(a, &buffer); err != nil { 79 t.Fatalf("DumpProfile failed: %v", err) 80 } 81 82 if buffer.String() != expected { 83 t.Errorf("bad result.\n\nexpected:\n%s\nactual:\n%s\n", expected, buffer.String()) 84 } 85 } 86 87 func TestDumpProfileNoFiles(t *testing.T) { 88 var buffer bytes.Buffer 89 if err := cov.DumpProfile([]*cover.Profile{}, &buffer); err == nil { 90 t.Error("expected dumping no files to fail") 91 } 92 }