-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
56 lines (50 loc) · 1.66 KB
/
id.go
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
50
51
52
53
54
55
56
package testhelper
import (
"fmt"
"path/filepath"
"runtime"
)
// ID holds common identifying information about a test. Several of the
// testhelper functions take an ID (or an interface which it satisfies) which
// can simplify the tests. This is often combined with other testhelper mixin
// structs to record standard details for common tests.
type ID struct {
Name string
At string
AtFullName string
}
// MkID is a constructor for the ID type. It will record where it was called
// from and the resulting ID, when used to report an error, will give
// information on the location which should speed up locating the test setup
// for the failing test.
func MkID(name string) ID {
id := ID{Name: name}
if _, file, line, ok := runtime.Caller(1); ok {
id.At = fmt.Sprintf("%s:%d", filepath.Base(file), line)
id.AtFullName = fmt.Sprintf("%s:%d", file, line)
}
return id
}
// IDStr returns an identifying string describing the test
func (id ID) IDStr() string {
if id.At == "" {
return "test: " + id.Name
}
return "test: " + id.At + ": " + id.Name
}
// IDStrFullName returns an identifying string describing the test, using the
// full pathname of the file where the MkID func was called rather than just
// the last part of the path. You might want to use this if your test cases
// are initialised in a more complex way and there is some ambiguity as to
// the location of a source file.
func (id ID) IDStrFullName() string {
if id.AtFullName == "" {
return "test: " + id.Name
}
return "test: " + id.AtFullName + ": " + id.Name
}
// TestCase is an interface wrapping the IDStr methods
type TestCase interface {
IDStr() string
IDStrFullName() string
}