1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

cmd/guru: adjust test output for go1.5 (fix build)

In go1.5, go/constant (floating-point) numeric values are printed
as fractions. Correct output as necessary so that they always
appear as floating-point numbers to match golden files.

Change-Id: If63a14d8d87bb664bf6272b16345e38e8d638ead
Reviewed-on: https://go-review.googlesource.com/20719
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2016-03-15 14:36:36 -07:00
parent f42ec616d3
commit ce9c53141f

View File

@ -353,7 +353,7 @@ type describeValueResult struct {
func (r *describeValueResult) display(printf printfFunc) {
var prefix, suffix string
if r.constVal != nil {
suffix = fmt.Sprintf(" of constant value %s", r.constVal)
suffix = fmt.Sprintf(" of constant value %s", constValString(r.constVal))
}
switch obj := r.obj.(type) {
case *types.Func:
@ -652,13 +652,26 @@ func (r *describePackageResult) display(printf printfFunc) {
}
}
// Helper function to adjust go1.5 numeric go/constant formatting.
// Can be removed once we give up compatibility with go1.5.
func constValString(v exact.Value) string {
if v.Kind() == exact.Float {
// In go1.5, go/constant floating-point values are printed
// as fractions. Make them appear as floating-point numbers.
if f, ok := exact.Float64Val(v); ok {
return fmt.Sprintf("%g", f)
}
}
return v.String()
}
func formatMember(obj types.Object, maxname int) string {
qualifier := types.RelativeTo(obj.Pkg())
var buf bytes.Buffer
fmt.Fprintf(&buf, "%-5s %-*s", tokenOf(obj), maxname, obj.Name())
switch obj := obj.(type) {
case *types.Const:
fmt.Fprintf(&buf, " %s = %s", types.TypeString(obj.Type(), qualifier), obj.Val().String())
fmt.Fprintf(&buf, " %s = %s", types.TypeString(obj.Type(), qualifier), constValString(obj.Val()))
case *types.Func:
fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type(), qualifier))
@ -695,7 +708,7 @@ func (r *describePackageResult) toSerial(res *serial.Result, fset *token.FileSet
var val string
switch mem := mem.obj.(type) {
case *types.Const:
val = mem.Val().String()
val = constValString(mem.Val())
case *types.TypeName:
typ = typ.Underlying()
}