File: util.go

package info (click to toggle)
golang-github-smartystreets-goconvey 1.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,672 kB
  • sloc: makefile: 8
file content (49 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package parser

import (
	"math"
	"strings"
	"time"
)

// parseTestFunctionDuration parses the duration in seconds as a float64
// from a line of go test output that looks something like this:
// --- PASS: TestOldSchool_PassesWithMessage (0.03 seconds)
func parseTestFunctionDuration(line string) float64 {
	line = strings.Replace(line, "(", "", 1)
	line = strings.Replace(line, ")", "", 1)
	fields := strings.Split(line, " ")
	return parseDurationInSeconds(fields[3], 2)
}

func parseDurationInSeconds(raw string, precision int) float64 {
	elapsed, err := time.ParseDuration(raw)
	if err != nil {
		elapsed, _ = time.ParseDuration(raw + "s")
	}
	return round(elapsed.Seconds(), precision)
}

// round returns the rounded version of x with precision.
//
// Special cases are:
//  round(±0) = ±0
//  round(±Inf) = ±Inf
//  round(NaN) = NaN
//
// Why, oh why doesn't the math package come with a round function?
// Inspiration: http://play.golang.org/p/ZmFfr07oHp
func round(x float64, precision int) float64 {
	var rounder float64
	pow := math.Pow(10, float64(precision))
	intermediate := x * pow

	if intermediate < 0.0 {
		intermediate -= 0.5
	} else {
		intermediate += 0.5
	}
	rounder = float64(int64(intermediate))

	return rounder / float64(pow)
}