func TestEncodeStrings(t *testing.T) {
	tests := []struct {
		in           string
		wantOut      string
		wantOutASCII string
	}{
		{
			in:      `"`,
			wantOut: `"\""`,
		},
		{
			in:      `'`,
			wantOut: `"'"`,
		},
		{
			in:           "hello\u1234world",
			wantOut:      "\"hello\u1234world\"",
			wantOutASCII: `"hello\u1234world"`,
		},
		{
			// String that has as few escaped characters as possible.
			in: func() string {
				var b []byte
				for i := 0; i < utf8.RuneSelf; i++ {
					switch i {
					case 0, '\\', '\n', '\'': // these must be escaped, so ignore them
					default:
						b = append(b, byte(i))
					}
				}
				return string(b)
			}(),
			wantOut:      `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
			wantOutASCII: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
		},
		{
			// Valid UTF-8 wire encoding of the RuneError rune.
			in:           string(utf8.RuneError),
			wantOut:      `"` + string(utf8.RuneError) + `"`,
			wantOutASCII: `"\ufffd"`,
		},
		{
			in:           "\"'\\?\a\b\n\r\t\v\f\x01\nS\n\xab\x12\uab8f\U0010ffff",
			wantOut:      `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12` + "\uab8f\U0010ffff" + `"`,
			wantOutASCII: `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12\uab8f\U0010ffff"`,
		},
		{
			in:           "\001x",
			wantOut:      `"\x01x"`,
			wantOutASCII: `"\x01x"`,
		},
		{
			in:           "\012x",
			wantOut:      `"\nx"`,
			wantOutASCII: `"\nx"`,
		},
		{
			in:           "\123x",
			wantOut:      `"Sx"`,
			wantOutASCII: `"Sx"`,
		},
		{
			in:           "\1234x",
			wantOut:      `"S4x"`,
			wantOutASCII: `"S4x"`,
		},
		{
			in:           "\001",
			wantOut:      `"\x01"`,
			wantOutASCII: `"\x01"`,
		},
		{
			in:           "\012",
			wantOut:      `"\n"`,
			wantOutASCII: `"\n"`,
		},
		{
			in:           "\123",
			wantOut:      `"S"`,
			wantOutASCII: `"S"`,
		},
		{
			in:           "\1234",
			wantOut:      `"S4"`,
			wantOutASCII: `"S4"`,
		},
		{
			in:           "\377",
			wantOut:      `"\xff"`,
			wantOutASCII: `"\xff"`,
		},
		{
			in:           "\x0fx",
			wantOut:      `"\x0fx"`,
			wantOutASCII: `"\x0fx"`,
		},
		{
			in:           "\xffx",
			wantOut:      `"\xffx"`,
			wantOutASCII: `"\xffx"`,
		},
		{
			in:           "\xfffx",
			wantOut:      `"\xfffx"`,
			wantOutASCII: `"\xfffx"`,
		},
		{
			in:           "\x0f",
			wantOut:      `"\x0f"`,
			wantOutASCII: `"\x0f"`,
		},
		{
			in:           "\xff",
			wantOut:      `"\xff"`,
			wantOutASCII: `"\xff"`,
		},
		{
			in:           "\xfff",
			wantOut:      `"\xfff"`,
			wantOutASCII: `"\xfff"`,
		},
	}
	for _, tc := range tests {
		t.Run("", func(t *testing.T) {
			if tc.wantOut != "" {
				runEncodeStringsTest(t, tc.in, tc.wantOut, false)
			}
			if tc.wantOutASCII != "" {
				runEncodeStringsTest(t, tc.in, tc.wantOutASCII, true)
			}
		})
	}
}
// Test for UTF-8 and ASCII outputs.
Test for UTF-8 and ASCII outputs.
// Test for UTF - 8 and ASCII outputs .
func TestEncodeStrings(t *testing.T) {
	tests := []struct {
		in           string
		wantOut      string
		wantOutASCII string
	}{
		{
			in:      `"`,
			wantOut: `"\""`,
		},
		{
			in:      `'`,
			wantOut: `"'"`,
		},
		{
			in:           "hello\u1234world",
			wantOut:      "\"hello\u1234world\"",
			wantOutASCII: `"hello\u1234world"`,
		},
		{
			in: func() string {
				var b []byte
				for i := 0; i < utf8.RuneSelf; i++ {
					switch i {
					case 0, '\\', '\n', '\'': 
					default:
						b = append(b, byte(i))
					}
				}
				return string(b)
			}(),
			wantOut:      `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
			wantOutASCII: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
		},
		{
			in:           string(utf8.RuneError),
			wantOut:      `"` + string(utf8.RuneError) + `"`,
			wantOutASCII: `"\ufffd"`,
		},
		{
			in:           "\"'\\?\a\b\n\r\t\v\f\x01\nS\n\xab\x12\uab8f\U0010ffff",
			wantOut:      `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12` + "\uab8f\U0010ffff" + `"`,
			wantOutASCII: `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12\uab8f\U0010ffff"`,
		},
		{
			in:           "\001x",
			wantOut:      `"\x01x"`,
			wantOutASCII: `"\x01x"`,
		},
		{
			in:           "\012x",
			wantOut:      `"\nx"`,
			wantOutASCII: `"\nx"`,
		},
		{
			in:           "\123x",
			wantOut:      `"Sx"`,
			wantOutASCII: `"Sx"`,
		},
		{
			in:           "\1234x",
			wantOut:      `"S4x"`,
			wantOutASCII: `"S4x"`,
		},
		{
			in:           "\001",
			wantOut:      `"\x01"`,
			wantOutASCII: `"\x01"`,
		},
		{
			in:           "\012",
			wantOut:      `"\n"`,
			wantOutASCII: `"\n"`,
		},
		{
			in:           "\123",
			wantOut:      `"S"`,
			wantOutASCII: `"S"`,
		},
		{
			in:           "\1234",
			wantOut:      `"S4"`,
			wantOutASCII: `"S4"`,
		},
		{
			in:           "\377",
			wantOut:      `"\xff"`,
			wantOutASCII: `"\xff"`,
		},
		{
			in:           "\x0fx",
			wantOut:      `"\x0fx"`,
			wantOutASCII: `"\x0fx"`,
		},
		{
			in:           "\xffx",
			wantOut:      `"\xffx"`,
			wantOutASCII: `"\xffx"`,
		},
		{
			in:           "\xfffx",
			wantOut:      `"\xfffx"`,
			wantOutASCII: `"\xfffx"`,
		},
		{
			in:           "\x0f",
			wantOut:      `"\x0f"`,
			wantOutASCII: `"\x0f"`,
		},
		{
			in:           "\xff",
			wantOut:      `"\xff"`,
			wantOutASCII: `"\xff"`,
		},
		{
			in:           "\xfff",
			wantOut:      `"\xfff"`,
			wantOutASCII: `"\xfff"`,
		},
	}
	for _, tc := range tests {
		t.Run("", func(t *testing.T) {
			if tc.wantOut != "" {
				runEncodeStringsTest(t, tc.in, tc.wantOut, false)
			}
			if tc.wantOutASCII != "" {
				runEncodeStringsTest(t, tc.in, tc.wantOutASCII, true)
			}
		})
	}
}
func TestEncodeStrings ( t * testing . T ) { tests := [ ] struct { in string wantOut string wantOutASCII string } { { in : `"` , wantOut : `"\""` , } , { in : `'` , wantOut : `"'"` , } , { in : " \u1234 " , wantOut : " \" \u1234 \" " , wantOutASCII : `"hello\u1234world"` , } , { in : func ( ) string { var b [ ] byte for i := 0 ; i < utf8 . RuneSelf ; i ++ { switch i { case 0 , '\\' , '\n' , '\'' : default : b = append ( b , byte ( i ) ) } } return string ( b ) } ( ) , wantOut : `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + " \x7f \" " , wantOutASCII : `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + " \x7f \" " , } , { in : string ( utf8 . RuneError ) , wantOut : `"` + string ( utf8 . RuneError ) + `"` , wantOutASCII : `"\ufffd"` , } , { in : " \" \\ \a \b \n \r \t \v \f \x01 \n \n \xab \x12 \uab8f \U0010ffff " , wantOut : `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12` + " \uab8f \U0010ffff " + `"` , wantOutASCII : `"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12\uab8f\U0010ffff"` , } , { in : " \001 " , wantOut : `"\x01x"` , wantOutASCII : `"\x01x"` , } , { in : " \012 " , wantOut : `"\nx"` , wantOutASCII : `"\nx"` , } , { in : " \123 " , wantOut : `"Sx"` , wantOutASCII : `"Sx"` , } , { in : " \123 " , wantOut : `"S4x"` , wantOutASCII : `"S4x"` , } , { in : " \001 " , wantOut : `"\x01"` , wantOutASCII : `"\x01"` , } , { in : " \012 " , wantOut : `"\n"` , wantOutASCII : `"\n"` , } , { in : " \123 " , wantOut : `"S"` , wantOutASCII : `"S"` , } , { in : " \123 " , wantOut : `"S4"` , wantOutASCII : `"S4"` , } , { in : " \377 " , wantOut : `"\xff"` , wantOutASCII : `"\xff"` , } , { in : " \x0f " , wantOut : `"\x0fx"` , wantOutASCII : `"\x0fx"` , } , { in : " \xff " , wantOut : `"\xffx"` , wantOutASCII : `"\xffx"` , } , { in : " \xff " , wantOut : `"\xfffx"` , wantOutASCII : `"\xfffx"` , } , { in : " \x0f " , wantOut : `"\x0f"` , wantOutASCII : `"\x0f"` , } , { in : " \xff " , wantOut : `"\xff"` , wantOutASCII : `"\xff"` , } , { in : " \xff " , wantOut : `"\xfff"` , wantOutASCII : `"\xfff"` , } , } for _ , tc := range tests { t . Run ( " " , func ( t * testing . T ) { if tc . wantOut != " " { runEncodeStringsTest ( t , tc . in , tc . wantOut , false ) } if tc . wantOutASCII != " " { runEncodeStringsTest ( t , tc . in , tc . wantOutASCII , true ) } } ) } }
Test for UTF-8 and ASCII outputs.
Test for UTF - 8 and ASCII outputs .
// String that has as few escaped characters as possible. // these must be escaped, so ignore them // Valid UTF-8 wire encoding of the RuneError rune.