github.com/jxskiss/gopkg@v0.17.3/structtag/tag_options_test.go (about)

     1  package structtag
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestParseOptions_Gorm(t *testing.T) {
     9  	gormTests := []struct {
    10  		tag     string
    11  		options Options
    12  	}{
    13  		{
    14  			tag: `type:varchar(100);unique_index`,
    15  			options: Options{
    16  				{raw: "type:varchar(100)", k: "type", v: "varchar(100)"},
    17  				{raw: "unique_index", k: "unique_index", v: ""},
    18  			},
    19  		},
    20  		{
    21  			tag: `unique;not null`,
    22  			options: Options{
    23  				{raw: "unique", k: "unique", v: ""},
    24  				{raw: "not null", k: "not null", v: ""},
    25  			},
    26  		},
    27  		{
    28  			tag:     `AUTO_INCREMENT`,
    29  			options: Options{{raw: "AUTO_INCREMENT", k: "AUTO_INCREMENT", v: ""}},
    30  		},
    31  		{
    32  			tag:     `-`,
    33  			options: Options{{raw: "-", k: "-", v: ""}},
    34  		},
    35  		{
    36  			tag:     "",
    37  			options: nil,
    38  		},
    39  	}
    40  
    41  	for _, ts := range gormTests {
    42  		options := ParseOptions(ts.tag, ";", ":")
    43  		assert.Equal(t, ts.options, options)
    44  	}
    45  }
    46  
    47  func TestOptions_Get(t *testing.T) {
    48  	options := Options{{k: "type", v: "varchar(100)"}, {k: "unique_index", v: ""}}
    49  	tests := []struct {
    50  		opt   string
    51  		exp   string
    52  		found bool
    53  	}{
    54  		{
    55  			opt:   "type",
    56  			exp:   "varchar(100)",
    57  			found: true,
    58  		},
    59  		{
    60  			opt:   "unique_index",
    61  			exp:   "",
    62  			found: true,
    63  		},
    64  		{
    65  			opt:   "column",
    66  			found: false,
    67  		},
    68  	}
    69  
    70  	for _, ts := range tests {
    71  		opt, found := options.Get(ts.opt)
    72  		assert.Equal(t, ts.found, found)
    73  		if found {
    74  			assert.Equal(t, ts.exp, opt.Value())
    75  		}
    76  	}
    77  }