github.com/neilgarb/delve@v1.9.2-nobreaks/service/test/common_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/go-delve/delve/service/api"
    12  	"github.com/go-delve/delve/service/rpc1"
    13  	"github.com/go-delve/delve/service/rpc2"
    14  )
    15  
    16  func assertNoError(err error, t *testing.T, s string) {
    17  	if err != nil {
    18  		_, file, line, _ := runtime.Caller(1)
    19  		fname := filepath.Base(file)
    20  		t.Fatalf("failed assertion at %s:%d: %s - %s\n", fname, line, s, err)
    21  	}
    22  }
    23  
    24  func assertError(err error, t *testing.T, s string) {
    25  	if err == nil {
    26  		_, file, line, _ := runtime.Caller(1)
    27  		fname := filepath.Base(file)
    28  		t.Fatalf("failed assertion at %s:%d: %s (no error)\n", fname, line, s)
    29  	}
    30  
    31  	if strings.Contains(err.Error(), "Internal debugger error") {
    32  		_, file, line, _ := runtime.Caller(1)
    33  		fname := filepath.Base(file)
    34  		t.Fatalf("failed assertion at %s:%d: %s internal debugger error: %v\n", fname, line, s, err)
    35  	}
    36  }
    37  
    38  func init() {
    39  	runtime.GOMAXPROCS(2)
    40  }
    41  
    42  type nextTest struct {
    43  	begin, end int
    44  }
    45  
    46  func testProgPath(t *testing.T, name string) string {
    47  	fp, err := filepath.Abs(fmt.Sprintf("_fixtures/%s.go", name))
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if _, err := os.Stat(fp); err != nil {
    52  		fp, err = filepath.Abs(fmt.Sprintf("../../_fixtures/%s.go", name))
    53  		if err != nil {
    54  			t.Fatal(err)
    55  		}
    56  	}
    57  	sympath, err := filepath.EvalSymlinks(fp)
    58  	if err == nil {
    59  		fp = strings.Replace(sympath, "\\", "/", -1)
    60  	}
    61  	return fp
    62  }
    63  
    64  type BreakpointLister interface {
    65  	ListBreakpoints() ([]*api.Breakpoint, error)
    66  }
    67  
    68  func countBreakpoints(t *testing.T, c interface{}) int {
    69  	var bps []*api.Breakpoint
    70  	var err error
    71  	switch c := c.(type) {
    72  	case *rpc2.RPCClient:
    73  		bps, err = c.ListBreakpoints(false)
    74  	case *rpc1.RPCClient:
    75  		bps, err = c.ListBreakpoints()
    76  	}
    77  	assertNoError(err, t, "ListBreakpoints()")
    78  	bpcount := 0
    79  	for _, bp := range bps {
    80  		if bp.ID >= 0 {
    81  			bpcount++
    82  		}
    83  	}
    84  	return bpcount
    85  }
    86  
    87  type locationFinder1 interface {
    88  	FindLocation(api.EvalScope, string) ([]api.Location, error)
    89  }
    90  
    91  type locationFinder2 interface {
    92  	FindLocation(api.EvalScope, string, bool, [][2]string) ([]api.Location, error)
    93  }
    94  
    95  func findLocationHelper(t *testing.T, c interface{}, loc string, shouldErr bool, count int, checkAddr uint64) []uint64 {
    96  	var locs []api.Location
    97  	var err error
    98  
    99  	switch c := c.(type) {
   100  	case locationFinder1:
   101  		locs, err = c.FindLocation(api.EvalScope{GoroutineID: -1}, loc)
   102  	case locationFinder2:
   103  		locs, err = c.FindLocation(api.EvalScope{GoroutineID: -1}, loc, false, nil)
   104  	default:
   105  		t.Errorf("unexpected type %T passed to findLocationHelper", c)
   106  	}
   107  
   108  	t.Logf("FindLocation(\"%s\") → %v\n", loc, locs)
   109  
   110  	if shouldErr {
   111  		if err == nil {
   112  			t.Fatalf("Resolving location <%s> didn't return an error: %v", loc, locs)
   113  		}
   114  	} else {
   115  		if err != nil {
   116  			t.Fatalf("Error resolving location <%s>: %v", loc, err)
   117  		}
   118  	}
   119  
   120  	if (count >= 0) && (len(locs) != count) {
   121  		t.Fatalf("Wrong number of breakpoints returned for location <%s> (got %d, expected %d)", loc, len(locs), count)
   122  	}
   123  
   124  	if checkAddr != 0 && checkAddr != locs[0].PC {
   125  		t.Fatalf("Wrong address returned for location <%s> (got %#x, expected %#x)", loc, locs[0].PC, checkAddr)
   126  	}
   127  
   128  	addrs := make([]uint64, len(locs))
   129  	for i := range locs {
   130  		addrs[i] = locs[i].PC
   131  	}
   132  	return addrs
   133  }
   134  
   135  func getCurinstr(d3 api.AsmInstructions) *api.AsmInstruction {
   136  	for i := range d3 {
   137  		if d3[i].AtPC {
   138  			return &d3[i]
   139  		}
   140  	}
   141  	return nil
   142  }