File: errorcode_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (105 lines) | stat: -rw-r--r-- 2,703 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package typesinternal_test

import (
	"fmt"
	"go/ast"
	"go/constant"
	"go/parser"
	"go/token"
	"go/types"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"testing"
)

func TestErrorCodes(t *testing.T) {
	t.Skip("unskip this test to verify the correctness of errorcode.go for the current Go version")

	// For older go versions, this file was src/go/types/errorcodes.go.
	stdPath := filepath.Join(runtime.GOROOT(), "src", "internal", "types", "errors", "codes.go")
	stdCodes, err := loadCodes(stdPath)
	if err != nil {
		t.Fatalf("loading std codes: %v", err)
	}

	localPath := "errorcode.go"
	localCodes, err := loadCodes(localPath)
	if err != nil {
		t.Fatalf("loading local codes: %v", err)
	}

	// Verify that all std codes are present, with the correct value.
	type codeVal struct {
		Name  string
		Value int64
	}
	var byValue []codeVal
	for k, v := range stdCodes {
		byValue = append(byValue, codeVal{k, v})
	}
	sort.Slice(byValue, func(i, j int) bool {
		return byValue[i].Value < byValue[j].Value
	})

	localLookup := make(map[int64]string)
	for k, v := range localCodes {
		if _, ok := localLookup[v]; ok {
			t.Errorf("duplicate error code value %d", v)
		}
		localLookup[v] = k
	}

	for _, std := range byValue {
		local, ok := localCodes[std.Name]
		if !ok {
			if v, ok := localLookup[std.Value]; ok {
				t.Errorf("Missing code for %s (code %d is %s)", std.Name, std.Value, v)
			} else {
				t.Errorf("Missing code for %s", std.Name)
			}
		}
		if local != std.Value {
			t.Errorf("Mismatching value for %s: got %d, but stdlib has %d", std.Name, local, std.Value)
		}
	}
}

// loadCodes loads all constant values found in filepath.
//
// The given file must type-check cleanly as a standalone file.
func loadCodes(filepath string) (map[string]int64, error) {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, filepath, nil, 0)
	if err != nil {
		return nil, err
	}
	var config types.Config
	pkg, err := config.Check("p", fset, []*ast.File{f}, nil)
	if err != nil {
		return nil, err
	}

	codes := make(map[string]int64)
	for _, name := range pkg.Scope().Names() {
		obj := pkg.Scope().Lookup(name)
		c, ok := obj.(*types.Const)
		if !ok {
			continue
		}
		name := strings.TrimPrefix(name, "_") // compatibility with earlier go versions
		codes[name], ok = constant.Int64Val(c.Val())
		if !ok {
			return nil, fmt.Errorf("non integral value %v for %s", c.Val(), name)
		}
	}
	if len(codes) < 100 {
		return nil, fmt.Errorf("sanity check: got %d codes but expected at least 100", len(codes))
	}
	return codes, nil
}