Skip to content

Commit

Permalink
go1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Nov 11, 2023
1 parent ff5b1b0 commit ea1ab48
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Nikifor Seryakov
Copyright (c) 2020 Nikifor Seriakov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
66 changes: 46 additions & 20 deletions location_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package loc

import (
"fmt"
"path"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +16,7 @@ func TestLocationFillCallers(t *testing.T) {
st = CallersFill(0, st)

assert.Len(t, st, 1)
assert.Equal(t, "location_stack_test.go:14", st[0].String())
assert.Equal(t, "location_stack_test.go:16", st[0].String())
}

func testLocationsInside() (st PCs) {
Expand All @@ -36,11 +38,11 @@ func TestLocationPCsString(t *testing.T) {
}()

assert.Len(t, st, 3)
assert.Equal(t, "location_stack_test.go:24", st[0].String())
assert.Equal(t, "location_stack_test.go:25", st[1].String())
assert.Equal(t, "location_stack_test.go:34", st[2].String())
assert.Equal(t, "location_stack_test.go:26", st[0].String())
assert.Equal(t, "location_stack_test.go:27", st[1].String())
assert.Equal(t, "location_stack_test.go:36", st[2].String())

re := `location_stack_test.go:24 at location_stack_test.go:25 at location_stack_test.go:34`
re := `location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:36`

assert.Equal(t, re, st.String())
}
Expand All @@ -53,15 +55,16 @@ func TestLocationPCsFormat(t *testing.T) {
}()
}()

assert.Equal(t, "location_stack_test.go:24 at location_stack_test.go:25 at location_stack_test.go:52", fmt.Sprintf("%v", st))
assert.Equal(t, "location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:54", fmt.Sprintf("%v", st))

addAllSubs := innerFuncName(Caller(0), 2)
t.Logf("go version: %q: %q", gover(), addAllSubs)

assert.Equal(t, "loc.testLocationsInside.func1:24 at loc.testLocationsInside:25 at loc.TestLocationPCsFormat.func1"+addAllSubs+":52", fmt.Sprintf("%#v", st))
assert.Equal(t, "loc.testLocationsInside.func1:26 at loc.testLocationsInside:27 at loc.TestLocationPCsFormat"+addAllSubs+":54", fmt.Sprintf("%#v", st))

re := `at [\w.-/]*location_stack_test.go:24
at [\w.-/]*location_stack_test.go:25
at [\w.-/]*location_stack_test.go:52
re := `at [\w.-/]*location_stack_test.go:26
at [\w.-/]*location_stack_test.go:27
at [\w.-/]*location_stack_test.go:54
`
v := fmt.Sprintf("%+v", st)
assert.True(t, regexp.MustCompile(re).MatchString(v), "expected:\n%vgot:\n%v", re, v)
Expand All @@ -75,26 +78,49 @@ func TestLocationPCsFormatString(t *testing.T) {
}()
}()

assert.Equal(t, "location_stack_test.go:24 at location_stack_test.go:25 at location_stack_test.go:74", st.FormatString(""))
assert.Equal(t, "location_stack_test.go:26 at location_stack_test.go:27 at location_stack_test.go:77", st.FormatString(""))

addAllSubs := innerFuncName(Caller(0), 2)
t.Logf("all sub funs suffix (go ver %q): %q", gover(), addAllSubs)

assert.Equal(t, "loc.testLocationsInside.func1:24 at loc.testLocationsInside:25 at loc.TestLocationPCsFormatString.func1"+addAllSubs+":74", st.FormatString("#"))
assert.Equal(t, "loc.testLocationsInside.func1:26 at loc.testLocationsInside:27 at loc.TestLocationPCsFormatString"+addAllSubs+":77", st.FormatString("#"))

re := `at [\w.-/]*location_stack_test.go:24
at [\w.-/]*location_stack_test.go:25
at [\w.-/]*location_stack_test.go:74
re := `at [\w.-/]*location_stack_test.go:26
at [\w.-/]*location_stack_test.go:27
at [\w.-/]*location_stack_test.go:77
`

v := st.FormatString("+")
assert.True(t, regexp.MustCompile(re).MatchString(v), "expected:\n%vgot:\n%v", re, v)
}

var addAllSubs = func() string {
s := ".1"
if regexp.MustCompile("go1.16.*").MatchString(gover()) {
s = ""
func innerFuncName(fn PC, n int) string {
var s string

switch {
// case regexp.MustCompile("go1.16.*").MatchString(gover()):
// return ".func1"
case regexp.MustCompile("go1.21.*").MatchString(gover()):
name, _, _ := fn.NameFileLine()
name = path.Base(name)
name = name[strings.IndexByte(name, '.')+1:]

s = "." + name

for i := 0; i < n; i++ {
s += fmt.Sprintf(".func%v", i+1)
}
default:
s = ".func"

for i := 0; i < n; i++ {
if i != 0 {
s += "."
}

s += fmt.Sprintf("%v", 1)
}
}

return s
}()
}

0 comments on commit ea1ab48

Please sign in to comment.