cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.1.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/checks/base_test.go

72lines · modecode

1package checks_test
2
3import (
4 "testing"
5
6 "github.com/cloudflare/pint/internal/checks"
7 "github.com/cloudflare/pint/internal/parser"
8 "github.com/google/go-cmp/cmp"
9)
10
11type checkTest struct {
12 description string
13 content string
14 checker checks.RuleChecker
15 problems []checks.Problem
16}
17
18func runTests(t *testing.T, testCases []checkTest, opts ...cmp.Option) {
19 p := parser.NewParser()
20 for _, tc := range testCases {
21 t.Run(tc.description, func(t *testing.T) {
22 rules, err := p.Parse([]byte(tc.content))
23 if err != nil {
24 t.Fatal(err)
25 }
26 for _, rule := range rules {
27 problems := tc.checker.Check(rule)
28 if diff := cmp.Diff(tc.problems, problems, opts...); diff != "" {
29 t.Errorf("Check() returned wrong problem list (-want +got):\n%s", diff)
30 return
31 }
32 }
33 })
34 }
35}
36
37func TestParseSeverity(t *testing.T) {
38 type testCaseT struct {
39 input string
40 output string
41 shouldError bool
42 }
43
44 testCases := []testCaseT{
45 {"xxx", "", true},
46 {"Bug", "", true},
47 {"fatal", "Fatal", false},
48 {"bug", "Bug", false},
49 {"info", "Information", false},
50 {"warning", "Warning", false},
51 }
52
53 for _, tc := range testCases {
54 t.Run(tc.input, func(t *testing.T) {
55 sev, err := checks.ParseSeverity(tc.input)
56 hadError := err != nil
57
58 if hadError != tc.shouldError {
59 t.Errorf("checks.ParseSeverity() returned err=%v, expected=%v", err, tc.shouldError)
60 return
61 }
62
63 if hadError {
64 return
65 }
66
67 if sev.String() != tc.output {
68 t.Errorf("checks.ParseSeverity() returned severity=%q, expected=%q", sev, tc.output)
69 }
70 })
71 }
72}
73