From 655402f5278805358a54db207b161a53e9660337 Mon Sep 17 00:00:00 2001 From: mgechev Date: Sun, 4 Feb 2018 15:10:35 -0800 Subject: [PATCH] Add tests for the arguments limit --- fixtures/argument-limit.go | 17 +++++++++++++++++ rule/argument-limit.go | 7 ++++++- test/argument-limit_test.go | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 fixtures/argument-limit.go create mode 100644 test/argument-limit_test.go diff --git a/fixtures/argument-limit.go b/fixtures/argument-limit.go new file mode 100644 index 0000000..2c339b5 --- /dev/null +++ b/fixtures/argument-limit.go @@ -0,0 +1,17 @@ +package fixtures + +func foo(a, b, c, d int) { // MATCH /maximum number of arguments per function exceeded; max 3 but got 4/ + +} + +func bar(a, b int) { + +} + +func baz(a string, b int) { + +} + +func qux(a string, b int, c int, d string, e int64) { // MATCH /maximum number of arguments per function exceeded; max 3 but got 5/ + +} diff --git a/rule/argument-limit.go b/rule/argument-limit.go index 01b1383..2b11d49 100644 --- a/rule/argument-limit.go +++ b/rule/argument-limit.go @@ -48,7 +48,12 @@ type lintArgsNum struct { func (w lintArgsNum) Visit(n ast.Node) ast.Visitor { node, ok := n.(*ast.FuncDecl) if ok { - num := len(node.Type.Params.List) + num := 0 + for _, l := range node.Type.Params.List { + for range l.Names { + num++ + } + } if num > w.total { w.onFailure(lint.Failure{ Confidence: 1, diff --git a/test/argument-limit_test.go b/test/argument-limit_test.go new file mode 100644 index 0000000..e3aef82 --- /dev/null +++ b/test/argument-limit_test.go @@ -0,0 +1,14 @@ +package test + +import ( + "testing" + + "github.com/mgechev/revive/lint" + "github.com/mgechev/revive/rule" +) + +func TestArgumentLimit(t *testing.T) { + testRule(t, "argument-limit", &rule.ArgumentsLimitRule{}, &lint.RuleConfig{ + Arguments: []interface{}{int64(3)}, + }) +}