github.com/mailru/activerecord@v1.12.2/internal/pkg/parser/serializer_b_test.go (about)

     1  package parser_test
     2  
     3  import (
     4  	"go/ast"
     5  	"testing"
     6  
     7  	"github.com/mailru/activerecord/internal/pkg/ds"
     8  	"github.com/mailru/activerecord/internal/pkg/parser"
     9  )
    10  
    11  func TestParseSerializer(t *testing.T) {
    12  	dst := ds.NewRecordPackage()
    13  
    14  	if _, err := dst.AddImport("github.com/mailru/activerecord/notexistsfolder/dictionary"); err != nil {
    15  		t.Errorf("can't prepare test data: %s", err)
    16  		return
    17  	}
    18  
    19  	type args struct {
    20  		dst    *ds.RecordPackage
    21  		fields []*ast.Field
    22  	}
    23  	tests := []struct {
    24  		name    string
    25  		args    args
    26  		wantErr bool
    27  	}{
    28  		{
    29  			name: "simple serializer",
    30  			args: args{
    31  				dst: dst,
    32  				fields: []*ast.Field{
    33  					{
    34  						Names: []*ast.Ident{{Name: "Foo"}},
    35  						Tag:   &ast.BasicLit{Value: "`ar:\"pkg:github.com/mailru/activerecord/notexistsfolder/serializer\"`"},
    36  						Type: &ast.StarExpr{
    37  							X: &ast.SelectorExpr{
    38  								X:   &ast.Ident{Name: "dictionary"},
    39  								Sel: &ast.Ident{Name: "Bar"},
    40  							},
    41  						},
    42  					},
    43  				},
    44  			},
    45  			wantErr: false,
    46  		},
    47  		{
    48  			name: "not imported package for serializer type",
    49  			args: args{
    50  				dst: dst,
    51  				fields: []*ast.Field{
    52  					{
    53  						Names: []*ast.Ident{{Name: "Foo"}},
    54  						Tag:   &ast.BasicLit{Value: "`ar:\"pkg:github.com/mailru/activerecord/notexistsfolder/serializer\"`"},
    55  						Type: &ast.StarExpr{
    56  							X: &ast.SelectorExpr{
    57  								X:   &ast.Ident{Name: "notimportedpackage"},
    58  								Sel: &ast.Ident{Name: "Bar"},
    59  							},
    60  						},
    61  					},
    62  				},
    63  			},
    64  			wantErr: true,
    65  		},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			if err := parser.ParseSerializer(tt.args.dst, tt.args.fields); (err != nil) != tt.wantErr {
    70  				t.Errorf("ParseSerializer() error = %v, wantErr %v", err, tt.wantErr)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestParseTypeSerializer(t *testing.T) {
    77  	dst := ds.NewRecordPackage()
    78  	if _, err := dst.AddImport("github.com/mailru/activerecord/notexistsfolder/dictionary"); err != nil {
    79  		t.Errorf("can't prepare test data: %s", err)
    80  		return
    81  	}
    82  
    83  	type args struct {
    84  		dst            *ds.RecordPackage
    85  		serializerName string
    86  		t              interface{}
    87  	}
    88  	tests := []struct {
    89  		name    string
    90  		args    args
    91  		want    string
    92  		wantErr bool
    93  	}{
    94  		{
    95  			name: "simple type",
    96  			args: args{
    97  				dst:            dst,
    98  				serializerName: "Foo",
    99  				t: &ast.StarExpr{
   100  					X: &ast.SelectorExpr{
   101  						X:   &ast.Ident{Name: "dictionary"},
   102  						Sel: &ast.Ident{Name: "Bar"},
   103  					},
   104  				},
   105  			},
   106  			want:    "*dictionary.Bar",
   107  			wantErr: false,
   108  		},
   109  	}
   110  	for _, tt := range tests {
   111  		t.Run(tt.name, func(t *testing.T) {
   112  			got, err := parser.ParseTypeSerializer(tt.args.dst, tt.args.serializerName, tt.args.t)
   113  			if (err != nil) != tt.wantErr {
   114  				t.Errorf("ParseTypeSerializer() error = %v, wantErr %v", err, tt.wantErr)
   115  				return
   116  			}
   117  			if got != tt.want {
   118  				t.Errorf("ParseTypeSerializer() = %v, want %v", got, tt.want)
   119  			}
   120  		})
   121  	}
   122  }