github.com/nikandfor/tlog@v0.21.5-0.20231108111739-3ef89426a96d/low/safeadd_test.go (about) 1 package low 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 "unicode/utf8" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestSafeAdd(t *testing.T) { 13 t.Parallel() 14 15 b := AppendSafeString(nil, `"\'`) 16 assert.Equal(t, []byte(`\"\\'`), b) 17 18 q := "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98" 19 20 b = AppendQuoteString(nil, q) 21 assert.Equal(t, []byte(`"\xbd\xb2=\xbc ⌘"`), b, "quoted: %q", q) 22 23 if t.Failed() { 24 i := 0 25 for i < len(q) { 26 r, w := utf8.DecodeRuneInString(q[i:]) 27 28 t.Logf("rune '%c' %8x w %d\n", r, r, w) 29 30 i += w 31 } 32 33 data, err := json.Marshal(q) 34 35 t.Logf("json: << %s >> err %v", data, err) 36 } 37 38 // t.Logf("res: '%s'", w.Bytes()) 39 } 40 41 func TestSafeMultiline(t *testing.T) { 42 t.Parallel() 43 44 const data = `flagfile: /etc/flags.flagfile 45 --friends , 46 --clickhouse tcp://127.0.0.1:9000 47 --discovery=true 48 --debug :6061 49 ` 50 51 b := AppendSafeString(nil, data) 52 53 // var dec string 54 55 assert.Equal(t, strings.ReplaceAll(data, "\n", "\\n"), string(b)) 56 }