...

Source file src/github.com/99designs/gqlgen/graphql/string_test.go

Documentation: github.com/99designs/gqlgen/graphql

     1  package graphql
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestString(t *testing.T) {
    11  	t.Run("marshal", func(t *testing.T) {
    12  		assert.Equal(t, `"hello"`, m2s(MarshalString("hello")))
    13  		assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he\tllo")))
    14  		assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he	llo")))
    15  		assert.Equal(t, `"he\nllo"`, m2s(MarshalString("he\nllo")))
    16  		assert.Equal(t, `"he\r\nllo"`, m2s(MarshalString("he\r\nllo")))
    17  		assert.Equal(t, `"he\\llo"`, m2s(MarshalString(`he\llo`)))
    18  		assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, m2s(MarshalString(`quotes"nested"in"quotes"`)))
    19  		assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000")))
    20  		assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000")))
    21  		assert.Equal(t, "\"\U000fe4ed\"", m2s(MarshalString("\U000fe4ed")))
    22  	})
    23  
    24  	t.Run("unmarshal", func(t *testing.T) {
    25  		assert.Equal(t, "123", mustUnmarshalString("123"))
    26  		assert.Equal(t, "123", mustUnmarshalString(123))
    27  		assert.Equal(t, "123", mustUnmarshalString(int64(123)))
    28  		assert.Equal(t, "123", mustUnmarshalString(float64(123)))
    29  		assert.Equal(t, "123", mustUnmarshalString(json.Number("123")))
    30  		assert.Equal(t, "true", mustUnmarshalString(true))
    31  		assert.Equal(t, "false", mustUnmarshalString(false))
    32  		assert.Equal(t, "null", mustUnmarshalString(nil))
    33  	})
    34  }
    35  
    36  func mustUnmarshalString(v interface{}) string {
    37  	res, err := UnmarshalString(v)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return res
    42  }
    43  

View as plain text