github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/struct_tag_test.go (about) 1 package jzon 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestStructTag_parseTag(t *testing.T) { 10 t.Run("no comma", func(t *testing.T) { 11 name, opts := parseTag("test") 12 require.Equal(t, "test", name) 13 require.Empty(t, opts) 14 }) 15 t.Run("with comma", func(t *testing.T) { 16 name, opts := parseTag("test,opts") 17 require.Equal(t, "test", name) 18 require.Equal(t, tagOptions("opts"), opts) 19 }) 20 } 21 22 func TestStructTag_TagOptions_Contains(t *testing.T) { 23 t.Run("empty", func(t *testing.T) { 24 var opts tagOptions 25 require.False(t, opts.Contains("opt")) 26 }) 27 t.Run("non empty", func(t *testing.T) { 28 opts := tagOptions("opt1,opt2,opt3") 29 require.True(t, opts.Contains("opt1")) 30 require.True(t, opts.Contains("opt2")) 31 require.True(t, opts.Contains("opt3")) 32 require.False(t, opts.Contains("opt4")) 33 }) 34 } 35 36 func TestStructTag_isValidTag(t *testing.T) { 37 t.Run("empty", func(t *testing.T) { 38 require.False(t, isValidTag("")) 39 }) 40 t.Run("true", func(t *testing.T) { 41 require.True(t, isValidTag("!#$%&()*+-./:<=>?@[]^_{|}~ ")) 42 }) 43 t.Run("false", func(t *testing.T) { 44 require.False(t, isValidTag("\u00b6")) 45 }) 46 }