github.com/mundipagg/tracer-splunk-writer@v1.0.6/json/encoder/map_test.go (about)

     1  package encoder
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"strconv"
     7  	"testing"
     8  	"unsafe"
     9  
    10  	jsoniter "github.com/json-iterator/go"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestMap_Encode(t *testing.T) {
    15  	t.Parallel()
    16  	t.Run("when the input is 'empty'", func(t *testing.T) {
    17  		t.Parallel()
    18  		is := assert.New(t)
    19  		called := 0
    20  		expected := ""
    21  		subject := &Map{
    22  			Strategy: func(s string) string {
    23  				called++
    24  				return expected
    25  			},
    26  		}
    27  		input := ""
    28  		pointer := reflect.ValueOf(&input).Pointer()
    29  		buf := &bytes.Buffer{}
    30  		stream := jsoniter.NewStream(jsoniter.ConfigFastest, buf, 100)
    31  		subject.Encode(unsafe.Pointer(pointer), stream)
    32  		_ = stream.Flush()
    33  		is.Equal(strconv.Quote(expected), buf.String())
    34  	})
    35  	t.Run("when the input is not 'empty'", func(t *testing.T) {
    36  		t.Parallel()
    37  		is := assert.New(t)
    38  		called := 0
    39  		expected := "output"
    40  		subject := &Map{
    41  			Strategy: func(s string) string {
    42  				called++
    43  				return expected
    44  			},
    45  		}
    46  		input := "input"
    47  		pointer := reflect.ValueOf(&input).Pointer()
    48  		buf := &bytes.Buffer{}
    49  		stream := jsoniter.NewStream(jsoniter.ConfigFastest, buf, 100)
    50  		subject.Encode(unsafe.Pointer(pointer), stream)
    51  		_ = stream.Flush()
    52  		is.Equal(strconv.Quote(expected), buf.String())
    53  	})
    54  }
    55  
    56  func TestMap_IsEmpty(t *testing.T) {
    57  	t.Parallel()
    58  	is := assert.New(t)
    59  	subject := &Map{}
    60  	input := "input"
    61  	pointer := reflect.ValueOf(&input).Pointer()
    62  	is.False(subject.IsEmpty(unsafe.Pointer(pointer)))
    63  	input = ""
    64  	pointer = reflect.ValueOf(&input).Pointer()
    65  	is.True(subject.IsEmpty(unsafe.Pointer(pointer)))
    66  	is.True(subject.IsEmpty(nil))
    67  }