Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json.Number support to UnmarshalString (#2396)
* Add json.Number support to UnmarshalString * Add UnmarshalString tests * Remove trailing zeros when calling UnmarshalString with float64
- Loading branch information
Showing
2 changed files
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
package graphql | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestString(t *testing.T) { | ||
assert.Equal(t, `"hello"`, doStrMarshal("hello")) | ||
assert.Equal(t, `"he\tllo"`, doStrMarshal("he\tllo")) | ||
assert.Equal(t, `"he\tllo"`, doStrMarshal("he llo")) | ||
assert.Equal(t, `"he\nllo"`, doStrMarshal("he\nllo")) | ||
assert.Equal(t, `"he\r\nllo"`, doStrMarshal("he\r\nllo")) | ||
assert.Equal(t, `"he\\llo"`, doStrMarshal(`he\llo`)) | ||
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, doStrMarshal(`quotes"nested"in"quotes"`)) | ||
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000")) | ||
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000")) | ||
assert.Equal(t, "\"\U000fe4ed\"", doStrMarshal("\U000fe4ed")) | ||
t.Run("marshal", func(t *testing.T) { | ||
assert.Equal(t, `"hello"`, m2s(MarshalString("hello"))) | ||
assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he\tllo"))) | ||
assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he llo"))) | ||
assert.Equal(t, `"he\nllo"`, m2s(MarshalString("he\nllo"))) | ||
assert.Equal(t, `"he\r\nllo"`, m2s(MarshalString("he\r\nllo"))) | ||
assert.Equal(t, `"he\\llo"`, m2s(MarshalString(`he\llo`))) | ||
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, m2s(MarshalString(`quotes"nested"in"quotes"`))) | ||
assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000"))) | ||
assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000"))) | ||
assert.Equal(t, "\"\U000fe4ed\"", m2s(MarshalString("\U000fe4ed"))) | ||
}) | ||
|
||
t.Run("unmarshal", func(t *testing.T) { | ||
assert.Equal(t, "123", mustUnmarshalString("123")) | ||
assert.Equal(t, "123", mustUnmarshalString(123)) | ||
assert.Equal(t, "123", mustUnmarshalString(int64(123))) | ||
assert.Equal(t, "123", mustUnmarshalString(float64(123))) | ||
assert.Equal(t, "123", mustUnmarshalString(json.Number("123"))) | ||
assert.Equal(t, "true", mustUnmarshalString(true)) | ||
assert.Equal(t, "false", mustUnmarshalString(false)) | ||
assert.Equal(t, "null", mustUnmarshalString(nil)) | ||
}) | ||
} | ||
|
||
func doStrMarshal(s string) string { | ||
var buf bytes.Buffer | ||
MarshalString(s).MarshalGQL(&buf) | ||
return buf.String() | ||
func mustUnmarshalString(v interface{}) string { | ||
res, err := UnmarshalString(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res | ||
} |