mirror of
https://github.com/mgechev/revive.git
synced 2024-11-24 08:32:22 +02:00
documents test fixture
This commit is contained in:
parent
611c6f7c81
commit
e544b60e1f
@ -9,8 +9,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Test IF and Boolean expr
|
||||||
func f(x int) bool { // MATCH /function f has cognitive complexity 3 (> max enabled 0)/
|
func f(x int) bool { // MATCH /function f has cognitive complexity 3 (> max enabled 0)/
|
||||||
if x > 0 && true || false {
|
if x > 0 && true || false { // +3
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
log.Printf("non-positive x: %d", x)
|
log.Printf("non-positive x: %d", x)
|
||||||
@ -18,30 +19,33 @@ func f(x int) bool { // MATCH /function f has cognitive complexity 3 (> max enab
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test IF
|
||||||
func g(f func() bool) string { // MATCH /function g has cognitive complexity 1 (> max enabled 0)/
|
func g(f func() bool) string { // MATCH /function g has cognitive complexity 1 (> max enabled 0)/
|
||||||
if ok := f(); ok {
|
if ok := f(); ok { // +1
|
||||||
return "it's okay"
|
return "it's okay"
|
||||||
} else {
|
} else {
|
||||||
return "it's NOT okay!"
|
return "it's NOT okay!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test Boolean expr
|
||||||
func h(a, b, c, d, e, f bool) bool { // MATCH /function h has cognitive complexity 2 (> max enabled 0)/
|
func h(a, b, c, d, e, f bool) bool { // MATCH /function h has cognitive complexity 2 (> max enabled 0)/
|
||||||
return a && b && c || d || e && f //FIXME: complexity should be 3
|
return a && b && c || d || e && f //FIXME: complexity should be 3
|
||||||
}
|
}
|
||||||
|
|
||||||
func i(a, b, c, d, e, f bool) bool { // MATCH /function i has cognitive complexity 2 (> max enabled 0)/
|
func i(a, b, c, d, e, f bool) bool { // MATCH /function i has cognitive complexity 2 (> max enabled 0)/
|
||||||
result := a && b && c || d || e
|
result := a && b && c || d || e // +2
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func j(a, b, c, d, e, f bool) bool { // MATCH /function j has cognitive complexity 2 (> max enabled 0)/
|
func j(a, b, c, d, e, f bool) bool { // MATCH /function j has cognitive complexity 2 (> max enabled 0)/
|
||||||
result := z(a && !(b && c))
|
result := z(a && !(b && c)) // +2
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test Switch expr
|
||||||
func k(a, b, c, d, e, f bool) bool { // MATCH /function k has cognitive complexity 1 (> max enabled 0)/
|
func k(a, b, c, d, e, f bool) bool { // MATCH /function k has cognitive complexity 1 (> max enabled 0)/
|
||||||
switch expr {
|
switch expr { // +1
|
||||||
case cond1:
|
case cond1:
|
||||||
case cond2:
|
case cond2:
|
||||||
default:
|
default:
|
||||||
@ -50,10 +54,11 @@ func k(a, b, c, d, e, f bool) bool { // MATCH /function k has cognitive complexi
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test nesting FOR expr + nested IF
|
||||||
func l() { // MATCH /function l has cognitive complexity 6 (> max enabled 0)/
|
func l() { // MATCH /function l has cognitive complexity 6 (> max enabled 0)/
|
||||||
for i := 1; i <= max; i++ {
|
for i := 1; i <= max; i++ { // +1
|
||||||
for j := 2; j < i; j++ {
|
for j := 2; j < i; j++ { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,10 +68,11 @@ func l() { // MATCH /function l has cognitive complexity 6 (> max enabled 0)/
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test nesting IF
|
||||||
func m() { // MATCH /function m has cognitive complexity 6 (> max enabled 0)/
|
func m() { // MATCH /function m has cognitive complexity 6 (> max enabled 0)/
|
||||||
if i <= max {
|
if i <= max { // +1
|
||||||
if j < i {
|
if j < i { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,10 +82,11 @@ func m() { // MATCH /function m has cognitive complexity 6 (> max enabled 0)/
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test nesting IF + nested FOR
|
||||||
func n() { // MATCH /function n has cognitive complexity 6 (> max enabled 0)/
|
func n() { // MATCH /function n has cognitive complexity 6 (> max enabled 0)/
|
||||||
if i > max {
|
if i > max { // +1
|
||||||
for j := 2; j < i; j++ {
|
for j := 2; j < i; j++ { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,10 +96,11 @@ func n() { // MATCH /function n has cognitive complexity 6 (> max enabled 0)/
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test nesting
|
||||||
func o() { // MATCH /function o has cognitive complexity 12 (> max enabled 0)/
|
func o() { // MATCH /function o has cognitive complexity 12 (> max enabled 0)/
|
||||||
if i > max {
|
if i > max { // +1
|
||||||
if j < i {
|
if j < i { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,9 +108,9 @@ func o() { // MATCH /function o has cognitive complexity 12 (> max enabled 0)/
|
|||||||
total += i
|
total += i
|
||||||
}
|
}
|
||||||
|
|
||||||
if i > max {
|
if i > max { // +1
|
||||||
if j < i {
|
if j < i { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,8 +119,9 @@ func o() { // MATCH /function o has cognitive complexity 12 (> max enabled 0)/
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests TYPE SWITCH
|
||||||
func p() { // MATCH /function p has cognitive complexity 1 (> max enabled 0)/
|
func p() { // MATCH /function p has cognitive complexity 1 (> max enabled 0)/
|
||||||
switch n := n.(type) {
|
switch n := n.(type) { // +1
|
||||||
case *ast.IfStmt:
|
case *ast.IfStmt:
|
||||||
targets := []ast.Node{n.Cond, n.Body, n.Else}
|
targets := []ast.Node{n.Cond, n.Body, n.Else}
|
||||||
v.walk(targets...)
|
v.walk(targets...)
|
||||||
@ -125,18 +134,20 @@ func p() { // MATCH /function p has cognitive complexity 1 (> max enabled 0)/
|
|||||||
return nil
|
return nil
|
||||||
case *ast.BinaryExpr:
|
case *ast.BinaryExpr:
|
||||||
v.complexity += v.binExpComplexity(n)
|
v.complexity += v.binExpComplexity(n)
|
||||||
return nil // skip visiting binexp sub-tree (already visited by binExpComplexity)
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test RANGE
|
||||||
func q() { // MATCH /function q has cognitive complexity 1 (> max enabled 0)/
|
func q() { // MATCH /function q has cognitive complexity 1 (> max enabled 0)/
|
||||||
for _, t := range targets {
|
for _, t := range targets { // +1
|
||||||
ast.Walk(v, t)
|
ast.Walk(v, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests SELECT
|
||||||
func r() { // MATCH /function r has cognitive complexity 1 (> max enabled 0)/
|
func r() { // MATCH /function r has cognitive complexity 1 (> max enabled 0)/
|
||||||
select {
|
select { // +1
|
||||||
case c <- x:
|
case c <- x:
|
||||||
x, y = y, x+y
|
x, y = y, x+y
|
||||||
case <-quit:
|
case <-quit:
|
||||||
@ -145,56 +156,59 @@ func r() { // MATCH /function r has cognitive complexity 1 (> max enabled 0)/
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test jump to label
|
||||||
func s() { // MATCH /function s has cognitive complexity 3 (> max enabled 0)/
|
func s() { // MATCH /function s has cognitive complexity 3 (> max enabled 0)/
|
||||||
FirstLoop:
|
FirstLoop:
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ { // +1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ { // +1
|
||||||
break FirstLoop
|
break FirstLoop // +1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func t() { // MATCH /function t has cognitive complexity 2 (> max enabled 0)/
|
func t() { // MATCH /function t has cognitive complexity 2 (> max enabled 0)/
|
||||||
FirstLoop:
|
FirstLoop:
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ { // +1
|
||||||
goto FirstLoop
|
goto FirstLoop // +1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func u() { // MATCH /function u has cognitive complexity 3 (> max enabled 0)/
|
func u() { // MATCH /function u has cognitive complexity 3 (> max enabled 0)/
|
||||||
FirstLoop:
|
FirstLoop:
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ { // +1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ { // +1
|
||||||
continue FirstLoop
|
continue FirstLoop // +1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests FUNC LITERAL
|
||||||
func v() { // MATCH /function v has cognitive complexity 2 (> max enabled 0)/
|
func v() { // MATCH /function v has cognitive complexity 2 (> max enabled 0)/
|
||||||
myFunc := func(b bool) {
|
myFunc := func(b bool) {
|
||||||
if b {
|
if b { // +1 +1(nesting)
|
||||||
// do something
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func w() { // MATCH /function w has cognitive complexity 3 (> max enabled 0)/
|
func w() { // MATCH /function w has cognitive complexity 3 (> max enabled 0)/
|
||||||
defer func(b bool) {
|
defer func(b bool) {
|
||||||
if b {
|
if b { // +1 +1(nesting)
|
||||||
// do something
|
|
||||||
}
|
}
|
||||||
}(false || true)
|
}(false || true) // +1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test from Cognitive Complexity white paper
|
||||||
func sumOfPrimes(max int) int { // MATCH /function sumOfPrimes has cognitive complexity 7 (> max enabled 0)/
|
func sumOfPrimes(max int) int { // MATCH /function sumOfPrimes has cognitive complexity 7 (> max enabled 0)/
|
||||||
total := 0
|
total := 0
|
||||||
OUT:
|
OUT:
|
||||||
for i := 1; i <= max; i++ {
|
for i := 1; i <= max; i++ { // +1
|
||||||
for j := 2; j < i; j++ {
|
for j := 2; j < i; j++ { // +1 +1(nesting)
|
||||||
if i%j == 0 {
|
if i%j == 0 { // +1 +2(nesting)
|
||||||
continue OUT
|
continue OUT // +1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user