-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathreflect_comments_test.go
61 lines (55 loc) · 1.75 KB
/
reflect_comments_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
package jsonschema
import (
"fmt"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/invopop/jsonschema/examples"
)
func TestCommentsSchemaGeneration(t *testing.T) {
tests := []struct {
typ any
reflector *Reflector
fixture string
}{
{&examples.User{}, prepareCommentReflector(t), "fixtures/go_comments.json"},
{&examples.User{}, prepareCommentReflector(t, WithFullComment()), "fixtures/go_comments_full.json"},
{&examples.User{}, prepareCustomCommentReflector(t), "fixtures/custom_comments.json"},
}
for _, tt := range tests {
name := strings.TrimSuffix(filepath.Base(tt.fixture), ".json")
t.Run(name, func(t *testing.T) {
compareSchemaOutput(t,
tt.fixture, tt.reflector, tt.typ,
)
})
}
}
func prepareCommentReflector(t *testing.T, opts ...CommentOption) *Reflector {
t.Helper()
r := new(Reflector)
err := r.AddGoComments("github.com/invopop/jsonschema", "./examples", opts...)
require.NoError(t, err, "did not expect error while adding comments")
return r
}
func prepareCustomCommentReflector(t *testing.T) *Reflector {
t.Helper()
r := new(Reflector)
r.LookupComment = func(t reflect.Type, f string) string {
if t != reflect.TypeOf(examples.User{}) {
// To test the interaction between a custom LookupComment function and the
// AddGoComments function, we only override comments for the User type.
return ""
}
if f == "" {
return fmt.Sprintf("Go type %s, defined in package %s.", t.Name(), t.PkgPath())
}
return fmt.Sprintf("Field %s of Go type %s.%s.", f, t.PkgPath(), t.Name())
}
// Also add the Go comments.
err := r.AddGoComments("github.com/invopop/jsonschema", "./examples")
require.NoError(t, err, "did not expect error while adding comments")
return r
}