1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:54:40 -07:00

go.tools/go/types: print interface methods in sorted order

Required for testing code of clients.

R=adonovan
CC=golang-dev
https://golang.org/cl/35940049
This commit is contained in:
Robert Griesemer 2013-12-16 13:53:08 -08:00
parent 59db975409
commit 91abc02562
2 changed files with 20 additions and 3 deletions

View File

@ -48,7 +48,11 @@ func Id(pkg *Package, name string) string {
return name
}
// unexported names need the package path for differentiation
path := ""
// (if there's no package, make sure we don't start with '.'
// as that may change the order of methods between a setup
// inside a package and outside a package - which breaks some
// tests)
path := "_"
// TODO(gri): shouldn't !ast.IsExported(name) => pkg != nil be an precondition?
// if pkg == nil {
// panic("nil package in lookup of unexported name")
@ -56,7 +60,7 @@ func Id(pkg *Package, name string) string {
if pkg != nil {
path = pkg.path
if path == "" {
path = "?"
path = "_"
}
}
return path + "." + name

View File

@ -10,6 +10,7 @@ import (
"bytes"
"fmt"
"go/ast"
"sort"
)
// TypeString returns the string representation of typ.
@ -84,7 +85,19 @@ func WriteType(buf *bytes.Buffer, this *Package, typ Type) {
// }
//
buf.WriteString("interface{")
for i, m := range t.methods {
// Sort methods instead of printing them in source order.
// This is needed at the moment because interfaces are
// created by providing the list of source methods and
// embedded interfaces, but only have an accessor to the list
// of all methods (which is sorted by name). By sorting here
// we guarantee that the list is printed the same independent
// of how the Interface was created.
// TODO(gri) remove this extra step once we have the complete
// set of accessors for Interface.
var methods []*Func
methods = append(methods, t.methods...) // make a copy
sort.Sort(byUniqueMethodName(methods))
for i, m := range methods {
if i > 0 {
buf.WriteString("; ")
}