From 61cece76d15bcc18df3fad3e2293e24e7565a1a0 Mon Sep 17 00:00:00 2001 From: Thomas <73077675+tmzane@users.noreply.github.com> Date: Sun, 29 Oct 2023 00:15:01 +0300 Subject: [PATCH] test: add tests for `Options` (#19) --- sloglint.go | 31 ++++++++++++++++++------------- sloglint_internal_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 sloglint_internal_test.go diff --git a/sloglint.go b/sloglint.go index 9e50f8c..e480c22 100644 --- a/sloglint.go +++ b/sloglint.go @@ -39,7 +39,12 @@ func New(opts *Options) *analysis.Analyzer { Requires: []*analysis.Analyzer{inspect.Analyzer}, Run: func(pass *analysis.Pass) (any, error) { if opts.KVOnly && opts.AttrOnly { - return nil, errors.New("sloglint: incompatible options provided") + return nil, fmt.Errorf("sloglint: Options.KVOnly and Options.AttrOnly: %w", errIncompatible) + } + switch opts.KeyNamingCase { + case "", snakeCase, kebabCase, camelCase, pascalCase: + default: + return nil, fmt.Errorf("sloglint: Options.KeyNamingCase=%s: %w", opts.KeyNamingCase, errInvalidValue) } run(pass, opts) return nil, nil @@ -47,11 +52,9 @@ func New(opts *Options) *analysis.Analyzer { } } -const ( - snakeCase = "snake" - kebabCase = "kebab" - camelCase = "camel" - pascalCase = "pascal" +var ( + errIncompatible = errors.New("incompatible options") + errInvalidValue = errors.New("invalid value") ) func flags(opts *Options) flag.FlagSet { @@ -72,13 +75,8 @@ func flags(opts *Options) flag.FlagSet { boolVar(&opts.ArgsOnSepLines, "args-on-sep-lines", "enforce putting arguments on separate lines") fs.Func("key-naming-case", "enforce a single key naming convention (snake|kebab|camel|pascal)", func(s string) error { - switch s { - case snakeCase, kebabCase, camelCase, pascalCase: - opts.KeyNamingCase = s - return nil - default: - return fmt.Errorf("sloglint: -key-naming-case=%s: invalid value", s) - } + opts.KeyNamingCase = s + return nil }) return *fs @@ -118,6 +116,13 @@ var attrFuncs = map[string]struct{}{ "log/slog.Any": {}, } +const ( + snakeCase = "snake" + kebabCase = "kebab" + camelCase = "camel" + pascalCase = "pascal" +) + func run(pass *analysis.Pass, opts *Options) { visit := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) filter := []ast.Node{(*ast.CallExpr)(nil)} diff --git a/sloglint_internal_test.go b/sloglint_internal_test.go new file mode 100644 index 0000000..c9a6d47 --- /dev/null +++ b/sloglint_internal_test.go @@ -0,0 +1,31 @@ +package sloglint + +import ( + "errors" + "testing" +) + +func TestOptions(t *testing.T) { + tests := map[string]struct { + opts Options + err error + }{ + "incompatible": { + opts: Options{KVOnly: true, AttrOnly: true}, + err: errIncompatible, + }, + "invalid value": { + opts: Options{KeyNamingCase: "-"}, + err: errInvalidValue, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + analyzer := New(&test.opts) + if _, err := analyzer.Run(nil); !errors.Is(err, test.err) { + t.Errorf("errors.Is() mismatch\ngot: %v\nwant: %v", err, test.err) + } + }) + } +}