aboutsummaryrefslogtreecommitdiff
path: root/main_test.go
blob: bc0d0eecec29e5255ea745893abf92ff93e8f9f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main

import (
	"github.com/Fuwn/kivia/internal/analyze"
	"github.com/Fuwn/kivia/internal/collect"
	"testing"
)

func TestParseOptionsDefaults(testingContext *testing.T) {
	options, err := parseOptions([]string{"--path", "./testdata"})

	if err != nil {
		testingContext.Fatalf("parseOptions returned an error: %v", err)
	}

	if options.MinimumEvaluationLength != 1 {
		testingContext.Fatalf("Expected min-eval-length to default to 1, got %d.", options.MinimumEvaluationLength)
	}
}

func TestParseOptionsRejectsStrictFlag(testingContext *testing.T) {
	_, err := parseOptions([]string{"--path", "./testdata", "--strict"})

	if err == nil {
		testingContext.Fatalf("Expected parseOptions to fail when --strict is provided.")
	}
}

func TestParseOptionsReadsMultipleIgnoreFlags(testingContext *testing.T) {
	options, err := parseOptions([]string{
		"--path", "./testdata",
		"--ignore", "name=ctx",
		"--ignore", "file=_test.go",
		"--ignore", "reason=too short",
	})

	if err != nil {
		testingContext.Fatalf("parseOptions returned an error: %v", err)
	}

	if len(options.Ignore) != 3 {
		testingContext.Fatalf("Expected three ignore values, got %d.", len(options.Ignore))
	}
}

func TestParseOptionsRejectsInvalidMinimumEvaluationLength(testingContext *testing.T) {
	_, err := parseOptions([]string{"--path", "./testdata", "--min-eval-length", "0"})

	if err == nil {
		testingContext.Fatalf("Expected parseOptions to fail for min-eval-length=0.")
	}
}

func TestApplyIgnoreFilters(testingContext *testing.T) {
	input := analyzeResultFixture()
	filtered := applyIgnoreFilters(input, []string{
		"name=ctx",
		"reason=too short",
		"file=_test.go",
	})

	if len(filtered.Violations) != 1 {
		testingContext.Fatalf("Expected one remaining violation, got %d.", len(filtered.Violations))
	}

	if filtered.Violations[0].Identifier.Name != "userNum" {
		testingContext.Fatalf("Unexpected remaining violation: %q.", filtered.Violations[0].Identifier.Name)
	}
}

func analyzeResultFixture() analyze.Result {
	return analyze.Result{
		Violations: []analyze.Violation{
			{
				Identifier: collect.Identifier{
					Name: "ctx",
					Kind: "parameter",
					File: "sample.go",
					Context: collect.Context{
						EnclosingFunction: "Handle",
					},
				},
				Reason: "Contains abbreviation: ctx.",
			},
			{
				Identifier: collect.Identifier{
					Name: "t",
					Kind: "parameter",
					File: "main_test.go",
				},
				Reason: "Name is too short to be self-documenting.",
			},
			{
				Identifier: collect.Identifier{
					Name: "userNum",
					Kind: "parameter",
					File: "sample.go",
				},
				Reason: "Contains abbreviation: num.",
			},
		},
	}
}