Skip to content

Commit 2569917

Browse files
committed
Tests for checker package preslavmihaylov#173
Changed checker to accept interface of type Fetcher for easier mocking. Added tests for all the possible errors that could happen in Check
1 parent c6fe4aa commit 2569917

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

checker/checker.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import (
55
"fmt"
66

77
checkererrors "github.com/preslavmihaylov/todocheck/checker/errors"
8-
"github.com/preslavmihaylov/todocheck/fetcher"
98
"github.com/preslavmihaylov/todocheck/issuetracker/taskstatus"
109
"github.com/preslavmihaylov/todocheck/matchers"
1110
)
1211

12+
type Fetcher interface {
13+
Fetch(taskID string) (taskstatus.TaskStatus, error)
14+
}
15+
1316
// Checker for todo lines
1417
type Checker struct {
15-
statusFetcher *fetcher.Fetcher
18+
statusFetcher Fetcher
1619
}
1720

1821
// New checker
19-
func New(statusFetcher *fetcher.Fetcher) *Checker {
22+
func New(statusFetcher Fetcher) *Checker {
2023
return &Checker{statusFetcher}
2124
}
2225

checker/checker_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package checker
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
8+
checkerrors "github.com/preslavmihaylov/todocheck/checker/errors"
9+
"github.com/preslavmihaylov/todocheck/issuetracker/taskstatus"
10+
)
11+
12+
func TestCheck(t *testing.T) {
13+
fetcher := mockFetcher{}
14+
checker := New(&fetcher)
15+
matcher := mockMatcher{}
16+
17+
testLines := []string{}
18+
testLineCnt := 0
19+
20+
testData := []struct {
21+
comment, filename string
22+
todoErr *checkerrors.TODO
23+
err error
24+
}{
25+
{"NotMatch", "", nil, nil},
26+
{"NotValid", "test.go", checkerrors.MalformedTODOErr("test.go", testLines, testLineCnt), nil},
27+
{"FailedFetch", "", nil, errors.New("")},
28+
{"ClosedIssue", "test.go", checkerrors.IssueClosedErr("test.go", testLines, testLineCnt, "ClosedIssue"), nil},
29+
{"NonExistentIssue", "test.go", checkerrors.IssueNonExistentErr("test.go", testLines, testLineCnt, "NonExistentIssue"), nil},
30+
{"Valid", "", nil, nil},
31+
}
32+
for _, tt := range testData {
33+
t.Run(tt.comment, func(t *testing.T) {
34+
35+
todoErr, err := checker.Check(matcher, tt.comment, tt.filename, testLines, testLineCnt)
36+
if !reflect.DeepEqual(todoErr, tt.todoErr) {
37+
t.Errorf("Expected toddErr to be %v, got %v", tt.todoErr, todoErr)
38+
}
39+
if (err == nil) != (tt.err == nil) { // Don't care about the error string
40+
t.Errorf("Expected err to be %v, got %v", tt.err, err)
41+
}
42+
})
43+
}
44+
45+
t.Run("NilMatcher", func(t *testing.T) {
46+
todoErr, err := checker.Check(nil, "", "", testLines, testLineCnt)
47+
if todoErr != nil {
48+
t.Errorf("Expected toddErr to be nil, got %v", todoErr)
49+
}
50+
if err == nil { // Don't care about the error string
51+
t.Errorf("Expected err to be not nil")
52+
}
53+
})
54+
t.Run("InvalidExtract", func(t *testing.T) {
55+
defer func() {
56+
if r := recover(); r == nil {
57+
t.Logf("Recovered in %v", r)
58+
}
59+
}()
60+
checker.Check(matcher, "InvalidExtract", "", testLines, testLineCnt)
61+
t.Errorf("Expected code to panic")
62+
})
63+
}
64+
65+
type mockMatcher struct {
66+
}
67+
68+
func (m mockMatcher) IsMatch(expr string) bool {
69+
if expr == "NotMatch" {
70+
return false
71+
}
72+
return true
73+
}
74+
func (m mockMatcher) IsValid(expr string) bool {
75+
if expr == "NotValid" {
76+
return false
77+
}
78+
return true
79+
}
80+
func (m mockMatcher) ExtractIssueRef(expr string) (string, error) {
81+
if expr == "InvalidExtract" {
82+
return expr, errors.New("Invalid todo")
83+
}
84+
return expr, nil
85+
}
86+
87+
type mockFetcher struct {
88+
}
89+
90+
func (f *mockFetcher) Fetch(taskID string) (taskstatus.TaskStatus, error) {
91+
if taskID == "FailedFetch" {
92+
return 0, errors.New("FailedFetch")
93+
}
94+
if taskID == "ClosedIssue" {
95+
return 2, nil
96+
}
97+
if taskID == "NonExistentIssue" {
98+
return 3, nil
99+
}
100+
return 0, nil
101+
}

0 commit comments

Comments
 (0)