File: evalop_test.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (75 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download
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
package evalop

import (
	"go/ast"
	"go/parser"
	"go/token"
	"testing"
)

func assertNoError(err error, t testing.TB, s string) {
	t.Helper()
	if err != nil {
		t.Fatalf("failed assertion %s: %s\n", s, err)
	}
}

func TestEvalSwitchExhaustiveness(t *testing.T) {
	// Checks that the switch statement in (*EvalScope).executeOp of
	// pkg/proc/eval.go exhaustively covers all implementations of the
	// evalop.Op interface.

	ops := make(map[string]bool)

	var fset, fset2 token.FileSet
	f, err := parser.ParseFile(&fset, "ops.go", nil, 0)
	assertNoError(err, t, "ParseFile")
	for _, decl := range f.Decls {
		decl, _ := decl.(*ast.FuncDecl)
		if decl == nil {
			continue
		}
		if decl.Name.Name != "depthCheck" {
			continue
		}
		ops[decl.Recv.List[0].Type.(*ast.StarExpr).X.(*ast.Ident).Name] = false
	}

	f, err = parser.ParseFile(&fset2, "../eval.go", nil, 0)
	assertNoError(err, t, "ParseFile")
	for _, decl := range f.Decls {
		decl, _ := decl.(*ast.FuncDecl)
		if decl == nil {
			continue
		}
		if decl.Name.Name != "executeOp" {
			continue
		}
		ast.Inspect(decl, func(n ast.Node) bool {
			sw, _ := n.(*ast.TypeSwitchStmt)
			if sw == nil {
				return true
			}

			for _, c := range sw.Body.List {
				if len(c.(*ast.CaseClause).List) == 0 {
					// default clause
					continue
				}
				sel := c.(*ast.CaseClause).List[0].(*ast.StarExpr).X.(*ast.SelectorExpr)
				if sel.X.(*ast.Ident).Name != "evalop" {
					t.Fatalf("wrong case statement at: %v", fset2.Position(sel.Pos()))
				}

				ops[sel.Sel.Name] = true
			}
			return false
		})
	}

	for op := range ops {
		if !ops[op] {
			t.Errorf("evalop.Op %s not used in executeOp", op)
		}
	}
}