-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcommon_test.go
82 lines (67 loc) · 1.43 KB
/
common_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
package generator
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"reflect"
parseutil "gopkg.in/src-d/go-parse-utils.v1"
)
func mkField(name, typ, tag string, fields ...*Field) *Field {
f := NewField(name, typ, reflect.StructTag(tag))
f.SetFields(fields)
return f
}
func withKind(f *Field, kind FieldKind) *Field {
f.Kind = kind
return f
}
func withPtr(f *Field) *Field {
f.IsPtr = true
return f
}
func withAlias(f *Field) *Field {
f.IsAlias = true
return f
}
func withJSON(f *Field) *Field {
f.IsJSON = true
return f
}
func withParent(f *Field, parent *Field) *Field {
f.Parent = parent
return f
}
func withNode(f *Field, name string, typ types.Type) *Field {
f.Node = types.NewVar(token.NoPos, nil, name, typ)
return f
}
func inline(f *Field) *Field {
f.Tag = reflect.StructTag(`kallax:",inline"`)
return f
}
func processorFixture(source string) (*Processor, error) {
fset := &token.FileSet{}
astFile, err := parser.ParseFile(fset, "fixture.go", source, 0)
if err != nil {
return nil, err
}
cfg := &types.Config{
Importer: parseutil.NewImporter(),
}
p, err := cfg.Check("foo", fset, []*ast.File{astFile}, nil)
if err != nil {
return nil, err
}
prc := NewProcessor("fixture", []string{"foo.go"})
prc.Package = p
return prc, nil
}
func processFixture(source string) (*Package, error) {
prc, err := processorFixture(source)
if err != nil {
return nil, err
}
prc.Silent()
return prc.processPackage()
}