main
1package serde
2
3import (
4 "bytes"
5 "fmt"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10)
11
12type nostringer struct {
13 Value string
14}
15
16type stringer struct {
17 Value string
18}
19
20func (s stringer) String() string {
21 return fmt.Sprintf("The %s", s.Value)
22}
23
24func TestToPlain(t *testing.T) {
25
26 t.Run("stringafies an item that doesn't implement fmt.Stringer", func(t *testing.T) {
27 w := bytes.NewBuffer(nil)
28 require.NoError(t, ToPlain[nostringer](w, nostringer{Value: "example"}))
29 assert.Equal(t, `{example}`, w.String())
30 })
31
32 t.Run("stringafies an item that implements fmt.Stringer", func(t *testing.T) {
33 w := bytes.NewBuffer(nil)
34 require.NoError(t, ToPlain[stringer](w, stringer{Value: "example"}))
35 assert.Equal(t, `The example`, w.String())
36 })
37}