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

     1  package parser
     2  
     3  import (
     4  	"go/ast"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func Test_getNodeName(t *testing.T) {
    10  	type args struct {
    11  		node string
    12  	}
    13  	tests := []struct {
    14  		name            string
    15  		args            args
    16  		wantName        string
    17  		wantPublicName  string
    18  		wantPackageName string
    19  		wantErr         bool
    20  	}{
    21  		{
    22  			name: "error fileds",
    23  			args: args{
    24  				node: "Fields",
    25  			},
    26  			wantName:        "Fields",
    27  			wantPublicName:  "",
    28  			wantPackageName: "",
    29  			wantErr:         true,
    30  		},
    31  		{
    32  			name: "unknown fields",
    33  			args: args{
    34  				node: "bla",
    35  			},
    36  			wantName:        "",
    37  			wantPublicName:  "",
    38  			wantPackageName: "",
    39  			wantErr:         true,
    40  		},
    41  		{
    42  			name: "success fields",
    43  			args: args{
    44  				node: "FieldsUser",
    45  			},
    46  			wantName:        "Fields",
    47  			wantPublicName:  "User",
    48  			wantPackageName: "user",
    49  			wantErr:         false,
    50  		},
    51  		{
    52  			name: "fields object",
    53  			args: args{
    54  				node: "FieldsObjectUser",
    55  			},
    56  			wantName:        "FieldsObject",
    57  			wantPublicName:  "User",
    58  			wantPackageName: "user",
    59  			wantErr:         false,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			gotName, gotPublicName, gotPackageName, err := getNodeName(tt.args.node)
    65  			if (err != nil) != tt.wantErr {
    66  				t.Errorf("getNodeName() error = %v, wantErr %v", err, tt.wantErr)
    67  				return
    68  			}
    69  			if gotName != tt.wantName {
    70  				t.Errorf("getNodeName() gotName = %v, want %v", gotName, tt.wantName)
    71  			}
    72  			if gotPublicName != tt.wantPublicName {
    73  				t.Errorf("getNodeName() gotPublicName = %v, want %v", gotPublicName, tt.wantPublicName)
    74  			}
    75  			if gotPackageName != tt.wantPackageName {
    76  				t.Errorf("getNodeName() gotPackageName = %v, want %v", gotPackageName, tt.wantPackageName)
    77  			}
    78  		})
    79  	}
    80  }
    81  
    82  func Test_splitParam(t *testing.T) {
    83  	type args struct {
    84  		str  string
    85  		rule map[TagNameType]ParamValueRule
    86  	}
    87  	tests := []struct {
    88  		name    string
    89  		args    args
    90  		want    [][]string
    91  		wantErr bool
    92  	}{
    93  		{
    94  			name: "emptytag", wantErr: false,
    95  			args: args{
    96  				str:  "",
    97  				rule: map[TagNameType]ParamValueRule{},
    98  			},
    99  			want: [][]string{},
   100  		},
   101  		{
   102  			name: "onetag", wantErr: false,
   103  			args: args{
   104  				str:  "a:b",
   105  				rule: map[TagNameType]ParamValueRule{},
   106  			},
   107  			want: [][]string{{"a", "b"}},
   108  		},
   109  		{
   110  			name: "anytag", wantErr: false,
   111  			args: args{
   112  				str:  "a:b;d:f",
   113  				rule: map[TagNameType]ParamValueRule{},
   114  			},
   115  			want: [][]string{{"a", "b"}, {"d", "f"}},
   116  		},
   117  		{
   118  			name: "anytagempty", wantErr: false,
   119  			args: args{
   120  				str:  "a:b;;d:f",
   121  				rule: map[TagNameType]ParamValueRule{},
   122  			},
   123  			want: [][]string{{"a", "b"}, {"d", "f"}},
   124  		},
   125  		{
   126  			name: "tagisflag", wantErr: false,
   127  			args: args{
   128  				str:  "a:b;d:f;g",
   129  				rule: map[TagNameType]ParamValueRule{"g": ParamNotNeedValue},
   130  			},
   131  			want: [][]string{{"a", "b"}, {"d", "f"}, {"g"}},
   132  		},
   133  		{
   134  			name:    "tagflagerr",
   135  			wantErr: false,
   136  			args: args{
   137  				str:  "a",
   138  				rule: map[TagNameType]ParamValueRule{},
   139  			},
   140  			want: [][]string{{"a"}},
   141  		},
   142  		{
   143  			name: "tagflagerr", wantErr: true,
   144  			args: args{
   145  				str:  "a:b",
   146  				rule: map[TagNameType]ParamValueRule{"a": ParamNotNeedValue},
   147  			},
   148  		},
   149  	}
   150  	for _, tt := range tests {
   151  		t.Run(tt.name, func(t *testing.T) {
   152  			got, err := splitParam(tt.args.str, tt.args.rule)
   153  			if (err != nil) != tt.wantErr {
   154  				t.Errorf("splitParam() error = %v, wantErr %v", err, tt.wantErr)
   155  				return
   156  			}
   157  			if !reflect.DeepEqual(got, tt.want) {
   158  				t.Errorf("splitParam() = %v, want %v", got, tt.want)
   159  			}
   160  		})
   161  	}
   162  }
   163  
   164  func Test_splitTag(t *testing.T) {
   165  	type args struct {
   166  		field     *ast.Field
   167  		checkFlag uint32
   168  		rule      map[TagNameType]ParamValueRule
   169  	}
   170  	tests := []struct {
   171  		name    string
   172  		args    args
   173  		want    [][]string
   174  		wantErr bool
   175  	}{
   176  		{
   177  			name: "emptytag", wantErr: false,
   178  			args: args{
   179  				field: &ast.Field{Tag: &ast.BasicLit{Value: "`ar:\"\"`"}},
   180  				rule:  map[TagNameType]ParamValueRule{},
   181  			},
   182  			want: [][]string{},
   183  		},
   184  		{
   185  			name: "emptytagcheck", wantErr: true,
   186  			args: args{
   187  				field:     &ast.Field{Tag: &ast.BasicLit{Value: "`ar:\"\"`"}},
   188  				rule:      map[TagNameType]ParamValueRule{},
   189  				checkFlag: CheckFlagEmpty,
   190  			},
   191  			want: nil,
   192  		},
   193  		{
   194  			name: "tagnoprefix", wantErr: true,
   195  			args: args{
   196  				field: &ast.Field{Tag: &ast.BasicLit{Value: "dsjfgsadkjgfdskj"}},
   197  				rule:  map[TagNameType]ParamValueRule{},
   198  			},
   199  			want: nil,
   200  		},
   201  		{
   202  			name: "tag", wantErr: false,
   203  			args: args{
   204  				field: &ast.Field{Tag: &ast.BasicLit{Value: "`ar:\"a:b;c:d;d:r,t\"`"}},
   205  				rule:  map[TagNameType]ParamValueRule{},
   206  			},
   207  			want: [][]string{{"a", "b"}, {"c", "d"}, {"d", "r,t"}},
   208  		},
   209  	}
   210  	for _, tt := range tests {
   211  		t.Run(tt.name, func(t *testing.T) {
   212  			got, err := splitTag(tt.args.field, tt.args.checkFlag, tt.args.rule)
   213  			if (err != nil) != tt.wantErr {
   214  				t.Errorf("splitTag() error = %v, wantErr %v", err, tt.wantErr)
   215  				return
   216  			}
   217  			if !reflect.DeepEqual(got, tt.want) {
   218  				t.Errorf("splitTag() = %v, want %v", got, tt.want)
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  func Test_checkBoolType(t *testing.T) {
   225  	type args struct {
   226  		indType ast.Expr
   227  	}
   228  	tests := []struct {
   229  		name    string
   230  		args    args
   231  		wantErr bool
   232  	}{
   233  		{name: "bool", args: args{indType: &ast.Ident{Name: "bool"}}, wantErr: false},
   234  		{name: "bool", args: args{indType: &ast.Ident{Name: "int"}}, wantErr: true},
   235  	}
   236  	for _, tt := range tests {
   237  		t.Run(tt.name, func(t *testing.T) {
   238  			if err := checkBoolType(tt.args.indType); (err != nil) != tt.wantErr {
   239  				t.Errorf("checkBoolType() error = %v, wantErr %v", err, tt.wantErr)
   240  			}
   241  		})
   242  	}
   243  }