github.com/bytedance/go-tagexpr@v2.7.5-0.20210114074101-de5b8743ad85+incompatible/tagparser_test.go (about)

     1  package tagexpr
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestTagparser(t *testing.T) {
    11  	cases := []struct {
    12  		tag    reflect.StructTag
    13  		expect map[string]string
    14  		fail   bool
    15  	}{
    16  		{
    17  			tag: `tagexpr:"$>0"`,
    18  			expect: map[string]string{
    19  				"@": "$>0",
    20  			},
    21  		}, {
    22  			tag:  `tagexpr:"$>0;'xxx'"`,
    23  			fail: true,
    24  		}, {
    25  			tag: `tagexpr:"$>0;b:sprintf('%[1]T; %[1]v',(X)$)"`,
    26  			expect: map[string]string{
    27  				"@": `$>0`,
    28  				"b": `sprintf('%[1]T; %[1]v',(X)$)`,
    29  			},
    30  		}, {
    31  			tag: `tagexpr:"a:$=='0;1;';b:sprintf('%[1]T; %[1]v',(X)$)"`,
    32  			expect: map[string]string{
    33  				"a": `$=='0;1;'`,
    34  				"b": `sprintf('%[1]T; %[1]v',(X)$)`,
    35  			},
    36  		}, {
    37  			tag: `tagexpr:"a:1;;b:2"`,
    38  			expect: map[string]string{
    39  				"a": `1`,
    40  				"b": `2`,
    41  			},
    42  		}, {
    43  			tag: `tagexpr:";a:1;;b:2;;;"`,
    44  			expect: map[string]string{
    45  				"a": `1`,
    46  				"b": `2`,
    47  			},
    48  		}, {
    49  			tag: `tagexpr:";a:'123\\'';;b:'1\\'23';c:'1\\'2\\'3';;"`,
    50  			expect: map[string]string{
    51  				"a": `'123\''`,
    52  				"b": `'1\'23'`,
    53  				"c": `'1\'2\'3'`,
    54  			},
    55  		}, {
    56  			tag: `tagexpr:"email($)"`,
    57  			expect: map[string]string{
    58  				"@": `email($)`,
    59  			},
    60  		}, {
    61  			tag: `tagexpr:"false"`,
    62  			expect: map[string]string{
    63  				"@": `false`,
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, c := range cases {
    69  		r, e := parseTag(c.tag.Get("tagexpr"))
    70  		if e != nil == c.fail {
    71  			assert.Equal(t, c.expect, r, c.tag)
    72  		} else {
    73  			assert.Failf(t, string(c.tag), "kvs:%v, err:%v", r, e)
    74  		}
    75  		if e != nil {
    76  			t.Logf("tag:%q, errMsg:%v", c.tag, e)
    77  		}
    78  	}
    79  }