Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created September 27, 2019 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subfuzion/b498ad99f6b68076f3db4981629a93d1 to your computer and use it in GitHub Desktop.
Save subfuzion/b498ad99f6b68076f3db4981629a93d1 to your computer and use it in GitHub Desktop.
[Example] Generate helpful test names
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