|
1 |
| -package sloglint_test |
| 1 | +package sloglint |
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "testing"
|
5 | 6 |
|
6 |
| - "go-simpler.org/sloglint" |
7 | 7 | "golang.org/x/tools/go/analysis/analysistest"
|
8 | 8 | )
|
9 | 9 |
|
10 | 10 | func TestAnalyzer(t *testing.T) {
|
11 |
| - testdata := analysistest.TestData() |
12 |
| - |
13 |
| - t.Run("no mixed arguments", func(t *testing.T) { |
14 |
| - analyzer := sloglint.New(nil) |
15 |
| - analysistest.Run(t, testdata, analyzer, "no_mixed_args") |
16 |
| - }) |
17 |
| - |
18 |
| - t.Run("key-value pairs only", func(t *testing.T) { |
19 |
| - analyzer := sloglint.New(&sloglint.Options{KVOnly: true}) |
20 |
| - analysistest.Run(t, testdata, analyzer, "kv_only") |
21 |
| - }) |
22 |
| - |
23 |
| - t.Run("attributes only", func(t *testing.T) { |
24 |
| - analyzer := sloglint.New(&sloglint.Options{AttrOnly: true}) |
25 |
| - analysistest.Run(t, testdata, analyzer, "attr_only") |
26 |
| - }) |
27 |
| - |
28 |
| - t.Run("no global (all)", func(t *testing.T) { |
29 |
| - analyzer := sloglint.New(&sloglint.Options{NoGlobal: "all"}) |
30 |
| - analysistest.Run(t, testdata, analyzer, "no_global_all") |
31 |
| - }) |
32 |
| - |
33 |
| - t.Run("no global (default)", func(t *testing.T) { |
34 |
| - analyzer := sloglint.New(&sloglint.Options{NoGlobal: "default"}) |
35 |
| - analysistest.Run(t, testdata, analyzer, "no_global_default") |
36 |
| - }) |
37 |
| - |
38 |
| - t.Run("context only (all)", func(t *testing.T) { |
39 |
| - analyzer := sloglint.New(&sloglint.Options{ContextOnly: "all"}) |
40 |
| - analysistest.Run(t, testdata, analyzer, "context_only_all") |
41 |
| - }) |
42 |
| - |
43 |
| - t.Run("context only (scope)", func(t *testing.T) { |
44 |
| - analyzer := sloglint.New(&sloglint.Options{ContextOnly: "scope"}) |
45 |
| - analysistest.Run(t, testdata, analyzer, "context_only_scope") |
46 |
| - }) |
47 |
| - |
48 |
| - t.Run("static message", func(t *testing.T) { |
49 |
| - analyzer := sloglint.New(&sloglint.Options{StaticMsg: true}) |
50 |
| - analysistest.Run(t, testdata, analyzer, "static_msg") |
51 |
| - }) |
52 |
| - |
53 |
| - t.Run("no raw keys", func(t *testing.T) { |
54 |
| - analyzer := sloglint.New(&sloglint.Options{NoRawKeys: true}) |
55 |
| - analysistest.Run(t, testdata, analyzer, "no_raw_keys") |
56 |
| - }) |
57 |
| - |
58 |
| - t.Run("key naming case", func(t *testing.T) { |
59 |
| - analyzer := sloglint.New(&sloglint.Options{KeyNamingCase: "snake"}) |
60 |
| - analysistest.Run(t, testdata, analyzer, "key_naming_case") |
61 |
| - }) |
62 |
| - |
63 |
| - t.Run("arguments on separate lines", func(t *testing.T) { |
64 |
| - analyzer := sloglint.New(&sloglint.Options{ArgsOnSepLines: true}) |
65 |
| - analysistest.Run(t, testdata, analyzer, "args_on_sep_lines") |
66 |
| - }) |
67 |
| - |
68 |
| - t.Run("forbidden keys", func(t *testing.T) { |
69 |
| - analyzer := sloglint.New(&sloglint.Options{ForbiddenKeys: []string{"foo_bar"}}) |
70 |
| - analysistest.Run(t, testdata, analyzer, "forbidden_keys") |
71 |
| - }) |
72 |
| - |
73 |
| - t.Run("message style (lowercased)", func(t *testing.T) { |
74 |
| - analyzer := sloglint.New(&sloglint.Options{MsgStyle: "lowercased"}) |
75 |
| - analysistest.Run(t, testdata, analyzer, "msg_style_lowercased") |
76 |
| - }) |
| 11 | + tests := map[string]struct { |
| 12 | + opts Options |
| 13 | + dir string |
| 14 | + }{ |
| 15 | + "no mixed arguments": {Options{NoMixedArgs: true}, "no_mixed_args"}, |
| 16 | + "key-value pairs only": {Options{KVOnly: true}, "kv_only"}, |
| 17 | + "attributes only": {Options{AttrOnly: true}, "attr_only"}, |
| 18 | + "no global (all)": {Options{NoGlobal: "all"}, "no_global_all"}, |
| 19 | + "no global (default)": {Options{NoGlobal: "default"}, "no_global_default"}, |
| 20 | + "context only (all)": {Options{ContextOnly: "all"}, "context_only_all"}, |
| 21 | + "context only (scope)": {Options{ContextOnly: "scope"}, "context_only_scope"}, |
| 22 | + "static message": {Options{StaticMsg: true}, "static_msg"}, |
| 23 | + "no raw keys": {Options{NoRawKeys: true}, "no_raw_keys"}, |
| 24 | + "key naming case": {Options{KeyNamingCase: "snake"}, "key_naming_case"}, |
| 25 | + "arguments on separate lines": {Options{ArgsOnSepLines: true}, "args_on_sep_lines"}, |
| 26 | + "forbidden keys": {Options{ForbiddenKeys: []string{"foo_bar"}}, "forbidden_keys"}, |
| 27 | + "message style (lowercased)": {Options{MsgStyle: "lowercased"}, "msg_style_lowercased"}, |
| 28 | + "message style (capitalized)": {Options{MsgStyle: "capitalized"}, "msg_style_capitalized"}, |
| 29 | + "slog.DiscardHandler": {Options{go124: true}, "discard_handler"}, |
| 30 | + } |
| 31 | + |
| 32 | + for name, tt := range tests { |
| 33 | + t.Run(name, func(t *testing.T) { |
| 34 | + analyzer := New(&tt.opts) |
| 35 | + testdata := analysistest.TestData() |
| 36 | + analysistest.RunWithSuggestedFixes(t, testdata, analyzer, tt.dir) |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
77 | 40 |
|
78 |
| - t.Run("message style (capitalized)", func(t *testing.T) { |
79 |
| - analyzer := sloglint.New(&sloglint.Options{MsgStyle: "capitalized"}) |
80 |
| - analysistest.Run(t, testdata, analyzer, "msg_style_capitalized") |
81 |
| - }) |
| 41 | +func TestOptions(t *testing.T) { |
| 42 | + tests := map[string]struct { |
| 43 | + opts Options |
| 44 | + err error |
| 45 | + }{ |
| 46 | + "KVOnly+AttrOnly: incompatible": {Options{KVOnly: true, AttrOnly: true}, errIncompatible}, |
| 47 | + "NoGlobal: invalid value": {Options{NoGlobal: "-"}, errInvalidValue}, |
| 48 | + "ContextOnly: invalid value": {Options{ContextOnly: "-"}, errInvalidValue}, |
| 49 | + "MsgStyle: invalid value": {Options{MsgStyle: "-"}, errInvalidValue}, |
| 50 | + "KeyNamingCase: invalid value": {Options{KeyNamingCase: "-"}, errInvalidValue}, |
| 51 | + } |
| 52 | + |
| 53 | + for name, test := range tests { |
| 54 | + t.Run(name, func(t *testing.T) { |
| 55 | + analyzer := New(&test.opts) |
| 56 | + if _, err := analyzer.Run(nil); !errors.Is(err, test.err) { |
| 57 | + t.Errorf("errors.Is() mismatch\ngot: %v\nwant: %v", err, test.err) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
82 | 61 | }
|
0 commit comments