github.com/mailru/activerecord@v1.12.2/internal/pkg/parser/mutator_b_test.go (about) 1 package parser_test 2 3 import ( 4 "fmt" 5 "go/ast" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 "gotest.tools/assert" 10 "gotest.tools/assert/cmp" 11 12 "github.com/mailru/activerecord/internal/pkg/ds" 13 "github.com/mailru/activerecord/internal/pkg/parser" 14 ) 15 16 func NewRecordPackage(t *testing.T) (*ds.RecordPackage, error) { 17 dst := ds.NewRecordPackage() 18 dst.Namespace.ModuleName = "github.com/mailru/activerecord/internal/pkg/parser" 19 20 if _, err := dst.AddImport("github.com/mailru/activerecord/internal/pkg/parser/testdata/foo"); err != nil { 21 return nil, fmt.Errorf("can't create test package: %w", err) 22 } 23 24 return dst, nil 25 } 26 27 func TestParseMutator(t *testing.T) { 28 type args struct { 29 fields []*ast.Field 30 } 31 tests := []struct { 32 name string 33 args args 34 want *ds.RecordPackage 35 wantErr bool 36 }{ 37 { 38 name: "parse mutator decl", 39 args: args{ 40 fields: []*ast.Field{ 41 { 42 Names: []*ast.Ident{{Name: "FooMutatorField"}}, 43 Tag: &ast.BasicLit{ 44 Value: "`ar:\"update:updateFunc,param1,param2;replace:replaceFunc;pkg:github.com/mailru/activerecord/internal/pkg/conv\"`", 45 }, 46 Type: &ast.StarExpr{ 47 X: &ast.SelectorExpr{ 48 X: &ast.Ident{Name: "foo"}, 49 Sel: &ast.Ident{Name: "Foo"}, 50 }, 51 }, 52 }, 53 { 54 Names: []*ast.Ident{{Name: "SimpleTypeMutatorField"}}, 55 Tag: &ast.BasicLit{ 56 Value: "`ar:\"update:updateSimpleTypeFunc\"`", 57 }, 58 Type: &ast.Ident{Name: "int"}, 59 }, 60 }, 61 }, 62 want: &ds.RecordPackage{ 63 Server: ds.ServerDeclaration{}, 64 Namespace: ds.NamespaceDeclaration{ 65 ModuleName: "github.com/mailru/activerecord/internal/pkg/parser", 66 }, 67 Fields: []ds.FieldDeclaration{}, 68 FieldsMap: map[string]int{}, 69 FieldsObjectMap: map[string]ds.FieldObject{}, 70 Indexes: []ds.IndexDeclaration{}, 71 IndexMap: map[string]int{}, 72 SelectorMap: map[string]int{}, 73 Backends: []string{}, 74 SerializerMap: map[string]ds.SerializerDeclaration{}, 75 MutatorMap: map[string]ds.MutatorDeclaration{ 76 "FooMutatorField": { 77 Name: "FooMutatorField", 78 Pkg: "github.com/mailru/activerecord/internal/pkg/conv", 79 Type: "*foo.Foo", 80 ImportName: "mutatorFooMutatorField", 81 Update: "updateFunc,param1,param2", 82 Replace: "replaceFunc", 83 PartialFields: []ds.PartialFieldDeclaration{ 84 {Name: "Key", Type: "string"}, 85 {Name: "Bar", Type: "ds.AppInfo"}, 86 {Name: "BeerData", Type: "[]foo.Beer"}, 87 {Name: "MapData", Type: "map[string]any"}, 88 }, 89 }, 90 "SimpleTypeMutatorField": { 91 Name: "SimpleTypeMutatorField", 92 Type: "int", 93 ImportName: "mutatorSimpleTypeMutatorField", 94 Update: "updateSimpleTypeFunc", 95 }, 96 }, 97 ImportPackage: ds.ImportPackage{ 98 Imports: []ds.ImportDeclaration{ 99 {Path: "github.com/mailru/activerecord/internal/pkg/parser/testdata/foo"}, 100 {Path: "github.com/mailru/activerecord/internal/pkg/conv", ImportName: "mutatorFooMutatorField"}, 101 {Path: "github.com/mailru/activerecord/internal/pkg/parser/testdata/ds"}, 102 }, 103 ImportMap: map[string]int{ 104 "github.com/mailru/activerecord/internal/pkg/conv": 1, 105 "github.com/mailru/activerecord/internal/pkg/parser/testdata/foo": 0, 106 "github.com/mailru/activerecord/internal/pkg/parser/testdata/ds": 2, 107 }, 108 ImportPkgMap: map[string]int{ 109 "mutatorFooMutatorField": 1, 110 "ds": 2, 111 "foo": 0, 112 }, 113 }, 114 TriggerMap: map[string]ds.TriggerDeclaration{}, 115 FlagMap: map[string]ds.FlagDeclaration{}, 116 ProcOutFields: map[int]ds.ProcFieldDeclaration{}, 117 ProcFieldsMap: map[string]int{}, 118 LinkedStructsMap: map[string]ds.LinkedPackageDeclaration{ 119 "ds": { 120 Types: map[string]struct{}{"AppInfo": {}}, 121 Import: struct { 122 Imports []ds.ImportDeclaration 123 ImportMap map[string]int 124 ImportPkgMap map[string]int 125 }{Imports: []ds.ImportDeclaration{}, ImportMap: map[string]int{}, ImportPkgMap: map[string]int{}}, 126 }, 127 "foo": { 128 Types: map[string]struct{}{"Beer": {}, "Foo": {}}, 129 Import: struct { 130 Imports []ds.ImportDeclaration 131 ImportMap map[string]int 132 ImportPkgMap map[string]int 133 }{ 134 Imports: []ds.ImportDeclaration{ 135 {Path: "github.com/mailru/activerecord/internal/pkg/parser/testdata/ds"}, 136 }, 137 ImportMap: map[string]int{ 138 "github.com/mailru/activerecord/internal/pkg/parser/testdata/ds": 0, 139 }, 140 ImportPkgMap: map[string]int{ 141 "ds": 0, 142 }, 143 }, 144 }, 145 }, 146 ImportStructFieldsMap: map[string][]ds.PartialFieldDeclaration{ 147 "ds.AppInfo": { 148 {Name: "appName", Type: "string"}, 149 {Name: "version", Type: "string"}, 150 {Name: "buildTime", Type: "string"}, 151 {Name: "buildOS", Type: "string"}, 152 {Name: "buildCommit", Type: "string"}, 153 {Name: "generateTime", Type: "string"}, 154 }, 155 "foo.Foo": { 156 {Name: "Key", Type: "string"}, 157 {Name: "Bar", Type: "ds.AppInfo"}, 158 {Name: "BeerData", Type: "[]foo.Beer"}, 159 {Name: "MapData", Type: "map[string]any"}, 160 }, 161 }, 162 }, 163 wantErr: false, 164 }, 165 { 166 name: "not imported package for mutator type", 167 args: args{ 168 fields: []*ast.Field{ 169 { 170 Names: []*ast.Ident{{Name: "Foo"}}, 171 Tag: &ast.BasicLit{Value: "`ar:\"pkg:github.com/mailru/activerecord/notexistsfolder\"`"}, 172 Type: &ast.StarExpr{ 173 X: &ast.SelectorExpr{ 174 X: &ast.Ident{Name: "notimportedpackage"}, 175 Sel: &ast.Ident{Name: "Bar"}, 176 }, 177 }, 178 }, 179 }, 180 }, 181 wantErr: true, 182 }, 183 } 184 for _, tt := range tests { 185 t.Run(tt.name, func(t *testing.T) { 186 dst, err := NewRecordPackage(t) 187 require.NoError(t, err) 188 189 if err := parser.ParseMutators(dst, tt.args.fields); (err != nil) != tt.wantErr { 190 t.Errorf("ParseMutators() error = %v, wantErr %v", err, tt.wantErr) 191 } 192 193 if !tt.wantErr { 194 assert.Check(t, cmp.DeepEqual(dst, tt.want), "Invalid response package, test `%s`", tt.name) 195 } 196 }) 197 } 198 }