| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"golang.org/x/telemetry/internal/chartconfig" |
| 14 |
) |
| 15 |
|
| 16 |
func TestLoadedChartsAreValid(t *testing.T) { |
| 17 |
|
| 18 |
charts, err := chartconfig.Load() |
| 19 |
if err != nil { |
| 20 |
t.Errorf("Load() failed: %v", err) |
| 21 |
} |
| 22 |
for i, chart := range charts { |
| 23 |
if err := ValidateChartConfig(chart); err != nil { |
| 24 |
t.Errorf("Chart %d is invalid: %v", i, err) |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func TestValidateOK(t *testing.T) { |
| 30 |
|
| 31 |
const input = ` |
| 32 |
title: Editor Distribution |
| 33 |
counter: gopls/editor:{emacs,vim,vscode,other} |
| 34 |
type: partition |
| 35 |
issue: https://go.dev/issue/12345 |
| 36 |
program: golang.org/x/tools/gopls |
| 37 |
` |
| 38 |
records, err := chartconfig.Parse([]byte(input)) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
if len(records) != 1 { |
| 43 |
t.Fatalf("Parse(%q) returned %d records, want exactly 1", input, len(records)) |
| 44 |
} |
| 45 |
if err := ValidateChartConfig(records[0]); err != nil { |
| 46 |
t.Errorf("Validate(%q) = %v, want nil", input, err) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestValidate(t *testing.T) { |
| 51 |
tests := map[string][]string{ |
| 52 |
|
| 53 |
"description:bar": {"title", "program", "issue", "counter", "type"}, |
| 54 |
|
| 55 |
|
| 56 |
"version:1.2.3.4": {"semver"}, |
| 57 |
|
| 58 |
|
| 59 |
"depth:-1": {"non-negative", "stack"}, |
| 60 |
} |
| 61 |
|
| 62 |
for input, wantErrs := range tests { |
| 63 |
records, err := chartconfig.Parse([]byte(input)) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
if len(records) != 1 { |
| 68 |
t.Fatalf("Parse(%q) returned %d records, want exactly 1", input, len(records)) |
| 69 |
} |
| 70 |
err = ValidateChartConfig(records[0]) |
| 71 |
if err == nil { |
| 72 |
t.Fatalf("Validate(%q) succeeded unexpectedly", input) |
| 73 |
} |
| 74 |
errs := err.Error() |
| 75 |
for _, want := range wantErrs { |
| 76 |
if !strings.Contains(errs, want) { |
| 77 |
t.Errorf("Validate(%q) = %v, want containing %q", input, err, want) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |