2025-03-28 01:34:20 -07:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2025-04-16 12:30:28 +03:00
|
|
|
"reflect"
|
2025-03-28 01:34:20 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestVarNamingRule_Configure(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
arguments lint.Arguments
|
|
|
|
|
wantErr error
|
|
|
|
|
wantAllowList []string
|
|
|
|
|
wantBlockList []string
|
|
|
|
|
wantAllowUpperCaseConst bool
|
|
|
|
|
wantSkipPackageNameChecks bool
|
2025-04-16 12:30:28 +03:00
|
|
|
wantBadPackageNames map[string]struct{}
|
2025-03-28 01:34:20 -07:00
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no arguments",
|
|
|
|
|
arguments: lint.Arguments{},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantAllowList: nil,
|
|
|
|
|
wantBlockList: nil,
|
|
|
|
|
wantAllowUpperCaseConst: false,
|
|
|
|
|
wantSkipPackageNameChecks: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
[]any{"ID"},
|
|
|
|
|
[]any{"VM"},
|
|
|
|
|
[]any{map[string]any{
|
|
|
|
|
"upperCaseConst": true,
|
|
|
|
|
"skipPackageNameChecks": true,
|
2025-04-16 12:30:28 +03:00
|
|
|
"extraBadPackageNames": []string{"helpers", "models"},
|
2025-03-28 01:34:20 -07:00
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantAllowList: []string{"ID"},
|
|
|
|
|
wantBlockList: []string{"VM"},
|
|
|
|
|
wantAllowUpperCaseConst: true,
|
|
|
|
|
wantSkipPackageNameChecks: true,
|
2025-04-16 12:30:28 +03:00
|
|
|
wantBadPackageNames: map[string]struct{}{"helpers": {}, "models": {}},
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid lowercased arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
[]any{"ID"},
|
|
|
|
|
[]any{"VM"},
|
|
|
|
|
[]any{map[string]any{
|
|
|
|
|
"uppercaseconst": true,
|
|
|
|
|
"skippackagenamechecks": true,
|
2025-04-16 12:30:28 +03:00
|
|
|
"extrabadpackagenames": []string{"helpers", "models"},
|
2025-03-28 01:34:20 -07:00
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantAllowList: []string{"ID"},
|
|
|
|
|
wantBlockList: []string{"VM"},
|
|
|
|
|
wantAllowUpperCaseConst: true,
|
|
|
|
|
wantSkipPackageNameChecks: true,
|
2025-04-16 12:30:28 +03:00
|
|
|
wantBadPackageNames: map[string]struct{}{"helpers": {}, "models": {}},
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid kebab-cased arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
[]any{"ID"},
|
|
|
|
|
[]any{"VM"},
|
|
|
|
|
[]any{map[string]any{
|
|
|
|
|
"upper-case-const": true,
|
|
|
|
|
"skip-package-name-checks": true,
|
2025-04-16 12:30:28 +03:00
|
|
|
"extra-bad-package-names": []string{"helpers", "models"},
|
2025-03-28 01:34:20 -07:00
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantAllowList: []string{"ID"},
|
|
|
|
|
wantBlockList: []string{"VM"},
|
|
|
|
|
wantAllowUpperCaseConst: true,
|
|
|
|
|
wantSkipPackageNameChecks: true,
|
2025-04-16 12:30:28 +03:00
|
|
|
wantBadPackageNames: map[string]struct{}{"helpers": {}, "models": {}},
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid allowlist type",
|
|
|
|
|
arguments: lint.Arguments{123},
|
|
|
|
|
wantErr: errors.New("invalid argument to the var-naming rule. Expecting a allowlist of type slice with initialisms, got int"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid allowlist value type",
|
|
|
|
|
arguments: lint.Arguments{[]any{123}},
|
|
|
|
|
wantErr: errors.New("invalid 123 values of the var-naming rule. Expecting slice of strings but got element of type []interface {}"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid blocklist type",
|
|
|
|
|
arguments: lint.Arguments{[]any{"ID"}, 123},
|
|
|
|
|
wantErr: errors.New("invalid argument to the var-naming rule. Expecting a blocklist of type slice with initialisms, got int"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid third argument type",
|
|
|
|
|
arguments: lint.Arguments{[]any{"ID"}, []any{"VM"}, 123},
|
|
|
|
|
wantErr: errors.New("invalid third argument to the var-naming rule. Expecting a options of type slice, got int"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid third argument slice size",
|
|
|
|
|
arguments: lint.Arguments{[]any{"ID"}, []any{"VM"}, []any{}},
|
|
|
|
|
wantErr: errors.New("invalid third argument to the var-naming rule. Expecting a options of type slice, of len==1, but 0"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid third argument first element type",
|
|
|
|
|
arguments: lint.Arguments{[]any{"ID"}, []any{"VM"}, []any{123}},
|
|
|
|
|
wantErr: errors.New("invalid third argument to the var-naming rule. Expecting a options of type slice, of len==1, with map, but int"),
|
|
|
|
|
},
|
2025-04-16 12:30:28 +03:00
|
|
|
{
|
|
|
|
|
name: "invalid third argument extraBadPackageNames",
|
|
|
|
|
arguments: lint.Arguments{[]any{""}, []any{""}, []any{map[string]any{"extraBadPackageNames": []int{1}}}},
|
|
|
|
|
wantErr: errors.New("invalid third argument to the var-naming rule. Expecting extraBadPackageNames of type slice of strings, but []int"),
|
|
|
|
|
},
|
2025-03-28 01:34:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
var rule VarNamingRule
|
|
|
|
|
|
|
|
|
|
err := rule.Configure(tt.arguments)
|
|
|
|
|
|
|
|
|
|
if tt.wantErr != nil {
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("unexpected error: got = nil, want = %v", tt.wantErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err.Error() != tt.wantErr.Error() {
|
|
|
|
|
t.Errorf("unexpected error: got = %v, want = %v", err, tt.wantErr)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("unexpected error: got = %v, want = nil", err)
|
|
|
|
|
}
|
|
|
|
|
if rule.allowUpperCaseConst != tt.wantAllowUpperCaseConst {
|
|
|
|
|
t.Errorf("unexpected allowUpperCaseConst: got = %v, want %v", rule.allowUpperCaseConst, tt.wantAllowUpperCaseConst)
|
|
|
|
|
}
|
|
|
|
|
if rule.skipPackageNameChecks != tt.wantSkipPackageNameChecks {
|
|
|
|
|
t.Errorf("unexpected skipPackageNameChecks: got = %v, want %v", rule.skipPackageNameChecks, tt.wantSkipPackageNameChecks)
|
|
|
|
|
}
|
2025-04-16 12:30:28 +03:00
|
|
|
if !reflect.DeepEqual(rule.extraBadPackageNames, tt.wantBadPackageNames) {
|
|
|
|
|
t.Errorf("unexpected extraBadPackageNames: got = %v, want %v", rule.extraBadPackageNames, tt.wantBadPackageNames)
|
|
|
|
|
}
|
2025-03-28 01:34:20 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|