package sentio import ( "encoding/json" "reflect" "testing" ) func TestOrderIntent_MarshalJSON(t *testing.T) { type Obj struct { Intent OrderIntent `json:"intent"` } tests := []struct { obj Obj want []byte }{ { obj: Obj{Intent: BuyToOpen}, want: []byte(`{"intent":"buy_to_open"}`), }, { obj: Obj{Intent: BuyToClose}, want: []byte(`{"intent":"buy_to_close"}`), }, { obj: Obj{Intent: SellToOpen}, want: []byte(`{"intent":"sell_to_open"}`), }, { obj: Obj{Intent: SellToClose}, want: []byte(`{"intent":"sell_to_close"}`), }, { obj: Obj{Intent: -1}, want: []byte(`{"intent":"undefined"}`), }, { obj: Obj{Intent: 100}, want: []byte(`{"intent":"undefined"}`), }, } for _, tt := range tests { t.Run("TestOrderIntent", func(t *testing.T) { if got, _ := json.Marshal(tt.obj); !reflect.DeepEqual(got, tt.want) { t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) } }) } } func TestOrderIntent_UnmarshalJSON(t *testing.T) { type Obj struct { Intent OrderIntent `json:"intent"` } tests := []struct { obj Obj data []byte wantErr bool }{ { obj: Obj{ Intent: BuyToOpen, }, data: []byte(`{"intent":"buy_to_open"}`), wantErr: false, }, { obj: Obj{ Intent: BuyToClose, }, data: []byte(`{"intent":"buy_to_close"}`), wantErr: false, }, { obj: Obj{ Intent: SellToOpen, }, data: []byte(`{"intent":"sell_to_open"}`), wantErr: false, }, { obj: Obj{ Intent: SellToClose, }, data: []byte(`{"intent":"sell_to_close"}`), wantErr: false, }, { obj: Obj{ Intent: SellToClose, }, data: []byte(`{"intent":3}`), wantErr: false, }, { obj: Obj{ Intent: BuyToOpen, }, data: []byte(`{"intent":19}`), wantErr: true, }, { obj: Obj{ Intent: BuyToOpen, }, data: []byte(`{"intent":null}`), wantErr: false, }, } for _, tt := range tests { t.Run("TestOrderIntent", func(t *testing.T) { var v Obj if err := json.Unmarshal(tt.data, &v); (err != nil) != tt.wantErr { t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(v, tt.obj) { t.Errorf("MarshalJSON() got = %v, want %v", v, tt.obj) } }) } }