From 8df00478fd63fd7c7b30008b15da039e0411f541 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 1 Jul 2022 15:28:59 +0300 Subject: [PATCH] Add unit test for methods with generic receivers --- gocognit118_test.go | 17 +++++++++++++++++ testdata/src/c/c.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 gocognit118_test.go create mode 100644 testdata/src/c/c.go diff --git a/gocognit118_test.go b/gocognit118_test.go new file mode 100644 index 0000000..9c20654 --- /dev/null +++ b/gocognit118_test.go @@ -0,0 +1,17 @@ +//go:build go1.18 +// +build go1.18 + +package gocognit_test + +import ( + "testing" + + "github.com/uudashr/gocognit" + "golang.org/x/tools/go/analysis/analysistest" +) + +func TestAnalyzerWithGenericMethods(t *testing.T) { + testdata := analysistest.TestData() + gocognit.Analyzer.Flags.Set("over", "0") + analysistest.Run(t, testdata, gocognit.Analyzer, "c") +} diff --git a/testdata/src/c/c.go b/testdata/src/c/c.go new file mode 100644 index 0000000..5a75c99 --- /dev/null +++ b/testdata/src/c/c.go @@ -0,0 +1,36 @@ +package testdata + +type Node[T any] struct { +} + +func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\[T\\]\\)\\.String is high \\(> 0\\)" + if n != nil { // +1 + return "Node" + } + + return "" +} // total complexity = 1 + +type Pair[K any, V any] struct { + Key K + Value V +} + +func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\[K, V\\]\\)\\.String is high \\(> 0\\)" + if p != nil { // +1 + return "Pair" + } + + return "" +} // total complexity = 1 + +type Triple[K any, V any, T any] struct { +} + +func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\[K, V, T\\]\\)\\.String is high \\(> 0\\)" + if t != nil { // +1 ` + return "Triple" + } + + return "" +} // total complexity = 1