github.com/mailru/activerecord@v1.12.2/internal/pkg/ds/package_b_test.go (about) 1 package ds_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/mailru/activerecord/internal/pkg/ds" 8 ) 9 10 func TestRecordPackage_FindImport(t *testing.T) { 11 rc := ds.NewRecordPackage() 12 13 imp, err := rc.AddImport("go/ast") 14 if err != nil { 15 t.Errorf("add import: %s", err) 16 return 17 } 18 19 type args struct { 20 path string 21 } 22 tests := []struct { 23 name string 24 rc *ds.RecordPackage 25 args args 26 want ds.ImportDeclaration 27 wantErr bool 28 }{ 29 { 30 name: "getByPkg", 31 rc: rc, 32 args: args{path: "ast"}, 33 want: ds.ImportDeclaration{}, 34 wantErr: true, 35 }, 36 { 37 name: "getByName", 38 rc: rc, 39 args: args{path: "go/ast"}, 40 want: imp, 41 wantErr: false, 42 }, 43 { 44 name: "Unknown", 45 rc: rc, 46 args: args{path: "bla"}, 47 want: ds.ImportDeclaration{}, 48 wantErr: true, 49 }, 50 } 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 got, err := tt.rc.FindImport(tt.args.path) 54 if (err != nil) != tt.wantErr { 55 t.Errorf("RecordPackage.FindImport() error = %v, wantErr %v", err, tt.wantErr) 56 return 57 } 58 if !reflect.DeepEqual(got, tt.want) { 59 t.Errorf("RecordPackage.FindImport() = %v, want %v", got, tt.want) 60 } 61 }) 62 } 63 } 64 65 func TestRecordPackage_AddImport(t *testing.T) { 66 rc := ds.NewRecordPackage() 67 68 type want struct { 69 importDeclaration ds.ImportDeclaration 70 RcImportPkgMap map[string]int 71 RcImportMap map[string]int 72 } 73 74 type args struct { 75 path string 76 importname []string 77 } 78 tests := []struct { 79 name string 80 rc *ds.RecordPackage 81 args args 82 want want 83 wantErr bool 84 }{ 85 { 86 name: "simple add", 87 rc: rc, 88 args: args{ 89 path: "go/ast", 90 }, 91 want: want{ 92 importDeclaration: ds.ImportDeclaration{ 93 Path: "go/ast", 94 ImportName: "", 95 }, 96 RcImportPkgMap: map[string]int{"ast": 0}, 97 RcImportMap: map[string]int{"go/ast": 0}, 98 }, 99 wantErr: false, 100 }, 101 { 102 name: "duplicate by import name", 103 rc: rc, 104 args: args{ 105 path: "another/ast", 106 }, 107 want: want{ 108 importDeclaration: ds.ImportDeclaration{}, 109 RcImportPkgMap: map[string]int{"ast": 0}, 110 RcImportMap: map[string]int{"go/ast": 0}, 111 }, 112 wantErr: true, 113 }, 114 { 115 name: "import into another scope", 116 rc: rc, 117 args: args{ 118 path: "go/ast", 119 importname: []string{"anotherast"}, 120 }, 121 want: want{ 122 importDeclaration: ds.ImportDeclaration{ 123 Path: "go/ast", 124 ImportName: "anotherast", 125 }, 126 RcImportPkgMap: map[string]int{"anotherast": 1, "ast": 0}, 127 RcImportMap: map[string]int{"go/ast": 1}, 128 }, 129 wantErr: false, 130 }, 131 { 132 name: "another import with empty importName", 133 rc: rc, 134 args: args{ 135 path: "github.com/mailru/activerecord-cookbook.git/example/model/dictionary", 136 importname: []string{""}, 137 }, 138 want: want{ 139 importDeclaration: ds.ImportDeclaration{ 140 Path: "github.com/mailru/activerecord-cookbook.git/example/model/dictionary", 141 ImportName: "", 142 }, 143 RcImportPkgMap: map[string]int{"anotherast": 1, "ast": 0, "dictionary": 2}, 144 RcImportMap: map[string]int{"go/ast": 1, "github.com/mailru/activerecord-cookbook.git/example/model/dictionary": 2}, 145 }, 146 wantErr: false, 147 }, 148 } 149 for _, tt := range tests { 150 t.Run(tt.name, func(t *testing.T) { 151 got, err := tt.rc.AddImport(tt.args.path, tt.args.importname...) 152 if (err != nil) != tt.wantErr { 153 t.Errorf("RecordPackage.AddImport() error = %v, wantErr %v", err, tt.wantErr) 154 return 155 } 156 157 if !reflect.DeepEqual(got, tt.want.importDeclaration) { 158 t.Errorf("RecordPackage.AddImport() = %v, want %v", got, tt.want.importDeclaration) 159 return 160 } 161 162 if !reflect.DeepEqual(tt.rc.ImportPkgMap, tt.want.RcImportPkgMap) { 163 t.Errorf("RecordPackage.AddImport() ImportPkgMap = %+v, want %+v", tt.rc.ImportPkgMap, tt.want.RcImportPkgMap) 164 return 165 } 166 167 if !reflect.DeepEqual(tt.rc.ImportMap, tt.want.RcImportMap) { 168 t.Errorf("RecordPackage.AddImport() ImportMap = %+v, want %+v", tt.rc.ImportMap, tt.want.RcImportMap) 169 return 170 } 171 }) 172 } 173 } 174 175 func TestRecordPackage_FindImportByPkg(t *testing.T) { 176 rc := ds.NewRecordPackage() 177 178 imp, err := rc.AddImport("go/ast") 179 if err != nil { 180 t.Errorf("add import: %s", err) 181 return 182 } 183 184 type args struct { 185 pkg string 186 } 187 tests := []struct { 188 name string 189 rc *ds.RecordPackage 190 args args 191 want *ds.ImportDeclaration 192 wantErr bool 193 }{ 194 { 195 name: "getByPkg", 196 rc: rc, 197 args: args{pkg: "ast"}, 198 want: &imp, 199 wantErr: false, 200 }, 201 { 202 name: "getByName", 203 rc: rc, 204 args: args{pkg: "go/ast"}, 205 want: nil, 206 wantErr: true, 207 }, 208 { 209 name: "Unknown", 210 rc: rc, 211 args: args{pkg: "bla"}, 212 want: nil, 213 wantErr: true, 214 }, 215 } 216 for _, tt := range tests { 217 t.Run(tt.name, func(t *testing.T) { 218 got, err := tt.rc.FindImportByPkg(tt.args.pkg) 219 if (err != nil) != tt.wantErr { 220 t.Errorf("RecordPackage.FindImport() error = %v, wantErr %v", err, tt.wantErr) 221 return 222 } 223 if !reflect.DeepEqual(got, tt.want) { 224 t.Errorf("RecordPackage.FindImport() = %v, want %v", got, tt.want) 225 } 226 }) 227 } 228 } 229 230 func TestRecordClass_AddField(t *testing.T) { 231 type args struct { 232 f ds.FieldDeclaration 233 } 234 235 rc := ds.NewRecordPackage() 236 237 tests := []struct { 238 name string 239 fields *ds.RecordPackage 240 args args 241 wantErr bool 242 }{ 243 {name: "newField", fields: rc, args: args{f: ds.FieldDeclaration{Name: "bla"}}, wantErr: false}, 244 {name: "dupField", fields: rc, args: args{f: ds.FieldDeclaration{Name: "bla"}}, wantErr: true}, 245 {name: "anyField", fields: rc, args: args{f: ds.FieldDeclaration{Name: "bla1"}}, wantErr: false}, 246 {name: "CaseField", fields: rc, args: args{f: ds.FieldDeclaration{Name: "Bla"}}, wantErr: false}, 247 } 248 for _, tt := range tests { 249 t.Run(tt.name, func(t *testing.T) { 250 if err := tt.fields.AddField(tt.args.f); (err != nil) != tt.wantErr { 251 t.Errorf("RecordClass.AddField() error = %v, wantErr %v", err, tt.wantErr) 252 } 253 }) 254 } 255 } 256 257 func TestRecordClass_AddFieldObject(t *testing.T) { 258 type args struct { 259 f ds.FieldObject 260 } 261 262 rc := ds.NewRecordPackage() 263 264 tests := []struct { 265 name string 266 fields *ds.RecordPackage 267 args args 268 wantErr bool 269 }{ 270 {name: "newField", fields: rc, args: args{f: ds.FieldObject{Name: "bla"}}, wantErr: false}, 271 {name: "dupField", fields: rc, args: args{f: ds.FieldObject{Name: "bla"}}, wantErr: true}, 272 {name: "anyField", fields: rc, args: args{f: ds.FieldObject{Name: "bla1"}}, wantErr: false}, 273 {name: "CaseField", fields: rc, args: args{f: ds.FieldObject{Name: "Bla"}}, wantErr: false}, 274 } 275 for _, tt := range tests { 276 t.Run(tt.name, func(t *testing.T) { 277 if err := tt.fields.AddFieldObject(tt.args.f); (err != nil) != tt.wantErr { 278 t.Errorf("RecordClass.AddFieldObject() error = %v, wantErr %v", err, tt.wantErr) 279 } 280 }) 281 } 282 } 283 func TestRecordClass_AddTrigger(t *testing.T) { 284 type args struct { 285 f ds.TriggerDeclaration 286 } 287 288 rc := ds.NewRecordPackage() 289 290 tests := []struct { 291 name string 292 fields *ds.RecordPackage 293 args args 294 wantErr bool 295 }{ 296 {name: "newField", fields: rc, args: args{f: ds.TriggerDeclaration{Name: "bla"}}, wantErr: false}, 297 {name: "dupField", fields: rc, args: args{f: ds.TriggerDeclaration{Name: "bla"}}, wantErr: true}, 298 {name: "anyField", fields: rc, args: args{f: ds.TriggerDeclaration{Name: "bla1"}}, wantErr: false}, 299 {name: "CaseField", fields: rc, args: args{f: ds.TriggerDeclaration{Name: "Bla"}}, wantErr: false}, 300 } 301 for _, tt := range tests { 302 t.Run(tt.name, func(t *testing.T) { 303 if err := tt.fields.AddTrigger(tt.args.f); (err != nil) != tt.wantErr { 304 t.Errorf("RecordClass.AddTrigger() error = %v, wantErr %v", err, tt.wantErr) 305 } 306 }) 307 } 308 } 309 func TestRecordClass_AddSerializer(t *testing.T) { 310 type args struct { 311 f ds.SerializerDeclaration 312 } 313 314 rc := ds.NewRecordPackage() 315 316 tests := []struct { 317 name string 318 fields *ds.RecordPackage 319 args args 320 wantErr bool 321 }{ 322 {name: "newField", fields: rc, args: args{f: ds.SerializerDeclaration{Name: "bla"}}, wantErr: false}, 323 {name: "dupField", fields: rc, args: args{f: ds.SerializerDeclaration{Name: "bla"}}, wantErr: true}, 324 {name: "anyField", fields: rc, args: args{f: ds.SerializerDeclaration{Name: "bla1"}}, wantErr: false}, 325 {name: "CaseField", fields: rc, args: args{f: ds.SerializerDeclaration{Name: "Bla"}}, wantErr: false}, 326 } 327 for _, tt := range tests { 328 t.Run(tt.name, func(t *testing.T) { 329 if err := tt.fields.AddSerializer(tt.args.f); (err != nil) != tt.wantErr { 330 t.Errorf("RecordClass.AddSerializer() error = %v, wantErr %v", err, tt.wantErr) 331 } 332 }) 333 } 334 } 335 func TestRecordClass_AddFlag(t *testing.T) { 336 type args struct { 337 f ds.FlagDeclaration 338 } 339 340 rc := ds.NewRecordPackage() 341 342 tests := []struct { 343 name string 344 fields *ds.RecordPackage 345 args args 346 wantErr bool 347 }{ 348 {name: "newField", fields: rc, args: args{f: ds.FlagDeclaration{Name: "bla"}}, wantErr: false}, 349 {name: "dupField", fields: rc, args: args{f: ds.FlagDeclaration{Name: "bla"}}, wantErr: true}, 350 {name: "anyField", fields: rc, args: args{f: ds.FlagDeclaration{Name: "bla1"}}, wantErr: false}, 351 {name: "CaseField", fields: rc, args: args{f: ds.FlagDeclaration{Name: "Bla"}}, wantErr: false}, 352 } 353 for _, tt := range tests { 354 t.Run(tt.name, func(t *testing.T) { 355 if err := tt.fields.AddFlag(tt.args.f); (err != nil) != tt.wantErr { 356 t.Errorf("RecordClass.AddFlag() error = %v, wantErr %v", err, tt.wantErr) 357 } 358 }) 359 } 360 }