github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_json_rawmessage_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  func TestValEncoder_JsonRawMessage(t *testing.T) {
     9  	t.Run("non pointer", func(t *testing.T) {
    10  		f := func(t *testing.T, s string) {
    11  			msg := json.RawMessage(s)
    12  			checkEncodeValueWithStandard(t, msg, nil)
    13  		}
    14  		t.Run("null", func(t *testing.T) {
    15  			f(t, "null")
    16  		})
    17  		t.Run("true", func(t *testing.T) {
    18  			f(t, "true")
    19  		})
    20  	})
    21  	t.Run("pointer", func(t *testing.T) {
    22  		f := func(t *testing.T, msg *json.RawMessage, err error) {
    23  			checkEncodeValueWithStandard(t, msg, err)
    24  		}
    25  		t.Run("nil", func(t *testing.T) {
    26  			f(t, nil, nil)
    27  		})
    28  		t.Run("non nil", func(t *testing.T) {
    29  			msg := json.RawMessage("false")
    30  			f(t, &msg, nil)
    31  		})
    32  	})
    33  	t.Run("pointer of pointer", func(t *testing.T) {
    34  		f := func(t *testing.T, msg **json.RawMessage, err error) {
    35  			checkEncodeValueWithStandard(t, msg, err)
    36  		}
    37  		t.Run("nil", func(t *testing.T) {
    38  			f(t, nil, nil)
    39  		})
    40  		t.Run("pointer of nil", func(t *testing.T) {
    41  			ptr := (*json.RawMessage)(nil)
    42  			f(t, &ptr, nil)
    43  		})
    44  		t.Run("non nil", func(t *testing.T) {
    45  			msg := json.RawMessage("false")
    46  			ptr := &msg
    47  			f(t, &ptr, nil)
    48  		})
    49  	})
    50  }
    51  
    52  func TestValEncoder_JsonRawMessage_OmitEmpty(t *testing.T) {
    53  	type st struct {
    54  		A json.RawMessage `json:",omitempty"`
    55  	}
    56  	t.Run("nil", func(t *testing.T) {
    57  		checkEncodeValueWithStandard(t, st{}, nil)
    58  	})
    59  	t.Run("empty", func(t *testing.T) {
    60  		checkEncodeValueWithStandard(t, st{
    61  			A: json.RawMessage{},
    62  		}, nil)
    63  	})
    64  	t.Run("null", func(t *testing.T) {
    65  		checkEncodeValueWithStandard(t, st{
    66  			A: json.RawMessage("null"),
    67  		}, nil)
    68  	})
    69  }