-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn.go
118 lines (102 loc) · 2.79 KB
/
fn.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2018 The go-ego Project Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package tt
import (
"fmt"
"runtime"
"strings"
"unicode"
"unicode/utf8"
)
// Blue returns a blue string
func Blue(message string) string {
return fmt.Sprintf("\x1b[34m%s\x1b[0m", message)
}
// Red returns a red string
func Red(message string) string {
return fmt.Sprintf("\x1b[31m%s\x1b[0m", message)
}
// Yellow returns a yellow string
func Yellow(message string) string {
return fmt.Sprintf("\x1b[33m%s\x1b[0m", message)
}
// Bold returns a blod string
func Bold(message string) string {
return fmt.Sprintf("\x1b[1m%s\x1b[21m", message)
}
// RedBold returns a red bold string
func RedBold(message string) string {
return fmt.Sprintf("\x1b[31m%s\x1b[0m", Bold(message))
}
func isTest(name, prefix string) bool {
if !strings.HasPrefix(name, prefix) {
return false
}
if len(name) == len(prefix) { // "Test" is ok
return true
}
rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
return !unicode.IsLower(rune)
}
// CallerInfo returns an array of strings containing the file and line number,
// and each stack frame leading from the current test, when the assert call that
// failed.
func CallerInfo() (callers []string) {
var (
pc uintptr
file, name string
line int
ok bool
)
for i := 0; ; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
// This is a huge edge case, but it will panic if this is the case, see #180
if file == "<autogenerated>" {
break
}
f := runtime.FuncForPC(pc)
if f == nil {
break
}
name = f.Name()
// testing.tRunner is the standard library function that calls
// tests.
if name == "testing.tRunner" {
break
}
parts := strings.Split(file, "/")
file = parts[len(parts)-1]
if len(parts) > 1 {
dir := parts[len(parts)-2]
if (dir != "assert" && dir != "mock" && dir != "require") ||
file == "mock_test.go" {
callers = append(callers, fmt.Sprintf("%s:%d", file, line))
}
}
// Drop the package
segments := strings.Split(name, ".")
name = segments[len(segments)-1]
if isTest(name, "Test") ||
isTest(name, "Benchmark") ||
isTest(name, "Example") {
break
}
}
return
}