github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/reflectkit/structtag_parser_test.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package reflectkit
     7  
     8  import (
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestParseStructTag(t *testing.T) {
    16  	for _, tc := range []struct {
    17  		tag    reflect.StructTag
    18  		findFn func(name, qvalue string) bool
    19  		name   string
    20  		value  string
    21  		found  bool
    22  	}{
    23  		{},
    24  		{
    25  			tag: reflect.StructTag(""),
    26  		},
    27  		{
    28  			tag: reflect.StructTag("   "),
    29  		},
    30  		{
    31  			tag: reflect.StructTag(`tag:""`),
    32  			findFn: func(name, qvalue string) bool {
    33  				return true
    34  			},
    35  			found: true,
    36  			name:  "tag",
    37  		},
    38  		{
    39  			tag: reflect.StructTag(`tag:"value"`),
    40  			findFn: func(name, qvalue string) bool {
    41  				return true
    42  			},
    43  			found: true,
    44  			name:  "tag",
    45  			value: "value",
    46  		},
    47  		{
    48  			tag: reflect.StructTag(`   :"value"`),
    49  			findFn: func(name, qvalue string) bool {
    50  				return true
    51  			},
    52  			found: false,
    53  		},
    54  		{
    55  			tag: reflect.StructTag(`   tag:"value"`),
    56  			findFn: func(name, qvalue string) bool {
    57  				return true
    58  			},
    59  			found: true,
    60  			name:  "tag",
    61  			value: "value",
    62  		},
    63  		{
    64  			tag: reflect.StructTag(`tag:"value`),
    65  			findFn: func(name, qvalue string) bool {
    66  				return true
    67  			},
    68  			found: false,
    69  		},
    70  		{
    71  			tag: reflect.StructTag(`tag:"value\\\"`),
    72  			findFn: func(name, qvalue string) bool {
    73  				return true
    74  			},
    75  			found: false,
    76  		},
    77  		{
    78  			tag: reflect.StructTag(`tag:"value"`),
    79  			findFn: func(name, qvalue string) bool {
    80  				return name == "nottag"
    81  			},
    82  			found: false,
    83  		},
    84  	} {
    85  		name, value, found := ParseStructTag(tc.tag, tc.findFn)
    86  		require.Equal(t, tc.found, found, "tag %s, found", tc.tag)
    87  		require.Equal(t, tc.name, name, "tag %s, name", tc.tag)
    88  		require.Equal(t, tc.value, value, "tag %s, value", tc.tag)
    89  	}
    90  }