github.com/jxskiss/gopkg@v0.17.3/strutil/format_test.go (about) 1 package strutil 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestFormat_AutoNumber(t *testing.T) { 9 str := "some test {} and {test1} {} with {test2}" 10 got := Format(str, map[string]interface{}{"test1": "abc", "test2": 123}, "position1", []string{"sliceElem1", "sliceElem2"}) 11 want := "some test position1 and abc [sliceElem1 sliceElem2] with 123" 12 assert.Equal(t, want, got) 13 } 14 15 func TestFormat_ManualNumber(t *testing.T) { 16 str := "some test {0} and {test1} {1} with {test2} {0}" 17 got := Format(str, map[string]interface{}{"test1": "abc", "test2": 123}, "position1", []string{"sliceElem1", "sliceElem2"}) 18 want := "some test position1 and abc [sliceElem1 sliceElem2] with 123 position1" 19 assert.Equal(t, want, got) 20 } 21 22 func TestFormat_Struct(t *testing.T) { 23 str := "some test {Field1:%s} and {Field2:08d}" 24 got := Format(str, &testObject{Field1: "abc", Field2: 123}) 25 want := "some test abc and 00000123" 26 assert.Equal(t, want, got) 27 } 28 29 func TestFormat_EscapeWing(t *testing.T) { 30 got := Format("{{ some text }}", nil) 31 want := "{ some text }" 32 assert.Equal(t, want, got) 33 } 34 35 func TestFormat_Malformed(t *testing.T) { 36 str1 := "some test {} {0} {} {1}" 37 got1 := Format(str1, nil, "abc", "123") 38 want1 := "some test abc {0} 123 {1}" 39 assert.Equal(t, want1, got1) 40 41 str2 := "some test {1} {} {0} {}" 42 got2 := Format(str2, nil, "abc", "123") 43 want2 := "some test 123 {} abc {}" 44 assert.Equal(t, want2, got2) 45 46 str3 := "some test {abc} {{abc}} {key2}" 47 got3 := Format(str3, map[string]interface{}{"abc": "123"}) 48 want3 := "some test 123 {abc} {key2}" 49 assert.Equal(t, want3, got3) 50 51 str4 := "some test {Field1} {Field3:%.8f} {private:08d}" 52 got4 := Format(str4, &testObject{Field1: "abc", Field2: 123, private: 456}) 53 want4 := "some test abc {Field3:%.8f} 00000456" 54 assert.Equal(t, want4, got4) 55 } 56 57 func TestFormat_StringInterfaceMap(t *testing.T) { 58 str := "some test {abc} {{abc}} {key2}" 59 kwArgs := map[String]interface{}{"abc": "123"} 60 got := Format(str, kwArgs) 61 want := "some test 123 {abc} {key2}" 62 assert.Equal(t, want, got) 63 } 64 65 type String string 66 67 type testObject struct { 68 Field1 string 69 Field2 int64 70 private int32 71 }