// Copyright 2013 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Support for test coverage.package testingimport ()// CoverBlock records the coverage data for a single basic block.// The fields are 1-indexed, as in an editor: The opening line of// the file is number 1, for example. Columns are measured// in bytes.// NOTE: This struct is internal to the testing infrastructure and may change.// It is not covered (yet) by the Go 1 compatibility guidelines.typeCoverBlockstruct { Line0 uint32// Line number for block start. Col0 uint16// Column number for block start. Line1 uint32// Line number for block end. Col1 uint16// Column number for block end. Stmts uint16// Number of statements included in this block.}var cover Cover// Cover records information about test coverage checking.// NOTE: This struct is internal to the testing infrastructure and may change.// It is not covered (yet) by the Go 1 compatibility guidelines.typeCoverstruct { Mode string Counters map[string][]uint32 Blocks map[string][]CoverBlock CoveredPackages string}// Coverage reports the current code coverage as a fraction in the range [0, 1].// If coverage is not enabled, Coverage returns 0.//// When running a large set of sequential test cases, checking Coverage after each one// can be useful for identifying which test cases exercise new code paths.// It is not a replacement for the reports generated by 'go test -cover' and// 'go tool cover'.func () float64 {ifgoexperiment.CoverageRedesign {returncoverage2() }var , int64for , := rangecover.Counters {for := range {ifatomic.LoadUint32(&[]) > 0 { ++ } ++ } }if == 0 {return0 }returnfloat64() / float64()}// RegisterCover records the coverage data accumulators for the tests.// NOTE: This function is internal to the testing infrastructure and may change.// It is not covered (yet) by the Go 1 compatibility guidelines.func ( Cover) {cover = }// mustBeNil checks the error and, if present, reports it and exits.func mustBeNil( error) {if != nil {fmt.Fprintf(os.Stderr, "testing: %s\n", )os.Exit(2) }}// coverReport reports the coverage percentage and writes a coverage profile if requested.func coverReport() {ifgoexperiment.CoverageRedesign {coverReport2()return }var *os.Filevarerrorif *coverProfile != "" { , = os.Create(toOutputDir(*coverProfile))mustBeNil()fmt.Fprintf(, "mode: %s\n", cover.Mode)deferfunc() { mustBeNil(.Close()) }() }var , int64varuint32for , := rangecover.Counters { := cover.Blocks[]for := range { := int64([].Stmts) += = atomic.LoadUint32(&[]) // For -mode=atomic.if > 0 { += }if != nil { , := fmt.Fprintf(, "%s:%d.%d,%d.%d %d %d\n", , [].Line0, [].Col0, [].Line1, [].Col1, , )mustBeNil() } } }if == 0 {fmt.Println("coverage: [no statements]")return }fmt.Printf("coverage: %.1f%% of statements%s\n", 100*float64()/float64(), cover.CoveredPackages)}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.