File: validate_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (152 lines) | stat: -rw-r--r-- 3,788 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020 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 analysis

import (
	"strings"
	"testing"
)

func TestValidate(t *testing.T) {
	var (
		run = func(p *Pass) (interface{}, error) {
			return nil, nil
		}
		dependsOnSelf = &Analyzer{
			Name: "dependsOnSelf",
			Doc:  "this analyzer depends on itself",
			Run:  run,
		}
		inCycleA = &Analyzer{
			Name: "inCycleA",
			Doc:  "this analyzer depends on inCycleB",
			Run:  run,
		}
		inCycleB = &Analyzer{
			Name: "inCycleB",
			Doc:  "this analyzer depends on inCycleA and notInCycleA",
			Run:  run,
		}
		pointsToCycle = &Analyzer{
			Name: "pointsToCycle",
			Doc:  "this analyzer depends on inCycleA",
			Run:  run,
		}
		notInCycleA = &Analyzer{
			Name: "notInCycleA",
			Doc:  "this analyzer depends on notInCycleB and notInCycleC",
			Run:  run,
		}
		notInCycleB = &Analyzer{
			Name: "notInCycleB",
			Doc:  "this analyzer depends on notInCycleC",
			Run:  run,
		}
		notInCycleC = &Analyzer{
			Name: "notInCycleC",
			Doc:  "this analyzer has no dependencies",
			Run:  run,
		}
	)

	dependsOnSelf.Requires = append(dependsOnSelf.Requires, dependsOnSelf)
	inCycleA.Requires = append(inCycleA.Requires, inCycleB)
	inCycleB.Requires = append(inCycleB.Requires, inCycleA, notInCycleA)
	pointsToCycle.Requires = append(pointsToCycle.Requires, inCycleA)
	notInCycleA.Requires = append(notInCycleA.Requires, notInCycleB, notInCycleC)
	notInCycleB.Requires = append(notInCycleB.Requires, notInCycleC)
	notInCycleC.Requires = []*Analyzer{}

	cases := []struct {
		analyzers        []*Analyzer
		wantErr          bool
		analyzersInCycle map[string]bool
	}{
		{
			[]*Analyzer{dependsOnSelf},
			true,
			map[string]bool{"dependsOnSelf": true},
		},
		{
			[]*Analyzer{inCycleA, inCycleB},
			true,
			map[string]bool{"inCycleA": true, "inCycleB": true},
		},
		{
			[]*Analyzer{pointsToCycle},
			true,
			map[string]bool{"inCycleA": true, "inCycleB": true},
		},
		{
			[]*Analyzer{notInCycleA},
			false,
			map[string]bool{},
		},
	}

	for _, c := range cases {
		got := Validate(c.analyzers)

		if !c.wantErr {
			if got == nil {
				continue
			}
			t.Errorf("got unexpected error while validating analyzers %v: %v", c.analyzers, got)
		}

		if got == nil {
			t.Errorf("expected error while validating analyzers %v, but got nil", c.analyzers)
		}

		err, ok := got.(*CycleInRequiresGraphError)
		if !ok {
			t.Errorf("want CycleInRequiresGraphError, got %T", err)
		}

		for a := range c.analyzersInCycle {
			if !err.AnalyzerNames[a] {
				t.Errorf("analyzer %s should be in cycle", a)
			}
		}
		for a := range err.AnalyzerNames {
			if !c.analyzersInCycle[a] {
				t.Errorf("analyzer %s should not be in cycle", a)
			}
		}
	}
}

func TestCycleInRequiresGraphErrorMessage(t *testing.T) {
	err := CycleInRequiresGraphError{}
	errMsg := err.Error()
	wantSubstring := "cycle detected"
	if !strings.Contains(errMsg, wantSubstring) {
		t.Errorf("error string %s does not contain expected substring %q", errMsg, wantSubstring)
	}
}

func TestValidateEmptyDoc(t *testing.T) {
	withoutDoc := &Analyzer{
		Name: "withoutDoc",
		Run: func(p *Pass) (interface{}, error) {
			return nil, nil
		},
	}
	err := Validate([]*Analyzer{withoutDoc})
	if err == nil || !strings.Contains(err.Error(), "is undocumented") {
		t.Errorf("got unexpected error while validating analyzers withoutDoc: %v", err)
	}
}

func TestValidateNoRun(t *testing.T) {
	withoutRun := &Analyzer{
		Name: "withoutRun",
		Doc:  "this analyzer has no Run",
	}
	err := Validate([]*Analyzer{withoutRun})
	if err == nil || !strings.Contains(err.Error(), "has nil Run") {
		t.Errorf("got unexpected error while validating analyzers withoutRun: %v", err)
	}
}