Bladeren bron

increase coverage

Xargin 8 jaren geleden
bovenliggende
commit
93ce14316d
2 gewijzigde bestanden met toevoegingen van 54 en 2 verwijderingen
  1. 42 0
      jsoniter_encode_interface_test.go
  2. 12 2
      jsoniter_string_test.go

+ 42 - 0
jsoniter_encode_interface_test.go

@@ -0,0 +1,42 @@
+package jsoniter
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func Test_encode_interface(t *testing.T) {
+	should := require.New(t)
+	var a interface{}
+	a = int8(10)
+	str, err := MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "10")
+	a = float32(3)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "3")
+	a = map[string]interface{}{"abc": 1}
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, `{"abc":1}`)
+	a = uintptr(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = uint(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = uint8(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = json.RawMessage("abc")
+	MarshalToString(a)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "abc")
+}

+ 12 - 2
jsoniter_string_test.go

@@ -6,9 +6,10 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
-	"github.com/stretchr/testify/require"
 	"testing"
 	"unicode/utf8"
+
+	"github.com/stretchr/testify/require"
 )
 
 func Test_read_normal_string(t *testing.T) {
@@ -44,7 +45,16 @@ func Test_read_normal_string(t *testing.T) {
 func Test_read_exotic_string(t *testing.T) {
 	cases := map[string]string{
 		`"hel\"lo"`:      `hel"lo`,
-		`"hel\nlo"`:      "hel\nlo",
+		`"hel\\\/lo"`:    `hel\/lo`,
+		`"hel\\blo"`:     `hel\blo`,
+		`"hel\\\blo"`:    "hel\\\blo",
+		`"hel\\nlo"`:     `hel\nlo`,
+		`"hel\\\nlo"`:    "hel\\\nlo",
+		`"hel\\tlo"`:     `hel\tlo`,
+		`"hel\\flo"`:     `hel\flo`,
+		`"hel\\\flo"`:    "hel\\\flo",
+		`"hel\\\rlo"`:    "hel\\\rlo",
+		`"hel\\\tlo"`:    "hel\\\tlo",
 		`"\u4e2d\u6587"`: "中文",
 		`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
 	}