github.com/nikandfor/loc@v0.5.1/location_stack_test.go (about)

     1  package loc
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"regexp"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestLocationFillCallers(t *testing.T) {
    14  	st := make(PCs, 1)
    15  
    16  	st = CallersFill(0, st)
    17  
    18  	assert.Len(t, st, 1)
    19  	assert.Equal(t, "location_stack_test.go:16", st[0].String())
    20  }
    21  
    22  func testLocationsInside() (st PCs) {
    23  	func() {
    24  		func() {
    25  			st = Callers(1, 3)
    26  		}()
    27  	}()
    28  
    29  	return
    30  }
    31  
    32  func TestLocationPCsString(t *testing.T) {
    33  	var st PCs
    34  	func() {
    35  		func() {
    36  			st = testLocationsInside()
    37  		}()
    38  	}()
    39  
    40  	assert.Len(t, st, 3)
    41  	assert.Equal(t, "location_stack_test.go:26", st[0].String())
    42  	assert.Equal(t, "location_stack_test.go:27", st[1].String())
    43  	assert.Equal(t, "location_stack_test.go:36", st[2].String())
    44  
    45  	re := `location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:36`
    46  
    47  	assert.Equal(t, re, st.String())
    48  }
    49  
    50  func TestLocationPCsFormat(t *testing.T) {
    51  	var st PCs
    52  	func() {
    53  		func() {
    54  			st = testLocationsInside()
    55  		}()
    56  	}()
    57  
    58  	assert.Equal(t, "location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:54", st.String())
    59  
    60  	addAllSubs := innerFuncName(Caller(0), 2)
    61  	t.Logf("go version: %q: %q", gover(), addAllSubs)
    62  
    63  	assert.Equal(t, "loc.testLocationsInside.func1:26 at loc.testLocationsInside:27 at loc.TestLocationPCsFormat"+addAllSubs+":54", fmt.Sprintf("%#v", st))
    64  
    65  	re := `at [\w.-/]*location_stack_test.go:26
    66  at [\w.-/]*location_stack_test.go:27
    67  at [\w.-/]*location_stack_test.go:54
    68  `
    69  	v := fmt.Sprintf("%+v", st)
    70  	assert.True(t, regexp.MustCompile(re).MatchString(v), "expected:\n%vgot:\n%v", re, v)
    71  }
    72  
    73  func TestLocationPCsFormatString(t *testing.T) {
    74  	var st PCs
    75  	func() {
    76  		func() {
    77  			st = testLocationsInside()
    78  		}()
    79  	}()
    80  
    81  	assert.Equal(t, "location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:77", st.FormatString(""))
    82  
    83  	addAllSubs := innerFuncName(Caller(0), 2)
    84  	t.Logf("all sub funs suffix (go ver %q): %q", gover(), addAllSubs)
    85  
    86  	assert.Equal(t, "loc.testLocationsInside.func1:26 at loc.testLocationsInside:27 at loc.TestLocationPCsFormatString"+addAllSubs+":77", st.FormatString("#"))
    87  
    88  	re := `at [\w.-/]*location_stack_test.go:26
    89  at [\w.-/]*location_stack_test.go:27
    90  at [\w.-/]*location_stack_test.go:77
    91  `
    92  
    93  	v := st.FormatString("+")
    94  	assert.True(t, regexp.MustCompile(re).MatchString(v), "expected:\n%vgot:\n%v", re, v)
    95  }
    96  
    97  func innerFuncName(fn PC, n int) string {
    98  	var s string
    99  
   100  	switch {
   101  	//	case regexp.MustCompile("go1.16.*").MatchString(gover()):
   102  	//		return ".func1"
   103  	case regexp.MustCompile("go1.21.*").MatchString(gover()):
   104  		name, _, _ := fn.NameFileLine()
   105  		name = path.Base(name)
   106  		name = name[strings.IndexByte(name, '.')+1:]
   107  
   108  		s = "." + name
   109  
   110  		for i := 0; i < n; i++ {
   111  			s += fmt.Sprintf(".func%v", i+1)
   112  		}
   113  	default:
   114  		s = ".func"
   115  
   116  		for i := 0; i < n; i++ {
   117  			if i != 0 {
   118  				s += "."
   119  			}
   120  
   121  			s += "1"
   122  		}
   123  	}
   124  
   125  	return s
   126  }