diff --git a/test/argument_limit_test.go b/test/argument_limit_test.go index c4f0d93ef..cbe6be2c4 100644 --- a/test/argument_limit_test.go +++ b/test/argument_limit_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestArgumentLimitDefault(t *testing.T) { + testRule(t, "argument_limit_default", &rule.ArgumentsLimitRule{}, &lint.RuleConfig{}) +} + func TestArgumentLimit(t *testing.T) { testRule(t, "argument_limit", &rule.ArgumentsLimitRule{}, &lint.RuleConfig{ Arguments: []any{int64(3)}, diff --git a/test/banned_characters_test.go b/test/banned_characters_test.go index a639842d7..bbd756b33 100644 --- a/test/banned_characters_test.go +++ b/test/banned_characters_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestBannedCharactersDefault(t *testing.T) { + testRule(t, "banned_characters_default", &rule.BannedCharsRule{}, &lint.RuleConfig{}) +} + // Test banned characters in a const, var and func names. // One banned character is in the comment and should not be checked. // One banned character from the list is not present in the fixture file. diff --git a/test/cognitive_complexity_test.go b/test/cognitive_complexity_test.go index f35a445dc..4524240fb 100644 --- a/test/cognitive_complexity_test.go +++ b/test/cognitive_complexity_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestCognitiveComplexityDefault(t *testing.T) { + testRule(t, "cognitive_complexity_default", &rule.CognitiveComplexityRule{}, &lint.RuleConfig{}) +} + func TestCognitiveComplexity(t *testing.T) { testRule(t, "cognitive_complexity", &rule.CognitiveComplexityRule{}, &lint.RuleConfig{ Arguments: []any{int64(0)}, diff --git a/test/context_as_argument_test.go b/test/context_as_argument_test.go index 2e8af0a39..67fa54da9 100644 --- a/test/context_as_argument_test.go +++ b/test/context_as_argument_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestContextAsArgumentDefault(t *testing.T) { + testRule(t, "context_as_argument_default", &rule.ContextAsArgumentRule{}, &lint.RuleConfig{}) +} + func TestContextAsArgument(t *testing.T) { testRule(t, "context_as_argument", &rule.ContextAsArgumentRule{}, &lint.RuleConfig{ Arguments: []any{ diff --git a/test/cyclomatic_test.go b/test/cyclomatic_test.go index cdfce30a5..b09f1c0a2 100644 --- a/test/cyclomatic_test.go +++ b/test/cyclomatic_test.go @@ -7,7 +7,12 @@ import ( "github.com/mgechev/revive/rule" ) +func TestCyclomaticDefault(t *testing.T) { + testRule(t, "cyclomatic_default", &rule.CyclomaticRule{}, &lint.RuleConfig{}) +} + func TestCyclomatic(t *testing.T) { + testRule(t, "cyclomatic_default", &rule.CyclomaticRule{}, &lint.RuleConfig{}) testRule(t, "cyclomatic", &rule.CyclomaticRule{}, &lint.RuleConfig{ Arguments: []any{int64(1)}, }) diff --git a/test/dot_imports_test.go b/test/dot_imports_test.go index e6067a641..368021b10 100644 --- a/test/dot_imports_test.go +++ b/test/dot_imports_test.go @@ -7,12 +7,14 @@ import ( "github.com/mgechev/revive/rule" ) -func TestDotImports(t *testing.T) { - args := []any{map[string]any{ - "allowedPackages": []any{"errors", "context", "github.com/BurntSushi/toml"}, - }} +func TestDotImportsDefault(t *testing.T) { + testRule(t, "dot_imports_default", &rule.DotImportsRule{}, &lint.RuleConfig{}) +} - testRule(t, "import_dot", &rule.DotImportsRule{}, &lint.RuleConfig{ - Arguments: args, +func TestDotImports(t *testing.T) { + testRule(t, "dot_imports", &rule.DotImportsRule{}, &lint.RuleConfig{ + Arguments: []any{map[string]any{ + "allowedPackages": []any{"errors", "context", "github.com/BurntSushi/toml"}, + }}, }) } diff --git a/test/enforce_repeated_arg_type_style_test.go b/test/enforce_repeated_arg_type_style_test.go index 51c18bb33..153e3aed6 100644 --- a/test/enforce_repeated_arg_type_style_test.go +++ b/test/enforce_repeated_arg_type_style_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestEnforceRepeatedArgTypeStyleDefault(t *testing.T) { + testRule(t, "enforce_repeated_arg_type_style_default", &rule.EnforceRepeatedArgTypeStyleRule{}, &lint.RuleConfig{}) +} + func TestEnforceRepeatedArgTypeStyleShort(t *testing.T) { testRule(t, "enforce_repeated_arg_type_style_short_args", &rule.EnforceRepeatedArgTypeStyleRule{}, &lint.RuleConfig{ Arguments: []any{"short"}, diff --git a/test/file_header_test.go b/test/file_header_test.go index b340a96b8..71d0840ba 100644 --- a/test/file_header_test.go +++ b/test/file_header_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestLintFileHeaderDefault(t *testing.T) { + testRule(t, "lint_file_header_default", &rule.FileHeaderRule{}, &lint.RuleConfig{}) +} + func TestLintFileHeader(t *testing.T) { testRule(t, "lint_file_header1", &rule.FileHeaderRule{}, &lint.RuleConfig{ Arguments: []any{"foobar"}, diff --git a/test/function_length_test.go b/test/function_length_test.go index fb531a501..be7bea148 100644 --- a/test/function_length_test.go +++ b/test/function_length_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestFuncLengthDefault(t *testing.T) { + testRule(t, "function_length_default", &rule.FunctionLength{}, &lint.RuleConfig{}) +} + func TestFuncLengthLimitsStatements(t *testing.T) { testRule(t, "function_length1", &rule.FunctionLength{}, &lint.RuleConfig{ Arguments: []any{int64(2), int64(100)}, diff --git a/test/line_length_limit_test.go b/test/line_length_limit_test.go index c29e19bcb..4aad357ce 100644 --- a/test/line_length_limit_test.go +++ b/test/line_length_limit_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestLineLengthLimitDefault(t *testing.T) { + testRule(t, "line_length_limit_default", &rule.LineLengthLimitRule{}, &lint.RuleConfig{}) +} + func TestLineLengthLimit(t *testing.T) { testRule(t, "line_length_limit", &rule.LineLengthLimitRule{}, &lint.RuleConfig{ Arguments: []any{int64(100)}, diff --git a/test/max_control_nesting_test.go b/test/max_control_nesting_test.go index d6906ba5a..38250e2dd 100644 --- a/test/max_control_nesting_test.go +++ b/test/max_control_nesting_test.go @@ -7,6 +7,10 @@ import ( "github.com/mgechev/revive/rule" ) +func TestMaxControlNestingDefault(t *testing.T) { + testRule(t, "max_control_nesting_default", &rule.MaxControlNestingRule{}, &lint.RuleConfig{}) +} + func TestMaxControlNesting(t *testing.T) { testRule(t, "max_control_nesting", &rule.MaxControlNestingRule{}, &lint.RuleConfig{ Arguments: []any{int64(2)}}, diff --git a/testdata/argument_limit_default.go b/testdata/argument_limit_default.go new file mode 100644 index 000000000..264f40a0d --- /dev/null +++ b/testdata/argument_limit_default.go @@ -0,0 +1,7 @@ +package fixtures + +func foo(a, b, c, d, e, f, g, h int) { +} + +func bar(a, b, c, d, e, f, g, h, i int64) { // MATCH /maximum number of arguments per function exceeded; max 8 but got 9/ +} diff --git a/testdata/banned_characters_default.go b/testdata/banned_characters_default.go new file mode 100644 index 000000000..2c3b6ea9c --- /dev/null +++ b/testdata/banned_characters_default.go @@ -0,0 +1,3 @@ +package fixtures + +const Ω = "Omega" diff --git a/testdata/cognitive_complexity_default.go b/testdata/cognitive_complexity_default.go new file mode 100644 index 000000000..0b643f783 --- /dev/null +++ b/testdata/cognitive_complexity_default.go @@ -0,0 +1,13 @@ +package pkg + +func l() { // MATCH /function l has cognitive complexity 8 (> max enabled 7)/ + for i := 1; i <= max; i++ { + for j := 2; j < i; j++ { + if (i%j == 0) || (i%j == 1) { + continue + } + total += i + } + } + return total && max +} diff --git a/testdata/context_as_argument_default.go b/testdata/context_as_argument_default.go new file mode 100644 index 000000000..6ca81e587 --- /dev/null +++ b/testdata/context_as_argument_default.go @@ -0,0 +1,9 @@ +package foo + +import ( + "context" + "testing" +) + +func x(_ AllowedBeforePtrStruct, ctx context.Context) { // MATCH /context.Context should be the first parameter of a function/ +} diff --git a/testdata/cyclomatic_default.go b/testdata/cyclomatic_default.go new file mode 100644 index 000000000..b60751e4b --- /dev/null +++ b/testdata/cyclomatic_default.go @@ -0,0 +1,19 @@ +package pkg + +import "log" + +func f(x int) bool { // MATCH /function f has cyclomatic complexity 11 (> max enabled 10)/ + if x > 0 && true || false { + return true + } else { + log.Printf("non-positive x: %d", x) + } + switch x { + case 1: + case 2: + case 3: + case 4: + default: + } + return true || true && true +} diff --git a/testdata/import_dot.go b/testdata/dot_imports.go similarity index 100% rename from testdata/import_dot.go rename to testdata/dot_imports.go diff --git a/testdata/dot_imports_default.go b/testdata/dot_imports_default.go new file mode 100644 index 000000000..4a33659fa --- /dev/null +++ b/testdata/dot_imports_default.go @@ -0,0 +1,5 @@ +package fixtures + +import ( + . "context" // MATCH /should not use dot imports/ +) diff --git a/testdata/enforce_repeated_arg_type_style_default.go b/testdata/enforce_repeated_arg_type_style_default.go new file mode 100644 index 000000000..b88ad72a0 --- /dev/null +++ b/testdata/enforce_repeated_arg_type_style_default.go @@ -0,0 +1,21 @@ +package fixtures + +func compliantFunc(a, b int, c string) {} + +func nonCompliantFunc1(a int, b int, c string) {} +func nonCompliantFunc2(a int, b, c int) {} + +type myStruct struct{} + +func (m myStruct) compliantMethod(a, b int, c string) {} + +func (m myStruct) nonCompliantMethod1(a int, b int, c string) {} +func (m myStruct) nonCompliantMethod2(a int, b, c int) {} + +func variadicFunction(a int, b ...int) {} + +func singleArgFunction(a int) {} + +func multiTypeArgs(a int, b string, c float64) {} + +func mixedCompliance(a, b int, c int, d string) {} diff --git a/testdata/function_length3.go b/testdata/function_length3.go index 876bdfa34..0ae23125a 100644 --- a/testdata/function_length3.go +++ b/testdata/function_length3.go @@ -21,4 +21,289 @@ func funLengthA() (a int) { println() println() println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() } diff --git a/testdata/function_length_default.go b/testdata/function_length_default.go new file mode 100644 index 000000000..2b5663eef --- /dev/null +++ b/testdata/function_length_default.go @@ -0,0 +1,134 @@ +package fixtures + +func funLengthA() (a int) { // MATCH /maximum number of statements per function exceeded; max 50 but got 51/ + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() + println() +} + +func funLengthB(file *ast.File, fset *token.FileSet, lineLimit, stmtLimit int) []Message { // MATCH /maximum number of lines per function exceeded; max 75 but got 76/ + if true { + a = b + if false { + c = d + for _, f := range list { + _, ok := f.(int64) + if !ok { + continue + } + } + } + } + if true { + a = b + if false { + c = d + for _, f := range list { + _, ok := f.(int64) + if !ok { + continue + } + } + switch a { + case 1: + println() + case 2: + println() + println() + default: + println() + + } + } + } + if true { + a = b + if false { + c = d + for _, f := range list { + _, ok := f.(int64) + if !ok { + continue + } + } + switch a { + case 1: + println() + case 2: + println() + println() + default: + println() + + } + } + } + if true { + a = b + if false { + c = d + for _, f := range list { + _, ok := f.(int64) + if !ok { + continue + } + } + switch a { + case 1: + println() + default: + println() + + } + } + } + return +} \ No newline at end of file diff --git a/testdata/line_length_limit_default.go b/testdata/line_length_limit_default.go new file mode 100644 index 000000000..e92a1f650 --- /dev/null +++ b/testdata/line_length_limit_default.go @@ -0,0 +1,9 @@ +package fixtures + +import "fmt" + +func foo(a, b int) { + fmt.Printf("loooooooooooooooooooooooooooooooooooooooooong line out of limit") +} + +// MATCH:6 /line is 81 characters, out of limit 80/ diff --git a/testdata/lint_file_header_default.go b/testdata/lint_file_header_default.go new file mode 100644 index 000000000..331393d4e --- /dev/null +++ b/testdata/lint_file_header_default.go @@ -0,0 +1 @@ +package fixtures diff --git a/testdata/max_control_nesting_default.go b/testdata/max_control_nesting_default.go new file mode 100644 index 000000000..357adf62a --- /dev/null +++ b/testdata/max_control_nesting_default.go @@ -0,0 +1,16 @@ +package fixtures + +func mcn() { + if true { + if true { + if true { + if true { + if true { + if true { // MATCH /control flow nesting exceeds 5/ + } + } + } + } + } + } +}