2025-03-28 01:34:20 -07:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExportedRule_Configure(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
2025-04-07 20:59:55 +02:00
|
|
|
name string
|
|
|
|
|
arguments lint.Arguments
|
|
|
|
|
wantErr error
|
|
|
|
|
wantDisabledChecks disabledChecks
|
|
|
|
|
wantIsRepetitiveMsg string
|
2025-03-28 01:34:20 -07:00
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "default configuration",
|
|
|
|
|
arguments: lint.Arguments{},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: true,
|
|
|
|
|
PublicInterfaces: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "stutters",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"checkPrivateReceivers",
|
|
|
|
|
"disableStutteringCheck",
|
|
|
|
|
"checkPublicInterface",
|
|
|
|
|
"disableChecksOnConstants",
|
|
|
|
|
"disableChecksOnFunctions",
|
|
|
|
|
"disableChecksOnMethods",
|
|
|
|
|
"disableChecksOnTypes",
|
|
|
|
|
"disableChecksOnVariables",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: false,
|
|
|
|
|
PublicInterfaces: false,
|
|
|
|
|
Const: true,
|
|
|
|
|
Function: true,
|
|
|
|
|
Method: true,
|
2025-04-07 20:59:55 +02:00
|
|
|
RepetitiveNames: true,
|
2025-03-28 01:34:20 -07:00
|
|
|
Type: true,
|
|
|
|
|
Var: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "stutters",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid lowercased arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"checkprivatereceivers",
|
|
|
|
|
"disablestutteringcheck",
|
|
|
|
|
"checkpublicinterface",
|
|
|
|
|
"disablechecksonconstants",
|
|
|
|
|
"disablechecksonfunctions",
|
|
|
|
|
"disablechecksonmethods",
|
|
|
|
|
"disablechecksontypes",
|
|
|
|
|
"disablechecksonvariables",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: false,
|
|
|
|
|
PublicInterfaces: false,
|
|
|
|
|
Const: true,
|
|
|
|
|
Function: true,
|
|
|
|
|
Method: true,
|
2025-04-07 20:59:55 +02:00
|
|
|
RepetitiveNames: true,
|
2025-03-28 01:34:20 -07:00
|
|
|
Type: true,
|
|
|
|
|
Var: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "stutters",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid kebab-cased arguments",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"check-private-receivers",
|
|
|
|
|
"disable-stuttering-check",
|
|
|
|
|
"check-public-interface",
|
|
|
|
|
"disable-checks-on-constants",
|
|
|
|
|
"disable-checks-on-functions",
|
|
|
|
|
"disable-checks-on-methods",
|
|
|
|
|
"disable-checks-on-types",
|
|
|
|
|
"disable-checks-on-variables",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: false,
|
|
|
|
|
PublicInterfaces: false,
|
|
|
|
|
Const: true,
|
|
|
|
|
Function: true,
|
|
|
|
|
Method: true,
|
2025-04-07 20:59:55 +02:00
|
|
|
RepetitiveNames: true,
|
2025-03-28 01:34:20 -07:00
|
|
|
Type: true,
|
|
|
|
|
Var: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "stutters",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid sayRepetitiveInsteadOfStutters",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"sayRepetitiveInsteadOfStutters",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: true,
|
|
|
|
|
PublicInterfaces: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "is repetitive",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid lowercased sayRepetitiveInsteadOfStutters",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"sayrepetitiveinsteadofstutters",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: true,
|
|
|
|
|
PublicInterfaces: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "is repetitive",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "valid kebab-cased sayRepetitiveInsteadOfStutters",
|
|
|
|
|
arguments: lint.Arguments{
|
|
|
|
|
"say-repetitive-instead-of-stutters",
|
|
|
|
|
},
|
|
|
|
|
wantErr: nil,
|
|
|
|
|
wantDisabledChecks: disabledChecks{
|
|
|
|
|
PrivateReceivers: true,
|
|
|
|
|
PublicInterfaces: true,
|
|
|
|
|
},
|
2025-04-07 20:59:55 +02:00
|
|
|
wantIsRepetitiveMsg: "is repetitive",
|
2025-03-28 01:34:20 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "unknown configuration flag",
|
|
|
|
|
arguments: lint.Arguments{"unknownFlag"},
|
|
|
|
|
wantErr: errors.New("unknown configuration flag unknownFlag for exported rule"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid argument type",
|
|
|
|
|
arguments: lint.Arguments{123},
|
|
|
|
|
wantErr: errors.New("invalid argument for the exported rule: expecting a string, got int"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
var rule ExportedRule
|
|
|
|
|
|
|
|
|
|
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.disabledChecks != tt.wantDisabledChecks {
|
|
|
|
|
t.Errorf("unexpected disabledChecks: got = %+v, want %+v", rule.disabledChecks, tt.wantDisabledChecks)
|
|
|
|
|
}
|
2025-04-07 20:59:55 +02:00
|
|
|
if rule.isRepetitiveMsg != tt.wantIsRepetitiveMsg {
|
|
|
|
|
t.Errorf("unexpected stuttersMsg: got = %v, want %v", rule.isRepetitiveMsg, tt.wantIsRepetitiveMsg)
|
2025-03-28 01:34:20 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|