Created
September 27, 2019 01:11
-
-
Save subfuzion/b498ad99f6b68076f3db4981629a93d1 to your computer and use it in GitHub Desktop.
[Example] Generate helpful test names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testhelper | |
import ( | |
"path" | |
"reflect" | |
"runtime" | |
"strings" | |
) | |
// GetFileFunc takes a function and returns a string formatted as | |
// "basedir/filename/function" to make it easier to identify the | |
// source in printed test names (which will consequently appear as | |
// "TestFoo/basedir/filename/function") | |
func GetFileFunc(f interface{}) string { | |
pc := reflect.ValueOf(f).Pointer() | |
fpc := runtime.FuncForPC(pc) | |
parts := strings.Split(path.Base(fpc.Name()), ".") | |
file, _ := fpc.FileLine(pc) | |
base := path.Base(file) | |
// parts[0] => basedir | |
// base => filename | |
// parts[1] => function | |
name := path.Join(parts[0], base, parts[1]) | |
return name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment