v0.4.0 21d1792

business_logic_facts_test.go RawHistoryFolder

1package analyzer
2
3import (
4	"go/ast"
5	"go/parser"
6	"go/token"
7	"go/types"
8	"testing"
9
10	"github.com/bnema/hexcheck/config"
11	"golang.org/x/tools/go/analysis"
12)
13
14func TestOversizedFunctionSkipsTypeAndLocalCollection(t *testing.T) {
15	fn := &ast.FuncDecl{Body: &ast.BlockStmt{List: []ast.Stmt{
16		&ast.AssignStmt{Lhs: []ast.Expr{&ast.SelectorExpr{X: ast.NewIdent("repo"), Sel: ast.NewIdent("Name")}}, Rhs: []ast.Expr{ast.NewIdent("value")}},
17	}}}
18	// No analysis pass: any type query before rejection would panic.
19	r := runner{cfg: &config.Config{Heuristics: config.Heuristics{BusinessLogicMaxFunctionNodes: testIntPtr(1)}}}
20	facts := r.collectBusinessFunctionFacts(fn, config.RoleAdapter)
21	if facts.nodeCount <= 1 || facts.constructedLocals != nil || facts.domainMutation {
22		t.Fatalf("oversized function was analyzed: %+v", facts)
23	}
24}
25
26func TestConstructionFactsCachedByObject(t *testing.T) {
27	fset := token.NewFileSet()
28	file, err := parser.ParseFile(fset, "locals.go", `package p
29 type Value struct { N int }
30 func fetch() Value { return Value{} }
31 func build(param Value) {
32  fresh := Value{}
33  fresh.N = 1
34  var zero Value
35  zero.N = 2
36  replaced := Value{}
37  replaced = fetch()
38  replaced.N = 3
39  fetched := fetch()
40  fetched.N = 4
41  param.N = 5
42  _ = fresh; _ = zero; _ = replaced; _ = fetched
43 }`, 0)
44	if err != nil {
45		t.Fatal(err)
46	}
47	info := &types.Info{Defs: map[*ast.Ident]types.Object{}, Uses: map[*ast.Ident]types.Object{}, Types: map[ast.Expr]types.TypeAndValue{}}
48	if _, err := new(types.Config).Check("p", fset, []*ast.File{file}, info); err != nil {
49		t.Fatal(err)
50	}
51	fn := file.Decls[2].(*ast.FuncDecl)
52	facts := businessFunctionFacts{fn: fn, runner: runner{pass: &analysis.Pass{TypesInfo: info}}}
53	facts.collectLocals()
54	want := map[string]bool{"fresh": true, "zero": true, "replaced": false, "fetched": false, "param": false}
55	checked := 0
56	ast.Inspect(fn.Body, func(n ast.Node) bool {
57		if sel, ok := n.(*ast.SelectorExpr); ok {
58			id := sel.X.(*ast.Ident)
59			if got := facts.isConstructedLocal(sel); got != want[id.Name] {
60				t.Errorf("%s constructed=%v, want %v", id.Name, got, want[id.Name])
61			}
62			checked++
63		}
64		return true
65	})
66	if checked != len(want) {
67		t.Fatalf("checked %d receivers, want %d", checked, len(want))
68	}
69}
70
71func TestCollectBusinessFunctionFactsInspectsFunctionBody(t *testing.T) {
72	fn := &ast.FuncDecl{
73		Name: ast.NewIdent("ResolveMode"),
74		Type: &ast.FuncType{},
75		Body: &ast.BlockStmt{List: []ast.Stmt{
76			&ast.IfStmt{
77				Cond: ast.NewIdent("ok"),
78				Body: &ast.BlockStmt{},
79			},
80		}},
81	}
82	r := runner{
83		cfg:  &config.Config{Heuristics: config.Heuristics{BusinessLogicMaxFunctionNodes: testIntPtr(2000)}},
84		pass: &analysis.Pass{TypesInfo: &types.Info{Types: map[ast.Expr]types.TypeAndValue{}}},
85	}
86
87	facts := r.collectBusinessFunctionFacts(fn, config.RoleAdapter)
88
89	if facts.fn != fn {
90		t.Fatalf("collectBusinessFunctionFacts() fn = %p, want %p", facts.fn, fn)
91	}
92	if facts.branchCount != 1 {
93		t.Fatalf("collectBusinessFunctionFacts() branchCount = %d, want 1", facts.branchCount)
94	}
95	if facts.nodeCount == 0 {
96		t.Fatal("collectBusinessFunctionFacts() nodeCount = 0, want inspected nodes")
97	}
98}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 5

↑↓ moveenter openesc close