-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjotbot_test.go
123 lines (104 loc) · 3.32 KB
/
jotbot_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package jotbot_test
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/modernice/jotbot"
"github.com/modernice/jotbot/generate"
"github.com/modernice/jotbot/generate/mockgenerate"
"github.com/modernice/jotbot/internal/tests"
"github.com/modernice/jotbot/langs/golang"
)
func TestJotBot_Find(t *testing.T) {
root := filepath.Join(tests.Must(os.Getwd()), "testdata", "gen", "find")
tests.InitRepo("basic", root)
bot := newJotBot(root)
findings, err := bot.Find(context.Background())
if err != nil {
t.Fatalf("Find() failed: %v", err)
}
tests.ExpectFound(t, []jotbot.Finding{
{File: "foo.go", Identifier: "func:Foo", Language: "go"},
{File: "bar.go", Identifier: "var:Foo", Language: "go"},
{File: "bar.go", Identifier: "type:Bar", Language: "go"},
{File: "baz.go", Identifier: "type:X", Language: "go"},
{File: "baz.go", Identifier: "func:X.Foo", Language: "go"},
{File: "baz.go", Identifier: "func:(*X).Bar", Language: "go"},
{File: "baz.go", Identifier: "func:Y.Foo", Language: "go"},
}, findings)
}
func TestJotBot_Generate(t *testing.T) {
svc := mockgenerate.NewMockService()
svc.GenerateDocFunc.SetDefaultHook(func(ctx generate.Context) (string, error) {
switch ctx.Input().Identifier {
case "func:Foo":
return "Foo is a foo.", nil
case "var:Foo":
return "Foo is a foo.", nil
case "type:Bar":
return "Bar is a bar.", nil
default:
return "", fmt.Errorf("unknown identifier %q", ctx.Input().Identifier)
}
})
root := filepath.Join(tests.Must(os.Getwd()), "testdata", "gen", "generate")
tests.WithRepo("basic", root, func(repo fs.FS) {
bot := newJotBot(root)
findings := append(
makeFindings(
"foo.go",
"func:Foo",
),
makeFindings(
"bar.go",
"var:Foo",
"type:Bar",
)...,
)
patch, err := bot.Generate(context.Background(), findings, svc)
if err != nil {
t.Fatalf("Generate() failed: %v", err)
}
if err := patch.Apply(context.Background(), root); err != nil {
t.Fatalf("patch.Apply() failed: %v", err)
}
tests.ExpectCommentIn(t, repo, "foo.go", "func:Foo", "Foo is a foo.")
tests.ExpectCommentIn(t, repo, "bar.go", "var:Foo", "Foo is a foo.")
tests.ExpectCommentIn(t, repo, "bar.go", "type:Bar", "Bar is a bar.")
})
}
func TestMatch(t *testing.T) {
root := filepath.Join(tests.Must(os.Getwd()), "testdata", "gen", "filter")
tests.InitRepo("basic", root)
filters := []*regexp.Regexp{
regexp.MustCompile(`^(var:|type:)`),
regexp.MustCompile(`^func:\(\*X\)\.Bar$`),
}
bot := newJotBot(root, jotbot.Match(filters...))
findings, err := bot.Find(context.Background())
if err != nil {
t.Fatalf("Find() failed: %v", err)
}
tests.ExpectFound(t, []jotbot.Finding{
{Identifier: "var:Foo", Language: "go", File: "bar.go"},
{Identifier: "type:Bar", Language: "go", File: "bar.go"},
{Identifier: "type:X", Language: "go", File: "baz.go"},
{Identifier: "func:(*X).Bar", Language: "go", File: "baz.go"},
}, findings)
}
func makeFindings(file string, findings ...string) []jotbot.Finding {
out := make([]jotbot.Finding, len(findings))
for i, id := range findings {
out[i] = jotbot.Finding{File: file, Identifier: id, Language: "go"}
}
return out
}
func newJotBot(root string, opts ...jotbot.Option) *jotbot.JotBot {
bot := jotbot.New(root, opts...)
bot.ConfigureLanguage("go", golang.Must())
return bot
}