github.com/naemono/pop@v4.13.1+incompatible/slices/string_test.go (about)

     1  package slices
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_String_Scan(t *testing.T) {
    11  	r := require.New(t)
    12  	in := `{"This has a comma,","This has a double quote\"","Also a single'"}`
    13  	s := &String{}
    14  	r.NoError(s.Scan(in))
    15  	ss := []string(*s)
    16  	r.Len(ss, 3)
    17  	r.Equal([]string{"This has a comma,", "This has a double quote\"", "Also a single'"}, ss)
    18  }
    19  
    20  func Test_String_Value(t *testing.T) {
    21  	r := require.New(t)
    22  	s := String{"This has a comma,", "This has a double quote\"", "Also a single'"}
    23  	v, err := s.Value()
    24  	r.NoError(err)
    25  	r.Equal(`{"This has a comma,","This has a double quote\"","Also a single'"}`, v)
    26  }
    27  
    28  func Test_String_UnmarshalText(t *testing.T) {
    29  	r := require.New(t)
    30  	in := "foo,bar,\"baz,bax\""
    31  	s := &String{}
    32  	r.NoError(s.UnmarshalText([]byte(in)))
    33  
    34  	ss := []string(*s)
    35  	r.Len(ss, 3)
    36  	r.Equal([]string{"foo", "bar", "baz,bax"}, ss)
    37  }
    38  
    39  func Test_String_JSON_Unmarshal(t *testing.T) {
    40  	r := require.New(t)
    41  
    42  	x := `["foo", "bar"]`
    43  	s := String{}
    44  	r.NoError(json.Unmarshal([]byte(x), &s))
    45  	r.Equal(String{"foo", "bar"}, s)
    46  }
    47  
    48  func Test_String_JSON_Marshal(t *testing.T) {
    49  	r := require.New(t)
    50  
    51  	x := `["foo","bar"]`
    52  	s := String{"foo", "bar"}
    53  	b, err := json.Marshal(s)
    54  	r.NoError(err)
    55  	r.Equal(x, string(b))
    56  }