123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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)
- }
- })
- }
- }
|