1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-24 23:16:47 +02:00

#53 support escapeHtml

This commit is contained in:
Tao Wen
2017-06-15 23:54:43 +08:00
parent d867c8ba5c
commit 5f22e50c89
30 changed files with 590 additions and 249 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/json-iterator/go/require"
"testing"
"unicode/utf8"
)
func Test_read_normal_string(t *testing.T) {
@ -17,22 +18,22 @@ func Test_read_normal_string(t *testing.T) {
for input, output := range cases {
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(DEFAULT_CONFIG, input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(DEFAULT_CONFIG, input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, string(iter.ReadStringAsSlice()))
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, string(iter.ReadStringAsSlice()))
})
}
@ -48,12 +49,12 @@ func Test_read_exotic_string(t *testing.T) {
for input, output := range cases {
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(DEFAULT_CONFIG, input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, iter.ReadString())
})
}
@ -61,7 +62,7 @@ func Test_read_exotic_string(t *testing.T) {
func Test_read_string_as_interface(t *testing.T) {
should := require.New(t)
iter := ParseString(DEFAULT_CONFIG, `"hello"`)
iter := ParseString(ConfigOfDefault, `"hello"`)
should.Equal("hello", iter.Read())
}
@ -98,7 +99,7 @@ func Test_write_string(t *testing.T) {
func Test_write_val_string(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal("hello")
stream.Flush()
should.Nil(stream.Error)
@ -112,25 +113,39 @@ func Test_decode_slash(t *testing.T) {
should.NotNil(UnmarshalFromString("\\", &obj))
}
//func Test_html_escape(t *testing.T) {
// should := require.New(t)
// output, err := json.Marshal(`>`)
// should.Nil(err)
// should.Equal(`"\u003e"`, string(output))
// output, err = Marshal(`>`)
// should.Nil(err)
// should.Equal(`"\u003e"`, string(output))
//}
func Test_html_escape(t *testing.T) {
should := require.New(t)
output, err := json.Marshal(`>`)
should.Nil(err)
should.Equal(`"\u003e"`, string(output))
output, err = ConfigCompatibleWithStandardLibrary.Marshal(`>`)
should.Nil(err)
should.Equal(`"\u003e"`, string(output))
}
func Test_string_encode_with_std(t *testing.T) {
should := require.New(t)
for i := 0; i < utf8.RuneSelf; i++ {
input := string([]byte{byte(i)})
stdOutputBytes, err := json.Marshal(input)
should.Nil(err)
stdOutput := string(stdOutputBytes)
jsoniterOutputBytes, err := ConfigCompatibleWithStandardLibrary.Marshal(input)
should.Nil(err)
jsoniterOutput := string(jsoniterOutputBytes)
should.Equal(stdOutput, jsoniterOutput)
}
}
func Benchmark_jsoniter_unicode(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(DEFAULT_CONFIG, `"\ud83d\udc4a"`)
iter := ParseString(ConfigOfDefault, `"\ud83d\udc4a"`)
iter.ReadString()
}
}
func Benchmark_jsoniter_ascii(b *testing.B) {
iter := NewIterator(DEFAULT_CONFIG)
iter := NewIterator(ConfigOfDefault)
input := []byte(`"hello, world! hello, world!"`)
b.ResetTimer()
for n := 0; n < b.N; n++ {
@ -140,7 +155,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
}
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
iter := ParseString(DEFAULT_CONFIG, `"hello, world!"`)
iter := ParseString(ConfigOfDefault, `"hello, world!"`)
b.ResetTimer()
for n := 0; n < b.N; n++ {
iter.ResetBytes(iter.buf)