| 1 | // Copyright 2023 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 | |
| 5 | package driverutil_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "golang.org/x/tools/go/analysis" |
| 13 | "golang.org/x/tools/internal/analysis/driverutil" |
| 14 | ) |
| 15 | |
| 16 | func TestResolveURLs(t *testing.T) { |
| 17 | // TestResolveURL test the 12 different combinations for how URLs can be resolved |
| 18 | // when Analyzer.URL are Diagnostic.Category are empty or non-empty, and when |
| 19 | // Diagnostic.URL is empty, absolute or relative. |
| 20 | |
| 21 | aURL := &analysis.Analyzer{URL: "https://analyzer.example"} |
| 22 | noURL := &analysis.Analyzer{URL: ""} |
| 23 | tests := []struct { |
| 24 | analyzer *analysis.Analyzer |
| 25 | diagnostic analysis.Diagnostic |
| 26 | want string |
| 27 | }{ |
| 28 | {noURL, analysis.Diagnostic{Category: "", URL: ""}, ""}, |
| 29 | {noURL, analysis.Diagnostic{Category: "", URL: "#relative"}, "#relative"}, |
| 30 | {noURL, analysis.Diagnostic{Category: "", URL: "https://absolute.diagnostic"}, "https://absolute.diagnostic"}, |
| 31 | {noURL, analysis.Diagnostic{Category: "category", URL: ""}, "#category"}, |
| 32 | {noURL, analysis.Diagnostic{Category: "category", URL: "#relative"}, "#relative"}, |
| 33 | {noURL, analysis.Diagnostic{Category: "category", URL: "https://absolute.diagnostic"}, "https://absolute.diagnostic"}, |
| 34 | {aURL, analysis.Diagnostic{Category: "", URL: ""}, "https://analyzer.example"}, |
| 35 | {aURL, analysis.Diagnostic{Category: "", URL: "#relative"}, "https://analyzer.example#relative"}, |
| 36 | {aURL, analysis.Diagnostic{Category: "", URL: "https://absolute.diagnostic"}, "https://absolute.diagnostic"}, |
| 37 | {aURL, analysis.Diagnostic{Category: "category", URL: ""}, "https://analyzer.example#category"}, |
| 38 | {aURL, analysis.Diagnostic{Category: "category", URL: "#relative"}, "https://analyzer.example#relative"}, |
| 39 | {aURL, analysis.Diagnostic{Category: "category", URL: "https://absolute.diagnostic"}, "https://absolute.diagnostic"}, |
| 40 | } |
| 41 | for _, c := range tests { |
| 42 | got, err := driverutil.ResolveURL(c.analyzer, c.diagnostic) |
| 43 | if err != nil { |
| 44 | t.Errorf("Unexpected error from ResolveURL %s", err) |
| 45 | } else if got != c.want { |
| 46 | t.Errorf("ResolveURL(%q,%v)=%q. want %s", c.analyzer.URL, c.diagnostic, got, c.want) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestResolveURLErrors(t *testing.T) { |
| 52 | tests := []struct { |
| 53 | analyzer *analysis.Analyzer |
| 54 | diagnostic analysis.Diagnostic |
| 55 | want string |
| 56 | }{ |
| 57 | {&analysis.Analyzer{URL: ":not a url"}, analysis.Diagnostic{Category: "", URL: "#relative"}, "invalid Analyzer.URL"}, |
| 58 | {&analysis.Analyzer{URL: "https://analyzer.example"}, analysis.Diagnostic{Category: "", URL: ":not a URL"}, "invalid Diagnostic.URL"}, |
| 59 | } |
| 60 | for _, c := range tests { |
| 61 | _, err := driverutil.ResolveURL(c.analyzer, c.diagnostic) |
| 62 | if got := fmt.Sprint(err); !strings.HasPrefix(got, c.want) { |
| 63 | t.Errorf("ResolveURL(%q, %q) expected an error starting with %q. got %q", c.analyzer.URL, c.diagnostic.URL, c.want, got) |
| 64 | } |
| 65 | } |
| 66 | } |