|
| 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