go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/util_test.go (about)

     1  package instrumentation
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"go.undefinedlabs.com/scopeagent/ast"
     9  )
    10  
    11  func TestSplitPackageAndName(t *testing.T) {
    12  	cases := map[string][]string{
    13  		"pkg.TestBase":                 {"pkg", "TestBase"},
    14  		"pkg.TestBase.func1":           {"pkg", "TestBase.func1"},
    15  		"github.com/org/proj.TestBase": {"github.com/org/proj", "TestBase"},
    16  	}
    17  
    18  	for key, expected := range cases {
    19  		t.Run(key, func(t *testing.T) {
    20  			pkg, fname := splitPackageAndName(key)
    21  			if pkg != expected[0] {
    22  				t.Fatalf("value '%s', expected: '%s'", pkg, expected[0])
    23  			}
    24  			if fname != expected[1] {
    25  				t.Fatalf("value '%s', expected: '%s'", fname, expected[1])
    26  			}
    27  		})
    28  	}
    29  }
    30  
    31  func TestGetTestCodeBoundaries(t *testing.T) {
    32  	var pc uintptr
    33  	func() { pc, _, _, _ = runtime.Caller(1) }()
    34  	testName := t.Name()
    35  
    36  	pkg, fname, bound := GetPackageAndNameAndBoundaries(pc)
    37  
    38  	if pkg != "go.undefinedlabs.com/scopeagent/instrumentation" {
    39  		t.Fatalf("value '%s' not expected", pkg)
    40  	}
    41  	if fname != testName {
    42  		t.Fatalf("value '%s' not expected", fname)
    43  	}
    44  	boundaryExpected, _ := ast.GetFuncSourceForName(pc, testName)
    45  	calcExpected := fmt.Sprintf("%s:%d:%d", boundaryExpected.File, boundaryExpected.Start.Line, boundaryExpected.End.Line)
    46  	if bound != calcExpected {
    47  		t.Fatalf("value '%s' not expected", bound)
    48  	}
    49  }