1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestValidate(t *testing.T) {
14 dependsOnSelf = &Analyzer{
15 Name: "dependsOnSelf",
16 Doc: "this analyzer depends on itself",
20 Doc: "this analyzer depends on inCycleB",
24 Doc: "this analyzer depends on inCycleA and notInCycleA",
26 pointsToCycle = &Analyzer{
27 Name: "pointsToCycle",
28 Doc: "this analyzer depends on inCycleA",
30 notInCycleA = &Analyzer{
32 Doc: "this analyzer depends on notInCycleB and notInCycleC",
34 notInCycleB = &Analyzer{
36 Doc: "this analyzer depends on notInCycleC",
38 notInCycleC = &Analyzer{
40 Doc: "this analyzer has no dependencies",
44 dependsOnSelf.Requires = append(dependsOnSelf.Requires, dependsOnSelf)
45 inCycleA.Requires = append(inCycleA.Requires, inCycleB)
46 inCycleB.Requires = append(inCycleB.Requires, inCycleA, notInCycleA)
47 pointsToCycle.Requires = append(pointsToCycle.Requires, inCycleA)
48 notInCycleA.Requires = append(notInCycleA.Requires, notInCycleB, notInCycleC)
49 notInCycleB.Requires = append(notInCycleB.Requires, notInCycleC)
50 notInCycleC.Requires = []*Analyzer{}
55 analyzersInCycle map[string]bool
58 []*Analyzer{dependsOnSelf},
60 map[string]bool{"dependsOnSelf": true},
63 []*Analyzer{inCycleA, inCycleB},
65 map[string]bool{"inCycleA": true, "inCycleB": true},
68 []*Analyzer{pointsToCycle},
70 map[string]bool{"inCycleA": true, "inCycleB": true},
73 []*Analyzer{notInCycleA},
79 for _, c := range cases {
80 got := Validate(c.analyzers)
86 t.Errorf("got unexpected error while validating analyzers %v: %v", c.analyzers, got)
90 t.Errorf("expected error while validating analyzers %v, but got nil", c.analyzers)
93 err, ok := got.(*CycleInRequiresGraphError)
95 t.Errorf("want CycleInRequiresGraphError, got %T", err)
98 for a := range c.analyzersInCycle {
99 if !err.AnalyzerNames[a] {
100 t.Errorf("analyzer %s should be in cycle", a)
103 for a := range err.AnalyzerNames {
104 if !c.analyzersInCycle[a] {
105 t.Errorf("analyzer %s should not be in cycle", a)
111 func TestCycleInRequiresGraphErrorMessage(t *testing.T) {
112 err := CycleInRequiresGraphError{}
113 errMsg := err.Error()
114 wantSubstring := "cycle detected"
115 if !strings.Contains(errMsg, wantSubstring) {
116 t.Errorf("error string %s does not contain expected substring %q", errMsg, wantSubstring)