馃彴 Self-documenting Identifier Name Analyser for Go
1package main
2
3import (
4 "github.com/Fuwn/kivia/internal/analyze"
5 "github.com/Fuwn/kivia/internal/collect"
6 "testing"
7)
8
9func TestParseOptionsDefaults(testingContext *testing.T) {
10 options, err := parseOptions([]string{"--path", "./testdata"})
11
12 if err != nil {
13 testingContext.Fatalf("parseOptions returned an error: %v", err)
14 }
15
16 if options.MinimumEvaluationLength != 1 {
17 testingContext.Fatalf("Expected min-eval-length to default to 1, got %d.", options.MinimumEvaluationLength)
18 }
19}
20
21func TestParseOptionsRejectsStrictFlag(testingContext *testing.T) {
22 _, err := parseOptions([]string{"--path", "./testdata", "--strict"})
23
24 if err == nil {
25 testingContext.Fatalf("Expected parseOptions to fail when --strict is provided.")
26 }
27}
28
29func TestParseOptionsReadsMultipleIgnoreFlags(testingContext *testing.T) {
30 options, err := parseOptions([]string{
31 "--path", "./testdata",
32 "--ignore", "name=ctx",
33 "--ignore", "file=_test.go",
34 "--ignore", "reason=too short",
35 })
36
37 if err != nil {
38 testingContext.Fatalf("parseOptions returned an error: %v", err)
39 }
40
41 if len(options.Ignore) != 3 {
42 testingContext.Fatalf("Expected three ignore values, got %d.", len(options.Ignore))
43 }
44}
45
46func TestParseOptionsRejectsInvalidMinimumEvaluationLength(testingContext *testing.T) {
47 _, err := parseOptions([]string{"--path", "./testdata", "--min-eval-length", "0"})
48
49 if err == nil {
50 testingContext.Fatalf("Expected parseOptions to fail for min-eval-length=0.")
51 }
52}
53
54func TestApplyIgnoreFilters(testingContext *testing.T) {
55 input := analyzeResultFixture()
56 filtered := applyIgnoreFilters(input, []string{
57 "name=ctx",
58 "reason=too short",
59 "file=_test.go",
60 })
61
62 if len(filtered.Violations) != 1 {
63 testingContext.Fatalf("Expected one remaining violation, got %d.", len(filtered.Violations))
64 }
65
66 if filtered.Violations[0].Identifier.Name != "userNum" {
67 testingContext.Fatalf("Unexpected remaining violation: %q.", filtered.Violations[0].Identifier.Name)
68 }
69}
70
71func analyzeResultFixture() analyze.Result {
72 return analyze.Result{
73 Violations: []analyze.Violation{
74 {
75 Identifier: collect.Identifier{
76 Name: "ctx",
77 Kind: "parameter",
78 File: "sample.go",
79 Context: collect.Context{
80 EnclosingFunction: "Handle",
81 },
82 },
83 Reason: "Contains abbreviation: ctx.",
84 },
85 {
86 Identifier: collect.Identifier{
87 Name: "t",
88 Kind: "parameter",
89 File: "main_test.go",
90 },
91 Reason: "Name is too short to be self-documenting.",
92 },
93 {
94 Identifier: collect.Identifier{
95 Name: "userNum",
96 Kind: "parameter",
97 File: "sample.go",
98 },
99 Reason: "Contains abbreviation: num.",
100 },
101 },
102 }
103}