Source file src/debug/gosym/symtab_test.go

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gosym
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func assertString(t *testing.T, dsc, out, tgt string) {
    13  	if out != tgt {
    14  		t.Fatalf("Expected: %q Actual: %q for %s", tgt, out, dsc)
    15  	}
    16  }
    17  
    18  func TestStandardLibPackage(t *testing.T) {
    19  	s1 := Sym{Name: "io.(*LimitedReader).Read"}
    20  	s2 := Sym{Name: "io.NewSectionReader"}
    21  	assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "io")
    22  	assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "io")
    23  	assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LimitedReader)")
    24  	assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
    25  }
    26  
    27  func TestStandardLibPathPackage(t *testing.T) {
    28  	s1 := Sym{Name: "debug/gosym.(*LineTable).PCToLine"}
    29  	s2 := Sym{Name: "debug/gosym.NewTable"}
    30  	assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "debug/gosym")
    31  	assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "debug/gosym")
    32  	assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LineTable)")
    33  	assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
    34  }
    35  
    36  func TestGenericNames(t *testing.T) {
    37  	s1 := Sym{Name: "main.set[int]"}
    38  	s2 := Sym{Name: "main.(*value[int]).get"}
    39  	s3 := Sym{Name: "a/b.absDifference[c/d.orderedAbs[float64]]"}
    40  	s4 := Sym{Name: "main.testfunction[.shape.int]"}
    41  	assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "main")
    42  	assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "main")
    43  	assertString(t, fmt.Sprintf("package of %q", s3.Name), s3.PackageName(), "a/b")
    44  	assertString(t, fmt.Sprintf("package of %q", s4.Name), s4.PackageName(), "main")
    45  	assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "")
    46  	assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "(*value[int])")
    47  	assertString(t, fmt.Sprintf("receiver of %q", s3.Name), s3.ReceiverName(), "")
    48  	assertString(t, fmt.Sprintf("receiver of %q", s4.Name), s4.ReceiverName(), "")
    49  	assertString(t, fmt.Sprintf("base of %q", s1.Name), s1.BaseName(), "set[int]")
    50  	assertString(t, fmt.Sprintf("base of %q", s2.Name), s2.BaseName(), "get")
    51  	assertString(t, fmt.Sprintf("base of %q", s3.Name), s3.BaseName(), "absDifference[c/d.orderedAbs[float64]]")
    52  	assertString(t, fmt.Sprintf("base of %q", s4.Name), s4.BaseName(), "testfunction[.shape.int]")
    53  }
    54  
    55  func TestRemotePackage(t *testing.T) {
    56  	s1 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.(*FlagSet).PrintDefaults"}
    57  	s2 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.PrintDefaults"}
    58  	assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
    59  	assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
    60  	assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*FlagSet)")
    61  	assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
    62  }
    63  
    64  func TestIssue29551(t *testing.T) {
    65  	tests := []struct {
    66  		sym     Sym
    67  		pkgName string
    68  	}{
    69  		{Sym{goVersion: ver120, Name: "type:.eq.[9]debug/elf.intName"}, ""},
    70  		{Sym{goVersion: ver120, Name: "type:.hash.debug/elf.ProgHeader"}, ""},
    71  		{Sym{goVersion: ver120, Name: "type:.eq.runtime._panic"}, ""},
    72  		{Sym{goVersion: ver120, Name: "type:.hash.struct { runtime.gList; runtime.n int32 }"}, ""},
    73  		{Sym{goVersion: ver120, Name: "go:(*struct { sync.Mutex; math/big.table [64]math/big"}, ""},
    74  		{Sym{goVersion: ver120, Name: "go.uber.org/zap/buffer.(*Buffer).AppendString"}, "go.uber.org/zap/buffer"},
    75  		{Sym{goVersion: ver118, Name: "type..eq.[9]debug/elf.intName"}, ""},
    76  		{Sym{goVersion: ver118, Name: "type..hash.debug/elf.ProgHeader"}, ""},
    77  		{Sym{goVersion: ver118, Name: "type..eq.runtime._panic"}, ""},
    78  		{Sym{goVersion: ver118, Name: "type..hash.struct { runtime.gList; runtime.n int32 }"}, ""},
    79  		{Sym{goVersion: ver118, Name: "go.(*struct { sync.Mutex; math/big.table [64]math/big"}, ""},
    80  		// unfortunate
    81  		{Sym{goVersion: ver118, Name: "go.uber.org/zap/buffer.(*Buffer).AppendString"}, ""},
    82  	}
    83  
    84  	for _, tc := range tests {
    85  		assertString(t, fmt.Sprintf("package of %q", tc.sym.Name), tc.sym.PackageName(), tc.pkgName)
    86  	}
    87  }
    88  

View as plain text