github.com/yaegashi/msgraph.go@v0.1.4/jsonx/encode_extra_test.go (about)

     1  package jsonx
     2  
     3  import "testing"
     4  
     5  type MarshalExtra1 struct {
     6  	X string
     7  	Y int
     8  	R map[string]interface{} `json:"-" jsonx:"extra"`
     9  }
    10  
    11  type MarshalExtra2 struct {
    12  	R map[string]interface{} `json:"-" jsonx:"extra"`
    13  	X string
    14  	Y int
    15  }
    16  
    17  func TestMarshalExtra(t *testing.T) {
    18  	tests := []struct {
    19  		in   interface{}
    20  		want string
    21  		ok   bool
    22  	}{
    23  		{MarshalExtra1{X: "123", Y: 123, R: map[string]interface{}{"A": "123", "B": 123}}, `{"X":"123","Y":123,"A":"123","B":123}`, true},
    24  		{MarshalExtra2{X: "123", Y: 123, R: map[string]interface{}{"A": "123", "B": 123}}, `{"A":"123","B":123,"X":"123","Y":123}`, true},
    25  	}
    26  
    27  	for i, tt := range tests {
    28  		b, err := Marshal(tt.in)
    29  		if ok := (err == nil); ok != tt.ok {
    30  			if err != nil {
    31  				t.Errorf("test %d, unexpected failure: %v", i, err)
    32  			} else {
    33  				t.Errorf("test %d, unexpected success", i)
    34  			}
    35  		}
    36  		if got := string(b); got != tt.want {
    37  			t.Errorf("test %d, Marshal(%#v) = %q, want %q", i, tt.in, got, tt.want)
    38  		}
    39  	}
    40  }