Skip to content

Commit 61cece7

Browse files
authored
test: add tests for Options (#19)
1 parent 134336b commit 61cece7

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

sloglint.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@ func New(opts *Options) *analysis.Analyzer {
3939
Requires: []*analysis.Analyzer{inspect.Analyzer},
4040
Run: func(pass *analysis.Pass) (any, error) {
4141
if opts.KVOnly && opts.AttrOnly {
42-
return nil, errors.New("sloglint: incompatible options provided")
42+
return nil, fmt.Errorf("sloglint: Options.KVOnly and Options.AttrOnly: %w", errIncompatible)
43+
}
44+
switch opts.KeyNamingCase {
45+
case "", snakeCase, kebabCase, camelCase, pascalCase:
46+
default:
47+
return nil, fmt.Errorf("sloglint: Options.KeyNamingCase=%s: %w", opts.KeyNamingCase, errInvalidValue)
4348
}
4449
run(pass, opts)
4550
return nil, nil
4651
},
4752
}
4853
}
4954

50-
const (
51-
snakeCase = "snake"
52-
kebabCase = "kebab"
53-
camelCase = "camel"
54-
pascalCase = "pascal"
55+
var (
56+
errIncompatible = errors.New("incompatible options")
57+
errInvalidValue = errors.New("invalid value")
5558
)
5659

5760
func flags(opts *Options) flag.FlagSet {
@@ -72,13 +75,8 @@ func flags(opts *Options) flag.FlagSet {
7275
boolVar(&opts.ArgsOnSepLines, "args-on-sep-lines", "enforce putting arguments on separate lines")
7376

7477
fs.Func("key-naming-case", "enforce a single key naming convention (snake|kebab|camel|pascal)", func(s string) error {
75-
switch s {
76-
case snakeCase, kebabCase, camelCase, pascalCase:
77-
opts.KeyNamingCase = s
78-
return nil
79-
default:
80-
return fmt.Errorf("sloglint: -key-naming-case=%s: invalid value", s)
81-
}
78+
opts.KeyNamingCase = s
79+
return nil
8280
})
8381

8482
return *fs
@@ -118,6 +116,13 @@ var attrFuncs = map[string]struct{}{
118116
"log/slog.Any": {},
119117
}
120118

119+
const (
120+
snakeCase = "snake"
121+
kebabCase = "kebab"
122+
camelCase = "camel"
123+
pascalCase = "pascal"
124+
)
125+
121126
func run(pass *analysis.Pass, opts *Options) {
122127
visit := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
123128
filter := []ast.Node{(*ast.CallExpr)(nil)}

sloglint_internal_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sloglint
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestOptions(t *testing.T) {
9+
tests := map[string]struct {
10+
opts Options
11+
err error
12+
}{
13+
"incompatible": {
14+
opts: Options{KVOnly: true, AttrOnly: true},
15+
err: errIncompatible,
16+
},
17+
"invalid value": {
18+
opts: Options{KeyNamingCase: "-"},
19+
err: errInvalidValue,
20+
},
21+
}
22+
23+
for name, test := range tests {
24+
t.Run(name, func(t *testing.T) {
25+
analyzer := New(&test.opts)
26+
if _, err := analyzer.Run(nil); !errors.Is(err, test.err) {
27+
t.Errorf("errors.Is() mismatch\ngot: %v\nwant: %v", err, test.err)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)