GoPLS Viewer

Home|gopls/go/analysis/validate_test.go
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.
4
5package analysis
6
7import (
8    "strings"
9    "testing"
10)
11
12func TestValidate(t *testing.T) {
13    var (
14        run = func(p *Pass) (interface{}, error) {
15            return nilnil
16        }
17        dependsOnSelf = &Analyzer{
18            Name"dependsOnSelf",
19            Doc:  "this analyzer depends on itself",
20            Run:  run,
21        }
22        inCycleA = &Analyzer{
23            Name"inCycleA",
24            Doc:  "this analyzer depends on inCycleB",
25            Run:  run,
26        }
27        inCycleB = &Analyzer{
28            Name"inCycleB",
29            Doc:  "this analyzer depends on inCycleA and notInCycleA",
30            Run:  run,
31        }
32        pointsToCycle = &Analyzer{
33            Name"pointsToCycle",
34            Doc:  "this analyzer depends on inCycleA",
35            Run:  run,
36        }
37        notInCycleA = &Analyzer{
38            Name"notInCycleA",
39            Doc:  "this analyzer depends on notInCycleB and notInCycleC",
40            Run:  run,
41        }
42        notInCycleB = &Analyzer{
43            Name"notInCycleB",
44            Doc:  "this analyzer depends on notInCycleC",
45            Run:  run,
46        }
47        notInCycleC = &Analyzer{
48            Name"notInCycleC",
49            Doc:  "this analyzer has no dependencies",
50            Run:  run,
51        }
52    )
53
54    dependsOnSelf.Requires = append(dependsOnSelf.RequiresdependsOnSelf)
55    inCycleA.Requires = append(inCycleA.RequiresinCycleB)
56    inCycleB.Requires = append(inCycleB.RequiresinCycleAnotInCycleA)
57    pointsToCycle.Requires = append(pointsToCycle.RequiresinCycleA)
58    notInCycleA.Requires = append(notInCycleA.RequiresnotInCycleBnotInCycleC)
59    notInCycleB.Requires = append(notInCycleB.RequiresnotInCycleC)
60    notInCycleC.Requires = []*Analyzer{}
61
62    cases := []struct {
63        analyzers        []*Analyzer
64        wantErr          bool
65        analyzersInCycle map[string]bool
66    }{
67        {
68            []*Analyzer{dependsOnSelf},
69            true,
70            map[string]bool{"dependsOnSelf"true},
71        },
72        {
73            []*Analyzer{inCycleAinCycleB},
74            true,
75            map[string]bool{"inCycleA"true"inCycleB"true},
76        },
77        {
78            []*Analyzer{pointsToCycle},
79            true,
80            map[string]bool{"inCycleA"true"inCycleB"true},
81        },
82        {
83            []*Analyzer{notInCycleA},
84            false,
85            map[string]bool{},
86        },
87    }
88
89    for _c := range cases {
90        got := Validate(c.analyzers)
91
92        if !c.wantErr {
93            if got == nil {
94                continue
95            }
96            t.Errorf("got unexpected error while validating analyzers %v: %v"c.analyzersgot)
97        }
98
99        if got == nil {
100            t.Errorf("expected error while validating analyzers %v, but got nil"c.analyzers)
101        }
102
103        errok := got.(*CycleInRequiresGraphError)
104        if !ok {
105            t.Errorf("want CycleInRequiresGraphError, got %T"err)
106        }
107
108        for a := range c.analyzersInCycle {
109            if !err.AnalyzerNames[a] {
110                t.Errorf("analyzer %s should be in cycle"a)
111            }
112        }
113        for a := range err.AnalyzerNames {
114            if !c.analyzersInCycle[a] {
115                t.Errorf("analyzer %s should not be in cycle"a)
116            }
117        }
118    }
119}
120
121func TestCycleInRequiresGraphErrorMessage(t *testing.T) {
122    err := CycleInRequiresGraphError{}
123    errMsg := err.Error()
124    wantSubstring := "cycle detected"
125    if !strings.Contains(errMsgwantSubstring) {
126        t.Errorf("error string %s does not contain expected substring %q"errMsgwantSubstring)
127    }
128}
129
130func TestValidateEmptyDoc(t *testing.T) {
131    withoutDoc := &Analyzer{
132        Name"withoutDoc",
133        Run: func(p *Pass) (interface{}, error) {
134            return nilnil
135        },
136    }
137    err := Validate([]*Analyzer{withoutDoc})
138    if err == nil || !strings.Contains(err.Error(), "is undocumented") {
139        t.Errorf("got unexpected error while validating analyzers withoutDoc: %v"err)
140    }
141}
142
143func TestValidateNoRun(t *testing.T) {
144    withoutRun := &Analyzer{
145        Name"withoutRun",
146        Doc:  "this analyzer has no Run",
147    }
148    err := Validate([]*Analyzer{withoutRun})
149    if err == nil || !strings.Contains(err.Error(), "has nil Run") {
150        t.Errorf("got unexpected error while validating analyzers withoutRun: %v"err)
151    }
152}
153
MembersX
TestValidateEmptyDoc.err
TestValidateNoRun.t
TestValidateNoRun.err
TestCycleInRequiresGraphErrorMessage
TestCycleInRequiresGraphErrorMessage.t
TestCycleInRequiresGraphErrorMessage.errMsg
TestValidate.cases
TestValidateEmptyDoc
TestValidateEmptyDoc.t
TestValidate.RangeStmt_2110.BlockStmt.got
TestValidate.RangeStmt_2110.BlockStmt.RangeStmt_2548.a
TestCycleInRequiresGraphErrorMessage.err
TestValidateEmptyDoc.withoutDoc
testing
TestValidate.t
TestValidate.RangeStmt_2110.c
TestValidateNoRun
TestValidateNoRun.withoutRun
TestValidate
TestValidate.RangeStmt_2110.BlockStmt.RangeStmt_2675.a
TestCycleInRequiresGraphErrorMessage.wantSubstring
Members
X